001: // Copyright (C) 1999-2001 by Jason Hunter <jhunter_AT_acm_DOT_org>.
002: // All rights reserved. Use of this class is limited.
003: // Please see the LICENSE for more information.
004:
005: package com.oreilly.servlet;
006:
007: import java.io.*;
008: import java.util.*;
009: import javax.servlet.*;
010: import javax.servlet.http.*;
011:
012: /**
013: * A class to simplify cookie retrieval. It can retrieve cookie values by
014: * name and return the value as any primitive type (no casting or parsing
015: * required). It can also throw an exception when a cookie is not found
016: * (simplifying error handling), and can accept default values (eliminating
017: * error handling).
018: * <p>
019: * It is used like this:
020: * <blockquote><pre>
021: * CookieParser parser = new CookieParser(req);
022: *
023: * float ratio = parser.getFloatCookie("ratio", 1.0);
024: *
025: * int count = 0;
026: * try {
027: * count = parser.getIntCookie("count");
028: * }
029: * catch (NumberFormatException e) {
030: * handleMalformedCount();
031: * }
032: * catch (CookieNotFoundException e) {
033: * handleNoCount();
034: * }
035: * </pre></blockquote>
036: *
037: * @see com.oreilly.servlet.CookieNotFoundException
038: *
039: * @author <b>Jason Hunter</b>, Copyright © 2000
040: * @version 1.0, 2000/03/19
041: */
042: public class CookieParser {
043:
044: private HttpServletRequest req;
045: private Hashtable cookieJar = new Hashtable();
046:
047: /**
048: * Constructs a new CookieParser to handle the cookies of the
049: * given request.
050: *
051: * @param req the servlet request
052: */
053: public CookieParser(HttpServletRequest req) {
054: this .req = req;
055: parseCookies();
056: }
057:
058: // Load the cookie values into the cookies hashtable
059: void parseCookies() {
060: Cookie[] cookies = req.getCookies();
061: if (cookies != null) {
062: for (int i = 0; i < cookies.length; i++) {
063: String name = cookies[i].getName();
064: String value = cookies[i].getValue();
065: cookieJar.put(name, value);
066: }
067: }
068: }
069:
070: /**
071: * Gets the named cookie value as a String
072: *
073: * @param name the cookie name
074: * @return the cookie value as a String
075: * @exception CookieNotFoundException if the cookie was not found
076: */
077: public String getStringCookie(String name)
078: throws CookieNotFoundException {
079: String value = (String) cookieJar.get(name);
080: if (value == null)
081: throw new CookieNotFoundException(name + " not found");
082: else
083: return value;
084: }
085:
086: /**
087: * Gets the named cookie value as a String, with a default.
088: * Returns the default value if the cookie is not found
089: *
090: * @param name the cookie name
091: * @param def the default cookie value
092: * @return the cookie value as a String, or the default
093: */
094: public String getStringCookie(String name, String def) {
095: try {
096: return getStringCookie(name);
097: } catch (Exception e) {
098: return def;
099: }
100: }
101:
102: /**
103: * Gets the named cookie value as a boolean
104: *
105: * @param name the cookie name
106: * @return the cookie value as a boolean
107: * @exception CookieNotFoundException if the cookie was not found
108: */
109: public boolean getBooleanCookie(String name)
110: throws CookieNotFoundException {
111: return new Boolean(getStringCookie(name)).booleanValue();
112: }
113:
114: /**
115: * Gets the named cookie value as a boolean, with a default.
116: * Returns the default value if the cookie is not found.
117: *
118: * @param name the cookie name
119: * @param def the default cookie value
120: * @return the cookie value as a boolean, or the default
121: */
122: public boolean getBooleanCookie(String name, boolean def) {
123: try {
124: return getBooleanCookie(name);
125: } catch (Exception e) {
126: return def;
127: }
128: }
129:
130: /**
131: * Gets the named cookie value as a byte
132: *
133: * @param name the cookie name
134: * @return the cookie value as a byte
135: * @exception CookieNotFoundException if the cookie was not found
136: * @exception NumberFormatException if the cookie value could not
137: * be converted to a byte
138: */
139: public byte getByteCookie(String name)
140: throws CookieNotFoundException, NumberFormatException {
141: return Byte.parseByte(getStringCookie(name));
142: }
143:
144: /**
145: * Gets the named cookie value as a byte, with a default.
146: * Returns the default value if the cookie is not found or cannot
147: * be converted to a byte.
148: *
149: * @param name the cookie name
150: * @param def the default cookie value
151: * @return the cookie value as a byte, or the default
152: */
153: public byte getByteCookie(String name, byte def) {
154: try {
155: return getByteCookie(name);
156: } catch (Exception e) {
157: return def;
158: }
159: }
160:
161: /**
162: * Gets the named cookie value as a char
163: *
164: * @param name the cookie name
165: * @return the cookie value as a char
166: * @exception CookieNotFoundException if the cookie was not found
167: */
168: public char getCharCookie(String name)
169: throws CookieNotFoundException {
170: String param = getStringCookie(name);
171: if (param.length() == 0)
172: throw new CookieNotFoundException(name + " is empty string");
173: else
174: return (param.charAt(0));
175: }
176:
177: /**
178: * Gets the named cookie value as a char, with a default.
179: * Returns the default value if the cookie is not found.
180: *
181: * @param name the cookie name
182: * @param def the default cookie value
183: * @return the cookie value as a char, or the default
184: */
185: public char getCharCookie(String name, char def) {
186: try {
187: return getCharCookie(name);
188: } catch (Exception e) {
189: return def;
190: }
191: }
192:
193: /**
194: * Gets the named cookie value as a double
195: *
196: * @param name the cookie name
197: * @return the cookie value as a double
198: * @exception CookieNotFoundException if the cookie was not found
199: * @exception NumberFormatException if the cookie could not be converted
200: * to a double
201: */
202: public double getDoubleCookie(String name)
203: throws CookieNotFoundException, NumberFormatException {
204: return new Double(getStringCookie(name)).doubleValue();
205: }
206:
207: /**
208: * Gets the named cookie value as a double, with a default.
209: * Returns the default value if the cookie is not found.
210: *
211: * @param name the cookie name
212: * @param def the default cookie value
213: * @return the cookie value as a double, or the default
214: */
215: public double getDoubleCookie(String name, double def) {
216: try {
217: return getDoubleCookie(name);
218: } catch (Exception e) {
219: return def;
220: }
221: }
222:
223: /**
224: * Gets the named cookie value as a float
225: *
226: * @param name the cookie name
227: * @return the cookie value as a float
228: * @exception CookieNotFoundException if the cookie was not found
229: * @exception NumberFormatException if the cookie could not be converted
230: * to a float
231: */
232: public float getFloatCookie(String name)
233: throws CookieNotFoundException, NumberFormatException {
234: return new Float(getStringCookie(name)).floatValue();
235: }
236:
237: /**
238: * Gets the named cookie value as a float, with a default.
239: * Returns the default value if the cookie is not found.
240: *
241: * @param name the cookie name
242: * @param def the default cookie value
243: * @return the cookie value as a float, or the default
244: */
245: public float getFloatCookie(String name, float def) {
246: try {
247: return getFloatCookie(name);
248: } catch (Exception e) {
249: return def;
250: }
251: }
252:
253: /**
254: * Gets the named cookie value as a int
255: *
256: * @param name the cookie name
257: * @return the cookie value as a int
258: * @exception CookieNotFoundException if the cookie was not found
259: * @exception NumberFormatException if the cookie could not be converted
260: * to a int
261: */
262: public int getIntCookie(String name)
263: throws CookieNotFoundException, NumberFormatException {
264: return Integer.parseInt(getStringCookie(name));
265: }
266:
267: /**
268: * Gets the named cookie value as a int, with a default.
269: * Returns the default value if the cookie is not found.
270: *
271: * @param name the cookie name
272: * @param def the default cookie value
273: * @return the cookie value as a int, or the default
274: */
275: public int getIntCookie(String name, int def) {
276: try {
277: return getIntCookie(name);
278: } catch (Exception e) {
279: return def;
280: }
281: }
282:
283: /**
284: * Gets the named cookie value as a long
285: *
286: * @param name the cookie name
287: * @return the cookie value as a long
288: * @exception CookieNotFoundException if the cookie was not found
289: * @exception NumberFormatException if the cookie could not be converted
290: * to a long
291: */
292: public long getLongCookie(String name)
293: throws CookieNotFoundException, NumberFormatException {
294: return Long.parseLong(getStringCookie(name));
295: }
296:
297: /**
298: * Gets the named cookie value as a long, with a default.
299: * Returns the default value if the cookie is not found.
300: *
301: * @param name the cookie name
302: * @param def the default cookie value
303: * @return the cookie value as a long, or the default
304: */
305: public long getLongCookie(String name, long def) {
306: try {
307: return getLongCookie(name);
308: } catch (Exception e) {
309: return def;
310: }
311: }
312:
313: /**
314: * Gets the named cookie value as a short
315: *
316: * @param name the cookie name
317: * @return the cookie value as a short
318: * @exception CookieNotFoundException if the cookie was not found
319: * @exception NumberFormatException if the cookie could not be converted
320: * to a short
321: */
322: public short getShortCookie(String name)
323: throws CookieNotFoundException, NumberFormatException {
324: return Short.parseShort(getStringCookie(name));
325: }
326:
327: /**
328: * Gets the named cookie value as a short, with a default.
329: * Returns the default value if the cookie is not found.
330: *
331: * @param name the cookie name
332: * @param def the default cookie value
333: * @return the cookie value as a short, or the default
334: */
335: public short getShortCookie(String name, short def) {
336: try {
337: return getShortCookie(name);
338: } catch (Exception e) {
339: return def;
340: }
341: }
342: }
|