KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > httpclient > cookie > CookiePolicy


1 /*
2  * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/cookie/CookiePolicy.java,v 1.7.2.2 2004/02/22 18:21:15 olegk Exp $
3  * $Revision: 1.7.2.2 $
4  * $Date: 2004/02/22 18:21:15 $
5  *
6  * ====================================================================
7  *
8  * Copyright 2002-2004 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ====================================================================
22  *
23  * This software consists of voluntary contributions made by many
24  * individuals on behalf of the Apache Software Foundation. For more
25  * information on the Apache Software Foundation, please see
26  * <http://www.apache.org/>.
27  *
28  * [Additional notices, if required by prior licensing conditions]
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 JavaDoc 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 JavaDoc s = null;
84         try {
85             s = System.getProperty(SYSTEM_PROPERTY);
86         } catch (SecurityException JavaDoc 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 }
181
Popular Tags