javacommons/gcfprotocols/socket/socket/javasrc.s60/com/nokia/mj/impl/gcf/protocol/socket/Protocol.java
changeset 26 dc7c549001d5
child 64 0ea12c182930
equal deleted inserted replaced
23:98ccebc37403 26:dc7c549001d5
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 package com.nokia.mj.impl.gcf.protocol.socket;
       
    20 
       
    21 import java.io.IOException;
       
    22 import com.nokia.mj.impl.rt.support.Jvm;
       
    23 import com.nokia.mj.impl.gcf.utils.*;
       
    24 import com.nokia.mj.impl.socket.SocketConnectionImpl;
       
    25 import com.nokia.mj.impl.socket.ServerSocketConnectionImpl;
       
    26 import com.nokia.mj.impl.rt.support.ApplicationUtils;
       
    27 import com.nokia.mj.impl.gcf.utils.UrlParser;
       
    28 import com.nokia.mj.impl.utils.Logger;
       
    29 import com.nokia.mj.impl.connectionmanager.ConnectionManager;
       
    30 import com.nokia.mj.impl.connectionmanager.AccessPoint;
       
    31 import com.nokia.mj.impl.rt.support.ApplicationInfo;
       
    32 import com.nokia.mj.impl.utils.Uid;
       
    33 
       
    34 /*
       
    35  * Implementation of Connector class of GCF framework The protocol class is the
       
    36  * entry point for connection types When connector.open is called, the VM finds
       
    37  * the com.nokia.mj.impl.gcf.protocol."scheme".Protocol class which implements
       
    38  * openConnection method which is called by the Vm.
       
    39  */
       
    40 
       
    41 public class Protocol
       
    42 {
       
    43 
       
    44     static
       
    45     {
       
    46         try
       
    47         {
       
    48             Jvm.loadSystemLibrary("javasocket");
       
    49         }
       
    50         catch (Exception e)
       
    51         {
       
    52             Logger.ELOG(Logger.ESOCKET, e.toString());
       
    53         }
       
    54     }
       
    55 
       
    56     public Protocol()
       
    57     {
       
    58 
       
    59     }
       
    60 
       
    61     public javax.microedition.io.Connection openConnection(String aUri,
       
    62             int aMode, boolean aTimeouts) throws IOException
       
    63     {
       
    64 
       
    65         String apnstring = null;
       
    66         String urls = null;
       
    67         int apnid = -1;
       
    68         int index = -1;
       
    69         String fullurl = "socket:" + aUri;
       
    70         ConnectionManager cminstance = null;
       
    71         AccessPoint apn = null;
       
    72         index = aUri.indexOf(";nokia_netid=");
       
    73         if (index != -1)
       
    74         {
       
    75             apnstring = aUri.substring(index);
       
    76             int equalindex = apnstring.indexOf('=');
       
    77             if (equalindex != -1)
       
    78             {
       
    79                 String temp = apnstring.substring(equalindex + 1);
       
    80                 try
       
    81                 {
       
    82                     apnid = Integer.parseInt(temp);
       
    83                 }
       
    84                 catch (NumberFormatException e)
       
    85                 {
       
    86                     throw new IllegalArgumentException("Invalid URL: "
       
    87                                                        + fullurl);
       
    88                 }
       
    89             }
       
    90             else
       
    91             {
       
    92                 throw new IllegalArgumentException("Invalid URL: " + fullurl);
       
    93             }
       
    94             urls = aUri.substring(0, index);
       
    95         }
       
    96         else
       
    97         {
       
    98             index = aUri.indexOf(";nokia_apnid=");
       
    99             if (index != -1)
       
   100             {
       
   101                 apnstring = aUri.substring(index);
       
   102                 int equalindex = apnstring.indexOf('=');
       
   103                 if (equalindex != -1)
       
   104                 {
       
   105                     String temp = apnstring.substring(equalindex + 1);
       
   106                     try
       
   107                     {
       
   108                         apnid = Integer.parseInt(temp);
       
   109                     }
       
   110                     catch (NumberFormatException e)
       
   111                     {
       
   112                         throw new IllegalArgumentException("Invalid URL: "
       
   113                                                            + fullurl);
       
   114                     }
       
   115                 }
       
   116                 else
       
   117                 {
       
   118                     throw new IllegalArgumentException("Invalid URL: "
       
   119                                                        + fullurl);
       
   120                 }
       
   121                 urls = aUri.substring(0, index);
       
   122             }
       
   123             else
       
   124             {
       
   125                 urls = aUri;
       
   126             }
       
   127         }
       
   128         Logger.LOG(Logger.ESOCKET, Logger.EInfo,
       
   129                    "socket +openConnection -after removing nokia_apnid : " + urls);
       
   130 
       
   131         UrlParser SocURL = new UrlParser("socket", urls);
       
   132         Logger.LOG(Logger.ESOCKET, Logger.EInfo,
       
   133                    "socket - protocol class : port = " + SocURL.port);
       
   134         if (SocURL.getPort() == 8080 || SocURL.getPort() == 443
       
   135                 || SocURL.getPort() == 80)
       
   136         {
       
   137             if (ApplicationInfo.getInstance().getRuntimeType().equals(
       
   138                         ApplicationInfo.RUNTIME_MIDP) == true)
       
   139             {
       
   140                 if ((ApplicationInfo.getInstance().getProtectionDomain()
       
   141                         .equals(ApplicationInfo.UNIDENTIFIED_THIRD_PARTY_DOMAIN)) == true)
       
   142                 {
       
   143                     Logger.LOG(Logger.ESOCKET, Logger.EInfo,
       
   144                                "Throwing security exception for restricted port");
       
   145                     throw new SecurityException("Unable to open socket connection: port "+SocURL.getPort()+" is restricted");
       
   146                 }
       
   147             }
       
   148         }
       
   149         if (SocURL.host == null)
       
   150         {
       
   151             Logger.LOG(Logger.ESOCKET, Logger.EInfo,
       
   152                        "socket - protocol class : server connection");
       
   153             ApplicationUtils appUtils = ApplicationUtils.getInstance();
       
   154             SocketPermissionImpl per = new SocketPermissionImpl("socket://");
       
   155             appUtils.checkPermission(per);
       
   156             /* security related stuff */
       
   157             Logger.LOG(Logger.ESOCKET, Logger.EInfo,
       
   158                        "socket - protocol class : after security check");
       
   159             return new ServerSocketConnectionImpl(SocURL, aMode);
       
   160         }
       
   161         else
       
   162         {
       
   163             Logger.LOG(Logger.ESOCKET, Logger.EInfo,
       
   164                        "socket - protocol class : client connection");
       
   165             if (SocURL.port == -1)
       
   166             {
       
   167                 /* client connecction should have a port number */
       
   168                 throw new IllegalArgumentException("Invalid URL: " + fullurl);
       
   169             }
       
   170             ApplicationUtils appUtils = ApplicationUtils.getInstance();
       
   171             SocketPermissionImpl per = new SocketPermissionImpl("socket://*");
       
   172             appUtils.checkPermission(per);
       
   173             /* security related stuff */
       
   174             Logger.LOG(Logger.ESOCKET, Logger.EInfo,
       
   175                        "socket - protocol class : after security check");
       
   176 
       
   177             // remove comments if open c patch available
       
   178             cminstance = ConnectionManager.getInstance();
       
   179             Logger.LOG(Logger.ESOCKET, Logger.EInfo,
       
   180                        "after getting ConnectionManager instance");
       
   181             // get application suite uid
       
   182             try
       
   183             {
       
   184                 Uid appSuite = ApplicationInfo.getInstance().getSuiteUid();
       
   185                 try
       
   186                 {
       
   187                     // remove comments if open c patch available
       
   188                     apn = cminstance.getNetworkAccessPoint(appSuite, aUri);
       
   189                 }
       
   190                 catch (Exception e)
       
   191                 {
       
   192                     Logger.LOG(Logger.ESOCKET, Logger.EInfo,
       
   193                                "+secureconnection Protocol getNetworkAccessPoint threw Exception");
       
   194                 }
       
   195                 Logger.LOG(Logger.ESOCKET, Logger.EInfo, "apn.getType = "
       
   196                            + apn.getType() + " apn.getNapId= " + apn.getNapId());
       
   197             }
       
   198             catch (Exception e)
       
   199             {
       
   200                 Logger.LOG(Logger.ESOCKET, Logger.EInfo,
       
   201                            "getSuiteUid exception ");
       
   202             }
       
   203 
       
   204             return new SocketConnectionImpl(SocURL, aMode, -1, cminstance, apn);
       
   205         }
       
   206     }
       
   207 }