View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.impl.cookie;
29  
30  import java.util.ArrayList;
31  import java.util.List;
32  
33  import org.apache.http.FormattedHeader;
34  import org.apache.http.Header;
35  import org.apache.http.HeaderElement;
36  import org.apache.http.annotation.NotThreadSafe;
37  import org.apache.http.cookie.ClientCookie;
38  import org.apache.http.cookie.Cookie;
39  import org.apache.http.cookie.CookieOrigin;
40  import org.apache.http.cookie.MalformedCookieException;
41  import org.apache.http.cookie.SM;
42  import org.apache.http.message.BufferedHeader;
43  import org.apache.http.message.ParserCursor;
44  import org.apache.http.util.Args;
45  import org.apache.http.util.CharArrayBuffer;
46  
47  /**
48   * This {@link org.apache.http.cookie.CookieSpec} implementation conforms to
49   * the original draft specification published by Netscape Communications.
50   * It should be avoided unless absolutely necessary for compatibility with
51   * legacy applications.
52   *
53   * @since 4.0
54   */
55  @NotThreadSafe // superclass is @NotThreadSafe
56  public class NetscapeDraftSpec extends CookieSpecBase {
57  
58      protected static final String EXPIRES_PATTERN = "EEE, dd-MMM-yy HH:mm:ss z";
59  
60      private final String[] datepatterns;
61  
62      /** Default constructor */
63      public NetscapeDraftSpec(final String[] datepatterns) {
64          super();
65          if (datepatterns != null) {
66              this.datepatterns = datepatterns.clone();
67          } else {
68              this.datepatterns = new String[] { EXPIRES_PATTERN };
69          }
70          registerAttribHandler(ClientCookie.PATH_ATTR, new BasicPathHandler());
71          registerAttribHandler(ClientCookie.DOMAIN_ATTR, new NetscapeDomainHandler());
72          registerAttribHandler(ClientCookie.MAX_AGE_ATTR, new BasicMaxAgeHandler());
73          registerAttribHandler(ClientCookie.SECURE_ATTR, new BasicSecureHandler());
74          registerAttribHandler(ClientCookie.COMMENT_ATTR, new BasicCommentHandler());
75          registerAttribHandler(ClientCookie.EXPIRES_ATTR, new BasicExpiresHandler(
76                  this.datepatterns));
77      }
78  
79      /** Default constructor */
80      public NetscapeDraftSpec() {
81          this(null);
82      }
83  
84      /**
85        * Parses the Set-Cookie value into an array of {@code Cookie}s.
86        *
87        * <p>Syntax of the Set-Cookie HTTP Response Header:</p>
88        *
89        * <p>This is the format a CGI script would use to add to
90        * the HTTP headers a new piece of data which is to be stored by
91        * the client for later retrieval.</p>
92        *
93        * <PRE>
94        *  Set-Cookie: NAME=VALUE; expires=DATE; path=PATH; domain=DOMAIN_NAME; secure
95        * </PRE>
96        *
97        * <p>Please note that the Netscape draft specification does not fully conform to the HTTP
98        * header format. Comma character if present in <code>Set-Cookie</code> will not be treated
99        * as a header element separator</p>
100       *
101       * @see <a href="http://web.archive.org/web/20020803110822/http://wp.netscape.com/newsref/std/cookie_spec.html">
102       *  The Cookie Spec.</a>
103       *
104       * @param header the {@code Set-Cookie} received from the server
105       * @return an array of {@code Cookie}s parsed from the Set-Cookie value
106       * @throws MalformedCookieException if an exception occurs during parsing
107       */
108     @Override
109     public List<Cookie> parse(final Header header, final CookieOrigin origin)
110             throws MalformedCookieException {
111         Args.notNull(header, "Header");
112         Args.notNull(origin, "Cookie origin");
113         if (!header.getName().equalsIgnoreCase(SM.SET_COOKIE)) {
114             throw new MalformedCookieException("Unrecognized cookie header '"
115                     + header.toString() + "'");
116         }
117         final NetscapeDraftHeaderParser parser = NetscapeDraftHeaderParser.DEFAULT;
118         final CharArrayBuffer buffer;
119         final ParserCursor cursor;
120         if (header instanceof FormattedHeader) {
121             buffer = ((FormattedHeader) header).getBuffer();
122             cursor = new ParserCursor(
123                     ((FormattedHeader) header).getValuePos(),
124                     buffer.length());
125         } else {
126             final String s = header.getValue();
127             if (s == null) {
128                 throw new MalformedCookieException("Header value is null");
129             }
130             buffer = new CharArrayBuffer(s.length());
131             buffer.append(s);
132             cursor = new ParserCursor(0, buffer.length());
133         }
134         return parse(new HeaderElement[] { parser.parseHeader(buffer, cursor) }, origin);
135     }
136 
137     @Override
138     public List<Header> formatCookies(final List<Cookie> cookies) {
139         Args.notEmpty(cookies, "List of cookies");
140         final CharArrayBuffer buffer = new CharArrayBuffer(20 * cookies.size());
141         buffer.append(SM.COOKIE);
142         buffer.append(": ");
143         for (int i = 0; i < cookies.size(); i++) {
144             final Cookie cookie = cookies.get(i);
145             if (i > 0) {
146                 buffer.append("; ");
147             }
148             buffer.append(cookie.getName());
149             final String s = cookie.getValue();
150             if (s != null) {
151                 buffer.append("=");
152                 buffer.append(s);
153             }
154         }
155         final List<Header> headers = new ArrayList<Header>(1);
156         headers.add(new BufferedHeader(buffer));
157         return headers;
158     }
159 
160     @Override
161     public int getVersion() {
162         return 0;
163     }
164 
165     @Override
166     public Header getVersionHeader() {
167         return null;
168     }
169 
170     @Override
171     public String toString() {
172         return "netscape";
173     }
174 
175 }