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*/
26package gov.nist.javax.sip.parser;
27
28import gov.nist.javax.sip.header.*;
29import gov.nist.core.*;
30import java.text.ParseException;
31import javax.sip.*;
32
33/**
34 * Parser for SubscriptionState header.
35 *
36 * @version 1.2 $Revision: 1.7 $ $Date: 2009/07/17 18:58:05 $
37 *
38 * @author Olivier Deruelle
39 * @author M. Ranganathan
40 *
41 */
42public class SubscriptionStateParser extends HeaderParser {
43
44    /**
45     * Creates a new instance of SubscriptionStateParser
46     * @param subscriptionState the header to parse
47     */
48    public SubscriptionStateParser(String subscriptionState) {
49        super(subscriptionState);
50    }
51
52    /**
53     * Constructor
54     * @param lexer the lexer to use to parse the header
55     */
56    protected SubscriptionStateParser(Lexer lexer) {
57        super(lexer);
58    }
59
60    /**
61     * parse the String message
62     * @return SIPHeader (SubscriptionState  object)
63     * @throws SIPParseException if the message does not respect the spec.
64     */
65    public SIPHeader parse() throws ParseException {
66
67        if (debug)
68            dbg_enter("SubscriptionStateParser.parse");
69
70        SubscriptionState subscriptionState = new SubscriptionState();
71        try {
72            headerName(TokenTypes.SUBSCRIPTION_STATE);
73
74            subscriptionState.setHeaderName(SIPHeaderNames.SUBSCRIPTION_STATE);
75
76            // State:
77            lexer.match(TokenTypes.ID);
78            Token token = lexer.getNextToken();
79            subscriptionState.setState(token.getTokenValue());
80
81            while (lexer.lookAhead(0) == ';') {
82                this.lexer.match(';');
83                this.lexer.SPorHT();
84                lexer.match(TokenTypes.ID);
85                token = lexer.getNextToken();
86                String value = token.getTokenValue();
87                if (value.equalsIgnoreCase("reason")) {
88                    this.lexer.match('=');
89                    this.lexer.SPorHT();
90                    lexer.match(TokenTypes.ID);
91                    token = lexer.getNextToken();
92                    value = token.getTokenValue();
93                    subscriptionState.setReasonCode(value);
94                } else if (value.equalsIgnoreCase("expires")) {
95                    this.lexer.match('=');
96                    this.lexer.SPorHT();
97                    lexer.match(TokenTypes.ID);
98                    token = lexer.getNextToken();
99                    value = token.getTokenValue();
100                    try {
101                        int expires = Integer.parseInt(value);
102                        subscriptionState.setExpires(expires);
103                    } catch (NumberFormatException ex) {
104                        throw createParseException(ex.getMessage());
105                    } catch (InvalidArgumentException ex) {
106                        throw createParseException(ex.getMessage());
107                    }
108                } else if (value.equalsIgnoreCase("retry-after")) {
109                    this.lexer.match('=');
110                    this.lexer.SPorHT();
111                    lexer.match(TokenTypes.ID);
112                    token = lexer.getNextToken();
113                    value = token.getTokenValue();
114                    try {
115                        int retryAfter = Integer.parseInt(value);
116                        subscriptionState.setRetryAfter(retryAfter);
117                    } catch (NumberFormatException ex) {
118                        throw createParseException(ex.getMessage());
119                    } catch (InvalidArgumentException ex) {
120                        throw createParseException(ex.getMessage());
121                    }
122                } else {
123                    this.lexer.match('=');
124                    this.lexer.SPorHT();
125                    lexer.match(TokenTypes.ID);
126                    Token secondToken = lexer.getNextToken();
127                    String secondValue = secondToken.getTokenValue();
128                    subscriptionState.setParameter(value, secondValue);
129                }
130                this.lexer.SPorHT();
131            }
132        } finally {
133            if (debug)
134                dbg_leave("SubscriptionStateParser.parse");
135        }
136
137        return subscriptionState;
138    }
139
140    /** Test program
141    public static void main(String args[]) throws ParseException {
142        String subscriptionState[] = {
143            "Subscription-State: active \n",
144            "Subscription-State: terminated;reason=rejected \n",
145            "Subscription-State: pending;reason=probation;expires=36\n",
146            "Subscription-State: pending;retry-after=10;expires=36\n",
147            "Subscription-State: pending;generic=void\n"
148        };
149
150        for (int i = 0; i < subscriptionState.length; i++ ) {
151            SubscriptionStateParser parser =
152            new SubscriptionStateParser(subscriptionState[i]);
153            SubscriptionState ss= (SubscriptionState) parser.parse();
154            System.out.println("encoded = " + ss.encode());
155        }
156    }
157     */
158}
159/*
160 * $Log: SubscriptionStateParser.java,v $
161 * Revision 1.7  2009/07/17 18:58:05  emcho
162 * Converts indentation tabs to spaces so that we have a uniform indentation policy in the whole project.
163 *
164 * Revision 1.6  2006/07/13 09:02:25  mranga
165 * Issue number:
166 * Obtained from:
167 * Submitted by:  jeroen van bemmel
168 * Reviewed by:   mranga
169 * Moved some changes from jain-sip-1.2 to java.net
170 *
171 * CVS: ----------------------------------------------------------------------
172 * CVS: Issue number:
173 * CVS:   If this change addresses one or more issues,
174 * CVS:   then enter the issue number(s) here.
175 * CVS: Obtained from:
176 * CVS:   If this change has been taken from another system,
177 * CVS:   then name the system in this line, otherwise delete it.
178 * CVS: Submitted by:
179 * CVS:   If this code has been contributed to the project by someone else; i.e.,
180 * CVS:   they sent us a patch or a set of diffs, then include their name/email
181 * CVS:   address here. If this is your work then delete this line.
182 * CVS: Reviewed by:
183 * CVS:   If we are doing pre-commit code reviews and someone else has
184 * CVS:   reviewed your changes, include their name(s) here.
185 * CVS:   If you have not had it reviewed then delete this line.
186 *
187 * Revision 1.3  2006/06/19 06:47:27  mranga
188 * javadoc fixups
189 *
190 * Revision 1.2  2006/06/16 15:26:28  mranga
191 * Added NIST disclaimer to all public domain files. Clean up some javadoc. Fixed a leak
192 *
193 * Revision 1.1.1.1  2005/10/04 17:12:36  mranga
194 *
195 * Import
196 *
197 *
198 * Revision 1.4  2004/01/22 13:26:32  sverker
199 * Issue number:
200 * Obtained from:
201 * Submitted by:  sverker
202 * Reviewed by:   mranga
203 *
204 * Major reformat of code to conform with style guide. Resolved compiler and javadoc warnings. Added CVS tags.
205 *
206 * CVS: ----------------------------------------------------------------------
207 * CVS: Issue number:
208 * CVS:   If this change addresses one or more issues,
209 * CVS:   then enter the issue number(s) here.
210 * CVS: Obtained from:
211 * CVS:   If this change has been taken from another system,
212 * CVS:   then name the system in this line, otherwise delete it.
213 * CVS: Submitted by:
214 * CVS:   If this code has been contributed to the project by someone else; i.e.,
215 * CVS:   they sent us a patch or a set of diffs, then include their name/email
216 * CVS:   address here. If this is your work then delete this line.
217 * CVS: Reviewed by:
218 * CVS:   If we are doing pre-commit code reviews and someone else has
219 * CVS:   reviewed your changes, include their name(s) here.
220 * CVS:   If you have not had it reviewed then delete this line.
221 *
222 */
223