PMediaAuthorizationParser.java revision 600c7a4bbc7348167293eac928192e695b4ad5ba
1/*
2* Conditions Of Use
3*
4* This software was developed by employees of the National Institute of
5* Standards and Technology (NIST), an agency of the Federal Government.
6* Pursuant to title 15 Untied States Code Section 105, works of NIST
7* employees are not subject to copyright protection in the United States
8* and are considered to be in the public domain.  As a result, a formal
9* license is not needed to use the software.
10*
11* This software is provided by NIST as a service and is expressly
12* provided "AS IS."  NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
13* OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
14* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
15* AND DATA ACCURACY.  NIST does not warrant or make any representations
16* regarding the use of the software or the results thereof, including but
17* not limited to the correctness, accuracy, reliability or usefulness of
18* the software.
19*
20* Permission to use this software is contingent upon your acceptance
21* of the terms of this agreement
22*
23* .
24*
25*/
26/************************************************************************************************
27 * PRODUCT OF PT INOVACAO - EST DEPARTMENT and Telecommunications Institute (Aveiro, Portugal)  *
28 ************************************************************************************************/
29
30package gov.nist.javax.sip.parser.ims;
31
32import java.text.ParseException;
33
34import javax.sip.InvalidArgumentException;
35
36import gov.nist.javax.sip.header.ims.PMediaAuthorizationList;
37import gov.nist.javax.sip.header.ims.PMediaAuthorization;
38import gov.nist.javax.sip.header.ims.SIPHeaderNamesIms;
39import gov.nist.core.Token;
40import gov.nist.javax.sip.header.SIPHeader;
41import gov.nist.javax.sip.parser.HeaderParser;
42import gov.nist.javax.sip.parser.Lexer;
43import gov.nist.javax.sip.parser.TokenTypes;
44
45
46/**
47 * P-Media-Authorization header parser.
48 *
49 * @author Miguel Freitas (IT) PT-Inovacao
50 */
51
52public class PMediaAuthorizationParser
53    extends HeaderParser
54    implements TokenTypes
55{
56
57    public PMediaAuthorizationParser(String mediaAuthorization)
58    {
59        super(mediaAuthorization);
60
61    }
62
63    public PMediaAuthorizationParser(Lexer lexer)
64    {
65        super(lexer);
66
67    }
68
69
70
71
72
73    public SIPHeader parse() throws ParseException
74    {
75        PMediaAuthorizationList mediaAuthorizationList = new PMediaAuthorizationList();
76
77        if (debug)
78            dbg_enter("MediaAuthorizationParser.parse");
79
80
81        try
82        {
83            headerName(TokenTypes.P_MEDIA_AUTHORIZATION);
84
85            PMediaAuthorization mediaAuthorization = new PMediaAuthorization();
86            mediaAuthorization.setHeaderName(SIPHeaderNamesIms.P_MEDIA_AUTHORIZATION);
87
88            while (lexer.lookAhead(0) != '\n')
89            {
90                this.lexer.match(TokenTypes.ID);
91                Token token = lexer.getNextToken();
92                try {
93                    mediaAuthorization.setMediaAuthorizationToken(token.getTokenValue());
94                } catch (InvalidArgumentException e) {
95                    throw createParseException(e.getMessage());
96                }
97                mediaAuthorizationList.add(mediaAuthorization);
98
99                this.lexer.SPorHT();
100                if (lexer.lookAhead(0) == ',')
101                {
102                    this.lexer.match(',');
103                    mediaAuthorization = new PMediaAuthorization();
104                }
105                this.lexer.SPorHT();
106            }
107
108            return mediaAuthorizationList;
109
110        }
111        finally
112        {
113            if (debug)
114                dbg_leave("MediaAuthorizationParser.parse");
115        }
116
117    }
118
119
120
121
122    /*
123     * test
124     *
125    public static void main(String args[]) throws ParseException
126    {
127        String pHeader[] = {
128            "P-Media-Authorization: 0123456789 \n",
129            "P-Media-Authorization: 0123456789, ABCDEF\n"
130            };
131
132        for (int i = 0; i < pHeader.length; i++ )
133        {
134            PMediaAuthorizationParser mParser =
135                new PMediaAuthorizationParser(pHeader[i]);
136
137            PMediaAuthorizationList mList= (PMediaAuthorizationList) mParser.parse();
138            System.out.println("encoded = " + mList.encode());
139        }
140    }
141     */
142
143
144
145}
146