1 27 package org.htmlparser.http; 28 29 import java.io.Serializable ; 30 import java.util.Date ; 31 32 62 public class Cookie 63 implements 64 Cloneable , 65 Serializable 66 { 67 private static final String mSpecials = "()<>@,;:\\\"/[]?={} \t"; 71 72 75 protected String mName; 76 77 80 protected String mValue; 82 85 protected String mComment; 86 87 90 protected String mDomain; 91 92 95 protected Date mExpiry; 96 97 100 protected String mPath; 101 102 105 protected boolean mSecure; 106 107 110 protected int mVersion; 111 112 126 public Cookie (String name, String value) 127 { 128 if (!isToken (name) || name.equalsIgnoreCase ("Comment") || name.equalsIgnoreCase ("Discard") || name.equalsIgnoreCase ("Domain") 131 || name.equalsIgnoreCase ("Expires") || name.equalsIgnoreCase ("Max-Age") || name.equalsIgnoreCase ("Path") 134 || name.equalsIgnoreCase ("Secure") 135 || name.equalsIgnoreCase ("Version")) 136 throw new IllegalArgumentException ("invalid cookie name: " + name); 137 mName = name; 138 mValue = value; 139 mComment = null; 140 mDomain = null; 141 mExpiry = null; mPath = "/"; 143 mSecure = false; 144 mVersion = 0; 145 } 146 147 154 public void setComment (String purpose) 155 { 156 mComment = purpose; 157 } 158 159 165 public String getComment () 166 { 167 return (mComment); 168 } 169 170 180 public void setDomain (String pattern) 181 { 182 mDomain = pattern.toLowerCase (); } 184 185 190 public String getDomain () 191 { 192 return (mDomain); 193 } 194 195 203 public void setExpiryDate (Date expiry) 204 { 205 mExpiry = expiry; 206 } 207 208 215 public Date getExpiryDate () 216 { 217 return (mExpiry); 218 } 219 220 229 public void setPath (String uri) 230 { 231 mPath = uri; 232 } 233 234 239 public String getPath () 240 { 241 return (mPath); 242 } 243 244 251 public void setSecure (boolean flag) 252 { 253 mSecure = flag; 254 } 255 256 261 public boolean getSecure () 262 { 263 return (mSecure); 264 } 265 266 270 public String getName () 271 { 272 return (mName); 273 } 274 275 288 public void setValue (String newValue) 289 { 290 mValue = newValue; 291 } 292 293 298 public String getValue () 299 { 300 return (mValue); 301 } 302 303 312 public int getVersion () 313 { 314 return (mVersion); 315 } 316 317 324 public void setVersion (int version) 325 { 326 mVersion = version; 327 } 328 329 332 private boolean isToken (String value) 333 { 334 int length; 335 char c; 336 boolean ret; 337 338 ret = true; 339 340 length = value.length (); 341 for (int i = 0; i < length && ret; i++) 342 { 343 c = value.charAt (i); 344 if (c < 0x20 || c >= 0x7f || mSpecials.indexOf (c) != -1) 345 ret = false; 346 } 347 348 return (ret); 349 } 350 351 354 public Object clone () 355 { 356 try 357 { 358 return (super.clone ()); 359 } 360 catch (CloneNotSupportedException e) 361 { 362 throw new RuntimeException (e.getMessage ()); 363 } 364 } 365 366 370 public String toString () 371 { 372 StringBuffer ret; 373 374 ret = new StringBuffer (50); 375 if (getSecure ()) 376 ret.append ("secure "); 377 if (0 != getVersion ()) 378 { 379 ret.append ("version "); 380 ret.append (getVersion ()); 381 ret.append (" "); 382 } 383 ret.append ("cookie"); 384 if (null != getDomain ()) 385 { 386 ret.append (" for "); 387 ret.append (getDomain ()); 388 389 if (null != getPath ()) 390 ret.append (getPath ()); 391 } 392 else 393 { 394 if (null != getPath ()) 395 { 396 ret.append (" (path "); 397 ret.append (getPath ()); 398 ret.append (")"); 399 } 400 } 401 ret.append (": "); 402 ret.append (getName ()); 403 ret.append ("="); 404 if (getValue ().length () > 40) 405 { 406 ret.append (getValue ().substring (1, 40)); 407 ret.append ("..."); 408 } 409 else 410 ret.append (getValue ()); 411 if (null != getComment ()) 412 { 413 ret.append (" // "); 414 ret.append (getComment ()); 415 } 416 417 return (ret.toString ()); 418 } 419 } | Popular Tags |