Home | History | Annotate | Download | only in ims
      1 package gov.nist.javax.sip.parser.ims;
      2 /*
      3 * Conditions Of Use
      4 *
      5 * This software was developed by employees of the National Institute of
      6 * Standards and Technology (NIST), an agency of the Federal Government.
      7 * Pursuant to title 15 Untied States Code Section 105, works of NIST
      8 * employees are not subject to copyright protection in the United States
      9 * and are considered to be in the public domain.  As a result, a formal
     10 * license is not needed to use the software.
     11 *
     12 * This software is provided by NIST as a service and is expressly
     13 * provided "AS IS."  NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
     14 * OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
     15 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
     16 * AND DATA ACCURACY.  NIST does not warrant or make any representations
     17 * regarding the use of the software or the results thereof, including but
     18 * not limited to the correctness, accuracy, reliability or usefulness of
     19 * the software.
     20 *
     21 * Permission to use this software is contingent upon your acceptance
     22 * of the terms of this agreement
     23 *
     24 * .
     25 *
     26 */
     27 import java.text.ParseException;
     28 
     29 import javax.sip.InvalidArgumentException;
     30 
     31 import gov.nist.javax.sip.header.SIPHeader;
     32 import gov.nist.javax.sip.header.ims.PPreferredService;
     33 import gov.nist.javax.sip.header.ims.ParameterNamesIms;
     34 import gov.nist.javax.sip.parser.HeaderParser;
     35 import gov.nist.javax.sip.parser.Lexer;
     36 import gov.nist.javax.sip.parser.TokenTypes;
     37 /**
     38  *
     39  * @author aayush.bhatnagar
     40  * Rancore Technologies Pvt Ltd, Mumbai India.
     41  *
     42  * Parse this:
     43  * P-Preferred-Service: urn:urn-7:3gpp-service.exampletelephony.version1
     44  *
     45  */
     46 public class PPreferredServiceParser extends HeaderParser implements TokenTypes{
     47 
     48     protected PPreferredServiceParser(Lexer lexer) {
     49         super(lexer);
     50     }
     51 
     52     public PPreferredServiceParser(String pps)
     53     {
     54         super(pps);
     55     }
     56 
     57     /**
     58      * "The URN consists of a hierarchical service identifier or application
     59      * identifier, with a sequence of labels separated by periods.The left-most label is
     60      * the most significant one and is called 'top-level service
     61      * identifier', while names to the right are called 'sub-services' or
     62      * 'sub-applications'.
     63      *
     64      * For any given service identifier, labels can be removed right-to-left and
     65      * the resulting URN is still valid, referring a more generic
     66      * service, with the exception of the top-level service identifier
     67      * and possibly the first sub-service or sub-application identifier.
     68      *
     69      *  Labels cannot be removed beyond a defined basic service, for
     70      *  example, the label w.x may define a service, but the label w may
     71      *  only define an assignment authority for assigning subsequent
     72      *  values and not define a service in its own right.  In other words,
     73      *  if a service identifier 'w.x.y.z' exists, the URNs 'w.x' and
     74      *  'w.x.y' are also valid service identifiers, but w may not be a
     75      *  valid service identifier if it merely defines who is responsible"
     76      *
     77      * TODO: PLEASE VALIDATE MY UNDERSTANDING OF THE ABOVE TEXT :)
     78      * @ranga: Please validate my understanding of the above text in the draft :)
     79      *         This last para is a little ambiguous.I will only check that atleast
     80      *         1 sub-service or 1 sub-application is present in the URN declaration.
     81      *         If not, I throw an exception. I thought of not throwing an exception
     82      *         and returning whatever was encoded..but the resultant encoding wont
     83      *         make sense. It would be something like-->
     84      *         urn:urn-7:3gpp-service OR urn:urn-7:3gpp-application alone with no sub-services
     85      *         or sub-applications. This is bound to cause an error at the recepient later.
     86      *
     87      * Sub-service and Application identifiers are not maintained by IANA and
     88      * are organization/application dependent (Section 8.2). So we cannot gurantee what lies
     89      * beyond the first sub-service or sub-application identifier. It should be the responsibility
     90      * of the application to make sense of the entire URN holistically. We can only check for the
     91      * standardized part as per the ABNF.
     92      */
     93     public SIPHeader parse() throws ParseException {
     94         if(debug)
     95             dbg_enter("PPreferredServiceParser.parse");
     96         try
     97         {
     98 
     99         this.lexer.match(TokenTypes.P_PREFERRED_SERVICE);
    100         this.lexer.SPorHT();
    101         this.lexer.match(':');
    102         this.lexer.SPorHT();
    103 
    104         PPreferredService pps = new PPreferredService();
    105         String urn = this.lexer.getBuffer();
    106         if(urn.contains(ParameterNamesIms.SERVICE_ID)){
    107 
    108            if(urn.contains(ParameterNamesIms.SERVICE_ID_LABEL))
    109                    {
    110                     String serviceID = urn.split(ParameterNamesIms.SERVICE_ID_LABEL+".")[1];
    111 
    112                      if(serviceID.trim().equals(""))
    113                         try {
    114                             throw new InvalidArgumentException("URN should atleast have one sub-service");
    115                         } catch (InvalidArgumentException e) {
    116 
    117                             e.printStackTrace();
    118                         }
    119                         else
    120                     pps.setSubserviceIdentifiers(serviceID);
    121                    }
    122            else if(urn.contains(ParameterNamesIms.APPLICATION_ID_LABEL))
    123               {
    124                String appID = urn.split(ParameterNamesIms.APPLICATION_ID_LABEL)[1];
    125                if(appID.trim().equals(""))
    126                     try {
    127                         throw new InvalidArgumentException("URN should atleast have one sub-application");
    128                     } catch (InvalidArgumentException e) {
    129                         e.printStackTrace();
    130                     }
    131                     else
    132                   pps.setApplicationIdentifiers(appID);
    133               }
    134            else
    135            {
    136                try {
    137                 throw new InvalidArgumentException("URN is not well formed");
    138 
    139             } catch (InvalidArgumentException e) {
    140                 e.printStackTrace();
    141                     }
    142                   }
    143           }
    144 
    145             super.parse();
    146             return pps;
    147         }
    148         finally{
    149             if(debug)
    150                 dbg_enter("PPreferredServiceParser.parse");
    151         }
    152 
    153     }
    154 }
    155