1 20 package org.apache.cactus; 21 22 import java.io.Serializable ; 23 24 import java.util.Date ; 25 26 import org.apache.cactus.internal.util.CookieUtil; 27 28 35 public class Cookie implements Serializable 36 { 37 40 private String name; 41 42 45 private String value; 46 47 51 private String comment; 52 53 57 private String domain; 58 59 63 private Date expiryDate; 64 65 69 private String path; 70 71 75 private boolean isSecure = false; 76 77 84 public Cookie(String theDomain, String theName, String theValue) 85 { 86 if (theDomain == null) 87 { 88 throw new NullPointerException ("missing cookie domain"); 89 } 90 91 if (theName == null) 92 { 93 throw new NullPointerException ("missing cookie name"); 94 } 95 96 if (theValue == null) 97 { 98 throw new NullPointerException ("missing cookie value"); 99 } 100 101 setDomain(theDomain); 102 setName(theName); 103 setValue(theValue); 104 } 105 106 111 public void setName(String theName) 112 { 113 this.name = theName; 114 } 115 116 119 public String getName() 120 { 121 return this.name; 122 } 123 124 129 public void setValue(String theValue) 130 { 131 this.value = theValue; 132 } 133 134 137 public String getValue() 138 { 139 return this.value; 140 } 141 142 148 public String getComment() 149 { 150 return this.comment; 151 } 152 153 159 public void setComment(String theComment) 160 { 161 this.comment = theComment; 162 } 163 164 169 public Date getExpiryDate() 170 { 171 return this.expiryDate; 172 } 173 174 189 public void setExpiryDate(Date theExpiryDate) 190 { 191 this.expiryDate = theExpiryDate; 192 } 193 194 198 public boolean isToBeDiscarded() 199 { 200 return (this.getExpiryDate() != null); 201 } 202 203 208 public String getDomain() 209 { 210 return this.domain; 211 } 212 213 225 public void setDomain(String theDomain) 226 { 227 int ndx = theDomain.indexOf(":"); 228 229 if (ndx != -1) 230 { 231 theDomain = theDomain.substring(0, ndx); 232 } 233 234 this.domain = theDomain.toLowerCase(); 235 } 236 237 242 public String getPath() 243 { 244 return this.path; 245 } 246 247 256 public void setPath(String thePath) 257 { 258 this.path = thePath; 259 } 260 261 264 public boolean isSecure() 265 { 266 return this.isSecure; 267 } 268 269 278 public void setSecure(boolean isSecure) 279 { 280 this.isSecure = isSecure; 281 } 282 283 286 public boolean isExpired() 287 { 288 return (this.getExpiryDate() != null 289 && this.getExpiryDate().getTime() <= System.currentTimeMillis()); 290 } 291 292 297 public int hashCode() 298 { 299 return (this.getName().hashCode() + this.getValue().hashCode() 300 + this.getDomain().hashCode()); 301 } 302 303 310 public boolean equals(Object theObject) 311 { 312 if ((theObject != null) && (theObject instanceof Cookie)) 313 { 314 Cookie other = (Cookie) theObject; 315 316 return (this.getName().equals(other.getName()) 317 && this.getPath().equals(other.getPath()) 318 && this.getDomain().equals(other.getDomain())); 319 } 320 321 return false; 322 } 323 324 327 public String toString() 328 { 329 StringBuffer buffer = new StringBuffer (); 330 331 buffer.append("name = [" + getName() + "], "); 332 buffer.append("value = [" + getValue() + "], "); 333 buffer.append("domain = [" + getDomain() + "], "); 334 buffer.append("path = [" + getPath() + "], "); 335 buffer.append("isSecure = [" + isSecure() + "], "); 336 buffer.append("comment = [" + getComment() + "], "); 337 buffer.append("expiryDate = [" + getExpiryDate() + "]"); 338 339 return buffer.toString(); 340 } 341 342 346 public static String getCookieDomain(WebRequest theRequest, 347 String theRealHost) 348 { 349 return CookieUtil.getCookieDomain(theRequest, theRealHost); 350 } 351 352 356 public static int getCookiePort(WebRequest theRequest, int theRealPort) 357 { 358 return CookieUtil.getCookiePort(theRequest, theRealPort); 359 } 360 361 365 public static String getCookiePath(WebRequest theRequest, 366 String theRealPath) 367 { 368 return CookieUtil.getCookiePath(theRequest, theRealPath); 369 } 370 } 371 | Popular Tags |