AcceptEncodingParser.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*/
26package gov.nist.javax.sip.parser;
27import gov.nist.javax.sip.header.*;
28import gov.nist.core.*;
29import java.text.ParseException;
30import javax.sip.*;
31
32/**
33 * Accept-Encoding SIP (HTTP) Header parser.
34 *
35 * @version 1.2 $Revision: 1.7 $ $Date: 2009/07/17 18:57:56 $
36 *
37 * @author Olivier Deruelle
38 * @author M. Ranganathan
39 *
40 *
41 * <pre>
42 *
43 *   The Accept-Encoding request-header field is similar to Accept, but
44 *   restricts the content-codings (section 3.5) that are acceptable in
45 *   the response.
46 *
47 *
48 *       Accept-Encoding  = "Accept-Encoding" ":"
49 *                      ( encoding *( "," encoding) )
50 *       encoding         = ( codings *[ ";" "q" "=" qvalue ] )
51 *       codings          = ( content-coding | "*" )
52 *
53 *   Examples of its use are:
54 *
55 *       Accept-Encoding: compress, gzip
56 *       Accept-Encoding:
57 *       Accept-Encoding: *
58 *       Accept-Encoding: compress;q=0.5, gzip;q=1.0
59 *       Accept-Encoding: gzip;q=1.0, identity; q=0.5, *;q=0
60 * </pre>
61 *
62 */
63public class AcceptEncodingParser extends HeaderParser {
64
65    /**
66     * Constructor
67     * @param acceptEncoding message to parse
68     */
69    public AcceptEncodingParser(String acceptEncoding) {
70        super(acceptEncoding);
71    }
72
73    /**
74     * Constructor
75     * @param lexer Lexer to set
76     */
77    protected AcceptEncodingParser(Lexer lexer) {
78        super(lexer);
79    }
80
81    /**
82     * parse the String message
83     * @return SIPHeader (AcceptEncoding object)
84     * @throws ParseException if the message does not respect the spec.
85     */
86    public SIPHeader parse() throws ParseException {
87        AcceptEncodingList acceptEncodingList = new AcceptEncodingList();
88        if (debug)
89            dbg_enter("AcceptEncodingParser.parse");
90
91        try {
92            headerName(TokenTypes.ACCEPT_ENCODING);
93            // empty body is fine for this header.
94            if (lexer.lookAhead(0) == '\n') {
95                AcceptEncoding acceptEncoding = new AcceptEncoding();
96                acceptEncodingList.add(acceptEncoding);
97            } else {
98                while (lexer.lookAhead(0) != '\n') {
99                    AcceptEncoding acceptEncoding = new AcceptEncoding();
100                    if (lexer.lookAhead(0) != ';') {
101                        // Content-Coding:
102                        lexer.match(TokenTypes.ID);
103                        Token value = lexer.getNextToken();
104                        acceptEncoding.setEncoding(value.getTokenValue());
105                    }
106
107                    while (lexer.lookAhead(0) == ';') {
108                        this.lexer.match(';');
109                        this.lexer.SPorHT();
110                        this.lexer.match('q');
111                        this.lexer.SPorHT();
112                        this.lexer.match('=');
113                        this.lexer.SPorHT();
114                        lexer.match(TokenTypes.ID);
115                        Token value = lexer.getNextToken();
116                        try {
117                            float qv = Float.parseFloat(value.getTokenValue());
118                            acceptEncoding.setQValue(qv);
119                        } catch (NumberFormatException ex) {
120                            throw createParseException(ex.getMessage());
121                        } catch (InvalidArgumentException ex) {
122                            throw createParseException(ex.getMessage());
123                        }
124                        this.lexer.SPorHT();
125                    }
126
127                    acceptEncodingList.add(acceptEncoding);
128                    if (lexer.lookAhead(0) == ',') {
129                        this.lexer.match(',');
130                        this.lexer.SPorHT();
131                    }
132
133                }
134            }
135            return acceptEncodingList;
136        } finally {
137            if (debug)
138                dbg_leave("AcceptEncodingParser.parse");
139        }
140    }
141}
142/*
143 * $Log: AcceptEncodingParser.java,v $
144 * Revision 1.7  2009/07/17 18:57:56  emcho
145 * Converts indentation tabs to spaces so that we have a uniform indentation policy in the whole project.
146 *
147 * Revision 1.6  2006/07/13 09:02:03  mranga
148 * Issue number:
149 * Obtained from:
150 * Submitted by:  jeroen van bemmel
151 * Reviewed by:   mranga
152 * Moved some changes from jain-sip-1.2 to java.net
153 *
154 * CVS: ----------------------------------------------------------------------
155 * CVS: Issue number:
156 * CVS:   If this change addresses one or more issues,
157 * CVS:   then enter the issue number(s) here.
158 * CVS: Obtained from:
159 * CVS:   If this change has been taken from another system,
160 * CVS:   then name the system in this line, otherwise delete it.
161 * CVS: Submitted by:
162 * CVS:   If this code has been contributed to the project by someone else; i.e.,
163 * CVS:   they sent us a patch or a set of diffs, then include their name/email
164 * CVS:   address here. If this is your work then delete this line.
165 * CVS: Reviewed by:
166 * CVS:   If we are doing pre-commit code reviews and someone else has
167 * CVS:   reviewed your changes, include their name(s) here.
168 * CVS:   If you have not had it reviewed then delete this line.
169 *
170 * Revision 1.3  2006/06/19 06:47:27  mranga
171 * javadoc fixups
172 *
173 * Revision 1.2  2006/06/16 15:26:28  mranga
174 * Added NIST disclaimer to all public domain files. Clean up some javadoc. Fixed a leak
175 *
176 * Revision 1.1.1.1  2005/10/04 17:12:35  mranga
177 *
178 * Import
179 *
180 *
181 * Revision 1.4  2004/01/22 13:26:31  sverker
182 * Issue number:
183 * Obtained from:
184 * Submitted by:  sverker
185 * Reviewed by:   mranga
186 *
187 * Major reformat of code to conform with style guide. Resolved compiler and javadoc warnings. Added CVS tags.
188 *
189 * CVS: ----------------------------------------------------------------------
190 * CVS: Issue number:
191 * CVS:   If this change addresses one or more issues,
192 * CVS:   then enter the issue number(s) here.
193 * CVS: Obtained from:
194 * CVS:   If this change has been taken from another system,
195 * CVS:   then name the system in this line, otherwise delete it.
196 * CVS: Submitted by:
197 * CVS:   If this code has been contributed to the project by someone else; i.e.,
198 * CVS:   they sent us a patch or a set of diffs, then include their name/email
199 * CVS:   address here. If this is your work then delete this line.
200 * CVS: Reviewed by:
201 * CVS:   If we are doing pre-commit code reviews and someone else has
202 * CVS:   reviewed your changes, include their name(s) here.
203 * CVS:   If you have not had it reviewed then delete this line.
204 *
205 */
206