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.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.ClientCookie; 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.message.BufferedHeader; michael@0: import ch.boye.httpclientandroidlib.message.ParserCursor; michael@0: import ch.boye.httpclientandroidlib.util.CharArrayBuffer; michael@0: michael@0: /** michael@0: * This {@link CookieSpec} implementation conforms to the original draft michael@0: * specification published by Netscape Communications. It should be avoided michael@0: * unless absolutely necessary for compatibility with legacy code. michael@0: * michael@0: * @since 4.0 michael@0: */ michael@0: @NotThreadSafe // superclass is @NotThreadSafe michael@0: public class NetscapeDraftSpec extends CookieSpecBase { michael@0: michael@0: protected static final String EXPIRES_PATTERN = "EEE, dd-MMM-yy HH:mm:ss z"; michael@0: michael@0: private final String[] datepatterns; michael@0: michael@0: /** Default constructor */ michael@0: public NetscapeDraftSpec(final String[] datepatterns) { michael@0: super(); michael@0: if (datepatterns != null) { michael@0: this.datepatterns = datepatterns.clone(); michael@0: } else { michael@0: this.datepatterns = new String[] { EXPIRES_PATTERN }; michael@0: } michael@0: registerAttribHandler(ClientCookie.PATH_ATTR, new BasicPathHandler()); michael@0: registerAttribHandler(ClientCookie.DOMAIN_ATTR, new NetscapeDomainHandler()); 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 NetscapeDraftSpec() { michael@0: this(null); michael@0: } michael@0: michael@0: /** michael@0: * Parses the Set-Cookie value into an array of Cookies. michael@0: * michael@0: *

Syntax of the Set-Cookie HTTP Response Header:

michael@0: * michael@0: *

This is the format a CGI script would use to add to michael@0: * the HTTP headers a new piece of data which is to be stored by michael@0: * the client for later retrieval.

michael@0: * michael@0: *
michael@0:       *  Set-Cookie: NAME=VALUE; expires=DATE; path=PATH; domain=DOMAIN_NAME; secure
michael@0:       * 
michael@0: * michael@0: *

Please note that the Netscape draft specification does not fully conform to the HTTP michael@0: * header format. Comma character if present in Set-Cookie will not be treated michael@0: * as a header element separator

michael@0: * michael@0: * @see michael@0: * The Cookie Spec. michael@0: * michael@0: * @param header the Set-Cookie received from the server michael@0: * @return an array of Cookies parsed from the Set-Cookie value michael@0: * @throws MalformedCookieException if an exception occurs during parsing 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: 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: return parse(new HeaderElement[] { parser.parseHeader(buffer, cursor) }, origin); 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: if (cookies.isEmpty()) { michael@0: throw new IllegalArgumentException("List of cookies may not be empty"); michael@0: } michael@0: CharArrayBuffer buffer = new CharArrayBuffer(20 * cookies.size()); michael@0: buffer.append(SM.COOKIE); michael@0: buffer.append(": "); michael@0: for (int i = 0; i < cookies.size(); i++) { michael@0: Cookie cookie = cookies.get(i); michael@0: if (i > 0) { michael@0: buffer.append("; "); michael@0: } michael@0: buffer.append(cookie.getName()); michael@0: String s = cookie.getValue(); michael@0: if (s != null) { michael@0: buffer.append("="); michael@0: buffer.append(s); michael@0: } 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: public int getVersion() { michael@0: return 0; 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 "netscape"; michael@0: } michael@0: michael@0: }