1 /*
2 * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/cookie/CookiePolicy.java,v 1.7.2.1 2003/10/11 19:44:27 olegk Exp $
3 * $Revision: 1.7.2.1 $
4 * $Date: 2003/10/11 19:44:27 $
5 *
6 * ====================================================================
7 *
8 * The Apache Software License, Version 1.1
9 *
10 * Copyright (c) 2002-2003 The Apache Software Foundation. All rights
11 * reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 *
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 *
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in
22 * the documentation and/or other materials provided with the
23 * distribution.
24 *
25 * 3. The end-user documentation included with the redistribution, if
26 * any, must include the following acknowlegement:
27 * "This product includes software developed by the
28 * Apache Software Foundation (http://www.apache.org/)."
29 * Alternately, this acknowlegement may appear in the software itself,
30 * if and wherever such third-party acknowlegements normally appear.
31 *
32 * 4. The names "The Jakarta Project", "Commons", and "Apache Software
33 * Foundation" must not be used to endorse or promote products derived
34 * from this software without prior written permission. For written
35 * permission, please contact apache@apache.org.
36 *
37 * 5. Products derived from this software may not be called "Apache"
38 * nor may "Apache" appear in their names without prior written
39 * permission of the Apache Group.
40 *
41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * SUCH DAMAGE.
53 * ====================================================================
54 *
55 * This software consists of voluntary contributions made by many
56 * individuals on behalf of the Apache Software Foundation. For more
57 * information on the Apache Software Foundation, please see
58 * <http://www.apache.org/>.
59 *
60 * [Additional notices, if required by prior licensing conditions]
61 *
62 */
63
64 package org.apache.commons.httpclient.cookie;
65
66 import org.apache.commons.logging.Log;
67 import org.apache.commons.logging.LogFactory;
68
69 /***
70 * Cookie management policy class. The cookie policy provides corresponding
71 * cookie management interfrace for a given type or version of cookie.
72 * <p>RFC 2109 specification is used per default. Other supported specification
73 * can be chosen when appropriate or set default when desired
74 * <p>The following specifications are provided:
75 * <ul>
76 * <li><tt>COMPATIBILITY</tt>: compatible with the common cookie management
77 * practices * (even if they are not 100% standards compliant)
78 * <li><tt>NETSCAPE_DRAFT</tt>: Netscape cookie draft compliant
79 * <li><tt>RFC2109</tt>: RFC2109 compliant (default)
80 * </ul>
81 * <p>Default policy can be set on JVM start-up through the system property
82 * <tt>"apache.commons.httpclient.cookiespec"</tt>. Recognized values:
83 * <tt>COMPATIBILITY</tt>, <tt>NETSCAPE_DRAFT</tt>, <tt>RFC2109</tt>.
84 *
85 * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
86 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
87 *
88 * @since 2.0
89 */
90 public abstract class CookiePolicy {
91
92 /*** cookiespec system property. */
93 private static final String SYSTEM_PROPERTY =
94 "apache.commons.httpclient.cookiespec";
95
96 /***
97 * The <tt>COMPATIBILITY</tt> policy provides high compatibilty
98 * with common cookie management of popular HTTP agents.
99 */
100 public static final int COMPATIBILITY = 0;
101
102 /*** The <tt>NETSCAPE_DRAFT</tt> Netscape draft compliant policy. */
103 public static final int NETSCAPE_DRAFT = 1;
104
105 /*** The <tt>RFC2109</tt> RFC 2109 compliant policy. */
106 public static final int RFC2109 = 2;
107
108 /*** The default cookie policy. */
109 private static int defaultPolicy = RFC2109;
110
111 /*** Log object. */
112 protected static final Log LOG = LogFactory.getLog(CookiePolicy.class);
113
114 static {
115 String s = null;
116 try {
117 s = System.getProperty(SYSTEM_PROPERTY);
118 } catch (SecurityException e) {
119 }
120 if ("COMPATIBILITY".equalsIgnoreCase(s)) {
121 setDefaultPolicy(COMPATIBILITY);
122 } else if ("NETSCAPE_DRAFT".equalsIgnoreCase(s)) {
123 setDefaultPolicy(NETSCAPE_DRAFT);
124 } else if ("RFC2109".equalsIgnoreCase(s)) {
125 setDefaultPolicy(RFC2109);
126 } else {
127 if (s != null) {
128 LOG.warn("Unrecognized cookiespec property '" + s
129 + "' - using default");
130 }
131 setDefaultPolicy(defaultPolicy);
132 }
133 }
134
135 /***
136 * @return default cookie policy
137 * <tt>(COMPATIBILITY | NETSCAPE_DRAFT | RFC2109)</tt>
138 */
139 public static int getDefaultPolicy() {
140 return defaultPolicy;
141 }
142
143
144 /***
145 * @param policy new default cookie policy
146 * <tt>(COMPATIBILITY | NETSCAPE_DRAFT | RFC2109)</tt>
147 */
148 public static void setDefaultPolicy(int policy) {
149 defaultPolicy = policy;
150 }
151
152
153 /***
154 * @param policy cookie policy to get the CookieSpec for
155 * @return cookie specification interface for the given policy
156 * <tt>(COMPATIBILITY | NETSCAPE_DRAFT | RFC2109)</tt>
157 */
158 public static CookieSpec getSpecByPolicy(int policy) {
159 switch(policy) {
160 case COMPATIBILITY:
161 return new CookieSpecBase();
162 case NETSCAPE_DRAFT:
163 return new NetscapeDraftSpec();
164 case RFC2109:
165 return new RFC2109Spec();
166 default:
167 return getSpecByPolicy(defaultPolicy);
168 }
169 }
170
171
172 /***
173 * @return default cookie specification interface
174 */
175 public static CookieSpec getDefaultSpec() {
176 return getSpecByPolicy(defaultPolicy);
177 }
178
179
180 /***
181 * Gets the CookieSpec for a particular cookie version.
182 *
183 * <p>Supported versions:
184 * <ul>
185 * <li><tt>version 0</tt> corresponds to the NETSCAPE_DRAFT
186 * <li><tt>version 1</tt> corresponds to the RFC2109
187 * <li>Any other cookie value coresponds to the default spec
188 * <ul>
189 *
190 * @param ver the cookie version to get the spec for
191 * @return cookie specification interface intended for processing
192 * cookies with the given version
193 */
194 public static CookieSpec getSpecByVersion(int ver) {
195 switch(ver) {
196 case 0:
197 return new NetscapeDraftSpec();
198 case 1:
199 return new RFC2109Spec();
200 default:
201 return getDefaultSpec();
202 }
203 }
204
205 /***
206 * @return cookie specification interface that provides high compatibilty
207 * with common cookie management of popular HTTP agents
208 */
209 public static CookieSpec getCompatibilitySpec() {
210 return getSpecByPolicy(COMPATIBILITY);
211 }
212 }
This page was automatically generated by Maven