KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > web > util > CookieTokenizer


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
26  * Use is subject to license terms.
27  */

28
29 package com.sun.enterprise.web.util;
30
31 import java.lang.System JavaDoc;
32 import java.lang.String JavaDoc;
33 import java.lang.IndexOutOfBoundsException JavaDoc;
34 import java.lang.ArrayIndexOutOfBoundsException JavaDoc;
35
36 /**
37  * Parse a Cookie: header into individual tokens according to RFC 2109.
38  */

39 public class CookieTokenizer
40 {
41     /**
42      * Upper bound on the number of cookie tokens to accept. The limit is
43      * based on the 4 different attributes (4.3.4) and the 20 cookie minimum
44      * (6.3) given in RFC 2109 multiplied by 2 to accomodate the 2 tokens in
45      * each name=value pair ("JSESSIONID=1234" is 2 tokens).
46      */

47     private static final int MAX_COOKIE_TOKENS = 4 * 20 * 2;
48
49     /**
50      * Array of cookie tokens. Even indices contain name tokens while odd
51      * indices contain value tokens (or null).
52      */

53     private String JavaDoc tokens[] = new String JavaDoc[MAX_COOKIE_TOKENS];
54
55     /**
56      * Number of cookie tokens currently in the tokens[] array.
57      */

58     private int numTokens = 0;
59
60     /**
61      * Parse a name=value pair from the Cookie: header.
62      *
63      * @param cookies The Cookie: header to parse
64      * @param beginIndex The index in cookies to begin parsing from, inclusive
65      */

66     private int parseNameValue(String JavaDoc cookies, int beginIndex) {
67         int length = cookies.length();
68         int index = beginIndex;
69
70         while (index < length) {
71             switch (cookies.charAt(index)) {
72             case ';':
73             case ',':
74                 // Found end of name token without value
75
tokens[numTokens] = cookies.substring(beginIndex, index).trim();
76                 if (tokens[numTokens].length() > 0) {
77                     numTokens++;
78                     tokens[numTokens] = null;
79                     numTokens++;
80                 }
81                 return index + 1;
82
83             case '=':
84                 // Found end of name token with value
85
tokens[numTokens] = cookies.substring(beginIndex, index).trim();
86                 numTokens++;
87                 return parseValue(cookies, index + 1);
88
89             case '"':
90                 // Skip past quoted span
91
do index++; while (cookies.charAt(index) != '"');
92                 break;
93             }
94
95             index++;
96         }
97
98         if (index > beginIndex) {
99             // Found end of name token without value
100
tokens[numTokens] = cookies.substring(beginIndex, index).trim();
101             if (tokens[numTokens].length() > 0) {
102                 numTokens++;
103                 tokens[numTokens] = null;
104                 numTokens++;
105             }
106         }
107
108         return index;
109     }
110
111     /**
112      * Parse the name=value tokens from a Cookie: header.
113      *
114      * @param cookies The Cookie: header to parse
115      */

116     public int tokenize(String JavaDoc cookies) {
117         numTokens = 0;
118
119         if (cookies != null) {
120             try {
121                 // Advance through cookies, parsing name=value pairs
122
int length = cookies.length();
123                 int index = 0;
124                 while (index < length)
125                     index = parseNameValue(cookies, index);
126             }
127             catch (ArrayIndexOutOfBoundsException JavaDoc e) {
128                 // Filled up the tokens[] array
129
}
130             catch (IndexOutOfBoundsException JavaDoc e) {
131                 // Walked off the end of the cookies header
132
}
133         }
134
135         return numTokens;
136     }
137
138     /**
139      * Return the number of cookie tokens parsed from the Cookie: header.
140      */

141     public int getNumTokens() {
142         return numTokens;
143     }
144
145     /**
146      * Returns a given cookie token from the Cookie: header.
147      *
148      * @param index The index of the cookie token to return
149      */

150     public String JavaDoc tokenAt(int index) {
151         return tokens[index];
152     }
153
154     /**
155      * Parse the value token from a name=value pair.
156      *
157      * @param cookies The Cookie: header to parse
158      * @param beginIndex The index in cookies to begin parsing from, inclusive
159      */

160     private int parseValue(String JavaDoc cookies, int beginIndex) {
161         int length = cookies.length();
162         int index = beginIndex;
163
164         while (index < length) {
165             switch (cookies.charAt(index)) {
166             case ';':
167             case ',':
168                 // Found end of value token
169
tokens[numTokens] = cookies.substring(beginIndex, index).trim();
170                 numTokens++;
171                 return index + 1;
172
173             case '"':
174                 // Skip past quoted span
175
do index++; while (cookies.charAt(index) != '"');
176                 break;
177             }
178
179             index++;
180         }
181
182         // Found end of value token
183
tokens[numTokens] = cookies.substring(beginIndex, index).trim();
184         numTokens++;
185
186         return index;
187     }
188 }
189
Popular Tags