KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > oreilly > servlet > CookieParser


1 // Copyright (C) 1999-2001 by Jason Hunter <jhunter_AT_acm_DOT_org>.
2
// All rights reserved. Use of this class is limited.
3
// Please see the LICENSE for more information.
4

5 package com.oreilly.servlet;
6
7 import java.io.*;
8 import java.util.*;
9 import javax.servlet.*;
10 import javax.servlet.http.*;
11
12 /**
13  * A class to simplify cookie retrieval. It can retrieve cookie values by
14  * name and return the value as any primitive type (no casting or parsing
15  * required). It can also throw an exception when a cookie is not found
16  * (simplifying error handling), and can accept default values (eliminating
17  * error handling).
18  * <p>
19  * It is used like this:
20  * <blockquote><pre>
21  * CookieParser parser = new CookieParser(req);
22  * &nbsp;
23  * float ratio = parser.getFloatCookie("ratio", 1.0);
24  * &nbsp;
25  * int count = 0;
26  * try {
27  * count = parser.getIntCookie("count");
28  * }
29  * catch (NumberFormatException e) {
30  * handleMalformedCount();
31  * }
32  * catch (CookieNotFoundException e) {
33  * handleNoCount();
34  * }
35  * </pre></blockquote>
36  *
37  * @see com.oreilly.servlet.CookieNotFoundException
38  *
39  * @author <b>Jason Hunter</b>, Copyright &#169; 2000
40  * @version 1.0, 2000/03/19
41  */

42 public class CookieParser {
43
44   private HttpServletRequest req;
45   private Hashtable cookieJar = new Hashtable();
46
47   /**
48    * Constructs a new CookieParser to handle the cookies of the
49    * given request.
50    *
51    * @param req the servlet request
52    */

53   public CookieParser(HttpServletRequest req) {
54     this.req = req;
55     parseCookies();
56   }
57
58   // Load the cookie values into the cookies hashtable
59
void parseCookies() {
60     Cookie[] cookies = req.getCookies();
61     if (cookies != null) {
62       for (int i = 0; i < cookies.length; i++) {
63         String JavaDoc name = cookies[i].getName();
64         String JavaDoc value = cookies[i].getValue();
65         cookieJar.put(name, value);
66       }
67     }
68   }
69
70   /**
71    * Gets the named cookie value as a String
72    *
73    * @param name the cookie name
74    * @return the cookie value as a String
75    * @exception CookieNotFoundException if the cookie was not found
76    */

77   public String JavaDoc getStringCookie(String JavaDoc name)
78       throws CookieNotFoundException {
79     String JavaDoc value = (String JavaDoc) cookieJar.get(name);
80     if (value == null)
81       throw new CookieNotFoundException(name + " not found");
82     else
83       return value;
84   }
85
86   /**
87    * Gets the named cookie value as a String, with a default.
88    * Returns the default value if the cookie is not found
89    *
90    * @param name the cookie name
91    * @param def the default cookie value
92    * @return the cookie value as a String, or the default
93    */

94   public String JavaDoc getStringCookie(String JavaDoc name, String JavaDoc def) {
95     try { return getStringCookie(name); }
96     catch (Exception JavaDoc e) { return def; }
97   }
98
99   /**
100    * Gets the named cookie value as a boolean
101    *
102    * @param name the cookie name
103    * @return the cookie value as a boolean
104    * @exception CookieNotFoundException if the cookie was not found
105    */

106   public boolean getBooleanCookie(String JavaDoc name)
107       throws CookieNotFoundException {
108     return new Boolean JavaDoc(getStringCookie(name)).booleanValue();
109   }
110
111   /**
112    * Gets the named cookie value as a boolean, with a default.
113    * Returns the default value if the cookie is not found.
114    *
115    * @param name the cookie name
116    * @param def the default cookie value
117    * @return the cookie value as a boolean, or the default
118    */

119   public boolean getBooleanCookie(String JavaDoc name, boolean def) {
120     try { return getBooleanCookie(name); }
121     catch (Exception JavaDoc e) { return def; }
122   }
123
124   /**
125    * Gets the named cookie value as a byte
126    *
127    * @param name the cookie name
128    * @return the cookie value as a byte
129    * @exception CookieNotFoundException if the cookie was not found
130    * @exception NumberFormatException if the cookie value could not
131    * be converted to a byte
132    */

133   public byte getByteCookie(String JavaDoc name)
134       throws CookieNotFoundException, NumberFormatException JavaDoc {
135     return Byte.parseByte(getStringCookie(name));
136   }
137
138   /**
139    * Gets the named cookie value as a byte, with a default.
140    * Returns the default value if the cookie is not found or cannot
141    * be converted to a byte.
142    *
143    * @param name the cookie name
144    * @param def the default cookie value
145    * @return the cookie value as a byte, or the default
146    */

147   public byte getByteCookie(String JavaDoc name, byte def) {
148     try { return getByteCookie(name); }
149     catch (Exception JavaDoc e) { return def; }
150   }
151
152   /**
153    * Gets the named cookie value as a char
154    *
155    * @param name the cookie name
156    * @return the cookie value as a char
157    * @exception CookieNotFoundException if the cookie was not found
158    */

159   public char getCharCookie(String JavaDoc name)
160       throws CookieNotFoundException {
161     String JavaDoc param = getStringCookie(name);
162     if (param.length() == 0)
163       throw new CookieNotFoundException(name + " is empty string");
164     else
165       return (param.charAt(0));
166   }
167
168   /**
169    * Gets the named cookie value as a char, with a default.
170    * Returns the default value if the cookie is not found.
171    *
172    * @param name the cookie name
173    * @param def the default cookie value
174    * @return the cookie value as a char, or the default
175    */

176   public char getCharCookie(String JavaDoc name, char def) {
177     try { return getCharCookie(name); }
178     catch (Exception JavaDoc e) { return def; }
179   }
180
181   /**
182    * Gets the named cookie value as a double
183    *
184    * @param name the cookie name
185    * @return the cookie value as a double
186    * @exception CookieNotFoundException if the cookie was not found
187    * @exception NumberFormatException if the cookie could not be converted
188    * to a double
189    */

190   public double getDoubleCookie(String JavaDoc name)
191       throws CookieNotFoundException, NumberFormatException JavaDoc {
192     return new Double JavaDoc(getStringCookie(name)).doubleValue();
193   }
194
195   /**
196    * Gets the named cookie value as a double, with a default.
197    * Returns the default value if the cookie is not found.
198    *
199    * @param name the cookie name
200    * @param def the default cookie value
201    * @return the cookie value as a double, or the default
202    */

203   public double getDoubleCookie(String JavaDoc name, double def) {
204     try { return getDoubleCookie(name); }
205     catch (Exception JavaDoc e) { return def; }
206   }
207
208   /**
209    * Gets the named cookie value as a float
210    *
211    * @param name the cookie name
212    * @return the cookie value as a float
213    * @exception CookieNotFoundException if the cookie was not found
214    * @exception NumberFormatException if the cookie could not be converted
215    * to a float
216    */

217   public float getFloatCookie(String JavaDoc name)
218       throws CookieNotFoundException, NumberFormatException JavaDoc {
219     return new Float JavaDoc(getStringCookie(name)).floatValue();
220   }
221
222   /**
223    * Gets the named cookie value as a float, with a default.
224    * Returns the default value if the cookie is not found.
225    *
226    * @param name the cookie name
227    * @param def the default cookie value
228    * @return the cookie value as a float, or the default
229    */

230   public float getFloatCookie(String JavaDoc name, float def) {
231     try { return getFloatCookie(name); }
232     catch (Exception JavaDoc e) { return def; }
233   }
234
235   /**
236    * Gets the named cookie value as a int
237    *
238    * @param name the cookie name
239    * @return the cookie value as a int
240    * @exception CookieNotFoundException if the cookie was not found
241    * @exception NumberFormatException if the cookie could not be converted
242    * to a int
243    */

244   public int getIntCookie(String JavaDoc name)
245       throws CookieNotFoundException, NumberFormatException JavaDoc {
246     return Integer.parseInt(getStringCookie(name));
247   }
248
249   /**
250    * Gets the named cookie value as a int, with a default.
251    * Returns the default value if the cookie is not found.
252    *
253    * @param name the cookie name
254    * @param def the default cookie value
255    * @return the cookie value as a int, or the default
256    */

257   public int getIntCookie(String JavaDoc name, int def) {
258     try { return getIntCookie(name); }
259     catch (Exception JavaDoc e) { return def; }
260   }
261
262   /**
263    * Gets the named cookie value as a long
264    *
265    * @param name the cookie name
266    * @return the cookie value as a long
267    * @exception CookieNotFoundException if the cookie was not found
268    * @exception NumberFormatException if the cookie could not be converted
269    * to a long
270    */

271   public long getLongCookie(String JavaDoc name)
272       throws CookieNotFoundException, NumberFormatException JavaDoc {
273     return Long.parseLong(getStringCookie(name));
274   }
275
276   /**
277    * Gets the named cookie value as a long, with a default.
278    * Returns the default value if the cookie is not found.
279    *
280    * @param name the cookie name
281    * @param def the default cookie value
282    * @return the cookie value as a long, or the default
283    */

284   public long getLongCookie(String JavaDoc name, long def) {
285     try { return getLongCookie(name); }
286     catch (Exception JavaDoc e) { return def; }
287   }
288
289   /**
290    * Gets the named cookie value as a short
291    *
292    * @param name the cookie name
293    * @return the cookie value as a short
294    * @exception CookieNotFoundException if the cookie was not found
295    * @exception NumberFormatException if the cookie could not be converted
296    * to a short
297    */

298   public short getShortCookie(String JavaDoc name)
299       throws CookieNotFoundException, NumberFormatException JavaDoc {
300     return Short.parseShort(getStringCookie(name));
301   }
302
303   /**
304    * Gets the named cookie value as a short, with a default.
305    * Returns the default value if the cookie is not found.
306    *
307    * @param name the cookie name
308    * @param def the default cookie value
309    * @return the cookie value as a short, or the default
310    */

311   public short getShortCookie(String JavaDoc name, short def) {
312     try { return getShortCookie(name); }
313     catch (Exception JavaDoc e) { return def; }
314   }
315 }
316
Popular Tags