javaextensions/wma/sms_cbs/javasrc/com/nokia/mj/impl/smscbs/utils/WmaUrl.java
changeset 21 2a9601315dfc
child 56 abc41079b313
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     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 package com.nokia.mj.impl.smscbs.utils;
       
    19 
       
    20 /**
       
    21  * Provides some utilities to: 1. Parse an URI to obtain it's main URI
       
    22  * components. 2. Concatenates a base URI with a relative URI as specified.
       
    23  *
       
    24  * @author Nokia Corporation
       
    25  */
       
    26 public final class WmaUrl
       
    27 {
       
    28     public static final boolean SERVER_CONNECTION = true;
       
    29 
       
    30     public static final boolean CLIENT_CONNECTION = false;
       
    31 
       
    32     private static final String SMS_PROTOCOL = "sms";
       
    33 
       
    34     private static final String CBS_PROTOCOL = "cbs";
       
    35 
       
    36     private static final String INVALID_URL = "Invalid URL: ";
       
    37 
       
    38     private static final int MAX_PORT = 65535;
       
    39 
       
    40     private static final int[] RESTRICTED_PORTS =
       
    41     {
       
    42         2805, // WAP WTA secure connection-less session service
       
    43         2923, // WAP WTA secure session service
       
    44         2948, // WAP Push connectionless session service (client side)
       
    45         2949, // WAP Push secure connectionless session service (client side)
       
    46         5502, // Service Card reader
       
    47         5503, // Internet access configuration reader
       
    48         5508, // Dynamic Menu Control Protocol
       
    49         5511, // Message Access Protocol
       
    50         5512, // Simple Email Notification
       
    51         9200, // WAP connectionless session service
       
    52         9201, // WAP session service
       
    53         9202, // WAP secure connectionless session service
       
    54         9203, // WAP secure session service
       
    55         9207, // WAP vCal Secure
       
    56         49996,// SyncML OTA configuration
       
    57         49999 // WAP OTA configuration
       
    58     };
       
    59 
       
    60     private int iPortVal;
       
    61 
       
    62     private String iHost;
       
    63 
       
    64     private String iPort;
       
    65 
       
    66     private String iAbsoluteUri;
       
    67 
       
    68     private String iProtocol;
       
    69 
       
    70     public WmaUrl(String aUri)
       
    71     {
       
    72         iAbsoluteUri = aUri;
       
    73     }
       
    74 
       
    75     public WmaUrl(String aUri, String aProtocol)
       
    76     {
       
    77         iAbsoluteUri = aProtocol + aUri;
       
    78     }
       
    79 
       
    80     /**
       
    81      * Method to retrieve the protocol
       
    82      * @return the uri string
       
    83      */
       
    84     public String getProtocol()
       
    85     {
       
    86         return iProtocol;
       
    87     }
       
    88 
       
    89     /**
       
    90      * Validates the Uri.
       
    91      * @throws IllegalArgumentException it uri is not valid
       
    92      */
       
    93     public void validateUri() throws IllegalArgumentException
       
    94     {
       
    95         int index = iAbsoluteUri.indexOf("://");
       
    96         int nameLength = iAbsoluteUri.length();
       
    97         if ((index == -1) || (index != iAbsoluteUri.indexOf(':'))
       
    98                 || ((index + 3) > nameLength))
       
    99         {
       
   100             throw new IllegalArgumentException(INVALID_URL.concat(iAbsoluteUri));
       
   101         }
       
   102         iProtocol = iAbsoluteUri.substring(0, index);
       
   103         if (iProtocol.compareTo(SMS_PROTOCOL) != 0
       
   104                 && iProtocol.compareTo(CBS_PROTOCOL) != 0)
       
   105         {
       
   106             throw new IllegalArgumentException(INVALID_URL
       
   107                                                .concat(iAbsoluteUri));
       
   108         }
       
   109         String subUri = iAbsoluteUri.substring(index + 3, nameLength);
       
   110         nameLength = subUri.length();
       
   111         index = subUri.indexOf(':');
       
   112         if (index >= 0)
       
   113         {
       
   114             iHost = subUri.substring(0, index);
       
   115             iPort = subUri.substring(index + 1, nameLength);
       
   116         }
       
   117         else if (index < 0)
       
   118         {
       
   119             iHost = subUri;
       
   120         }
       
   121         if (0 != iHost.length())
       
   122         {
       
   123             if (0 == iProtocol.compareTo(CBS_PROTOCOL))
       
   124             {
       
   125                 throw new IllegalArgumentException(INVALID_URL
       
   126                                                    .concat(iAbsoluteUri));
       
   127             }
       
   128             nameLength = iHost.length();
       
   129             validateHost(iHost, nameLength);
       
   130         }
       
   131         if ("".equals(iPort))
       
   132         {
       
   133             throw new IllegalArgumentException(INVALID_URL
       
   134                                                .concat(iAbsoluteUri));
       
   135         }
       
   136         if (iPort != null)
       
   137         {
       
   138             validatePort(iPort);
       
   139         }
       
   140     }
       
   141 
       
   142     public String getHost()
       
   143     {
       
   144         return iHost;
       
   145     }
       
   146 
       
   147     public String getPort()
       
   148     {
       
   149         return iPort;
       
   150     }
       
   151 
       
   152     public int getPortVal()
       
   153     {
       
   154         return iPortVal;
       
   155     }
       
   156 
       
   157     public String getAbsoluteUri()
       
   158     {
       
   159         return iAbsoluteUri;
       
   160     }
       
   161     /**
       
   162     * Returns boolean value indicating whether it is a
       
   163     * server/client connection.
       
   164     */
       
   165     public boolean isServerModeConnection()
       
   166     {
       
   167         if (iHost.length() == 0)
       
   168         {
       
   169             return SERVER_CONNECTION;
       
   170         }
       
   171         return CLIENT_CONNECTION;
       
   172     }
       
   173     /**
       
   174      * Validates the Port
       
   175      *
       
   176      * @param aPort -Port that to be validated
       
   177      * @throws IllegalArgumentException if the Port is invalid
       
   178      * @throws SecurityException if the Port is restricted Port
       
   179      * @see
       
   180      */
       
   181     private void validatePort(String aPort)
       
   182     {
       
   183         for (int p = 0; p < aPort.length(); p++)
       
   184         {
       
   185             if (!Character.isDigit(aPort.charAt(p)))
       
   186             {
       
   187                 throw new IllegalArgumentException(INVALID_URL
       
   188                                                    .concat(iAbsoluteUri));
       
   189             }
       
   190         }
       
   191         iPortVal = Integer.parseInt(aPort);
       
   192         if (iPortVal > MAX_PORT || iPortVal < 0)
       
   193         {
       
   194             throw new IllegalArgumentException("Invalid Port: ".concat(aPort));
       
   195         }
       
   196     }
       
   197 
       
   198     public boolean isRestrictedPort()
       
   199     {
       
   200         final int aLength = RESTRICTED_PORTS.length;
       
   201         if (iProtocol != CBS_PROTOCOL)
       
   202         {
       
   203             for (int p = 0; p < aLength; p++)
       
   204             {
       
   205                 if (iPortVal < RESTRICTED_PORTS[p])
       
   206                 {
       
   207                     break;
       
   208                 }
       
   209                 if (iPortVal == RESTRICTED_PORTS[p])
       
   210                 {
       
   211                     return true;
       
   212                 }
       
   213             }
       
   214         }
       
   215         return false;
       
   216     }
       
   217 
       
   218     /**
       
   219      * Validates the Host
       
   220      *
       
   221      * @param aHost - Host to be validated
       
   222      * @param aLength - length of the Host
       
   223      * @throws IllegalArgumentException if the Host is invalid
       
   224      * @see
       
   225      */
       
   226     private void validateHost(String aHost, int aLength)
       
   227     {
       
   228         char ch;
       
   229         for (int p = 0; p < aLength; p++)
       
   230         {
       
   231             ch = aHost.charAt(p);
       
   232             if (p == 0 && ch == '+')
       
   233             {
       
   234                 continue;
       
   235             }
       
   236             else if (!Character.isDigit(ch))
       
   237             {
       
   238                 throw new IllegalArgumentException(INVALID_URL
       
   239                                                    .concat(iAbsoluteUri));
       
   240             }
       
   241         }
       
   242     }
       
   243 
       
   244 }