FileDocCategorySizeDatePackage
RFC2109Spec.javaAPI DocAndroid 1.5 API9294Wed May 06 22:41:10 BST 2009org.apache.http.impl.cookie

RFC2109Spec

public class RFC2109Spec extends CookieSpecBase
RFC 2109 compliant cookie policy
author
B.C. Holmes
author
Park, Sung-Gu
author
Doug Sale
author
Rod Waldhoff
author
dIon Gillard
author
Sean C. Sullivan
author
John Evans
author
Marc A. Saegesser
author
Oleg Kalnichevski
author
Mike Bowler
since
4.0

Fields Summary
private static final CookiePathComparator
PATH_COMPARATOR
private static final String[]
DATE_PATTERNS
private final String[]
datepatterns
private final boolean
oneHeader
Constructors Summary
public RFC2109Spec(String[] datepatterns, boolean oneHeader)
Default constructor

    
       
          
        super();
        if (datepatterns != null) {
            this.datepatterns = datepatterns.clone();
        } else {
            this.datepatterns = DATE_PATTERNS;
        }
        this.oneHeader = oneHeader;
        registerAttribHandler(ClientCookie.VERSION_ATTR, new RFC2109VersionHandler());
        registerAttribHandler(ClientCookie.PATH_ATTR, new BasicPathHandler());
        registerAttribHandler(ClientCookie.DOMAIN_ATTR, new RFC2109DomainHandler());
        registerAttribHandler(ClientCookie.MAX_AGE_ATTR, new BasicMaxAgeHandler());
        registerAttribHandler(ClientCookie.SECURE_ATTR, new BasicSecureHandler());
        registerAttribHandler(ClientCookie.COMMENT_ATTR, new BasicCommentHandler());
        registerAttribHandler(ClientCookie.EXPIRES_ATTR, new BasicExpiresHandler(
                this.datepatterns));
    
public RFC2109Spec()
Default constructor

        this(null, false);
    
Methods Summary
private java.util.ListdoFormatManyHeaders(java.util.List cookies)

        List<Header> headers = new ArrayList<Header>(cookies.size());
        for (Cookie cookie : cookies) {
            int version = cookie.getVersion();
            CharArrayBuffer buffer = new CharArrayBuffer(40);
            buffer.append("Cookie: ");
            buffer.append("$Version=");
            buffer.append(Integer.toString(version));
            buffer.append("; ");
            formatCookieAsVer(buffer, cookie, version);
            headers.add(new BufferedHeader(buffer));
        }
        return headers;
    
private java.util.ListdoFormatOneHeader(java.util.List cookies)

        int version = Integer.MAX_VALUE;
        // Pick the lowest common denominator
        for (Cookie cookie : cookies) {
            if (cookie.getVersion() < version) {
                version = cookie.getVersion();
            }
        }
        CharArrayBuffer buffer = new CharArrayBuffer(40 * cookies.size());
        buffer.append(SM.COOKIE);
        buffer.append(": ");
        buffer.append("$Version=");
        buffer.append(Integer.toString(version));
        for (Cookie cooky : cookies) {
            buffer.append("; ");
            Cookie cookie = cooky;
            formatCookieAsVer(buffer, cookie, version);
        }
        List<Header> headers = new ArrayList<Header>(1);
        headers.add(new BufferedHeader(buffer));
        return headers;
    
protected voidformatCookieAsVer(org.apache.http.util.CharArrayBuffer buffer, org.apache.http.cookie.Cookie cookie, int version)
Return a string suitable for sending in a "Cookie" header as defined in RFC 2109 for backward compatibility with cookie version 0

param
buffer The char array buffer to use for output
param
cookie The {@link Cookie} to be formatted as string
param
version The version to use.

        formatParamAsVer(buffer, cookie.getName(), cookie.getValue(), version);
        if (cookie.getPath() != null) {
            if (cookie instanceof ClientCookie 
                    && ((ClientCookie) cookie).containsAttribute(ClientCookie.PATH_ATTR)) {
                buffer.append("; ");
                formatParamAsVer(buffer, "$Path", cookie.getPath(), version);
            }
        }
        if (cookie.getDomain() != null) {
            if (cookie instanceof ClientCookie 
                    && ((ClientCookie) cookie).containsAttribute(ClientCookie.DOMAIN_ATTR)) {
                buffer.append("; ");
                formatParamAsVer(buffer, "$Domain", cookie.getDomain(), version);
            }
        }
    
public java.util.ListformatCookies(java.util.List cookies)

        if (cookies == null) {
            throw new IllegalArgumentException("List of cookies may not be null");
        }
        if (cookies.isEmpty()) {
            throw new IllegalArgumentException("List of cookies may not be empty");
        }
        if (cookies.size() > 1) {
            // Create a mutable copy and sort the copy.
            cookies = new ArrayList<Cookie>(cookies);
            Collections.sort(cookies, PATH_COMPARATOR);
        }
        if (this.oneHeader) {
            return doFormatOneHeader(cookies);
        } else {
            return doFormatManyHeaders(cookies);
        }
    
protected voidformatParamAsVer(org.apache.http.util.CharArrayBuffer buffer, java.lang.String name, java.lang.String value, int version)
Return a name/value string suitable for sending in a "Cookie" header as defined in RFC 2109 for backward compatibility with cookie version 0

param
buffer The char array buffer to use for output
param
name The cookie name
param
value The cookie value
param
version The cookie version

        buffer.append(name);
        buffer.append("=");
        if (value != null) {
            if (version > 0) {
                buffer.append('\"");
                buffer.append(value);
                buffer.append('\"");
            } else {
                buffer.append(value);
            }
        }
    
public intgetVersion()

        return 1;
    
public org.apache.http.HeadergetVersionHeader()

        return null;
    
public java.util.Listparse(org.apache.http.Header header, org.apache.http.cookie.CookieOrigin origin)

        if (header == null) {
            throw new IllegalArgumentException("Header may not be null");
        }
        if (origin == null) {
            throw new IllegalArgumentException("Cookie origin may not be null");
        }
        HeaderElement[] elems = header.getElements();
        return parse(elems, origin);
    
public voidvalidate(org.apache.http.cookie.Cookie cookie, org.apache.http.cookie.CookieOrigin origin)

        if (cookie == null) {
            throw new IllegalArgumentException("Cookie may not be null");
        }
        String name = cookie.getName();
        if (name.indexOf(' ") != -1) {
            throw new MalformedCookieException("Cookie name may not contain blanks");
        }
        if (name.startsWith("$")) {
            throw new MalformedCookieException("Cookie name may not start with $");
        }
        super.validate(cookie, origin);