michael@0: /* michael@0: * ==================================================================== michael@0: * Licensed to the Apache Software Foundation (ASF) under one michael@0: * or more contributor license agreements. See the NOTICE file michael@0: * distributed with this work for additional information michael@0: * regarding copyright ownership. The ASF licenses this file michael@0: * to you under the Apache License, Version 2.0 (the michael@0: * "License"); you may not use this file except in compliance michael@0: * with the License. You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, michael@0: * software distributed under the License is distributed on an michael@0: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY michael@0: * KIND, either express or implied. See the License for the michael@0: * specific language governing permissions and limitations michael@0: * under the License. michael@0: * ==================================================================== michael@0: * michael@0: * This software consists of voluntary contributions made by many michael@0: * individuals on behalf of the Apache Software Foundation. For more michael@0: * information on the Apache Software Foundation, please see michael@0: * . michael@0: * michael@0: */ michael@0: michael@0: package ch.boye.httpclientandroidlib.impl.cookie; michael@0: michael@0: import java.util.ArrayList; michael@0: import java.util.Collections; michael@0: import java.util.List; michael@0: michael@0: import ch.boye.httpclientandroidlib.annotation.NotThreadSafe; michael@0: michael@0: import ch.boye.httpclientandroidlib.Header; michael@0: import ch.boye.httpclientandroidlib.HeaderElement; michael@0: import ch.boye.httpclientandroidlib.cookie.ClientCookie; michael@0: import ch.boye.httpclientandroidlib.cookie.Cookie; michael@0: import ch.boye.httpclientandroidlib.cookie.CookieOrigin; michael@0: import ch.boye.httpclientandroidlib.cookie.CookiePathComparator; michael@0: import ch.boye.httpclientandroidlib.cookie.CookieRestrictionViolationException; michael@0: import ch.boye.httpclientandroidlib.cookie.CookieSpec; michael@0: import ch.boye.httpclientandroidlib.cookie.MalformedCookieException; michael@0: import ch.boye.httpclientandroidlib.cookie.SM; michael@0: import ch.boye.httpclientandroidlib.message.BufferedHeader; michael@0: import ch.boye.httpclientandroidlib.util.CharArrayBuffer; michael@0: michael@0: /** michael@0: * RFC 2109 compliant {@link CookieSpec} implementation. This is an older michael@0: * version of the official HTTP state management specification superseded michael@0: * by RFC 2965. michael@0: * michael@0: * @see RFC2965Spec michael@0: * michael@0: * @since 4.0 michael@0: */ michael@0: @NotThreadSafe // superclass is @NotThreadSafe michael@0: public class RFC2109Spec extends CookieSpecBase { michael@0: michael@0: private final static CookiePathComparator PATH_COMPARATOR = new CookiePathComparator(); michael@0: michael@0: private final static String[] DATE_PATTERNS = { michael@0: DateUtils.PATTERN_RFC1123, michael@0: DateUtils.PATTERN_RFC1036, michael@0: DateUtils.PATTERN_ASCTIME michael@0: }; michael@0: michael@0: private final String[] datepatterns; michael@0: private final boolean oneHeader; michael@0: michael@0: /** Default constructor */ michael@0: public RFC2109Spec(final String[] datepatterns, boolean oneHeader) { michael@0: super(); michael@0: if (datepatterns != null) { michael@0: this.datepatterns = datepatterns.clone(); michael@0: } else { michael@0: this.datepatterns = DATE_PATTERNS; michael@0: } michael@0: this.oneHeader = oneHeader; michael@0: registerAttribHandler(ClientCookie.VERSION_ATTR, new RFC2109VersionHandler()); michael@0: registerAttribHandler(ClientCookie.PATH_ATTR, new BasicPathHandler()); michael@0: registerAttribHandler(ClientCookie.DOMAIN_ATTR, new RFC2109DomainHandler()); michael@0: registerAttribHandler(ClientCookie.MAX_AGE_ATTR, new BasicMaxAgeHandler()); michael@0: registerAttribHandler(ClientCookie.SECURE_ATTR, new BasicSecureHandler()); michael@0: registerAttribHandler(ClientCookie.COMMENT_ATTR, new BasicCommentHandler()); michael@0: registerAttribHandler(ClientCookie.EXPIRES_ATTR, new BasicExpiresHandler( michael@0: this.datepatterns)); michael@0: } michael@0: michael@0: /** Default constructor */ michael@0: public RFC2109Spec() { michael@0: this(null, false); michael@0: } michael@0: michael@0: public List parse(final Header header, final CookieOrigin origin) michael@0: throws MalformedCookieException { michael@0: if (header == null) { michael@0: throw new IllegalArgumentException("Header may not be null"); michael@0: } michael@0: if (origin == null) { michael@0: throw new IllegalArgumentException("Cookie origin may not be null"); michael@0: } michael@0: if (!header.getName().equalsIgnoreCase(SM.SET_COOKIE)) { michael@0: throw new MalformedCookieException("Unrecognized cookie header '" michael@0: + header.toString() + "'"); michael@0: } michael@0: HeaderElement[] elems = header.getElements(); michael@0: return parse(elems, origin); michael@0: } michael@0: michael@0: @Override michael@0: public void validate(final Cookie cookie, final CookieOrigin origin) michael@0: throws MalformedCookieException { michael@0: if (cookie == null) { michael@0: throw new IllegalArgumentException("Cookie may not be null"); michael@0: } michael@0: String name = cookie.getName(); michael@0: if (name.indexOf(' ') != -1) { michael@0: throw new CookieRestrictionViolationException("Cookie name may not contain blanks"); michael@0: } michael@0: if (name.startsWith("$")) { michael@0: throw new CookieRestrictionViolationException("Cookie name may not start with $"); michael@0: } michael@0: super.validate(cookie, origin); michael@0: } michael@0: michael@0: public List
formatCookies(List cookies) { michael@0: if (cookies == null) { michael@0: throw new IllegalArgumentException("List of cookies may not be null"); michael@0: } michael@0: if (cookies.isEmpty()) { michael@0: throw new IllegalArgumentException("List of cookies may not be empty"); michael@0: } michael@0: if (cookies.size() > 1) { michael@0: // Create a mutable copy and sort the copy. michael@0: cookies = new ArrayList(cookies); michael@0: Collections.sort(cookies, PATH_COMPARATOR); michael@0: } michael@0: if (this.oneHeader) { michael@0: return doFormatOneHeader(cookies); michael@0: } else { michael@0: return doFormatManyHeaders(cookies); michael@0: } michael@0: } michael@0: michael@0: private List
doFormatOneHeader(final List cookies) { michael@0: int version = Integer.MAX_VALUE; michael@0: // Pick the lowest common denominator michael@0: for (Cookie cookie : cookies) { michael@0: if (cookie.getVersion() < version) { michael@0: version = cookie.getVersion(); michael@0: } michael@0: } michael@0: CharArrayBuffer buffer = new CharArrayBuffer(40 * cookies.size()); michael@0: buffer.append(SM.COOKIE); michael@0: buffer.append(": "); michael@0: buffer.append("$Version="); michael@0: buffer.append(Integer.toString(version)); michael@0: for (Cookie cooky : cookies) { michael@0: buffer.append("; "); michael@0: Cookie cookie = cooky; michael@0: formatCookieAsVer(buffer, cookie, version); michael@0: } michael@0: List
headers = new ArrayList
(1); michael@0: headers.add(new BufferedHeader(buffer)); michael@0: return headers; michael@0: } michael@0: michael@0: private List
doFormatManyHeaders(final List cookies) { michael@0: List
headers = new ArrayList
(cookies.size()); michael@0: for (Cookie cookie : cookies) { michael@0: int version = cookie.getVersion(); michael@0: CharArrayBuffer buffer = new CharArrayBuffer(40); michael@0: buffer.append("Cookie: "); michael@0: buffer.append("$Version="); michael@0: buffer.append(Integer.toString(version)); michael@0: buffer.append("; "); michael@0: formatCookieAsVer(buffer, cookie, version); michael@0: headers.add(new BufferedHeader(buffer)); michael@0: } michael@0: return headers; michael@0: } michael@0: michael@0: /** michael@0: * Return a name/value string suitable for sending in a "Cookie" michael@0: * header as defined in RFC 2109 for backward compatibility with cookie michael@0: * version 0 michael@0: * @param buffer The char array buffer to use for output michael@0: * @param name The cookie name michael@0: * @param value The cookie value michael@0: * @param version The cookie version michael@0: */ michael@0: protected void formatParamAsVer(final CharArrayBuffer buffer, michael@0: final String name, final String value, int version) { michael@0: buffer.append(name); michael@0: buffer.append("="); michael@0: if (value != null) { michael@0: if (version > 0) { michael@0: buffer.append('\"'); michael@0: buffer.append(value); michael@0: buffer.append('\"'); michael@0: } else { michael@0: buffer.append(value); michael@0: } michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Return a string suitable for sending in a "Cookie" header michael@0: * as defined in RFC 2109 for backward compatibility with cookie version 0 michael@0: * @param buffer The char array buffer to use for output michael@0: * @param cookie The {@link Cookie} to be formatted as string michael@0: * @param version The version to use. michael@0: */ michael@0: protected void formatCookieAsVer(final CharArrayBuffer buffer, michael@0: final Cookie cookie, int version) { michael@0: formatParamAsVer(buffer, cookie.getName(), cookie.getValue(), version); michael@0: if (cookie.getPath() != null) { michael@0: if (cookie instanceof ClientCookie michael@0: && ((ClientCookie) cookie).containsAttribute(ClientCookie.PATH_ATTR)) { michael@0: buffer.append("; "); michael@0: formatParamAsVer(buffer, "$Path", cookie.getPath(), version); michael@0: } michael@0: } michael@0: if (cookie.getDomain() != null) { michael@0: if (cookie instanceof ClientCookie michael@0: && ((ClientCookie) cookie).containsAttribute(ClientCookie.DOMAIN_ATTR)) { michael@0: buffer.append("; "); michael@0: formatParamAsVer(buffer, "$Domain", cookie.getDomain(), version); michael@0: } michael@0: } michael@0: } michael@0: michael@0: public int getVersion() { michael@0: return 1; michael@0: } michael@0: michael@0: public Header getVersionHeader() { michael@0: return null; michael@0: } michael@0: michael@0: @Override michael@0: public String toString() { michael@0: return "rfc2109"; michael@0: } michael@0: michael@0: }