package org.apache.http.impl.cookie;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.annotation.Contract;
import org.apache.http.annotation.Obsolete;
import org.apache.http.annotation.ThreadingBehavior;
import org.apache.http.client.utils.DateUtils;
import org.apache.http.cookie.ClientCookie;
import org.apache.http.cookie.CommonCookieAttributeHandler;
import org.apache.http.cookie.Cookie;
import org.apache.http.cookie.CookieOrigin;
import org.apache.http.cookie.CookiePathComparator;
import org.apache.http.cookie.CookieRestrictionViolationException;
import org.apache.http.cookie.MalformedCookieException;
import org.apache.http.cookie.SM;
import org.apache.http.message.BufferedHeader;
import org.apache.http.util.Args;
import org.apache.http.util.CharArrayBuffer;
@Obsolete
@Contract(threading = ThreadingBehavior.SAFE)
public class RFC2109Spec extends CookieSpecBase {
final static String[] DATE_PATTERNS = {
DateUtils.PATTERN_RFC1123,
DateUtils.PATTERN_RFC1036,
DateUtils.PATTERN_ASCTIME
};
private final boolean ;
public RFC2109Spec(final String[] datepatterns, final boolean oneHeader) {
super(new RFC2109VersionHandler(),
new BasicPathHandler() {
@Override
public void validate(
final Cookie cookie, final CookieOrigin origin) throws MalformedCookieException {
if (!match(cookie, origin)) {
throw new CookieRestrictionViolationException(
"Illegal 'path' attribute \"" + cookie.getPath()
+ "\". Path of origin: \"" + origin.getPath() + "\"");
}
}
},
new RFC2109DomainHandler(),
new BasicMaxAgeHandler(),
new BasicSecureHandler(),
new BasicCommentHandler(),
new BasicExpiresHandler(
datepatterns != null ? datepatterns.clone() : DATE_PATTERNS));
this.oneHeader = oneHeader;
}
public RFC2109Spec() {
this(null, false);
}
protected RFC2109Spec(final boolean oneHeader,
final CommonCookieAttributeHandler... handlers) {
super(handlers);
this.oneHeader = oneHeader;
}
@Override
public List<Cookie> (final Header header, final CookieOrigin origin)
throws MalformedCookieException {
Args.notNull(header, "Header");
Args.notNull(origin, "Cookie origin");
if (!header.getName().equalsIgnoreCase(SM.SET_COOKIE)) {
throw new MalformedCookieException("Unrecognized cookie header '"
+ header.toString() + "'");
}
final HeaderElement[] elems = header.getElements();
return parse(elems, origin);
}
@Override
public void validate(final Cookie cookie, final CookieOrigin origin)
throws MalformedCookieException {
Args.notNull(cookie, "Cookie");
final String name = cookie.getName();
if (name.indexOf(' ') != -1) {
throw new CookieRestrictionViolationException("Cookie name may not contain blanks");
}
if (name.startsWith("$")) {
throw new CookieRestrictionViolationException("Cookie name may not start with $");
}
super.validate(cookie, origin);
}
@Override
public List<Header> formatCookies(final List<Cookie> cookies) {
Args.notEmpty(cookies, "List of cookies");
final List<Cookie> cookieList;
if (cookies.size() > 1) {
cookieList = new ArrayList<Cookie>(cookies);
Collections.sort(cookieList, CookiePathComparator.INSTANCE);
} else {
cookieList = cookies;
}
return this.oneHeader ? doFormatOneHeader(cookieList) : doFormatManyHeaders(cookieList);
}
private List<Header> (final List<Cookie> cookies) {
int version = Integer.MAX_VALUE;
for (final Cookie cookie : cookies) {
if (cookie.getVersion() < version) {
version = cookie.getVersion();
}
}
final CharArrayBuffer buffer = new CharArrayBuffer(40 * cookies.size());
buffer.append(SM.COOKIE);
buffer.append(": ");
buffer.append("$Version=");
buffer.append(Integer.toString(version));
for (final Cookie cooky : cookies) {
buffer.append("; ");
final Cookie cookie = cooky;
formatCookieAsVer(buffer, cookie, version);
}
final List<Header> headers = new ArrayList<Header>(1);
headers.add(new BufferedHeader(buffer));
return headers;
}
private List<Header> (final List<Cookie> cookies) {
final List<Header> headers = new ArrayList<Header>(cookies.size());
for (final Cookie cookie : cookies) {
final int version = cookie.getVersion();
final 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;
}
protected void formatParamAsVer(final CharArrayBuffer buffer,
final String name, final String value, final int version) {
buffer.append(name);
buffer.append("=");
if (value != null) {
if (version > 0) {
buffer.append('\"');
buffer.append(value);
buffer.append('\"');
} else {
buffer.append(value);
}
}
}
protected void formatCookieAsVer(final CharArrayBuffer buffer,
final Cookie cookie, final int version) {
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);
}
}
}
@Override
public int getVersion() {
return 1;
}
@Override
public Header () {
return null;
}
@Override
public String toString() {
return "rfc2109";
}
}