1 /** 2 * Copyright 2014 Internet2 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 /* 17 * $Header: /home/hagleyj/i2mi/grouper-misc/grouperClient/src/ext/edu/internet2/middleware/grouperClientExt/org/apache/commons/httpclient/cookie/CookiePolicy.java,v 1.1 2008-11-30 10:57:19 mchyzer Exp $ 18 * $Revision: 1.1 $ 19 * $Date: 2008-11-30 10:57:19 $ 20 * 21 * ==================================================================== 22 * 23 * Licensed to the Apache Software Foundation (ASF) under one or more 24 * contributor license agreements. See the NOTICE file distributed with 25 * this work for additional information regarding copyright ownership. 26 * The ASF licenses this file to You under the Apache License, Version 2.0 27 * (the "License"); you may not use this file except in compliance with 28 * the License. You may obtain a copy of the License at 29 * 30 * http://www.apache.org/licenses/LICENSE-2.0 31 * 32 * Unless required by applicable law or agreed to in writing, software 33 * distributed under the License is distributed on an "AS IS" BASIS, 34 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 35 * See the License for the specific language governing permissions and 36 * limitations under the License. 37 * ==================================================================== 38 * 39 * This software consists of voluntary contributions made by many 40 * individuals on behalf of the Apache Software Foundation. For more 41 * information on the Apache Software Foundation, please see 42 * <http://www.apache.org/>. 43 * 44 */ 45 46 package edu.internet2.middleware.grouperClientExt.org.apache.commons.httpclient.cookie; 47 48 import java.util.Collections; 49 import java.util.HashMap; 50 import java.util.Map; 51 52 import edu.internet2.middleware.grouperClientExt.org.apache.commons.logging.Log; 53 import edu.internet2.middleware.grouperClientExt.org.apache.commons.logging.LogFactory; 54 55 /** 56 * Cookie management policy class. The cookie policy provides corresponding 57 * cookie management interfrace for a given type or version of cookie. 58 * <p>RFC 2109 specification is used per default. Other supported specification 59 * can be chosen when appropriate or set default when desired 60 * <p>The following specifications are provided: 61 * <ul> 62 * <li><tt>BROWSER_COMPATIBILITY</tt>: compatible with the common cookie 63 * management practices (even if they are not 100% standards compliant) 64 * <li><tt>NETSCAPE</tt>: Netscape cookie draft compliant 65 * <li><tt>RFC_2109</tt>: RFC2109 compliant (default) 66 * <li><tt>IGNORE_COOKIES</tt>: do not automcatically process cookies 67 * </ul> 68 * 69 * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a> 70 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a> 71 * 72 * @since 2.0 73 */ 74 public abstract class CookiePolicy { 75 76 private static Map SPECS = Collections.synchronizedMap(new HashMap()); 77 78 /** 79 * The policy that provides high degree of compatibilty 80 * with common cookie management of popular HTTP agents. 81 * 82 * @since 3.0 83 */ 84 public static final String BROWSER_COMPATIBILITY = "compatibility"; 85 86 /** 87 * The Netscape cookie draft compliant policy. 88 * 89 * @since 3.0 90 */ 91 public static final String NETSCAPE = "netscape"; 92 93 /** 94 * The RFC 2109 compliant policy. 95 * 96 * @since 3.0 97 */ 98 public static final String RFC_2109 = "rfc2109"; 99 100 /** 101 * The RFC 2965 compliant policy. 102 * 103 * @since 3.0 104 */ 105 public static final String RFC_2965 = "rfc2965"; 106 107 /** 108 * The policy that ignores cookies. 109 * 110 * @since 3.0 111 */ 112 public static final String IGNORE_COOKIES = "ignoreCookies"; 113 114 /** 115 * The default cookie policy. 116 * 117 * @since 3.0 118 */ 119 public static final String DEFAULT = "default"; 120 121 static { 122 CookiePolicy.registerCookieSpec(DEFAULT, RFC2109Spec.class); 123 CookiePolicy.registerCookieSpec(RFC_2109, RFC2109Spec.class); 124 CookiePolicy.registerCookieSpec(RFC_2965, RFC2965Spec.class); 125 CookiePolicy.registerCookieSpec(BROWSER_COMPATIBILITY, CookieSpecBase.class); 126 CookiePolicy.registerCookieSpec(NETSCAPE, NetscapeDraftSpec.class); 127 CookiePolicy.registerCookieSpec(IGNORE_COOKIES, IgnoreCookiesSpec.class); 128 } 129 130 /** 131 * The <tt>COMPATIBILITY</tt> policy provides high compatibilty 132 * with common cookie management of popular HTTP agents. 133 * 134 * @deprecated Use {@link #BROWSER_COMPATIBILITY} 135 */ 136 public static final int COMPATIBILITY = 0; 137 138 /** 139 * The <tt>NETSCAPE_DRAFT</tt> Netscape draft compliant policy. 140 * 141 * @deprecated Use {@link #NETSCAPE} 142 */ 143 public static final int NETSCAPE_DRAFT = 1; 144 145 /** 146 * The <tt>RFC2109</tt> RFC 2109 compliant policy. 147 * 148 * @deprecated Use {@link #RFC_2109} 149 */ 150 public static final int RFC2109 = 2; 151 152 /** 153 * The <tt>RFC2965</tt> RFC 2965 compliant policy. 154 * 155 * @deprecated Use {@link #RFC_2965} 156 */ 157 public static final int RFC2965 = 3; 158 159 /** 160 * The default cookie policy. 161 * 162 * @deprecated Use {@link #DEFAULT} 163 */ 164 private static int defaultPolicy = RFC2109; 165 166 /** Log object. */ 167 protected static final Log LOG = LogFactory.getLog(CookiePolicy.class); 168 169 /** 170 * Registers a new {@link CookieSpec cookie specification} with the given identifier. 171 * If a specification with the given ID already exists it will be overridden. 172 * This ID is the same one used to retrieve the {@link CookieSpec cookie specification} 173 * from {@link #getCookieSpec(String)}. 174 * 175 * @param id the identifier for this specification 176 * @param clazz the {@link CookieSpec cookie specification} class to register 177 * 178 * @see #getCookieSpec(String) 179 * 180 * @since 3.0 181 */ 182 public static void registerCookieSpec(final String id, final Class clazz) { 183 if (id == null) { 184 throw new IllegalArgumentException("Id may not be null"); 185 } 186 if (clazz == null) { 187 throw new IllegalArgumentException("Cookie spec class may not be null"); 188 } 189 SPECS.put(id.toLowerCase(), clazz); 190 } 191 192 /** 193 * Unregisters the {@link CookieSpec cookie specification} with the given ID. 194 * 195 * @param id the ID of the {@link CookieSpec cookie specification} to unregister 196 * 197 * @since 3.0 198 */ 199 public static void unregisterCookieSpec(final String id) { 200 if (id == null) { 201 throw new IllegalArgumentException("Id may not be null"); 202 } 203 SPECS.remove(id.toLowerCase()); 204 } 205 206 /** 207 * Gets the {@link CookieSpec cookie specification} with the given ID. 208 * 209 * @param id the {@link CookieSpec cookie specification} ID 210 * 211 * @return {@link CookieSpec cookie specification} 212 * 213 * @throws IllegalStateException if a policy with the ID cannot be found 214 * 215 * @since 3.0 216 */ 217 public static CookieSpec getCookieSpec(final String id) 218 throws IllegalStateException { 219 220 if (id == null) { 221 throw new IllegalArgumentException("Id may not be null"); 222 } 223 Class clazz = (Class)SPECS.get(id.toLowerCase()); 224 225 if (clazz != null) { 226 try { 227 return (CookieSpec)clazz.newInstance(); 228 } catch (Exception e) { 229 LOG.error("Error initializing cookie spec: " + id, e); 230 throw new IllegalStateException(id + 231 " cookie spec implemented by " + 232 clazz.getName() + " could not be initialized"); 233 } 234 } else { 235 throw new IllegalStateException("Unsupported cookie spec " + id); 236 } 237 } 238 239 /** 240 * @return default cookie policy 241 * 242 * @deprecated Use {@link #getDefaultSpec()} 243 * 244 * @see #getDefaultSpec() 245 */ 246 public static int getDefaultPolicy() { 247 return defaultPolicy; 248 } 249 250 251 /** 252 * @param policy new default cookie policy 253 * 254 * @deprecated Use {@link CookiePolicy#registerCookieSpec(String, Class)} 255 * @see #DEFAULT 256 */ 257 public static void setDefaultPolicy(int policy) { 258 defaultPolicy = policy; 259 } 260 261 /** 262 * @param policy cookie policy to get the CookieSpec for 263 * @return cookie specification interface for the given policy 264 * 265 * @deprecated Use {@link CookiePolicy#getCookieSpec(String)} 266 */ 267 public static CookieSpec getSpecByPolicy(int policy) { 268 switch(policy) { 269 case COMPATIBILITY: 270 return new CookieSpecBase(); 271 case NETSCAPE_DRAFT: 272 return new NetscapeDraftSpec(); 273 case RFC2109: 274 return new RFC2109Spec(); 275 case RFC2965: 276 return new RFC2965Spec(); 277 default: 278 return getDefaultSpec(); 279 } 280 } 281 282 283 /** 284 * Returns {@link CookieSpec cookie specification} registered as {@link #DEFAULT}. 285 * If no default {@link CookieSpec cookie specification} has been registered, 286 * {@link RFC2109Spec RFC2109 specification} is returned. 287 * 288 * @return default {@link CookieSpec cookie specification} 289 * 290 * @see #DEFAULT 291 */ 292 public static CookieSpec getDefaultSpec() { 293 try { 294 return getCookieSpec(DEFAULT); 295 } catch (IllegalStateException e) { 296 LOG.warn("Default cookie policy is not registered"); 297 return new RFC2109Spec(); 298 } 299 } 300 301 302 /** 303 * Gets the CookieSpec for a particular cookie version. 304 * 305 * <p>Supported versions: 306 * <ul> 307 * <li><tt>version 0</tt> corresponds to the Netscape draft 308 * <li><tt>version 1</tt> corresponds to the RFC 2109 309 * <li>Any other cookie value coresponds to the default spec 310 * <ul> 311 * 312 * @param ver the cookie version to get the spec for 313 * @return cookie specification interface intended for processing 314 * cookies with the given version 315 * 316 * @deprecated Use {@link CookiePolicy#getCookieSpec(String)} 317 */ 318 public static CookieSpec getSpecByVersion(int ver) { 319 switch(ver) { 320 case 0: 321 return new NetscapeDraftSpec(); 322 case 1: 323 return new RFC2109Spec(); 324 default: 325 return getDefaultSpec(); 326 } 327 } 328 329 /** 330 * @return cookie specification interface that provides high compatibilty 331 * with common cookie management of popular HTTP agents 332 * 333 * @deprecated Use {@link CookiePolicy#getCookieSpec(String)} 334 */ 335 public static CookieSpec getCompatibilitySpec() { 336 return getSpecByPolicy(COMPATIBILITY); 337 } 338 339 /** 340 * Obtains the currently registered cookie policy names. 341 * 342 * Note that the DEFAULT policy (if present) is likely to be the same 343 * as one of the other policies, but does not have to be. 344 * 345 * @return array of registered cookie policy names 346 * 347 * @since 3.1 348 */ 349 public static String[] getRegisteredCookieSpecs(){ 350 return (String[]) SPECS.keySet().toArray(new String [SPECS.size()]); 351 } 352 353 }