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.List; michael@0: michael@0: import ch.boye.httpclientandroidlib.annotation.NotThreadSafe; michael@0: michael@0: import ch.boye.httpclientandroidlib.FormattedHeader; michael@0: import ch.boye.httpclientandroidlib.Header; michael@0: import ch.boye.httpclientandroidlib.HeaderElement; michael@0: import ch.boye.httpclientandroidlib.cookie.Cookie; michael@0: import ch.boye.httpclientandroidlib.cookie.CookieOrigin; 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.cookie.SetCookie2; michael@0: import ch.boye.httpclientandroidlib.message.ParserCursor; michael@0: import ch.boye.httpclientandroidlib.util.CharArrayBuffer; michael@0: michael@0: /** michael@0: * 'Meta' cookie specification that picks up a cookie policy based on michael@0: * the format of cookies sent with the HTTP response. michael@0: * michael@0: * @since 4.0 michael@0: */ michael@0: @NotThreadSafe // CookieSpec fields are @NotThreadSafe michael@0: public class BestMatchSpec implements CookieSpec { michael@0: michael@0: private final String[] datepatterns; michael@0: private final boolean oneHeader; michael@0: michael@0: // Cached values of CookieSpec instances michael@0: private RFC2965Spec strict; // @NotThreadSafe michael@0: private RFC2109Spec obsoleteStrict; // @NotThreadSafe michael@0: private BrowserCompatSpec compat; // @NotThreadSafe michael@0: michael@0: public BestMatchSpec(final String[] datepatterns, boolean oneHeader) { michael@0: super(); michael@0: this.datepatterns = datepatterns == null ? null : datepatterns.clone(); michael@0: this.oneHeader = oneHeader; michael@0: } michael@0: michael@0: public BestMatchSpec() { michael@0: this(null, false); michael@0: } michael@0: michael@0: private RFC2965Spec getStrict() { michael@0: if (this.strict == null) { michael@0: this.strict = new RFC2965Spec(this.datepatterns, this.oneHeader); michael@0: } michael@0: return strict; michael@0: } michael@0: michael@0: private RFC2109Spec getObsoleteStrict() { michael@0: if (this.obsoleteStrict == null) { michael@0: this.obsoleteStrict = new RFC2109Spec(this.datepatterns, this.oneHeader); michael@0: } michael@0: return obsoleteStrict; michael@0: } michael@0: michael@0: private BrowserCompatSpec getCompat() { michael@0: if (this.compat == null) { michael@0: this.compat = new BrowserCompatSpec(this.datepatterns); michael@0: } michael@0: return compat; michael@0: } michael@0: michael@0: public List parse( michael@0: final Header header, michael@0: final CookieOrigin origin) 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: HeaderElement[] helems = header.getElements(); michael@0: boolean versioned = false; michael@0: boolean netscape = false; michael@0: for (HeaderElement helem: helems) { michael@0: if (helem.getParameterByName("version") != null) { michael@0: versioned = true; michael@0: } michael@0: if (helem.getParameterByName("expires") != null) { michael@0: netscape = true; michael@0: } michael@0: } michael@0: if (netscape || !versioned) { michael@0: // Need to parse the header again, because Netscape style cookies do not correctly michael@0: // support multiple header elements (comma cannot be treated as an element separator) michael@0: NetscapeDraftHeaderParser parser = NetscapeDraftHeaderParser.DEFAULT; michael@0: CharArrayBuffer buffer; michael@0: ParserCursor cursor; michael@0: if (header instanceof FormattedHeader) { michael@0: buffer = ((FormattedHeader) header).getBuffer(); michael@0: cursor = new ParserCursor( michael@0: ((FormattedHeader) header).getValuePos(), michael@0: buffer.length()); michael@0: } else { michael@0: String s = header.getValue(); michael@0: if (s == null) { michael@0: throw new MalformedCookieException("Header value is null"); michael@0: } michael@0: buffer = new CharArrayBuffer(s.length()); michael@0: buffer.append(s); michael@0: cursor = new ParserCursor(0, buffer.length()); michael@0: } michael@0: helems = new HeaderElement[] { parser.parseHeader(buffer, cursor) }; michael@0: return getCompat().parse(helems, origin); michael@0: } else { michael@0: if (SM.SET_COOKIE2.equals(header.getName())) { michael@0: return getStrict().parse(helems, origin); michael@0: } else { michael@0: return getObsoleteStrict().parse(helems, origin); michael@0: } michael@0: } michael@0: } michael@0: michael@0: public void validate( michael@0: final Cookie cookie, michael@0: final CookieOrigin origin) throws MalformedCookieException { michael@0: if (cookie == null) { michael@0: throw new IllegalArgumentException("Cookie 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 (cookie.getVersion() > 0) { michael@0: if (cookie instanceof SetCookie2) { michael@0: getStrict().validate(cookie, origin); michael@0: } else { michael@0: getObsoleteStrict().validate(cookie, origin); michael@0: } michael@0: } else { michael@0: getCompat().validate(cookie, origin); michael@0: } michael@0: } michael@0: michael@0: public boolean match(final Cookie cookie, final CookieOrigin origin) { michael@0: if (cookie == null) { michael@0: throw new IllegalArgumentException("Cookie 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 (cookie.getVersion() > 0) { michael@0: if (cookie instanceof SetCookie2) { michael@0: return getStrict().match(cookie, origin); michael@0: } else { michael@0: return getObsoleteStrict().match(cookie, origin); michael@0: } michael@0: } else { michael@0: return getCompat().match(cookie, origin); michael@0: } michael@0: } michael@0: michael@0: public List
formatCookies(final List cookies) { michael@0: if (cookies == null) { michael@0: throw new IllegalArgumentException("List of cookies may not be null"); michael@0: } michael@0: int version = Integer.MAX_VALUE; michael@0: boolean isSetCookie2 = true; michael@0: for (Cookie cookie: cookies) { michael@0: if (!(cookie instanceof SetCookie2)) { michael@0: isSetCookie2 = false; michael@0: } michael@0: if (cookie.getVersion() < version) { michael@0: version = cookie.getVersion(); michael@0: } michael@0: } michael@0: if (version > 0) { michael@0: if (isSetCookie2) { michael@0: return getStrict().formatCookies(cookies); michael@0: } else { michael@0: return getObsoleteStrict().formatCookies(cookies); michael@0: } michael@0: } else { michael@0: return getCompat().formatCookies(cookies); michael@0: } michael@0: } michael@0: michael@0: public int getVersion() { michael@0: return getStrict().getVersion(); michael@0: } michael@0: michael@0: public Header getVersionHeader() { michael@0: return getStrict().getVersionHeader(); michael@0: } michael@0: michael@0: @Override michael@0: public String toString() { michael@0: return "best-match"; michael@0: } michael@0: michael@0: }