com.ibm.wbi.protocol.http
Class HttpSetCookie

java.lang.Object
  |
  +--com.ibm.wbi.util.HeaderField
        |
        +--com.ibm.wbi.protocol.http.HttpSetCookie
All Implemented Interfaces:
java.lang.Cloneable

public class HttpSetCookie
extends HeaderField

This class is used to represent a HTTP set-cookie via the Netscape specification (http://home.netscape.com/newsref/std/cookie_spec.html). Example usage in an Editor

 // ...
 int WEEK = 7 * 24 * 60 * 60 * 1000;
 HttpSetCookie scookie = new HttpSetCookie("mycookie", "foo");
 scookie.setExpiresDate(new Date(new Date().getTime() + WEEK));
 DocumentInfo di = (DocumentInfo)e.getRequestInfo();
 HttpResponseHeader hrh = di.getHttpResponseHeader();
 addSetCookie(hrh, scookie);
 // ...
 
Or in a Generator
 // ...
 int WEEK = 7 * 24 * 60 * 60 * 1000;
 HttpSetCookie scookie = new HttpSetCookie("mycookie", "foo");
 scookie.setExpiresDate(new Date(new Date().getTime() + WEEK));
 HttpResponseHeader hrh = new HttpResponseHeader();
 addSetCookie(hrh, scookie);
 DocumentInfo di = (DocumentInfo)e.getRequestInfo();
 di.setResponseHeader(hrh);
 // ...
 
You might also want to modify the set-cookie you get from a server. For example, you might say "I don't want any cookies to last more than one week".
 // ...
 int WEEK = 7 * 24 * 60 * 60 * 1000;
 DocumentInfo di = (DocumentInfo)e.getRequestInfo();
 HttpResponseHeader hrh = di.getHttpResponseHeader();
 Vector scookies0 = getSetCookies(hrh);
 Vector modified_cookies0 = new Vector();
 Enumeration scookies = scookies0.elements();
 while(scookies.hasMoreElements()) {
   HttpSetCookie scookie = (HttpSetCookie)scookies.nextElement();
   Date exp = scookie.getExpiresDate();
   long now = new Date().getTime();
   if (exp != null && exp.getTime() > now + WEEK)
      scookie.setExpiresDate(new Date(now + WEEK));
   modified_cookies0.addElement(scookie);
 }
 hrh.remove("set-cookie");
 Enumeration modified_cookies = modified_cookies0.elements();
 while(modified_cookies.hasMoreElements())
   addSetCookie(hrh, (HttpSetCookie)modified_cookies.nextElement());
 // ...
 

See Also:
HttpCookie, HttpResponseHeader

Field Summary
static java.lang.String COMMENT
           
static java.lang.String DOMAIN
           
static java.lang.String EXPIRES
           
protected  java.lang.String name
           
static java.lang.String PATH
           
static java.lang.String SECURE
           
protected  java.lang.String value
           
 
Fields inherited from class com.ibm.wbi.util.HeaderField
params, val
 
Constructor Summary
HttpSetCookie(java.lang.String unparsed)
          Creates a new HttpSetCookie instance by parsing.
HttpSetCookie(java.lang.String name, java.lang.String value)
          Construct a new HttpSetCookie with the specified name and value.
 
Method Summary
static void addSetCookie(HttpResponseHeader h, HttpSetCookie scookie)
          Adds a Set-Cookie header line to an HTTP header.
 java.lang.String getComment()
          Tell the comment attribute for this cookie
 java.lang.String getDomain()
          Tell the domain attribute for this cookie
 java.lang.String getExpires()
          Get the value of the expires attribute as a java.lang.String.
 java.util.Date getExpiresDate()
          Get the value of the expires attribute of the cookie as a java.util.Date object.
 java.lang.String getName()
          Tell the name field for the cookie
 java.lang.String getPath()
          Get the path attribute of this cookie
 boolean getSecure()
          Tell whether the secure attribute is set.
static HttpSetCookie getSetCookie(HttpResponseHeader h)
          Get the first "Set-Cookie" line in an HTTP header.
static HttpSetCookie getSetCookie(HttpResponseHeader h, java.lang.String name)
          Get the first "Set-Cookie" line in an HTTP header.
static java.util.Vector getSetCookies(HttpResponseHeader h)
          Get all Set-Cookie lines in an HTTP header.
 java.lang.String getValue()
          Tell the value field for the cookie
 java.lang.String removeComment()
          Remove the comment attribute from this cookie
 java.lang.String removeDomain()
          Remove the domain attribute for this cookie
 java.lang.String removeExpires()
          Remove the expires attribute from this cookie
 java.lang.String removePath()
          Remove the path attribute from this cookie
 void setComment(java.lang.String v)
          Set the comment attribute for this cookie
 void setDomain(java.lang.String v)
          Set the domain attribute for this cookie
 void setExpires(java.lang.String v)
          Set the value of the expires attribute as a java.lang.String.
 void setExpiresDate(java.util.Date v)
          Set the value of the expires attribute of the cookie using a java.util.Date object.
 void setName(java.lang.String v)
          Modify the name field for this cookie.
 void setPath(java.lang.String v)
          Set the path attribute for this cookie
 void setSecure(boolean v)
          Set the secure attribute of the secure attribute.
static void setSetCookie(HttpResponseHeader h, HttpSetCookie scookie)
          Sets a value for the Set-Cookie header line in an HTTP header, eliminating all previous values for the Set-Cookie header.
 void setValue(java.lang.String v)
          Modify the value field for this cookie.
protected  void updateNameValue()
           
protected  void updateVal()
          val is a member variable of the baseclass, HeaderField.
 
Methods inherited from class com.ibm.wbi.util.HeaderField
clone, getParam, getParameters, main, removeParam, removeParameters, setParam, toString
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

COMMENT

public static final java.lang.String COMMENT

DOMAIN

public static final java.lang.String DOMAIN

EXPIRES

public static final java.lang.String EXPIRES

PATH

public static final java.lang.String PATH

SECURE

public static final java.lang.String SECURE

name

protected java.lang.String name

value

protected java.lang.String value
Constructor Detail

HttpSetCookie

public HttpSetCookie(java.lang.String name,
                     java.lang.String value)
Construct a new HttpSetCookie with the specified name and value. This is used in an Editor or Generator Meg to send a set-cookie header to the browser. In other words, to request the browser to store the cookie.
Parameters:
name - The name of the cookie
value - The value assigned to the cookie

HttpSetCookie

public HttpSetCookie(java.lang.String unparsed)
Creates a new HttpSetCookie instance by parsing. If the parse fails, then part or all of the unparsed string is ignored.
Parameters:
unparsed - a String value
Method Detail

updateVal

protected void updateVal()
val is a member variable of the baseclass, HeaderField. HttpSetCookie separates the val into a name and value, "="-delimited. This code takes care of making sure the val is correct after name or value have been changed.

updateNameValue

protected void updateNameValue()

getName

public java.lang.String getName()
Tell the name field for the cookie
Returns:
the name of the cookie

setName

public void setName(java.lang.String v)
Modify the name field for this cookie. Be sure to read the documentation for getValue().
Parameters:
v - The new name
See Also:
setValue(java.lang.String)

getValue

public java.lang.String getValue()
Tell the value field for the cookie
Overrides:
getValue in class HeaderField
Returns:
the value of the cookie

setValue

public void setValue(java.lang.String v)
Modify the value field for this cookie. Cookies use name=value for the first field. If you want to set the name, use setName(), if you want to set the value, use setValue(). If you do something like setValue("foo=bar"), we won't try to stop you, but you might get some unexpected behavior.
Overrides:
setValue in class HeaderField
Parameters:
v - The new value
See Also:
setName(java.lang.String)

getExpiresDate

public java.util.Date getExpiresDate()
Get the value of the expires attribute of the cookie as a java.util.Date object.
Returns:
The Date value or null if not present

setExpiresDate

public void setExpiresDate(java.util.Date v)
Set the value of the expires attribute of the cookie using a java.util.Date object. This method is careful to produce a formatted Date string that can be understood by browsers (they are very picky about the way the date is formatted).
Parameters:
v - a Date value

getExpires

public java.lang.String getExpires()
Get the value of the expires attribute as a java.lang.String. You might find getExpiresDate() more useful, since it returns a java.util.Date object.
Returns:
a String value
See Also:
getExpiresDate()

setExpires

public void setExpires(java.lang.String v)
Set the value of the expires attribute as a java.lang.String. You almost definitely want to use setExpiresDate() so you don't need to worry about how to correctly format the date for the browser.
Parameters:
v - a String value
See Also:
setExpiresDate(java.util.Date)

removeExpires

public java.lang.String removeExpires()
Remove the expires attribute from this cookie
Returns:
a String value, the old value or null

getSecure

public boolean getSecure()
Tell whether the secure attribute is set. This is a boolean attribute -- it doesn't have a value.
Returns:
true if the secure attribute is set

setSecure

public void setSecure(boolean v)
Set the secure attribute of the secure attribute. This is different than other attributes since it has no value. Setting secure to true will add the secure attribute, false will remove it. There is no removeSecure() method.
Parameters:
v - a boolean value

getComment

public java.lang.String getComment()
Tell the comment attribute for this cookie
Returns:
a String value

setComment

public void setComment(java.lang.String v)
Set the comment attribute for this cookie
Parameters:
v - a String value

removeComment

public java.lang.String removeComment()
Remove the comment attribute from this cookie
Returns:
a String value

getDomain

public java.lang.String getDomain()
Tell the domain attribute for this cookie
Returns:
a String value

setDomain

public void setDomain(java.lang.String v)
Set the domain attribute for this cookie
Parameters:
v - a String value

removeDomain

public java.lang.String removeDomain()
Remove the domain attribute for this cookie
Returns:
a String value

getPath

public java.lang.String getPath()
Get the path attribute of this cookie
Returns:
a String value

setPath

public void setPath(java.lang.String v)
Set the path attribute for this cookie
Parameters:
v - a String value

removePath

public java.lang.String removePath()
Remove the path attribute from this cookie
Returns:
a String value

getSetCookie

public static HttpSetCookie getSetCookie(HttpResponseHeader h)
Get the first "Set-Cookie" line in an HTTP header.
Parameters:
h - The HttpResponseHeader.
Returns:
The first Set-Cookie line as an HttpSetCookie, or null if there are no Set-Cookie lines.

getSetCookie

public static HttpSetCookie getSetCookie(HttpResponseHeader h,
                                         java.lang.String name)
Get the first "Set-Cookie" line in an HTTP header.
Parameters:
h - The HttpResponseHeader.
Returns:
The first Set-Cookie line as an HttpSetCookie, or null if there are no Set-Cookie lines.

getSetCookies

public static java.util.Vector getSetCookies(HttpResponseHeader h)
Get all Set-Cookie lines in an HTTP header.
Parameters:
h - The HttpResponseHeader.
Returns:
A Vector containing each Set-Cookie line as an HttpSetCookie; the Vector is empty if there are none.

setSetCookie

public static void setSetCookie(HttpResponseHeader h,
                                HttpSetCookie scookie)
Sets a value for the Set-Cookie header line in an HTTP header, eliminating all previous values for the Set-Cookie header.
Parameters:
h - The HttpResponseHeader.
scookie - The value to set.

addSetCookie

public static void addSetCookie(HttpResponseHeader h,
                                HttpSetCookie scookie)
Adds a Set-Cookie header line to an HTTP header.
Parameters:
h - The HttpResponseHeader.
scookie - The value to add.