View Javadoc
1 /* 2 * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/cookie/CookiePolicy.java,v 1.7 2003/02/01 23:35:43 olegk Exp $ 3 * $Revision: 1.7 $ 4 * $Date: 2003/02/01 23:35:43 $ 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 = System.getProperty(SYSTEM_PROPERTY); 116 117 if ("COMPATIBILITY".equalsIgnoreCase(s)) { 118 setDefaultPolicy(COMPATIBILITY); 119 } else if ("NETSCAPE_DRAFT".equalsIgnoreCase(s)) { 120 setDefaultPolicy(NETSCAPE_DRAFT); 121 } else if ("RFC2109".equalsIgnoreCase(s)) { 122 setDefaultPolicy(RFC2109); 123 } else { 124 if (s != null) { 125 LOG.warn("Unrecognized cookiespec property '" + s 126 + "' - using default"); 127 } 128 setDefaultPolicy(defaultPolicy); 129 } 130 } 131 132 /*** 133 * @return default cookie policy 134 * <tt>(COMPATIBILITY | NETSCAPE_DRAFT | RFC2109)</tt> 135 */ 136 public static int getDefaultPolicy() { 137 return defaultPolicy; 138 } 139 140 141 /*** 142 * @param policy new default cookie policy 143 * <tt>(COMPATIBILITY | NETSCAPE_DRAFT | RFC2109)</tt> 144 */ 145 public static void setDefaultPolicy(int policy) { 146 defaultPolicy = policy; 147 } 148 149 150 /*** 151 * @param policy cookie policy to get the CookieSpec for 152 * @return cookie specification interface for the given policy 153 * <tt>(COMPATIBILITY | NETSCAPE_DRAFT | RFC2109)</tt> 154 */ 155 public static CookieSpec getSpecByPolicy(int policy) { 156 switch(policy) { 157 case COMPATIBILITY: 158 return new CookieSpecBase(); 159 case NETSCAPE_DRAFT: 160 return new NetscapeDraftSpec(); 161 case RFC2109: 162 return new RFC2109Spec(); 163 default: 164 return getSpecByPolicy(defaultPolicy); 165 } 166 } 167 168 169 /*** 170 * @return default cookie specification interface 171 */ 172 public static CookieSpec getDefaultSpec() { 173 return getSpecByPolicy(defaultPolicy); 174 } 175 176 177 /*** 178 * Gets the CookieSpec for a particular cookie version. 179 * 180 * <p>Supported versions: 181 * <ul> 182 * <li><tt>version 0</tt> corresponds to the NETSCAPE_DRAFT 183 * <li><tt>version 1</tt> corresponds to the RFC2109 184 * <li>Any other cookie value coresponds to the default spec 185 * <ul> 186 * 187 * @param ver the cookie version to get the spec for 188 * @return cookie specification interface intended for processing 189 * cookies with the given version 190 */ 191 public static CookieSpec getSpecByVersion(int ver) { 192 switch(ver) { 193 case 0: 194 return new NetscapeDraftSpec(); 195 case 1: 196 return new RFC2109Spec(); 197 default: 198 return getDefaultSpec(); 199 } 200 } 201 202 /*** 203 * @return cookie specification interface that provides high compatibilty 204 * with common cookie management of popular HTTP agents 205 */ 206 public static CookieSpec getCompatibilitySpec() { 207 return getSpecByPolicy(COMPATIBILITY); 208 } 209 }

This page was automatically generated by Maven