1/* 2 * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/cookie/NetscapeDraftSpec.java $ 3 * $Revision: 677240 $ 4 * $Date: 2008-07-16 04:25:47 -0700 (Wed, 16 Jul 2008) $ 5 * 6 * ==================================================================== 7 * Licensed to the Apache Software Foundation (ASF) under one 8 * or more contributor license agreements. See the NOTICE file 9 * distributed with this work for additional information 10 * regarding copyright ownership. The ASF licenses this file 11 * to you under the Apache License, Version 2.0 (the 12 * "License"); you may not use this file except in compliance 13 * with the License. You may obtain a copy of the License at 14 * 15 * http://www.apache.org/licenses/LICENSE-2.0 16 * 17 * Unless required by applicable law or agreed to in writing, 18 * software distributed under the License is distributed on an 19 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 20 * KIND, either express or implied. See the License for the 21 * specific language governing permissions and limitations 22 * under the License. 23 * ==================================================================== 24 * 25 * This software consists of voluntary contributions made by many 26 * individuals on behalf of the Apache Software Foundation. For more 27 * information on the Apache Software Foundation, please see 28 * <http://www.apache.org/>. 29 * 30 */ 31 32package org.apache.http.impl.cookie; 33 34import java.util.ArrayList; 35import java.util.List; 36 37import org.apache.http.FormattedHeader; 38import org.apache.http.Header; 39import org.apache.http.HeaderElement; 40import org.apache.http.cookie.ClientCookie; 41import org.apache.http.cookie.Cookie; 42import org.apache.http.cookie.CookieOrigin; 43import org.apache.http.cookie.MalformedCookieException; 44import org.apache.http.cookie.SM; 45import org.apache.http.message.BufferedHeader; 46import org.apache.http.message.ParserCursor; 47import org.apache.http.util.CharArrayBuffer; 48 49/** 50 * Netscape cookie draft compliant cookie policy 51 * 52 * @author B.C. Holmes 53 * @author <a href="mailto:jericho@thinkfree.com">Park, Sung-Gu</a> 54 * @author <a href="mailto:dsale@us.britannica.com">Doug Sale</a> 55 * @author Rod Waldhoff 56 * @author dIon Gillard 57 * @author Sean C. Sullivan 58 * @author <a href="mailto:JEvans@Cyveillance.com">John Evans</a> 59 * @author Marc A. Saegesser 60 * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a> 61 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a> 62 * 63 * @since 4.0 64 * 65 * @deprecated Please use {@link java.net.URL#openConnection} instead. 66 * Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a> 67 * for further details. 68 */ 69@Deprecated 70public class NetscapeDraftSpec extends CookieSpecBase { 71 72 protected static final String EXPIRES_PATTERN = "EEE, dd-MMM-yyyy HH:mm:ss z"; 73 74 private final String[] datepatterns; 75 76 /** Default constructor */ 77 public NetscapeDraftSpec(final String[] datepatterns) { 78 super(); 79 if (datepatterns != null) { 80 this.datepatterns = datepatterns.clone(); 81 } else { 82 this.datepatterns = new String[] { EXPIRES_PATTERN }; 83 } 84 registerAttribHandler(ClientCookie.PATH_ATTR, new BasicPathHandler()); 85 registerAttribHandler(ClientCookie.DOMAIN_ATTR, new NetscapeDomainHandler()); 86 registerAttribHandler(ClientCookie.MAX_AGE_ATTR, new BasicMaxAgeHandler()); 87 registerAttribHandler(ClientCookie.SECURE_ATTR, new BasicSecureHandler()); 88 registerAttribHandler(ClientCookie.COMMENT_ATTR, new BasicCommentHandler()); 89 registerAttribHandler(ClientCookie.EXPIRES_ATTR, new BasicExpiresHandler( 90 this.datepatterns)); 91 } 92 93 /** Default constructor */ 94 public NetscapeDraftSpec() { 95 this(null); 96 } 97 98 /** 99 * Parses the Set-Cookie value into an array of <tt>Cookie</tt>s. 100 * 101 * <p>Syntax of the Set-Cookie HTTP Response Header:</p> 102 * 103 * <p>This is the format a CGI script would use to add to 104 * the HTTP headers a new piece of data which is to be stored by 105 * the client for later retrieval.</p> 106 * 107 * <PRE> 108 * Set-Cookie: NAME=VALUE; expires=DATE; path=PATH; domain=DOMAIN_NAME; secure 109 * </PRE> 110 * 111 * <p>Please note that Netscape draft specification does not fully 112 * conform to the HTTP header format. Netscape draft does not specify 113 * whether multiple cookies may be sent in one header. Hence, comma 114 * character may be present in unquoted cookie value or unquoted 115 * parameter value.</p> 116 * 117 * @see <a href="http://wp.netscape.com/newsref/std/cookie_spec.html"> 118 * The Cookie Spec.</a> 119 * 120 * @param header the <tt>Set-Cookie</tt> received from the server 121 * @return an array of <tt>Cookie</tt>s parsed from the Set-Cookie value 122 * @throws MalformedCookieException if an exception occurs during parsing 123 */ 124 public List<Cookie> parse(final Header header, final CookieOrigin origin) 125 throws MalformedCookieException { 126 if (header == null) { 127 throw new IllegalArgumentException("Header may not be null"); 128 } 129 if (origin == null) { 130 throw new IllegalArgumentException("Cookie origin may not be null"); 131 } 132 NetscapeDraftHeaderParser parser = NetscapeDraftHeaderParser.DEFAULT; 133 CharArrayBuffer buffer; 134 ParserCursor cursor; 135 if (header instanceof FormattedHeader) { 136 buffer = ((FormattedHeader) header).getBuffer(); 137 cursor = new ParserCursor( 138 ((FormattedHeader) header).getValuePos(), 139 buffer.length()); 140 } else { 141 String s = header.getValue(); 142 if (s == null) { 143 throw new MalformedCookieException("Header value is null"); 144 } 145 buffer = new CharArrayBuffer(s.length()); 146 buffer.append(s); 147 cursor = new ParserCursor(0, buffer.length()); 148 } 149 return parse(new HeaderElement[] { parser.parseHeader(buffer, cursor) }, origin); 150 } 151 152 public List<Header> formatCookies(final List<Cookie> cookies) { 153 if (cookies == null) { 154 throw new IllegalArgumentException("List of cookies may not be null"); 155 } 156 if (cookies.isEmpty()) { 157 throw new IllegalArgumentException("List of cookies may not be empty"); 158 } 159 CharArrayBuffer buffer = new CharArrayBuffer(20 * cookies.size()); 160 buffer.append(SM.COOKIE); 161 buffer.append(": "); 162 for (int i = 0; i < cookies.size(); i++) { 163 Cookie cookie = cookies.get(i); 164 if (i > 0) { 165 buffer.append("; "); 166 } 167 buffer.append(cookie.getName()); 168 String s = cookie.getValue(); 169 if (s != null) { 170 buffer.append("="); 171 buffer.append(s); 172 } 173 } 174 List<Header> headers = new ArrayList<Header>(1); 175 headers.add(new BufferedHeader(buffer)); 176 return headers; 177 } 178 179 public int getVersion() { 180 return 0; 181 } 182 183 public Header getVersionHeader() { 184 return null; 185 } 186 187} 188