Class Cookie
java.lang.Object
org.apache.commons.httpclient.NameValuePair
org.apache.commons.httpclient.Cookie
- All Implemented Interfaces:
Serializable, Comparator
- Direct Known Subclasses:
Cookie2
HTTP "magic-cookie" represents a piece of state information that the HTTP agent and the target server can exchange to maintain a session.
- Version:
- $Revision: 531354 $ $Date: 2007-04-23 08:53:20 +0200 (Mon, 23 Apr 2007) $
- Author:
- B.C. Holmes, Park, Sung-Gu, Doug Sale, Rod Waldhoff, dIon Gillard, Sean C. Sullivan, John Evans, Marc A. Saegesser, Oleg Kalnichevski, Mike Bowler
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCookie()Default constructor.Creates a cookie with the given name, value and domain attribute.Creates a cookie with the given name, value, domain attribute, path attribute, maximum age attribute, and secure attributeCreates a cookie with the given name, value, domain attribute, path attribute, expiration attribute, and secure attribute -
Method Summary
Modifier and TypeMethodDescriptionintCompares two cookies to determine order for cookie header.booleanTwo cookies are equal if the name, path and domain match.Returns the comment describing the purpose of this cookie, or null if no such comment has been defined.Returns domain attribute of the cookie.Returns the expirationDateof the cookie, or null if none exists.getPath()Returns the path attribute of the cookiebooleanintReturns the version of the cookie specification to which this cookie conforms.inthashCode()Returns a hash code in keeping with theObject.hashCode()general hashCode contract.booleanReturns true if cookie's domain was set via a domain attribute in the Set-Cookie header.booleanReturns true if this cookie has expired.booleanReturns true if this cookie has expired according to the time passed in.booleanReturns true if cookie's path was set via a path attribute in the Set-Cookie header.booleanReturns false if the cookie should be discarded at the end of the "session"; true otherwise.voidsetComment(String comment) If a user agent (web browser) presents this cookie to a user, the cookie's purpose will be described using this comment.voidSets the domain attribute.voidsetDomainAttributeSpecified(boolean value) Indicates whether the cookie had a domain specified in a domain attribute of the Set-Cookie header.voidsetExpiryDate(Date expiryDate) Sets expiration date.voidSets the path attribute.voidsetPathAttributeSpecified(boolean value) Indicates whether the cookie had a path specified in a path attribute of the Set-Cookie header.voidsetSecure(boolean secure) Sets the secure attribute of the cookie.voidsetVersion(int version) Sets the version of the cookie specification to which this cookie conforms.Return a textual representation of the cookie.toString()Return a textual representation of the cookie.Methods inherited from class NameValuePair
getName, getValue, setName, setValueMethods inherited from interface Comparator
reversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
-
Constructor Details
-
Cookie
public Cookie()Default constructor. Creates a blank cookie -
Cookie
-
Cookie
Creates a cookie with the given name, value, domain attribute, path attribute, expiration attribute, and secure attribute- Parameters:
domain- the domain this cookie can be sent toname- the cookie namevalue- the cookie valuepath- the path prefix for which this cookie can be sentexpires- theDateat which this cookie expires, or null if the cookie expires at the end of the sessionsecure- 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 $
-
Cookie
Creates a cookie with the given name, value, domain attribute, path attribute, maximum age attribute, and secure attribute- Parameters:
domain- the domain this cookie can be sent toname- the cookie namevalue- the cookie valuepath- the path prefix for which this cookie can be sentmaxAge- 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 Details
-
getComment
Returns the comment describing the purpose of this cookie, or null if no such comment has been defined.- Returns:
- comment
- See Also:
-
setComment
If a user agent (web browser) presents this cookie to a user, the cookie's purpose will be described using this comment.- Parameters:
comment-- See Also:
-
getExpiryDate
-
setExpiryDate
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.
- Parameters:
expiryDate- theDateafter which this cookie is no longer valid.- See Also:
-
isPersistent
public boolean isPersistent()Returns false if the cookie should be discarded at the end of the "session"; true otherwise.- Returns:
- false if the cookie should be discarded at the end of the "session"; true otherwise
-
getDomain
Returns domain attribute of the cookie.- Returns:
- the value of the domain attribute
- See Also:
-
setDomain
Sets the domain attribute.- Parameters:
domain- The value of the domain attribute- See Also:
-
getPath
Returns the path attribute of the cookie- Returns:
- The value of the path attribute.
- See Also:
-
setPath
Sets the path attribute.- Parameters:
path- The value of the path attribute- See Also:
-
getSecure
public boolean getSecure()- Returns:
trueif this cookie should only be sent over secure connections.- See Also:
-
setSecure
public void setSecure(boolean 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.
- Parameters:
secure- The value of the secure attribute- See Also:
-
getVersion
public int getVersion()Returns the version of the cookie specification to which this cookie conforms.- Returns:
- the version of the cookie.
- See Also:
-
setVersion
public void setVersion(int version) Sets the version of the cookie specification to which this cookie conforms.- Parameters:
version- the version of the cookie.- See Also:
-
isExpired
public boolean isExpired()Returns true if this cookie has expired.- Returns:
- true if the cookie has expired.
-
isExpired
Returns true if this cookie has expired according to the time passed in.- Parameters:
now- The current time.- Returns:
- true if the cookie expired.
-
setPathAttributeSpecified
public void setPathAttributeSpecified(boolean 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.- Parameters:
value- true if the cookie's path was explicitly set, false otherwise.- See Also:
-
isPathAttributeSpecified
public boolean isPathAttributeSpecified()Returns true if cookie's path was set via a path attribute in the Set-Cookie header.- Returns:
- value true if the cookie's path was explicitly set, false otherwise.
- See Also:
-
setDomainAttributeSpecified
public void setDomainAttributeSpecified(boolean 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.- Parameters:
value- true if the cookie's domain was explicitly set, false otherwise.- See Also:
-
isDomainAttributeSpecified
public boolean isDomainAttributeSpecified()Returns true if cookie's domain was set via a domain attribute in the Set-Cookie header.- Returns:
- value true if the cookie's domain was explicitly set, false otherwise.
- See Also:
-
hashCode
public int hashCode()Returns a hash code in keeping with theObject.hashCode()general hashCode contract.- Overrides:
hashCodein classNameValuePair- Returns:
- A hash code
-
equals
Two cookies are equal if the name, path and domain match.- Specified by:
equalsin interfaceComparator- Overrides:
equalsin classNameValuePair- Parameters:
obj- The object to compare against.- Returns:
- true if the two objects are equal.
-
toExternalForm
-
compare
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.
- Specified by:
comparein interfaceComparator- Parameters:
o1- The first object to be comparedo2- The second object to be compared- Returns:
- See
Comparator.compare(Object,Object)
-
toString
Return a textual representation of the cookie.- Overrides:
toStringin classNameValuePair- Returns:
- string.
- See Also:
-