Agent System POND 1.2 (28.2.2002)

FIM.Util.WWW.Cookies
Class Cookie

java.lang.Object
  |
  +--FIM.Util.WWW.Cookies.Cookie
All Implemented Interfaces:
Serializable

public class Cookie
extends Object
implements Serializable

A single cookie. It can be stored to a stream and supports matching the domain and path. Additionally stores also when the cookie was last sent (to allow deleting the least recently used cookie).

Version:
1.0, 1.7.2000
Author:
Michael Sonntag
See Also:
Serialized Form

Field Summary
static int COOKIE_SIZE
          The maximum size of the name and the value of a cookie together.
protected  String domain
          The domain of the cookie (from where it was sent; hostname).
protected  Date expires
          The date when to cookie expires (will be deleted after this time).
protected  Date lastSent
          The time this cookie was last handed out.
protected  String name
          The name of the cookie.
protected  String path
          The path of the cookie (the subset of URLs for which this cookie is valid).
protected  boolean secure
          Flag to indicate whether the cookie is secure or not (if true it will only be sent when using a secure communications channel).
protected  boolean strict
          Whether checking should be done strict (e. g. domain).
protected  String value
          The value of the cookie.
 
Constructor Summary
Cookie(String name, String value, String path, String domain, String expires, boolean secure)
          Creates a new cookie with loose checking.
Cookie(String name, String value, String path, String domain, String expireString, boolean secure, boolean strict)
          Creates a new cookie.
 
Method Summary
protected  void checkDomainValidity()
          Checks whther the domain provided is valid according to the specification or not.
protected  void checkString(String str)
          Checks wether the string contains ";", "," or a whitespace (" ", \r, \n, \t, \f).
 boolean equals(Object obj)
          Compares two cookies for equality.
 String getDomain()
          Retrieve the domain for this cookie.
 Date getExpiryDate()
          Retrieve the expiry date of the cookie.
 Date getLastSent()
          Retrieve the date this cookie was last sent.
 String getName()
          Retrieve the name of this cookie.
 String getPath()
          Retrieve the path for this cookie.
 String getValue()
          Retrieve the value of this cookie.
 boolean hasExpired()
          Retrieve whether this cookie has expired or not.
 int hashCode()
          Returns a hash code value for the object.
 boolean isSecure()
          Retrieve whther this cookie is secure (if yes, it will only be handed out over secure connections).
 boolean isSessionOnly()
          Retrieve whether this is a session-only cookie or not.
 boolean matchDomain(String dom)
          Checks whether this cookie matches a certain domain.
 boolean matchPath(String p)
          Checks whether this cookie matches a certain path.
 void sentNow()
          Update the time this cookie was last sent: It is set to the current time.
 String toString()
          Returns a string representation of the cookie.
 
Methods inherited from class java.lang.Object
, clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

COOKIE_SIZE

public static final int COOKIE_SIZE
The maximum size of the name and the value of a cookie together.

name

protected String name
The name of the cookie.

value

protected String value
The value of the cookie.

path

protected String path
The path of the cookie (the subset of URLs for which this cookie is valid).

domain

protected String domain
The domain of the cookie (from where it was sent; hostname).

expires

protected Date expires
The date when to cookie expires (will be deleted after this time).

secure

protected boolean secure
Flag to indicate whether the cookie is secure or not (if true it will only be sent when using a secure communications channel).

strict

protected boolean strict
Whether checking should be done strict (e. g. domain).

lastSent

protected Date lastSent
The time this cookie was last handed out. Necessary for purging when the limits are exceeded. Not the oldest, but the least recently used cookies should be thrown away. If null it was never sent.
Constructor Detail

Cookie

public Cookie(String name,
              String value,
              String path,
              String domain,
              String expireString,
              boolean secure,
              boolean strict)
       throws IllegalArgumentException
Creates a new cookie.
Parameters:
name - the name of the cookie (may not contain ";", "," or whitespace)
value - the value of the cookie (may not contain ";", "," or whitespace)
path - the path of the cookie (null or "" is changed to "/")
domain - the domain of the cookie (must not be null or "")
expires - the date the cookie expires (in string form as defined in the cookie definition)
secure - whether it is a secure cookie or not
strict - if true checking will be done strict (currently only for checking the domain).
Throws:
IllegalArgumentException - if the domain is null or not valid according to the specification

Cookie

public Cookie(String name,
              String value,
              String path,
              String domain,
              String expires,
              boolean secure)
       throws IllegalArgumentException
Creates a new cookie with loose checking.
Parameters:
name - the name of the cookie
value - the value of the cookie
path - the path of the cookie
domain - the domain of the cookie
expires - the date the cookie expires (in string form as defined in the cookie definition)
secure - whether it is a secure cookie or not
Throws:
IllegalArgumentException - if the domain is null or not valid according to the specification
Method Detail

toString

public String toString()
Returns a string representation of the cookie. Conforms to the definition for the transmission of cookies (i. e. sequence, ";" between elements, ...).
Overrides:
toString in class Object
Returns:
string representation as in the definition

hashCode

public int hashCode()
Returns a hash code value for the object. It consists of the name, the path and the domain.
Overrides:
hashCode in class Object
Returns:
a hash code value for this object

equals

public boolean equals(Object obj)
Compares two cookies for equality. Compares the name and the path in case-sensitive form and the domain in case-insensitive form.
Overrides:
equals in class Object
Parameters:
obj - the reference object with which to compare
Returns:
true if this object is the same as the obj argument

matchDomain

public boolean matchDomain(String dom)
Checks whether this cookie matches a certain domain. The domain of the cookie is matched against the tail of the fully qualified domain name of the host provided as parameter. In addition to the specification it must be a full part of the domain (so "acme.com" would match both "acme.com" and "www.acme.com" but not "wwwacme.com" or "www.myacme.com").
Parameters:
dom - the domain to match
Returns:
true if the domain of the cookie matches the parameter

matchPath

public boolean matchPath(String p)
Checks whether this cookie matches a certain path. Checks whether the parameter starts with the path or not. The path "/foo" would match "/foobar" and "/foo/bar.html".
Parameters:
p - the path to match (null or "" is interpreted as "/" )
Returns:
true if the path of the cookie matches the parameter

checkString

protected void checkString(String str)
                    throws IllegalArgumentException
Checks wether the string contains ";", "," or a whitespace (" ", \r, \n, \t, \f).
Parameters:
str - the string to check
Throws:
IllegalArgumentException - if an unallowed character is in the string

checkDomainValidity

protected void checkDomainValidity()
                            throws IllegalArgumentException
Checks whther the domain provided is valid according to the specification or not. If the matching is strict, domains with 2 dots must end in one of the following to be valid: com, edu, net, org, gov, mil or int. This does not allow e. g. "www.acme.at"; however these names are quite common so the default is non-strict matching (see constructors).
Returns:
true if the domain in valid
See Also:
Cookie(String,String,String,String,String,boolean,boolean), Cookie(String,String,String,String,String,boolean)

hasExpired

public boolean hasExpired()
Retrieve whether this cookie has expired or not. Expired cookies should be deleted.
Returns:
true if the cookie has expired

isSessionOnly

public boolean isSessionOnly()
Retrieve whether this is a session-only cookie or not. These should not be written to a stream when a set of cookies is stored.
Returns:
true if the cookie expires with the session

getName

public String getName()
Retrieve the name of this cookie.
Returns:
the name of the cookie

getValue

public String getValue()
Retrieve the value of this cookie.
Returns:
the value of the cookie

getPath

public String getPath()
Retrieve the path for this cookie.
Returns:
the path for the cookie

getDomain

public String getDomain()
Retrieve the domain for this cookie.
Returns:
the domain for the cookie

getExpiryDate

public Date getExpiryDate()
Retrieve the expiry date of the cookie.
Returns:
the date after which the cookie will no longer be handed out or null if it is a session cookie

isSecure

public boolean isSecure()
Retrieve whther this cookie is secure (if yes, it will only be handed out over secure connections).
Returns:
true if the cookie is marked as secure

getLastSent

public Date getLastSent()
Retrieve the date this cookie was last sent.
Returns:
the date the cookie was last sent (null if never sent)

sentNow

public void sentNow()
Update the time this cookie was last sent: It is set to the current time.

Agent System POND 1.2 (28.2.2002)

Submit a bug

Copyright 2001,2002 Michael Sonntag & Institute for Information Processing and Microprocessor Technology (FIM), Johannes-Kepler-University Linz, Altenbergerstr. 69, A-4040 Linz, Austria.