Index: examples/CookieDemoApp.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/examples/CookieDemoApp.java,v retrieving revision 1.11 diff -u -r1.11 CookieDemoApp.java --- examples/CookieDemoApp.java 3 Oct 2003 20:57:35 -0000 1.11 +++ examples/CookieDemoApp.java 19 Oct 2003 17:08:34 -0000 @@ -1,5 +1,5 @@ /* - * $Header: /home/cvspublic/jakarta-commons/httpclient/src/examples/CookieDemoApp.java,v 1.11 2003/10/03 20:57:35 olegk Exp $ + * $Header: /home/cvs/jakarta-commons/httpclient/src/examples/CookieDemoApp.java,v 1.11 2003/10/03 20:57:35 olegk Exp $ * $Revision: 1.11 $ * $Date: 2003/10/03 20:57:35 $ * ==================================================================== @@ -108,24 +108,26 @@ Cookie mycookie = new Cookie(".foobar.com", "mycookie", "stuff", "/", null, false); // and then added to your HTTP state instance initialState.addCookie(mycookie); + + // Get HTTP client instance + HttpClient httpclient = new HttpClient(); + httpclient.getParams().setConnectionTimeout(30000); + httpclient.setState(initialState); + // RFC 2101 cookie management spec is used per default // to parse, validate, format & match cookies - initialState.setCookiePolicy(CookiePolicy.RFC2109); + httpclient.getParams().setCookiePolicy(CookiePolicy.RFC_2109); // A different cookie management spec can be selected // when desired -// initialState.setCookiePolicy(CookiePolicy.NETSCAPE_DRAFT); + //httpclient.getParams().setCookiePolicy(CookiePolicy.NETSCAPE); // Netscape Cookie Draft spec is provided for completeness // You would hardly want to use this spec in real life situations -// initialState.setCookiePolicy(CookiePolicy.COMPATIBILITY); + //httppclient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); // Compatibility policy is provided in order to mimic cookie // management of popular web browsers that is in some areas // not 100% standards compliant - // Get HTTP client instance - HttpClient httpclient = new HttpClient(); - httpclient.getParams().setConnectionTimeout(30000); - httpclient.setState(initialState); // Get HTTP GET method GetMethod httpget = new GetMethod(strURL); // Execute HTTP GET Index: examples/FormLoginDemo.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/examples/FormLoginDemo.java,v retrieving revision 1.2 diff -u -r1.2 FormLoginDemo.java --- examples/FormLoginDemo.java 17 Jul 2003 21:57:42 -0000 1.2 +++ examples/FormLoginDemo.java 19 Oct 2003 17:08:35 -0000 @@ -84,7 +84,7 @@ HttpClient client = new HttpClient(); client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT, "http"); - client.getState().setCookiePolicy(CookiePolicy.COMPATIBILITY); + client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); // 'developer.java.sun.com' has cookie compliance problems // Their session cookie's domain attribute is in violation of the RFC2109 // We have to resort to using compatibility cookie policy Index: java/org/apache/commons/httpclient/Cookie.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/Cookie.java,v retrieving revision 1.39 diff -u -r1.39 Cookie.java --- java/org/apache/commons/httpclient/Cookie.java 5 Jul 2003 22:31:20 -0000 1.39 +++ java/org/apache/commons/httpclient/Cookie.java 19 Oct 2003 17:08:38 -0000 @@ -468,8 +468,13 @@ * @return a string suitable for sending in a Cookie header. */ public String toExternalForm() { - return CookiePolicy.getSpecByVersion( - getVersion()).formatCookie(this); + CookieSpec spec = null; + if (getVersion() > 0) { + spec = CookiePolicy.getDefaultSpec(); + } else { + spec = CookiePolicy.getCookieSpec(CookiePolicy.NETSCAPE); + } + return spec.formatCookie(this); } /** Index: java/org/apache/commons/httpclient/HttpMethodBase.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java,v retrieving revision 1.182 diff -u -r1.182 HttpMethodBase.java --- java/org/apache/commons/httpclient/HttpMethodBase.java 13 Oct 2003 12:22:24 -0000 1.182 +++ java/org/apache/commons/httpclient/HttpMethodBase.java 19 Oct 2003 17:08:50 -0000 @@ -1156,7 +1156,7 @@ // Clean up the cookie headers removeRequestHeader("cookie"); - CookieSpec matcher = CookiePolicy.getSpecByPolicy(state.getCookiePolicy()); + CookieSpec matcher = CookiePolicy.getCookieSpec(this.params.getCookiePolicy()); Cookie[] cookies = matcher.match(conn.getHost(), conn.getPort(), getPath(), conn.isSecure(), state.getCookies()); if ((cookies != null) && (cookies.length > 0)) { @@ -1487,7 +1487,7 @@ headers = getResponseHeaderGroup().getHeaders("set-cookie"); } - CookieSpec parser = CookiePolicy.getSpecByPolicy(state.getCookiePolicy()); + CookieSpec parser = CookiePolicy.getCookieSpec(this.params.getCookiePolicy()); for (int i = 0; i < headers.length; i++) { Header header = headers[i]; Cookie[] cookies = null; Index: java/org/apache/commons/httpclient/HttpState.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpState.java,v retrieving revision 1.27 diff -u -r1.27 HttpState.java --- java/org/apache/commons/httpclient/HttpState.java 16 Sep 2003 21:29:45 -0000 1.27 +++ java/org/apache/commons/httpclient/HttpState.java 19 Oct 2003 17:08:54 -0000 @@ -1,5 +1,5 @@ /* - * $Header: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpState.java,v 1.27 2003/09/16 21:29:45 olegk Exp $ + * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpState.java,v 1.27 2003/09/16 21:29:45 olegk Exp $ * $Revision: 1.27 $ * $Date: 2003/09/16 21:29:45 $ * @@ -124,15 +124,11 @@ * Array of {@link Cookie cookies} that this HTTP state contains. */ private ArrayList cookies = new ArrayList(); - /** - * Cookie policy that applies to this HTTP state. Default is - * {@link CookiePolicy#RFC2109} - */ - private int cookiePolicy = CookiePolicy.RFC2109; private boolean preemptive = false; - // -------------------------------------------------------- Class Variables + private int cookiePolicy = 0; + // -------------------------------------------------------- Class Variables /** Log object for this class. */ private static final Log LOG = LogFactory.getLog(HttpState.class); @@ -141,11 +137,7 @@ * Default constructor. */ public HttpState() { - super(); - - this.cookiePolicy = CookiePolicy.getDefaultPolicy(); - } // ------------------------------------------------------------- Properties @@ -290,10 +282,14 @@ * HTTP state. * * @return The {@link CookiePolicy cookie policy}. + * + * @deprecated Use + * {@link org.apache.commons.httpclient.params.HttpMethodParams#getCookiePolicy()}, + * {@link HttpMethod#getParams()}. */ public int getCookiePolicy() { - return this.cookiePolicy; + return CookiePolicy.getDefaultPolicy(); } @@ -338,6 +334,10 @@ * {@link CookiePolicy#RFC2109}. * * @param policy new {@link CookiePolicy cookie policy} + * + * @deprecated + * Use {@link org.apache.commons.httpclient.params.HttpMethodParams#setCookiePolicy(String)}, + * {@link HttpMethod#getParams()}. */ public void setCookiePolicy(int policy) { Index: java/org/apache/commons/httpclient/cookie/CookiePolicy.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/cookie/CookiePolicy.java,v retrieving revision 1.8 diff -u -r1.8 CookiePolicy.java --- java/org/apache/commons/httpclient/cookie/CookiePolicy.java 11 Oct 2003 19:46:51 -0000 1.8 +++ java/org/apache/commons/httpclient/cookie/CookiePolicy.java 19 Oct 2003 17:08:56 -0000 @@ -1,5 +1,5 @@ /* - * $Header: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/cookie/CookiePolicy.java,v 1.8 2003/10/11 19:46:51 olegk Exp $ + * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/cookie/CookiePolicy.java,v 1.8 2003/10/11 19:46:51 olegk Exp $ * $Revision: 1.8 $ * $Date: 2003/10/11 19:46:51 $ * @@ -63,6 +63,10 @@ package org.apache.commons.httpclient.cookie; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -73,14 +77,11 @@ * can be chosen when appropriate or set default when desired *

The following specifications are provided: *

- *

Default policy can be set on JVM start-up through the system property - * "apache.commons.httpclient.cookiespec". Recognized values: - * COMPATIBILITY, NETSCAPE_DRAFT, RFC2109. * * @author Oleg Kalnichevski * @author Mike Bowler @@ -89,52 +90,125 @@ */ public abstract class CookiePolicy { - /** cookiespec system property. */ - private static final String SYSTEM_PROPERTY = - "apache.commons.httpclient.cookiespec"; + private static Map SPECS = Collections.synchronizedMap(new HashMap()); + + /** + * The policy that provides high degree of compatibilty + * with common cookie management of popular HTTP agents. + */ + public static final String BROWSER_COMPATIBILITY = "compatibility"; + + /** + * The Netscape cookie draft compliant policy. + */ + public static final String NETSCAPE = "netscape"; + /** + * The RFC 2109 compliant policy. + */ + public static final String RFC_2109 = "rfc2109"; + + /** + * The default cookie policy. + */ + public static final String DEFAULT = "default"; + + static { + CookiePolicy.registerCookieSpec(DEFAULT, new RFC2109Spec()); + CookiePolicy.registerCookieSpec(RFC_2109, new RFC2109Spec()); + CookiePolicy.registerCookieSpec(BROWSER_COMPATIBILITY, new CookieSpecBase()); + CookiePolicy.registerCookieSpec(NETSCAPE, new NetscapeDraftSpec()); + } + /** * The COMPATIBILITY policy provides high compatibilty * with common cookie management of popular HTTP agents. + * + * @deprecated Use {@link #BROWSER_COMPATIBILITY} */ public static final int COMPATIBILITY = 0; - /** The NETSCAPE_DRAFT Netscape draft compliant policy. */ + /** + * The NETSCAPE_DRAFT Netscape draft compliant policy. + * + * @deprecated Use {@link #NETSCAPE} + */ public static final int NETSCAPE_DRAFT = 1; - /** The RFC2109 RFC 2109 compliant policy. */ + /** + * The RFC2109 RFC 2109 compliant policy. + * + * @deprecated Use {@link #RFC_2109} + */ public static final int RFC2109 = 2; - /** The default cookie policy. */ + /** + * The default cookie policy. + * + * @deprecated Use {@link #DEFAULT} + */ private static int defaultPolicy = RFC2109; /** Log object. */ protected static final Log LOG = LogFactory.getLog(CookiePolicy.class); - static { - String s = null; - try { - s = System.getProperty(SYSTEM_PROPERTY); - } catch (SecurityException ignore) { - } - if ("COMPATIBILITY".equalsIgnoreCase(s)) { - setDefaultPolicy(COMPATIBILITY); - } else if ("NETSCAPE_DRAFT".equalsIgnoreCase(s)) { - setDefaultPolicy(NETSCAPE_DRAFT); - } else if ("RFC2109".equalsIgnoreCase(s)) { - setDefaultPolicy(RFC2109); - } else { - if (s != null) { - LOG.warn("Unrecognized cookiespec property '" + s - + "' - using default"); - } - setDefaultPolicy(defaultPolicy); + /** + * Registers a new {@link CookieSpec cookie specification} with the given identifier. + * If a specification with the given ID already exists it will be overridden. + * This ID is the same one used to retrieve the {@link CookieSpec cookie specification} + * from {@link #getCookieSpec(String)}. + * + * @param id the identifier for this specification + * @param spec the {@link CookieSpec cookie specification} to register + * + * @see #getCookieSpec(String) + */ + public static void registerCookieSpec(final String id, final CookieSpec spec) { + if (id == null) { + throw new IllegalArgumentException("Id may not be null"); + } + if (spec == null) { + throw new IllegalArgumentException("Cookie spec may not be null"); } + SPECS.put(id, spec); + } + + /** + * Unregisters the {@link CookieSpec cookie specification} with the given ID. + * + * @param id the ID of the {@link CookieSpec cookie specification} to unregister + */ + public static void unregisterCookieSpec(final String id) { + if (id == null) { + throw new IllegalArgumentException("Id may not be null"); + } + SPECS.remove(id); } /** + * Gets the {@link CookieSpec cookie specification} with the given ID. + * + * @param id the {@link CookieSpec cookie specification} ID + * + * @return {@link CookieSpec cookie specification} + * + * @throws IllegalStateException if a policy with the ID cannot be found + */ + public static CookieSpec getCookieSpec(final String id) + throws IllegalStateException { + + if (id == null) { + throw new IllegalArgumentException("Id may not be null"); + } + return (CookieSpec)SPECS.get(id); + } + + /** * @return default cookie policy * (COMPATIBILITY | NETSCAPE_DRAFT | RFC2109) + * + * @deprecated Use {@link CookiePolicy#getCookieSpec(String)} + * @see #DEFAULT */ public static int getDefaultPolicy() { return defaultPolicy; @@ -144,16 +218,19 @@ /** * @param policy new default cookie policy * (COMPATIBILITY | NETSCAPE_DRAFT | RFC2109) + * + * @deprecated Use {@link CookiePolicy#registerCookieSpec(String, CookieSpec)} */ public static void setDefaultPolicy(int policy) { defaultPolicy = policy; } - /** * @param policy cookie policy to get the CookieSpec for * @return cookie specification interface for the given policy * (COMPATIBILITY | NETSCAPE_DRAFT | RFC2109) + * + * @deprecated Use {@link CookiePolicy#getCookieSpec(String)} */ public static CookieSpec getSpecByPolicy(int policy) { switch(policy) { @@ -164,16 +241,27 @@ case RFC2109: return new RFC2109Spec(); default: - return getSpecByPolicy(defaultPolicy); + return getDefaultSpec(); } } /** - * @return default cookie specification interface + * Returns {@link CookieSpec cookie specification} registered as {@link #DEFAULT}. + * If no default {@link CookieSpec cookie specification} has been registered, + * {@link RFC2109Spec RFC2109 specification} is returned. + * + * @return default {@link CookieSpec cookie specification} + * + * @see #DEFAULT */ public static CookieSpec getDefaultSpec() { - return getSpecByPolicy(defaultPolicy); + try { + return getCookieSpec(DEFAULT); + } catch (IllegalStateException e) { + LOG.warn("Default cookie policy is not registered"); + return new RFC2109Spec(); + } } @@ -182,14 +270,16 @@ * *

Supported versions: *