HTTP "magic-cookie" represents a piece of state information
that the HTTP agent and the target server can exchange to maintain
a session.
| Constructor: |
public Cookie() {
this(null, "noname", null, null, null, false);
}
Default constructor. Creates a blank cookie |
public Cookie(String domain,
String name,
String value) {
this(domain, name, value, null, null, false);
}
Creates a cookie with the given name, value and domain attribute. Parameters:
name - the cookie name
value - the cookie value
domain - the domain this cookie can be sent to
|
public Cookie(String domain,
String name,
String value,
String path,
Date expires,
boolean secure) {
super(name, value);
LOG.trace("enter Cookie(String, String, String, String, Date, boolean)");
if (name == null) {
throw new IllegalArgumentException("Cookie name may not be null");
}
if (name.trim().equals("")) {
throw new IllegalArgumentException("Cookie name may not be blank");
}
this.setPath(path);
this.setDomain(domain);
this.setExpiryDate(expires);
this.setSecure(secure);
}
Creates a cookie with the given name, value, domain attribute,
path attribute, expiration attribute, and secure attribute Parameters:
name - the cookie name
value - the cookie value
domain - the domain this cookie can be sent to
path - the path prefix for which this cookie can be sent
expires - the Date at which this cookie expires,
or null if the cookie expires at the end
of the session
secure - if true this cookie can only be sent over secure
connections
Throws:
IllegalArgumentException - If cookie name is null or blank,
cookie name contains a blank, or cookie name starts with character $
|
public Cookie(String domain,
String name,
String value,
String path,
int maxAge,
boolean secure) {
this(domain, name, value, path, null, secure);
if (maxAge < -1) {
throw new IllegalArgumentException("Invalid max age: " + Integer.toString(maxAge));
}
if (maxAge >= 0) {
setExpiryDate(new Date(System.currentTimeMillis() + maxAge * 1000L));
}
}
Creates a cookie with the given name, value, domain attribute,
path attribute, maximum age attribute, and secure attribute Parameters:
name - the cookie name
value - the cookie value
domain - the domain this cookie can be sent to
path - the path prefix for which this cookie can be sent
maxAge - the number of seconds for which this cookie is valid.
maxAge is expected to be a non-negative number.
-1 signifies that the cookie should never expire.
secure - if true this cookie can only be sent over secure
connections
|
| Method from org.apache.commons.httpclient.Cookie Detail: |
public int compare(Object o1,
Object o2) {
LOG.trace("enter Cookie.compare(Object, Object)");
if (!(o1 instanceof Cookie)) {
throw new ClassCastException(o1.getClass().getName());
}
if (!(o2 instanceof Cookie)) {
throw new ClassCastException(o2.getClass().getName());
}
Cookie c1 = (Cookie) o1;
Cookie c2 = (Cookie) o2;
if (c1.getPath() == null && c2.getPath() == null) {
return 0;
} else if (c1.getPath() == null) {
// null is assumed to be "/"
if (c2.getPath().equals(CookieSpec.PATH_DELIM)) {
return 0;
} else {
return -1;
}
} else if (c2.getPath() == null) {
// null is assumed to be "/"
if (c1.getPath().equals(CookieSpec.PATH_DELIM)) {
return 0;
} else {
return 1;
}
} else {
return c1.getPath().compareTo(c2.getPath());
}
}
Compares two cookies to determine order for cookie header.
Most specific should be first.
This method is implemented so a cookie can be used as a comparator for
a SortedSet of cookies. Specifically it's used above in the
createCookieHeader method.
|
public boolean equals(Object obj) {
if (obj == null) return false;
if (this == obj) return true;
if (obj instanceof Cookie) {
Cookie that = (Cookie) obj;
return LangUtils.equals(this.getName(), that.getName())
&& LangUtils.equals(this.cookieDomain, that.cookieDomain)
&& LangUtils.equals(this.cookiePath, that.cookiePath);
} else {
return false;
}
}
Two cookies are equal if the name, path and domain match. |
public String getComment() {
return cookieComment;
}
Returns the comment describing the purpose of this cookie, or
null if no such comment has been defined. |
public String getDomain() {
return cookieDomain;
}
Returns domain attribute of the cookie. |
public Date getExpiryDate() {
return cookieExpiryDate;
}
Returns the expiration Date of the cookie, or null
if none exists.
Note: the object returned by this method is
considered immutable. Changing it (e.g. using setTime()) could result
in undefined behaviour. Do so at your peril. |
public String getPath() {
return cookiePath;
}
Returns the path attribute of the cookie |
public boolean getSecure() {
return isSecure;
}
|
public int getVersion() {
return cookieVersion;
}
Returns the version of the cookie specification to which this
cookie conforms. |
public int hashCode() {
int hash = LangUtils.HASH_SEED;
hash = LangUtils.hashCode(hash, this.getName());
hash = LangUtils.hashCode(hash, this.cookieDomain);
hash = LangUtils.hashCode(hash, this.cookiePath);
return hash;
}
Returns a hash code in keeping with the
Object#hashCode general hashCode contract. |
public boolean isDomainAttributeSpecified() {
return hasDomainAttribute;
}
Returns true if cookie's domain was set via a domain
attribute in the Set-Cookie header. |
public boolean isExpired() {
return (cookieExpiryDate != null
&& cookieExpiryDate.getTime() < = System.currentTimeMillis());
}
Returns true if this cookie has expired. |
public boolean isExpired(Date now) {
return (cookieExpiryDate != null
&& cookieExpiryDate.getTime() < = now.getTime());
}
Returns true if this cookie has expired according to the time passed in. |
public boolean isPathAttributeSpecified() {
return hasPathAttribute;
}
Returns true if cookie's path was set via a path attribute
in the Set-Cookie header. |
public boolean isPersistent() {
return (null != cookieExpiryDate);
}
Returns false if the cookie should be discarded at the end
of the "session"; true otherwise. |
public void setComment(String comment) {
cookieComment = comment;
}
If a user agent (web browser) presents this cookie to a user, the
cookie's purpose will be described using this comment. |
public void setDomain(String domain) {
if (domain != null) {
int ndx = domain.indexOf(":");
if (ndx != -1) {
domain = domain.substring(0, ndx);
}
cookieDomain = domain.toLowerCase();
}
}
Sets the domain attribute. |
public void setDomainAttributeSpecified(boolean value) {
hasDomainAttribute = value;
}
Indicates whether the cookie had a domain specified in a
domain attribute of the Set-Cookie header. This value
is important for generating the Cookie header because
some cookie specifications require that the Cookie header
should only include a domain attribute if the cookie's domain
was specified in the Set-Cookie header. |
public void setExpiryDate(Date expiryDate) {
cookieExpiryDate = expiryDate;
}
Sets expiration date.
Note: the object returned by this method is considered
immutable. Changing it (e.g. using setTime()) could result in undefined
behaviour. Do so at your peril. |
public void setPath(String path) {
cookiePath = path;
}
|
public void setPathAttributeSpecified(boolean value) {
hasPathAttribute = value;
}
Indicates whether the cookie had a path specified in a
path attribute of the Set-Cookie header. This value
is important for generating the Cookie header because
some cookie specifications require that the Cookie header
should only include a path attribute if the cookie's path
was specified in the Set-Cookie header. |
public void setSecure(boolean secure) {
isSecure = secure;
}
Sets the secure attribute of the cookie.
When true the cookie should only be sent
using a secure protocol (https). This should only be set when
the cookie's originating server used a secure protocol to set the
cookie's value. |
public void setVersion(int version) {
cookieVersion = version;
}
Sets the version of the cookie specification to which this
cookie conforms. |
public String toExternalForm() {
CookieSpec spec = null;
if (getVersion() > 0) {
spec = CookiePolicy.getDefaultSpec();
} else {
spec = CookiePolicy.getCookieSpec(CookiePolicy.NETSCAPE);
}
return spec.formatCookie(this);
}
Return a textual representation of the cookie. |
public String toString() {
return toExternalForm();
}
Return a textual representation of the cookie. |