2
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/cookie/CookiePolicy.java,v 1.15 2004/09/14 20:11:31 olegk Exp $
4
* $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
6
* ====================================================================
8
* Licensed to the Apache Software Foundation (ASF) under one or more
9
* contributor license agreements. See the NOTICE file distributed with
10
* this work for additional information regarding copyright ownership.
11
* The ASF licenses this file to You under the Apache License, Version 2.0
12
* (the "License"); you may not use this file except in compliance with
13
* the License. You may obtain a copy of the License at
15
* http://www.apache.org/licenses/LICENSE-2.0
17
* Unless required by applicable law or agreed to in writing, software
18
* distributed under the License is distributed on an "AS IS" BASIS,
19
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
* See the License for the specific language governing permissions and
21
* limitations under the License.
22
* ====================================================================
24
* This software consists of voluntary contributions made by many
25
* individuals on behalf of the Apache Software Foundation. For more
26
* information on the Apache Software Foundation, please see
27
* <http://www.apache.org/>.
31
package org.apache.commons.httpclient.cookie;
33
import java.util.Collections;
34
import java.util.HashMap;
37
import org.apache.commons.logging.Log;
38
import org.apache.commons.logging.LogFactory;
41
* Cookie management policy class. The cookie policy provides corresponding
42
* cookie management interfrace for a given type or version of cookie.
43
* <p>RFC 2109 specification is used per default. Other supported specification
44
* can be chosen when appropriate or set default when desired
45
* <p>The following specifications are provided:
47
* <li><tt>BROWSER_COMPATIBILITY</tt>: compatible with the common cookie
48
* management practices (even if they are not 100% standards compliant)
49
* <li><tt>NETSCAPE</tt>: Netscape cookie draft compliant
50
* <li><tt>RFC_2109</tt>: RFC2109 compliant (default)
51
* <li><tt>IGNORE_COOKIES</tt>: do not automcatically process cookies
54
* @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
55
* @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
59
public abstract class CookiePolicy {
61
private static Map SPECS = Collections.synchronizedMap(new HashMap());
64
* The policy that provides high degree of compatibilty
65
* with common cookie management of popular HTTP agents.
69
public static final String BROWSER_COMPATIBILITY = "compatibility";
72
* The Netscape cookie draft compliant policy.
76
public static final String NETSCAPE = "netscape";
79
* The RFC 2109 compliant policy.
83
public static final String RFC_2109 = "rfc2109";
86
* The RFC 2965 compliant policy.
90
public static final String RFC_2965 = "rfc2965";
93
* The policy that ignores cookies.
97
public static final String IGNORE_COOKIES = "ignoreCookies";
100
* The default cookie policy.
104
public static final String DEFAULT = "default";
107
CookiePolicy.registerCookieSpec(DEFAULT, RFC2109Spec.class);
108
CookiePolicy.registerCookieSpec(RFC_2109, RFC2109Spec.class);
109
CookiePolicy.registerCookieSpec(RFC_2965, RFC2965Spec.class);
110
CookiePolicy.registerCookieSpec(BROWSER_COMPATIBILITY, CookieSpecBase.class);
111
CookiePolicy.registerCookieSpec(NETSCAPE, NetscapeDraftSpec.class);
112
CookiePolicy.registerCookieSpec(IGNORE_COOKIES, IgnoreCookiesSpec.class);
116
* The <tt>COMPATIBILITY</tt> policy provides high compatibilty
117
* with common cookie management of popular HTTP agents.
119
* @deprecated Use {@link #BROWSER_COMPATIBILITY}
121
public static final int COMPATIBILITY = 0;
124
* The <tt>NETSCAPE_DRAFT</tt> Netscape draft compliant policy.
126
* @deprecated Use {@link #NETSCAPE}
128
public static final int NETSCAPE_DRAFT = 1;
131
* The <tt>RFC2109</tt> RFC 2109 compliant policy.
133
* @deprecated Use {@link #RFC_2109}
135
public static final int RFC2109 = 2;
138
* The <tt>RFC2965</tt> RFC 2965 compliant policy.
140
* @deprecated Use {@link #RFC_2965}
142
public static final int RFC2965 = 3;
145
* The default cookie policy.
147
* @deprecated Use {@link #DEFAULT}
149
private static int defaultPolicy = RFC2109;
152
protected static final Log LOG = LogFactory.getLog(CookiePolicy.class);
155
* Registers a new {@link CookieSpec cookie specification} with the given identifier.
156
* If a specification with the given ID already exists it will be overridden.
157
* This ID is the same one used to retrieve the {@link CookieSpec cookie specification}
158
* from {@link #getCookieSpec(String)}.
160
* @param id the identifier for this specification
161
* @param clazz the {@link CookieSpec cookie specification} class to register
163
* @see #getCookieSpec(String)
167
public static void registerCookieSpec(final String id, final Class clazz) {
169
throw new IllegalArgumentException("Id may not be null");
172
throw new IllegalArgumentException("Cookie spec class may not be null");
174
SPECS.put(id.toLowerCase(), clazz);
178
* Unregisters the {@link CookieSpec cookie specification} with the given ID.
180
* @param id the ID of the {@link CookieSpec cookie specification} to unregister
184
public static void unregisterCookieSpec(final String id) {
186
throw new IllegalArgumentException("Id may not be null");
188
SPECS.remove(id.toLowerCase());
192
* Gets the {@link CookieSpec cookie specification} with the given ID.
194
* @param id the {@link CookieSpec cookie specification} ID
196
* @return {@link CookieSpec cookie specification}
198
* @throws IllegalStateException if a policy with the ID cannot be found
202
public static CookieSpec getCookieSpec(final String id)
203
throws IllegalStateException {
206
throw new IllegalArgumentException("Id may not be null");
208
Class clazz = (Class)SPECS.get(id.toLowerCase());
212
return (CookieSpec)clazz.newInstance();
213
} catch (Exception e) {
214
LOG.error("Error initializing cookie spec: " + id, e);
215
throw new IllegalStateException(id +
216
" cookie spec implemented by " +
217
clazz.getName() + " could not be initialized");
220
throw new IllegalStateException("Unsupported cookie spec " + id);
225
* @return default cookie policy
227
* @deprecated Use {@link #getDefaultSpec()}
229
* @see #getDefaultSpec()
231
public static int getDefaultPolicy() {
232
return defaultPolicy;
237
* @param policy new default cookie policy
239
* @deprecated Use {@link CookiePolicy#registerCookieSpec(String, Class)}
242
public static void setDefaultPolicy(int policy) {
243
defaultPolicy = policy;
247
* @param policy cookie policy to get the CookieSpec for
248
* @return cookie specification interface for the given policy
250
* @deprecated Use {@link CookiePolicy#getCookieSpec(String)}
252
public static CookieSpec getSpecByPolicy(int policy) {
255
return new CookieSpecBase();
257
return new NetscapeDraftSpec();
259
return new RFC2109Spec();
261
return new RFC2965Spec();
263
return getDefaultSpec();
269
* Returns {@link CookieSpec cookie specification} registered as {@link #DEFAULT}.
270
* If no default {@link CookieSpec cookie specification} has been registered,
271
* {@link RFC2109Spec RFC2109 specification} is returned.
273
* @return default {@link CookieSpec cookie specification}
277
public static CookieSpec getDefaultSpec() {
279
return getCookieSpec(DEFAULT);
280
} catch (IllegalStateException e) {
281
LOG.warn("Default cookie policy is not registered");
282
return new RFC2109Spec();
288
* Gets the CookieSpec for a particular cookie version.
290
* <p>Supported versions:
292
* <li><tt>version 0</tt> corresponds to the Netscape draft
293
* <li><tt>version 1</tt> corresponds to the RFC 2109
294
* <li>Any other cookie value coresponds to the default spec
297
* @param ver the cookie version to get the spec for
298
* @return cookie specification interface intended for processing
299
* cookies with the given version
301
* @deprecated Use {@link CookiePolicy#getCookieSpec(String)}
303
public static CookieSpec getSpecByVersion(int ver) {
306
return new NetscapeDraftSpec();
308
return new RFC2109Spec();
310
return getDefaultSpec();
315
* @return cookie specification interface that provides high compatibilty
316
* with common cookie management of popular HTTP agents
318
* @deprecated Use {@link CookiePolicy#getCookieSpec(String)}
320
public static CookieSpec getCompatibilitySpec() {
321
return getSpecByPolicy(COMPATIBILITY);
325
* Obtains the currently registered cookie policy names.
327
* Note that the DEFAULT policy (if present) is likely to be the same
328
* as one of the other policies, but does not have to be.
330
* @return array of registered cookie policy names
334
public static String[] getRegisteredCookieSpecs(){
335
return (String[]) SPECS.keySet().toArray(new String [SPECS.size()]);