1 31 32 package org.apache.commons.httpclient; 33 34 import java.io.Serializable ; 35 import java.text.RuleBasedCollator ; 36 import java.util.Comparator ; 37 import java.util.Date ; 38 import java.util.Locale ; 39 40 import org.apache.commons.httpclient.cookie.CookiePolicy; 41 import org.apache.commons.httpclient.cookie.CookieSpec; 42 import org.apache.commons.logging.Log; 43 import org.apache.commons.logging.LogFactory; 44 45 46 66 67 public class Cookie extends NameValuePair implements Serializable , Comparator { 68 69 71 74 75 public Cookie() { 76 this(null, "noname", null, null, null, false); 77 } 78 79 86 public Cookie(String domain, String name, String value) { 87 this(domain, name, value, null, null, false); 88 } 89 90 107 public Cookie(String domain, String name, String value, 108 String path, Date expires, boolean secure) { 109 110 super(name, value); 111 LOG.trace("enter Cookie(String, String, String, String, Date, boolean)"); 112 if (name == null) { 113 throw new IllegalArgumentException ("Cookie name may not be null"); 114 } 115 if (name.trim().equals("")) { 116 throw new IllegalArgumentException ("Cookie name may not be blank"); 117 } 118 this.setPath(path); 119 this.setDomain(domain); 120 this.setExpiryDate(expires); 121 this.setSecure(secure); 122 } 123 124 138 public Cookie(String domain, String name, String value, String path, 139 int maxAge, boolean secure) { 140 141 this(domain, name, value, path, null, secure); 142 if (maxAge < -1) { 143 throw new IllegalArgumentException ("Invalid max age: " + Integer.toString(maxAge)); 144 } 145 if (maxAge >= 0) { 146 setExpiryDate(new Date (System.currentTimeMillis() + maxAge * 1000L)); 147 } 148 } 149 150 158 public String getComment() { 159 return cookieComment; 160 } 161 162 170 public void setComment(String comment) { 171 cookieComment = comment; 172 } 173 174 185 public Date getExpiryDate() { 186 return cookieExpiryDate; 187 } 188 189 200 public void setExpiryDate (Date expiryDate) { 201 cookieExpiryDate = expiryDate; 202 } 203 204 205 212 public boolean isPersistent() { 213 return (null != cookieExpiryDate); 214 } 215 216 217 224 public String getDomain() { 225 return cookieDomain; 226 } 227 228 235 public void setDomain(String domain) { 236 if (domain != null) { 237 int ndx = domain.indexOf(":"); 238 if (ndx != -1) { 239 domain = domain.substring(0, ndx); 240 } 241 cookieDomain = domain.toLowerCase(); 242 } 243 } 244 245 246 253 public String getPath() { 254 return cookiePath; 255 } 256 257 265 public void setPath(String path) { 266 cookiePath = path; 267 } 268 269 273 public boolean getSecure() { 274 return isSecure; 275 } 276 277 289 public void setSecure (boolean secure) { 290 isSecure = secure; 291 } 292 293 302 public int getVersion() { 303 return cookieVersion; 304 } 305 306 314 public void setVersion(int version) { 315 cookieVersion = version; 316 } 317 318 323 public boolean isExpired() { 324 return (cookieExpiryDate != null 325 && cookieExpiryDate.getTime() <= System.currentTimeMillis()); 326 } 327 328 335 public boolean isExpired(Date now) { 336 return (cookieExpiryDate != null 337 && cookieExpiryDate.getTime() <= now.getTime()); 338 } 339 340 341 354 public void setPathAttributeSpecified(boolean value) { 355 hasPathAttribute = value; 356 } 357 358 367 public boolean isPathAttributeSpecified() { 368 return hasPathAttribute; 369 } 370 371 384 public void setDomainAttributeSpecified(boolean value) { 385 hasDomainAttribute = value; 386 } 387 388 397 public boolean isDomainAttributeSpecified() { 398 return hasDomainAttribute; 399 } 400 401 406 public int hashCode() { 407 return super.hashCode() 408 ^ (null == cookiePath ? 0 : cookiePath.hashCode()) 409 ^ (null == cookieDomain ? 0 : cookieDomain.hashCode()); 410 } 411 412 413 418 public boolean equals(Object obj) { 419 LOG.trace("enter Cookie.equals(Object)"); 420 421 if ((obj != null) && (obj instanceof Cookie)) { 422 Cookie that = (Cookie) obj; 423 return 424 (null == this.getName() 425 ? null == that.getName() 426 : this.getName().equals(that.getName())) 427 && (null == this.getPath() 428 ? null == that.getPath() 429 : this.getPath().equals(that.getPath())) 430 && (null == this.getDomain() 431 ? null == that.getDomain() 432 : this.getDomain().equals(that.getDomain())); 433 } else { 434 return false; 435 } 436 } 437 438 439 444 public String toExternalForm() { 445 return CookiePolicy.getSpecByVersion( 446 getVersion()).formatCookie(this); 447 } 448 449 462 public boolean matches( 463 String domain, int port, String path, boolean secure, Date date) { 464 465 LOG.trace("enter Cookie.matches(Strinng, int, String, boolean, Date"); 466 CookieSpec matcher = CookiePolicy.getDefaultSpec(); 467 return matcher.match(domain, port, path, secure, this); 468 } 469 470 481 public boolean matches( 482 String domain, int port, String path, boolean secure) { 483 LOG.trace("enter Cookie.matches(String, int, String, boolean"); 484 return matches(domain, port, path, secure, new Date ()); 485 } 486 487 502 public static Header createCookieHeader(String domain, String path, 503 Cookie[] cookies) { 504 505 LOG.trace("enter Cookie.createCookieHeader(String,String,Cookie[])"); 506 return Cookie.createCookieHeader(domain, path, false, cookies); 507 } 508 509 526 public static Header createCookieHeader(String domain, String path, 527 boolean secure, Cookie[] cookies) 528 throws IllegalArgumentException { 529 530 LOG.trace("enter Cookie.createCookieHeader(" 531 + "String, String, boolean, Cookie[])"); 532 533 if (domain == null) { 536 throw new IllegalArgumentException ("null domain in " 537 + "createCookieHeader."); 538 } 539 int port = secure ? 443 : 80; 541 int ndx = domain.indexOf(":"); 542 if (ndx != -1) { 543 try { 544 port = Integer.parseInt(domain.substring(ndx + 1, 545 domain.length())); 546 } catch (NumberFormatException e) { 547 LOG.warn("Cookie.createCookieHeader(): " 549 + "Invalid port number in domain " + domain); 550 } 551 } 552 return Cookie.createCookieHeader(domain, port, path, secure, cookies); 553 } 554 555 573 public static Header createCookieHeader(String domain, int port, 574 String path, boolean secure, Cookie[] cookies) 575 throws IllegalArgumentException { 576 LOG.trace("enter Cookie.createCookieHeader(String, int, String, boolean, Cookie[])"); 577 return Cookie.createCookieHeader(domain, port, path, secure, new Date (), cookies); 578 } 579 580 599 600 public static Header createCookieHeader( 601 String domain, int port, String path, boolean secure, 602 Date now, Cookie[] cookies) 603 throws IllegalArgumentException { 604 605 LOG.trace("enter Cookie.createCookieHeader(String, int, String, boolean, Date, Cookie[])"); 606 CookieSpec matcher = CookiePolicy.getDefaultSpec(); 607 cookies = matcher.match(domain, port, path, secure, cookies); 608 if ((cookies != null) && (cookies.length > 0)) { 609 return matcher.formatCookieHeader(cookies); 610 } else { 611 return null; 612 } 613 } 614 615 625 public int compare(Object o1, Object o2) { 626 LOG.trace("enter Cookie.compare(Object, Object)"); 627 628 if (!(o1 instanceof Cookie)) { 629 throw new ClassCastException (o1.getClass().getName()); 630 } 631 if (!(o2 instanceof Cookie)) { 632 throw new ClassCastException (o2.getClass().getName()); 633 } 634 Cookie c1 = (Cookie) o1; 635 Cookie c2 = (Cookie) o2; 636 if (c1.getPath() == null && c2.getPath() == null) { 637 return 0; 638 } else if (c1.getPath() == null) { 639 if (c2.getPath().equals(CookieSpec.PATH_DELIM)) { 641 return 0; 642 } else { 643 return -1; 644 } 645 } else if (c2.getPath() == null) { 646 if (c1.getPath().equals(CookieSpec.PATH_DELIM)) { 648 return 0; 649 } else { 650 return 1; 651 } 652 } else { 653 return STRING_COLLATOR.compare(c1.getPath(), c2.getPath()); 654 } 655 } 656 657 661 public String toString() { 662 return toExternalForm(); 663 } 664 665 683 public static Cookie[] parse( 684 String domain, int port, String path, Header setCookie) 685 throws HttpException, IllegalArgumentException { 686 687 LOG.trace("enter Cookie.parse(String, int, String, Header)"); 688 return Cookie.parse(domain, port, path, false, setCookie); 689 } 690 691 707 public static Cookie[] parse(String domain, String path, Header setCookie) 708 throws HttpException, IllegalArgumentException { 709 LOG.trace("enter Cookie.parse(String, String, Header)"); 710 return Cookie.parse (domain, 80, path, false, setCookie); 711 } 712 713 730 public static Cookie[] parse(String domain, String path, 731 boolean secure, Header setCookie) 732 throws HttpException, IllegalArgumentException { 733 734 LOG.trace ("enter Cookie.parse(String, String, boolean, Header)"); 735 return Cookie.parse ( 736 domain, (secure ? 443 : 80), path, secure, setCookie); 737 } 738 739 772 public static Cookie[] parse(String domain, int port, String path, 773 boolean secure, Header setCookie) 774 throws HttpException { 775 776 LOG.trace("enter Cookie.parse(String, int, String, boolean, Header)"); 777 778 CookieSpec parser = CookiePolicy.getDefaultSpec(); 779 Cookie[] cookies = parser.parse(domain, port, path, secure, setCookie); 780 781 for (int i = 0; i < cookies.length; i++) { 782 final Cookie cookie = cookies[i]; 783 final CookieSpec validator 784 = CookiePolicy.getSpecByVersion(cookie.getVersion()); 785 validator.validate(domain, port, path, secure, cookie); 786 } 787 return cookies; 788 } 789 790 792 793 private String cookieComment; 794 795 796 private String cookieDomain; 797 798 799 private Date cookieExpiryDate; 800 801 802 private String cookiePath; 803 804 805 private boolean isSecure; 806 807 811 private boolean hasPathAttribute = false; 812 813 817 private boolean hasDomainAttribute = false; 818 819 820 private int cookieVersion = 0; 821 822 824 828 private static final RuleBasedCollator STRING_COLLATOR = 829 (RuleBasedCollator ) RuleBasedCollator.getInstance( 830 new Locale ("en", "US", "")); 831 832 833 private static final Log LOG = LogFactory.getLog(Cookie.class); 834 835 } 836 837
| Popular Tags
|