RFC2109Specpublic class RFC2109Spec extends CookieSpecBase RFC 2109 compliant cookie policy |
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.List | doFormatManyHeaders(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.List | doFormatOneHeader(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 void | formatCookieAsVer(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
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.List | formatCookies(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 void | formatParamAsVer(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
buffer.append(name);
buffer.append("=");
if (value != null) {
if (version > 0) {
buffer.append('\"");
buffer.append(value);
buffer.append('\"");
} else {
buffer.append(value);
}
}
| public int | getVersion()
return 1;
| public org.apache.http.Header | getVersionHeader()
return null;
| public java.util.List | parse(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 void | validate(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);
|
|