Agent System POND 1.0 (1.7.2000)

FIM.Util.WWW.Cookies
Class CookieStore

java.lang.Object
  |
  +--FIM.Util.WWW.Cookies.CookieStore

public class CookieStore
extends Object
implements Serializable

A store for cookies. Adheres to the standard (number of cookies, size, ...) as defined in the Cookie definition (http://home.netscape.com/newsref/std/cookie_spec.html).

Sample code for usage (simplified and without exception handling):

private CookieStore cookies=new CookieStore();
...
URL url=new URL(...);
HttpURLConnection con=(HttpURLConnection)url.openConnection();
con.setRequestProperty(...);
cookies.sendCookies(con,false);
con.connect();
PrintWriter out=new PrintWriter(con.getOutputStream());
out.println(...);
out.close();
if(con.getResponseCode()!=200)
throw new ...Exception();
cookies.parseCookies(con);
BufferedReader in=new BufferedReader(new InputStreamReader(con.getInputStream()));
String line=in.readLine();
...

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

Field Summary
static int COOKIES_PER_DOMAIN
          The maximum number of cookies stored from one domain (exact match: "www.acme.com" and "acme.com" receive each 20 cookies)
protected  Hashtable table
          Store for the cookies.
static int TOTAL_COOKIES
          The maximum number of cookies stored in total.
 
Constructor Summary
CookieStore()
          Creates a new empty store for cookies.
 
Method Summary
 void clearCookies()
          Removes all cookies from the store.
protected  void enforceLimits(String domain)
          Removes all expired cookies form the store.
 Cookie getCookie(String name, String domain, String path)
          Retrieve a certain cookie from the store.
 CookieEnumeration getCookies(String domain, String path)
          Returns all cookies which match a specified domain and path.
 void parseCookies(HttpURLConnection con)
          Retrieves all cookies from a HttpURLConnection and stores them.
 void sendCookies(HttpURLConnection con, boolean secureConn)
          Sends all matching cookies to a HttpURLConnection.
 Cookie setCookie(Cookie c)
          Puts a certain cookie into the store and enforces the limits.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TOTAL_COOKIES

public static final int TOTAL_COOKIES
The maximum number of cookies stored in total.

COOKIES_PER_DOMAIN

public static final int COOKIES_PER_DOMAIN
The maximum number of cookies stored from one domain (exact match: "www.acme.com" and "acme.com" receive each 20 cookies)

table

protected Hashtable table
Store for the cookies. Key is domain+";"+path+";"+name .
Constructor Detail

CookieStore

public CookieStore()
Creates a new empty store for cookies.
Method Detail

clearCookies

public void clearCookies()
Removes all cookies from the store.

parseCookies

public void parseCookies(HttpURLConnection con)
Retrieves all cookies from a HttpURLConnection and stores them.
Parameters:
con - the connection to retrieve the cookies from. Must already be connected.

sendCookies

public void sendCookies(HttpURLConnection con,
                        boolean secureConn)
Sends all matching cookies to a HttpURLConnection.
Parameters:
con - the connection to retrieve the cookies from. May not be connected!
secureConn - whether this is a secure connection (cookies marked as secure will be sent) or not

enforceLimits

protected void enforceLimits(String domain)
Removes all expired cookies form the store. Enforces then the limit of 20 cookies per domain. If exceeded, the least recently used cookie from this domain will be deleted (only one!). Enforces also the limit of 300 cookies total maximum. If this limit is exceeded, the least recently used cookie of all will be deleted (only one!).
Parameters:
domain - the domain to check the limit for

getCookies

public CookieEnumeration getCookies(String domain,
                                    String path)
Returns all cookies which match a specified domain and path. If a parameter is null, all cookies will match it.
Parameters:
domain - the domain the returned cookies are from (if null ignored)
path - the path the returned cookies are valid for (if null ignored)
Returns:
an enumeration of all matching cookies

getCookie

public Cookie getCookie(String name,
                        String domain,
                        String path)
Retrieve a certain cookie from the store. All parameters must match exactly.
Parameters:
name - the name of the cookie
domain - the domain the cookie is from
path - the path the cookie is valid for (if "" or null, "/" is used)
Returns:
the cookie or null if not found

setCookie

public Cookie setCookie(Cookie c)
Puts a certain cookie into the store and enforces the limits. If it is already expired, an existing cookie with matching name, domain and path is removed from the store.
Parameters:
c - the cookie to store
Returns:
the previous cookie with the same name, domain and path or null if new

Agent System POND 1.0 (1.7.2000)

Submit a bug

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