1 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 42 public class CookieParser { 43 44 private HttpServletRequest req; 45 private Hashtable cookieJar = new Hashtable(); 46 47 53 public CookieParser(HttpServletRequest req) { 54 this.req = req; 55 parseCookies(); 56 } 57 58 void parseCookies() { 60 Cookie[] cookies = req.getCookies(); 61 if (cookies != null) { 62 for (int i = 0; i < cookies.length; i++) { 63 String name = cookies[i].getName(); 64 String value = cookies[i].getValue(); 65 cookieJar.put(name, value); 66 } 67 } 68 } 69 70 77 public String getStringCookie(String name) 78 throws CookieNotFoundException { 79 String value = (String ) cookieJar.get(name); 80 if (value == null) 81 throw new CookieNotFoundException(name + " not found"); 82 else 83 return value; 84 } 85 86 94 public String getStringCookie(String name, String def) { 95 try { return getStringCookie(name); } 96 catch (Exception e) { return def; } 97 } 98 99 106 public boolean getBooleanCookie(String name) 107 throws CookieNotFoundException { 108 return new Boolean (getStringCookie(name)).booleanValue(); 109 } 110 111 119 public boolean getBooleanCookie(String name, boolean def) { 120 try { return getBooleanCookie(name); } 121 catch (Exception e) { return def; } 122 } 123 124 133 public byte getByteCookie(String name) 134 throws CookieNotFoundException, NumberFormatException { 135 return Byte.parseByte(getStringCookie(name)); 136 } 137 138 147 public byte getByteCookie(String name, byte def) { 148 try { return getByteCookie(name); } 149 catch (Exception e) { return def; } 150 } 151 152 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 176 public char getCharCookie(String name, char def) { 177 try { return getCharCookie(name); } 178 catch (Exception e) { return def; } 179 } 180 181 190 public double getDoubleCookie(String name) 191 throws CookieNotFoundException, NumberFormatException { 192 return new Double (getStringCookie(name)).doubleValue(); 193 } 194 195 203 public double getDoubleCookie(String name, double def) { 204 try { return getDoubleCookie(name); } 205 catch (Exception e) { return def; } 206 } 207 208 217 public float getFloatCookie(String name) 218 throws CookieNotFoundException, NumberFormatException { 219 return new Float (getStringCookie(name)).floatValue(); 220 } 221 222 230 public float getFloatCookie(String name, float def) { 231 try { return getFloatCookie(name); } 232 catch (Exception e) { return def; } 233 } 234 235 244 public int getIntCookie(String name) 245 throws CookieNotFoundException, NumberFormatException { 246 return Integer.parseInt(getStringCookie(name)); 247 } 248 249 257 public int getIntCookie(String name, int def) { 258 try { return getIntCookie(name); } 259 catch (Exception e) { return def; } 260 } 261 262 271 public long getLongCookie(String name) 272 throws CookieNotFoundException, NumberFormatException { 273 return Long.parseLong(getStringCookie(name)); 274 } 275 276 284 public long getLongCookie(String name, long def) { 285 try { return getLongCookie(name); } 286 catch (Exception e) { return def; } 287 } 288 289 298 public short getShortCookie(String name) 299 throws CookieNotFoundException, NumberFormatException { 300 return Short.parseShort(getStringCookie(name)); 301 } 302 303 311 public short getShortCookie(String name, short def) { 312 try { return getShortCookie(name); } 313 catch (Exception e) { return def; } 314 } 315 } 316 | Popular Tags |