1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 package org.apache.commons.httpclient.cookie;
33
34 import org.apache.commons.httpclient.Header;
35 import org.apache.commons.httpclient.NameValuePair;
36 import org.apache.commons.httpclient.Cookie;
37
38 /***
39 * Defines the cookie management specification.
40 * <p>Cookie management specification must define
41 * <ul>
42 * <li> rules of parsing "Set-Cookie" header
43 * <li> rules of validation of parsed cookies
44 * <li> formatting of "Cookie" header
45 * </ul>
46 * for a given host, port and path of origin
47 *
48 * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
49 * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
50 *
51 * @since 2.0
52 */
53 public interface CookieSpec {
54
55 /*** Path delimiter */
56 String PATH_DELIM = "/";
57
58 /*** Path delimiting charachter */
59 char PATH_DELIM_CHAR = PATH_DELIM.charAt(0);
60
61 /***
62 * Parse the <tt>"Set-Cookie"</tt> header value into Cookie array.
63 *
64 * @param host the host which sent the <tt>Set-Cookie</tt> header
65 * @param port the port which sent the <tt>Set-Cookie</tt> header
66 * @param path the path which sent the <tt>Set-Cookie</tt> header
67 * @param secure <tt>true</tt> when the <tt>Set-Cookie</tt> header
68 * was received over secure conection
69 * @param header the <tt>Set-Cookie</tt> received from the server
70 * @return an array of <tt>Cookie</tt>s parsed from the Set-Cookie value
71 * @throws MalformedCookieException if an exception occurs during parsing
72 * @throws IllegalArgumentException if an input parameter is illegal
73 */
74 Cookie[] parse(String host, int port, String path, boolean secure,
75 final String header)
76 throws MalformedCookieException, IllegalArgumentException;
77
78 /***
79 * Parse the <tt>"Set-Cookie"</tt> Header into an array of Cookies.
80 *
81 * @param host the host which sent the <tt>Set-Cookie</tt> header
82 * @param port the port which sent the <tt>Set-Cookie</tt> header
83 * @param path the path which sent the <tt>Set-Cookie</tt> header
84 * @param secure <tt>true</tt> when the <tt>Set-Cookie</tt> header
85 * was received over secure conection
86 * @param header the <tt>Set-Cookie</tt> received from the server
87 * @return an array of <tt>Cookie</tt>s parsed from the header
88 * @throws MalformedCookieException if an exception occurs during parsing
89 * @throws IllegalArgumentException if an input parameter is illegal
90 */
91 Cookie[] parse(String host, int port, String path, boolean secure,
92 final Header header)
93 throws MalformedCookieException, IllegalArgumentException;
94
95 /***
96 * Parse the cookie attribute and update the corresponsing Cookie
97 * properties.
98 *
99 * @param attribute cookie attribute from the <tt>Set-Cookie</tt>
100 * @param cookie the to be updated
101 * @throws MalformedCookieException if an exception occurs during parsing
102 * @throws IllegalArgumentException if an input parameter is illegal
103 */
104 void parseAttribute(final NameValuePair attribute, final Cookie cookie)
105 throws MalformedCookieException, IllegalArgumentException;
106
107 /***
108 * Validate the cookie according to validation rules defined by the
109 * cookie specification.
110 *
111 * @param host the host from which the {@link Cookie} was received
112 * @param port the port from which the {@link Cookie} was received
113 * @param path the path from which the {@link Cookie} was received
114 * @param secure <tt>true</tt> when the {@link Cookie} was received
115 * using a secure connection
116 * @param cookie the Cookie to validate
117 * @throws MalformedCookieException if the cookie is invalid
118 * @throws IllegalArgumentException if an input parameter is illegal
119 */
120 void validate(String host, int port, String path, boolean secure,
121 final Cookie cookie)
122 throws MalformedCookieException, IllegalArgumentException;
123
124 /***
125 * Determines if a Cookie matches a location.
126 *
127 * @param host the host to which the request is being submitted
128 * @param port the port to which the request is being submitted
129 * @param path the path to which the request is being submitted
130 * @param secure <tt>true</tt> if the request is using a secure connection
131 * @param cookie the Cookie to be matched
132 *
133 * @return <tt>true</tt> if the cookie should be submitted with a request
134 * with given attributes, <tt>false</tt> otherwise.
135 */
136 boolean match(String host, int port, String path, boolean secure,
137 final Cookie cookie);
138
139 /***
140 * Determines which of an array of Cookies matches a location.
141 *
142 * @param host the host to which the request is being submitted
143 * @param port the port to which the request is being submitted
144 * (currenlty ignored)
145 * @param path the path to which the request is being submitted
146 * @param secure <tt>true</tt> if the request is using a secure protocol
147 * @param cookies an array of <tt>Cookie</tt>s to be matched
148 *
149 * @return <tt>true</tt> if the cookie should be submitted with a request
150 * with given attributes, <tt>false</tt> otherwise.
151 */
152 Cookie[] match(String host, int port, String path, boolean secure,
153 final Cookie cookies[]);
154
155 /***
156 * Create a <tt>"Cookie"</tt> header value for an array of cookies.
157 *
158 * @param cookie the cookie to be formatted as string
159 * @return a string suitable for sending in a <tt>"Cookie"</tt> header.
160 */
161 String formatCookie(Cookie cookie);
162
163 /***
164 * Create a <tt>"Cookie"</tt> header value for an array of cookies.
165 *
166 * @param cookies the Cookies to be formatted
167 * @return a string suitable for sending in a Cookie header.
168 * @throws IllegalArgumentException if an input parameter is illegal
169 */
170 String formatCookies(Cookie[] cookies) throws IllegalArgumentException;
171
172 /***
173 * Create a <tt>"Cookie"</tt> Header for an array of Cookies.
174 *
175 * @param cookies the Cookies format into a Cookie header
176 * @return a Header for the given Cookies.
177 * @throws IllegalArgumentException if an input parameter is illegal
178 */
179 Header formatCookieHeader(Cookie[] cookies) throws IllegalArgumentException;
180
181 /***
182 * Create a <tt>"Cookie"</tt> Header for single Cookie.
183 *
184 * @param cookie the Cookie format as a <tt>Cookie</tt> header
185 * @return a Cookie header.
186 * @throws IllegalArgumentException if an input parameter is illegal
187 */
188 Header formatCookieHeader(Cookie cookie) throws IllegalArgumentException;
189
190 }