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.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36
37 /***
38 * Cookie management policy class. The cookie policy provides corresponding
39 * cookie management interfrace for a given type or version of cookie.
40 * <p>RFC 2109 specification is used per default. Other supported specification
41 * can be chosen when appropriate or set default when desired
42 * <p>The following specifications are provided:
43 * <ul>
44 * <li><tt>COMPATIBILITY</tt>: compatible with the common cookie management
45 * practices * (even if they are not 100% standards compliant)
46 * <li><tt>NETSCAPE_DRAFT</tt>: Netscape cookie draft compliant
47 * <li><tt>RFC2109</tt>: RFC2109 compliant (default)
48 * </ul>
49 * <p>Default policy can be set on JVM start-up through the system property
50 * <tt>"apache.commons.httpclient.cookiespec"</tt>. Recognized values:
51 * <tt>COMPATIBILITY</tt>, <tt>NETSCAPE_DRAFT</tt>, <tt>RFC2109</tt>.
52 *
53 * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
54 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
55 *
56 * @since 2.0
57 */
58 public abstract class CookiePolicy {
59
60 /*** cookiespec system property. */
61 private static final String SYSTEM_PROPERTY =
62 "apache.commons.httpclient.cookiespec";
63
64 /***
65 * The <tt>COMPATIBILITY</tt> policy provides high compatibilty
66 * with common cookie management of popular HTTP agents.
67 */
68 public static final int COMPATIBILITY = 0;
69
70 /*** The <tt>NETSCAPE_DRAFT</tt> Netscape draft compliant policy. */
71 public static final int NETSCAPE_DRAFT = 1;
72
73 /*** The <tt>RFC2109</tt> RFC 2109 compliant policy. */
74 public static final int RFC2109 = 2;
75
76 /*** The default cookie policy. */
77 private static int defaultPolicy = RFC2109;
78
79 /*** Log object. */
80 protected static final Log LOG = LogFactory.getLog(CookiePolicy.class);
81
82 static {
83 String s = null;
84 try {
85 s = System.getProperty(SYSTEM_PROPERTY);
86 } catch (SecurityException e) {
87 }
88 if ("COMPATIBILITY".equalsIgnoreCase(s)) {
89 setDefaultPolicy(COMPATIBILITY);
90 } else if ("NETSCAPE_DRAFT".equalsIgnoreCase(s)) {
91 setDefaultPolicy(NETSCAPE_DRAFT);
92 } else if ("RFC2109".equalsIgnoreCase(s)) {
93 setDefaultPolicy(RFC2109);
94 } else {
95 if (s != null) {
96 LOG.warn("Unrecognized cookiespec property '" + s
97 + "' - using default");
98 }
99 setDefaultPolicy(defaultPolicy);
100 }
101 }
102
103 /***
104 * @return default cookie policy
105 * <tt>(COMPATIBILITY | NETSCAPE_DRAFT | RFC2109)</tt>
106 */
107 public static int getDefaultPolicy() {
108 return defaultPolicy;
109 }
110
111
112 /***
113 * @param policy new default cookie policy
114 * <tt>(COMPATIBILITY | NETSCAPE_DRAFT | RFC2109)</tt>
115 */
116 public static void setDefaultPolicy(int policy) {
117 defaultPolicy = policy;
118 }
119
120
121 /***
122 * @param policy cookie policy to get the CookieSpec for
123 * @return cookie specification interface for the given policy
124 * <tt>(COMPATIBILITY | NETSCAPE_DRAFT | RFC2109)</tt>
125 */
126 public static CookieSpec getSpecByPolicy(int policy) {
127 switch(policy) {
128 case COMPATIBILITY:
129 return new CookieSpecBase();
130 case NETSCAPE_DRAFT:
131 return new NetscapeDraftSpec();
132 case RFC2109:
133 return new RFC2109Spec();
134 default:
135 return getSpecByPolicy(defaultPolicy);
136 }
137 }
138
139
140 /***
141 * @return default cookie specification interface
142 */
143 public static CookieSpec getDefaultSpec() {
144 return getSpecByPolicy(defaultPolicy);
145 }
146
147
148 /***
149 * Gets the CookieSpec for a particular cookie version.
150 *
151 * <p>Supported versions:
152 * <ul>
153 * <li><tt>version 0</tt> corresponds to the NETSCAPE_DRAFT
154 * <li><tt>version 1</tt> corresponds to the RFC2109
155 * <li>Any other cookie value coresponds to the default spec
156 * <ul>
157 *
158 * @param ver the cookie version to get the spec for
159 * @return cookie specification interface intended for processing
160 * cookies with the given version
161 */
162 public static CookieSpec getSpecByVersion(int ver) {
163 switch(ver) {
164 case 0:
165 return new NetscapeDraftSpec();
166 case 1:
167 return new RFC2109Spec();
168 default:
169 return getDefaultSpec();
170 }
171 }
172
173 /***
174 * @return cookie specification interface that provides high compatibilty
175 * with common cookie management of popular HTTP agents
176 */
177 public static CookieSpec getCompatibilitySpec() {
178 return getSpecByPolicy(COMPATIBILITY);
179 }
180 }