org.apache.commons.httpclient
Class Cookie

java.lang.Object
  |
  +--org.apache.commons.httpclient.NameValuePair
        |
        +--org.apache.commons.httpclient.Cookie
All Implemented Interfaces:
Comparator, Serializable

public class Cookie
extends NameValuePair
implements Serializable, Comparator

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: 1.38.2.4 $ $Date: 2004/06/05 16:32:01 $
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:
Serialized Form

Constructor Summary
Cookie()
          Default constructor.
Cookie(String domain, String name, String value)
          Creates a cookie with the given name, value and domain attribute.
Cookie(String domain, String name, String value, String path, Date expires, boolean secure)
          Creates a cookie with the given name, value, domain attribute, path attribute, expiration attribute, and secure attribute
Cookie(String domain, String name, String value, String path, int maxAge, boolean secure)
          Creates a cookie with the given name, value, domain attribute, path attribute, maximum age attribute, and secure attribute
 
Method Summary
 int compare(Object o1, Object o2)
          Compares two cookies to determine order for cookie header.
static Header createCookieHeader(String domain, int port, String path, boolean secure, Cookie[] cookies)
          Deprecated. use CookieSpec interface
static Header createCookieHeader(String domain, int port, String path, boolean secure, Date now, Cookie[] cookies)
          Deprecated. use CookieSpec interface
static Header createCookieHeader(String domain, String path, boolean secure, Cookie[] cookies)
          Deprecated. use CookieSpec interface
static Header createCookieHeader(String domain, String path, Cookie[] cookies)
          Deprecated. use CookieSpec interface
 boolean equals(Object obj)
          Two cookies are equal if the name, path and domain match.
 String getComment()
          Returns the comment describing the purpose of this cookie, or null if no such comment has been defined.
 String getDomain()
          Returns domain attribute of the cookie.
 Date getExpiryDate()
          Returns the expiration Date of the cookie, or null if none exists.
 String getPath()
          Returns the path attribute of the cookie
 boolean getSecure()
           
 int getVersion()
          Returns the version of the cookie specification to which this cookie conforms.
 int hashCode()
          Returns a hash code in keeping with the Object.hashCode() general hashCode contract.
 boolean isDomainAttributeSpecified()
          Returns true if cookie's domain was set via a domain attribute in the Set-Cookie header.
 boolean isExpired()
          Returns true if this cookie has expired.
 boolean isExpired(Date now)
          Returns true if this cookie has expired according to the time passed in.
 boolean isPathAttributeSpecified()
          Returns true if cookie's path was set via a path attribute in the Set-Cookie header.
 boolean isPersistent()
          Returns false if the cookie should be discarded at the end of the "session"; true otherwise.
 boolean matches(String domain, int port, String path, boolean secure)
          Deprecated. use CookieSpec interface
 boolean matches(String domain, int port, String path, boolean secure, Date date)
          Deprecated. use CookieSpec interface
static Cookie[] parse(String domain, int port, String path, boolean secure, Header setCookie)
          Deprecated. use CookieSpec interface
static Cookie[] parse(String domain, int port, String path, Header setCookie)
          Deprecated. use CookieSpec interface
static Cookie[] parse(String domain, String path, boolean secure, Header setCookie)
          Deprecated. use CookieSpec interface
static Cookie[] parse(String domain, String path, Header setCookie)
          Deprecated. use CookieSpec interface
 void setComment(String comment)
          If a user agent (web browser) presents this cookie to a user, the cookie's purpose will be described using this comment.
 void setDomain(String domain)
          Sets the domain attribute.
 void setDomainAttributeSpecified(boolean value)
          Indicates whether the cookie had a domain specified in a domain attribute of the Set-Cookie header.
 void setExpiryDate(Date expiryDate)
          Sets expiration date.
 void setPath(String path)
          Sets the path attribute.
 void setPathAttributeSpecified(boolean value)
          Indicates whether the cookie had a path specified in a path attribute of the Set-Cookie header.
 void setSecure(boolean secure)
          Sets the secure attribute of the cookie.
 void setVersion(int version)
          Sets the version of the cookie specification to which this cookie conforms.
 String toExternalForm()
          Returns a textual representation of the cookie.
 String toString()
          Return a textual representation of the cookie.
 
Methods inherited from class org.apache.commons.httpclient.NameValuePair
getName, getValue, setName, setValue
 
Methods inherited from class java.lang.Object
, clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Cookie

public Cookie()
Default constructor. Creates a blank cookie

Cookie

public Cookie(String domain,
              String name,
              String value)
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

Cookie

public Cookie(String domain,
              String name,
              String value,
              String path,
              Date expires,
              boolean 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 $

Cookie

public Cookie(String domain,
              String name,
              String value,
              String path,
              int maxAge,
              boolean secure)
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 Detail

getComment

public String getComment()
Returns the comment describing the purpose of this cookie, or null if no such comment has been defined.
Returns:
comment
See Also:
setComment(String)

setComment

public void setComment(String comment)
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:
getComment()

getExpiryDate

public Date getExpiryDate()
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.

Returns:
Expiration Date, or null.
See Also:
setExpiryDate(java.util.Date)

setExpiryDate

public void setExpiryDate(Date 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.

Parameters:
expiryDate - the Date after which this cookie is no longer valid.
See Also:
getExpiryDate()

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

public String getDomain()
Returns domain attribute of the cookie.
Returns:
the value of the domain attribute
See Also:
setDomain(java.lang.String)

setDomain

public void setDomain(String domain)
Sets the domain attribute.
Parameters:
domain - The value of the domain attribute
See Also:
getDomain()

getPath

public String getPath()
Returns the path attribute of the cookie
Returns:
The value of the path attribute.
See Also:
setPath(java.lang.String)

setPath

public void setPath(String path)
Sets the path attribute.
Parameters:
path - The value of the path attribute
See Also:
getPath()

getSecure

public boolean getSecure()
Returns:
true if this cookie should only be sent over secure connections.
See Also:
setSecure(boolean)

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:
getSecure()

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(int)

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:
getVersion()

isExpired

public boolean isExpired()
Returns true if this cookie has expired.
Returns:
true if the cookie has expired.

isExpired

public boolean isExpired(Date now)
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()

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:
setPathAttributeSpecified(boolean)

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()

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:
setDomainAttributeSpecified(boolean)

hashCode

public int hashCode()
Returns a hash code in keeping with the Object.hashCode() general hashCode contract.
Overrides:
hashCode in class NameValuePair
Returns:
A hash code

equals

public boolean equals(Object obj)
Two cookies are equal if the name, path and domain match.
Specified by:
equals in interface Comparator
Overrides:
equals in class NameValuePair
Parameters:
obj - The object to compare against.
Returns:
true if the two objects are equal.

toExternalForm

public String toExternalForm()
Returns a textual representation of the cookie.
Returns:
string .

matches

public boolean matches(String domain,
                       int port,
                       String path,
                       boolean secure,
                       Date date)
Deprecated. use CookieSpec interface

Return true if I should be submitted with a request with given attributes, false otherwise.
Parameters:
domain - the host to which the request is being submitted
port - the port to which the request is being submitted (currently ignored)
path - the path to which the request is being submitted
secure - true if the request is using the HTTPS protocol
date - the time at which the request is submitted
Returns:
true if the cookie matches

matches

public boolean matches(String domain,
                       int port,
                       String path,
                       boolean secure)
Deprecated. use CookieSpec interface

Return true if I should be submitted with a request with given attributes, false otherwise.
Parameters:
domain - the host to which the request is being submitted
port - the port to which the request is being submitted (currently ignored)
path - the path to which the request is being submitted
secure - True if this cookie has the secure flag set
Returns:
true if I should be submitted as above.

createCookieHeader

public static Header createCookieHeader(String domain,
                                        String path,
                                        Cookie[] cookies)
Deprecated. use CookieSpec interface

Create a Cookie header containing all non-expired cookies in cookies, associated with the given domain and path, assuming the connection is not secure.

If no cookies match, returns null.

Parameters:
domain - The domain
path - The path
cookies - The cookies to use
Returns:
The new header.

createCookieHeader

public static Header createCookieHeader(String domain,
                                        String path,
                                        boolean secure,
                                        Cookie[] cookies)
                                 throws IllegalArgumentException
Deprecated. use CookieSpec interface

Create a Cookie header containing all non-expired cookies in cookies, associated with the given domain, path and https setting.

If no cookies match, returns null.

Parameters:
domain - The domain
path - The path
secure - True if this cookie has the secure flag set
cookies - The cookies to use.
Returns:
The new header
Throws:
IllegalArgumentException - if domain or path is null

createCookieHeader

public static Header createCookieHeader(String domain,
                                        int port,
                                        String path,
                                        boolean secure,
                                        Cookie[] cookies)
                                 throws IllegalArgumentException
Deprecated. use CookieSpec interface

Create a Cookie header containing all non-expired cookies in cookies, associated with the given domain, port, path and https setting.

If no cookies match, returns null.

Parameters:
domain - The domain
port - The port
path - The path
secure - True if this cookie has the secure flag set
cookies - The cookies to use.
Returns:
The new header
Throws:
IllegalArgumentException - if domain or path is null

createCookieHeader

public static Header createCookieHeader(String domain,
                                        int port,
                                        String path,
                                        boolean secure,
                                        Date now,
                                        Cookie[] cookies)
                                 throws IllegalArgumentException
Deprecated. use CookieSpec interface

Create a Cookie header containing all cookies in cookies, associated with the given domain, port, path and https setting, and which are not expired according to the given date.

If no cookies match, returns null.

Parameters:
domain - The domain
port - The port
path - The path
secure - True if this cookie has the secure flag set
now - The date to check for expiry
cookies - The cookies to use.
Returns:
The new header
Throws:
IllegalArgumentException - if domain or path is null

compare

public int compare(Object o1,
                   Object o2)

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:
compare in interface Comparator
Parameters:
o1 - The first object to be compared
o2 - The second object to be compared
Returns:
See Comparator.compare(Object,Object)

toString

public String toString()
Return a textual representation of the cookie.
Overrides:
toString in class NameValuePair
See Also:
toExternalForm()

parse

public static Cookie[] parse(String domain,
                             int port,
                             String path,
                             Header setCookie)
                      throws HttpException,
                             IllegalArgumentException
Deprecated. use CookieSpec interface

Parses the Set-Cookie Header into an array of Cookies, assuming that the cookies were recieved on an insecure channel.
Parameters:
domain - the domain from which the Header was received
port - the port from which the Header was received (currently ignored)
path - the path from which the Header was received
setCookie - the Set-Cookie Header received from the server
Returns:
an array of Cookies parsed from the Set-Cookie Header
Throws:
HttpException - if an exception occurs during parsing
IllegalArgumentException - if domain or path are null

parse

public static Cookie[] parse(String domain,
                             String path,
                             Header setCookie)
                      throws HttpException,
                             IllegalArgumentException
Deprecated. use CookieSpec interface

Parses the Set-Cookie Header into an array of Cookies, assuming that the cookies were recieved on an insecure channel.
Parameters:
domain - the domain from which the Header was received
path - the path from which the Header was received
setCookie - the Set-Cookie Header received from the server
Returns:
an array of Cookies parsed from the Set-Cookie Header
Throws:
HttpException - if an exception occurs during parsing
IllegalArgumentException - if domain or path are null

parse

public static Cookie[] parse(String domain,
                             String path,
                             boolean secure,
                             Header setCookie)
                      throws HttpException,
                             IllegalArgumentException
Deprecated. use CookieSpec interface

Parses the Set-Cookie Header into an array of Cookies.
Parameters:
domain - the domain from which the Header was received
path - the path from which the Header was received
secure - true when the header was recieved over a secure channel
setCookie - the Set-Cookie Header received from the server
Returns:
an array of Cookies parsed from the Set-Cookie Header
Throws:
HttpException - if an exception occurs during parsing
IllegalArgumentException - if domain or path are null

parse

public static Cookie[] parse(String domain,
                             int port,
                             String path,
                             boolean secure,
                             Header setCookie)
                      throws HttpException
Deprecated. use CookieSpec interface

Parses the Set-Cookie Header into an array of Cookies.

The syntax for the Set-Cookie response header is:

 set-cookie      =    "Set-Cookie:" cookies
 cookies         =    1#cookie
 cookie          =    NAME "=" VALUE * (";" cookie-av)
 NAME            =    attr
 VALUE           =    value
 cookie-av       =    "Comment" "=" value
                 |    "Domain" "=" value
                 |    "Max-Age" "=" value
                 |    "Path" "=" value
                 |    "Secure"
                 |    "Version" "=" 1*DIGIT
 
Parameters:
domain - the domain from which the Header was received
port - The port from which the Header was received.
path - the path from which the Header was received
secure - true when the Header was received over HTTPS
setCookie - the Set-Cookie Header received from the server
Returns:
an array of Cookies parsed from the Set-Cookie Header
Throws:
HttpException - if an exception occurs during parsing


Copyright © 2001-2004 Apache Software Foundation. All Rights Reserved.