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.cookie;
29  
30  import java.util.List;
31  
32  import org.apache.http.Header;
33  
34  /**
35   * Defines the cookie management specification.
36   * <p>Cookie management specification must define
37   * <ul>
38   *   <li> rules of parsing "Set-Cookie" header
39   *   <li> rules of validation of parsed cookies
40   *   <li>  formatting of "Cookie" header
41   * </ul>
42   * for a given host, port and path of origin
43   *
44   *
45   * @since 4.0
46   */
47  public interface CookieSpec {
48  
49      /**
50       * Returns version of the state management this cookie specification
51       * conforms to.
52       *
53       * @return version of the state management specification
54       */
55      int getVersion();
56  
57      /**
58        * Parse the {@code "Set-Cookie"} Header into an array of Cookies.
59        *
60        * <p>This method will not perform the validation of the resultant
61        * {@link Cookie}s</p>
62        *
63        * @see #validate
64        *
65        * @param header the {@code Set-Cookie} received from the server
66        * @param origin details of the cookie origin
67        * @return an array of {@code Cookie}s parsed from the header
68        * @throws MalformedCookieException if an exception occurs during parsing
69        */
70      List<Cookie> parse(Header header, CookieOrigin origin) throws MalformedCookieException;
71  
72      /**
73        * Validate the cookie according to validation rules defined by the
74        *  cookie specification.
75        *
76        * @param cookie the Cookie to validate
77        * @param origin details of the cookie origin
78        * @throws MalformedCookieException if the cookie is invalid
79        */
80      void validate(Cookie cookie, CookieOrigin origin) throws MalformedCookieException;
81  
82      /**
83       * Determines if a Cookie matches the target location.
84       *
85       * @param cookie the Cookie to be matched
86       * @param origin the target to test against
87       *
88       * @return {@code true} if the cookie should be submitted with a request
89       *  with given attributes, {@code false} otherwise.
90       */
91      boolean match(Cookie cookie, CookieOrigin origin);
92  
93      /**
94       * Create {@code "Cookie"} headers for an array of Cookies.
95       *
96       * @param cookies the Cookies format into a Cookie header
97       * @return a Header for the given Cookies.
98       * @throws IllegalArgumentException if an input parameter is illegal
99       */
100     List<Header> formatCookies(List<Cookie> cookies);
101 
102     /**
103      * Returns a request header identifying what version of the state management
104      * specification is understood. May be <code>null</code> if the cookie
105      * specification does not support {@code Cookie2} header.
106      */
107     Header getVersionHeader();
108 
109 }