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
005package com.oreilly.servlet;
006
007import java.io.*;
008import java.util.*;
009import javax.servlet.*;
010import 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 * &nbsp;
023 * float ratio = parser.getFloatCookie("ratio", 1.0);
024 * &nbsp;
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 &#169; 2000
040 * @version 1.0, 2000/03/19
041 */
042public 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 { return getStringCookie(name); }
096    catch (Exception e) { return def; }
097  }
098
099  /**
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 name)
107      throws CookieNotFoundException {
108    return new Boolean(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 name, boolean def) {
120    try { return getBooleanCookie(name); }
121    catch (Exception 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 name)
134      throws CookieNotFoundException, NumberFormatException {
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 name, byte def) {
148    try { return getByteCookie(name); }
149    catch (Exception 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 name)
160      throws CookieNotFoundException {
161    String 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 name, char def) {
177    try { return getCharCookie(name); }
178    catch (Exception 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 name)
191      throws CookieNotFoundException, NumberFormatException {
192    return new Double(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 name, double def) {
204    try { return getDoubleCookie(name); }
205    catch (Exception 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 name)
218      throws CookieNotFoundException, NumberFormatException {
219    return new Float(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 name, float def) {
231    try { return getFloatCookie(name); }
232    catch (Exception 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 name)
245      throws CookieNotFoundException, NumberFormatException {
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 name, int def) {
258    try { return getIntCookie(name); }
259    catch (Exception 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 name)
272      throws CookieNotFoundException, NumberFormatException {
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 name, long def) {
285    try { return getLongCookie(name); }
286    catch (Exception 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 name)
299      throws CookieNotFoundException, NumberFormatException {
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 name, short def) {
312    try { return getShortCookie(name); }
313    catch (Exception e) { return def; }
314  }
315}