diff --git a/src/java/org/apache/http/auth/AuthPolicy.java b/src/java/org/apache/http/auth/AuthPolicy.java index 2278468b6..2d7d6b953 100644 --- a/src/java/org/apache/http/auth/AuthPolicy.java +++ b/src/java/org/apache/http/auth/AuthPolicy.java @@ -31,161 +31,101 @@ package org.apache.http.auth; import java.util.ArrayList; -import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.http.impl.auth.BasicScheme; -import org.apache.http.impl.auth.DigestScheme; +import org.apache.http.cookie.CookieSpecFactory; +import org.apache.http.params.HttpParams; /** * Authentication policy class. The Authentication policy provides corresponding * authentication scheme interfrace for a given type of authorization challenge. - *
The following specifications are provided: - *
- * If several schemes are returned in the WWW-Authenticate - * or Proxy-Authenticate header, this parameter defines which - * {@link AuthScheme authentication schemes} takes precedence over others. - * The first item in the collection represents the most preferred - * {@link AuthScheme authentication scheme}, the last item represents the ID - * of the least preferred one. - *
- * - * @see org.apache.commons.httpclient.params.DefaultHttpParams - */ - public static final String AUTH_SCHEME_PRIORITY = "http.auth.scheme-priority"; - - /** - * The NTLM scheme is a proprietary Microsoft Windows Authentication - * protocol (considered to be the most secure among currently supported - * authentication schemes). - */ - public static final String NTLM = "NTLM"; - - /** - * Digest authentication scheme as defined in RFC2617. - */ - public static final String DIGEST = "Digest"; - - /** - * Basic authentication scheme as defined in RFC2617 (considered inherently - * insecure, but most widely supported) - */ - public static final String BASIC = "Basic"; - - static { -// AuthPolicy.registerAuthScheme(NTLM, NTLMScheme.class); - AuthPolicy.registerAuthScheme(DIGEST, DigestScheme.class); - AuthPolicy.registerAuthScheme(BASIC, BasicScheme.class); - } - - /** Log object. */ - protected static final Log LOG = LogFactory.getLog(AuthPolicy.class); - - /** - * Registers a class implementing an {@link AuthScheme authentication scheme} with - * the given identifier. If a class with the given ID already exists it will be overridden. - * This ID is the same one used to retrieve the {@link AuthScheme authentication scheme} - * from {@link #getAuthScheme(String)}. + * Registers a {@link CookieSpecFactory} with the given identifier. If a factory with the + * given name already exists it will be overridden. This name is the same one used to + * retrieve the {@link AuthScheme authentication scheme} from {@link #getAuthScheme(String)}. * ** Please note that custom authentication preferences, if used, need to be updated accordingly * for the new {@link AuthScheme authentication scheme} to take effect. *
* - * @param id the identifier for this scheme - * @param clazz the class to register + * @param name the identifier for this scheme + * @param factory the {@link AuthSchemeFactory} class to register * * @see #getAuthScheme(String) - * @see #AUTH_SCHEME_PRIORITY */ - public static synchronized void registerAuthScheme(final String id, Class clazz) { - if (id == null) { - throw new IllegalArgumentException("Id may not be null"); + public synchronized void registerAuthScheme( + final String name, + final AuthSchemeFactory factory) { + if (name == null) { + throw new IllegalArgumentException("Name may not be null"); } - if (clazz == null) { - throw new IllegalArgumentException("Authentication scheme class may not be null"); + if (factory == null) { + throw new IllegalArgumentException("Authentication scheme factory may not be null"); } - SCHEMES.put(id.toLowerCase(), clazz); - SCHEME_LIST.add(id.toLowerCase()); + registeredSchemes.put(name.toLowerCase(), factory); } /** * Unregisters the class implementing an {@link AuthScheme authentication scheme} with - * the given ID. + * the given name. * - * @param id the ID of the class to unregister + * @param name the identifier of the class to unregister */ - public static synchronized void unregisterAuthScheme(final String id) { - if (id == null) { - throw new IllegalArgumentException("Id may not be null"); + public synchronized void unregisterAuthScheme(final String name) { + if (name == null) { + throw new IllegalArgumentException("Name may not be null"); } - SCHEMES.remove(id.toLowerCase()); - SCHEME_LIST.remove(id.toLowerCase()); + registeredSchemes.remove(name.toLowerCase()); } /** - * Gets the {@link AuthScheme authentication scheme} with the given ID. + * Gets the {@link AuthScheme authentication scheme} with the given name. * - * @param id the {@link AuthScheme authentication scheme} ID + * @param name the {@link AuthScheme authentication scheme} identifier + * @param params the {@link HttpParams HTTP parameters} for the authentication + * scheme. * * @return {@link AuthScheme authentication scheme} * - * @throws IllegalStateException if a scheme with the ID cannot be found + * @throws IllegalStateException if a scheme with the given name cannot be found */ - public static synchronized AuthScheme getAuthScheme(final String id) + public synchronized AuthScheme getAuthScheme(final String name, final HttpParams params) throws IllegalStateException { - if (id == null) { - throw new IllegalArgumentException("Id may not be null"); + if (name == null) { + throw new IllegalArgumentException("Name may not be null"); } - Class clazz = (Class)SCHEMES.get(id.toLowerCase()); - if (clazz != null) { - try { - return (AuthScheme)clazz.newInstance(); - } catch (Exception e) { - LOG.error("Error initializing authentication scheme: " + id, e); - throw new IllegalStateException(id + - " authentication scheme implemented by " + - clazz.getName() + " could not be initialized"); - } + AuthSchemeFactory factory = (AuthSchemeFactory) registeredSchemes.get(name.toLowerCase()); + if (factory != null) { + return factory.newInstance(params); } else { - throw new IllegalStateException("Unsupported authentication scheme " + id); + throw new IllegalStateException("Unsupported authentication scheme: " + name); } } /** - * Returns a list containing all registered {@link AuthScheme authentication + * Obtains a list containing names of all registered {@link AuthScheme authentication * schemes} in their default order. * - * @return {@link AuthScheme authentication scheme} + * @return list of registered scheme names */ - public static synchronized List getDefaultAuthPrefs() { - return (List)SCHEME_LIST.clone(); + public synchronized List getSchemeNames() { + return new ArrayList(registeredSchemes.keySet()); } + } diff --git a/src/java/org/apache/http/auth/AuthSchemeFactory.java b/src/java/org/apache/http/auth/AuthSchemeFactory.java new file mode 100644 index 000000000..c58d831e1 --- /dev/null +++ b/src/java/org/apache/http/auth/AuthSchemeFactory.java @@ -0,0 +1,46 @@ +/* + * $HeadURL$ + * $Revision$ + * $Date$ + * + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + *null
if there is none by this name
*/
- public final Scheme get(String name) {
+ public synchronized final Scheme get(String name) {
if (name == null)
throw new IllegalArgumentException("Name must not be null.");
@@ -145,7 +144,7 @@ public final class SchemeRegistry {
* @return the scheme previously registered with that name, or
* null
if none was registered
*/
- public final Scheme register(Scheme sch) {
+ public synchronized final Scheme register(Scheme sch) {
if (sch == null)
throw new IllegalArgumentException("Scheme must not be null.");
@@ -162,7 +161,7 @@ public final class SchemeRegistry {
* @return the unregistered scheme, or
* null
if there was none
*/
- public final Scheme unregister(String name) {
+ public synchronized final Scheme unregister(String name) {
if (name == null)
throw new IllegalArgumentException("Name must not be null.");
@@ -174,13 +173,12 @@ public final class SchemeRegistry {
/**
- * Obtains the names of the registered schemes.
+ * Obtains the names of the registered schemes in their default order.
*
- * @return iterator over the registered scheme names.
- * The iterator supports {@link Iterator#remove remove()}.
+ * @return List containing registered scheme names.
*/
- public final Iterator getSchemeNames() {
- return registeredSchemes.keySet().iterator();
+ public final List getSchemeNames() {
+ return new ArrayList(registeredSchemes.keySet());
}
diff --git a/src/java/org/apache/http/cookie/CookiePolicy.java b/src/java/org/apache/http/cookie/CookiePolicy.java
index 9a405c304..350be0265 100644
--- a/src/java/org/apache/http/cookie/CookiePolicy.java
+++ b/src/java/org/apache/http/cookie/CookiePolicy.java
@@ -31,8 +31,9 @@
package org.apache.http.cookie;
-import java.util.Collections;
-import java.util.HashMap;
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
import java.util.Map;
import org.apache.http.cookie.params.CookieSpecParams;
@@ -47,74 +48,78 @@ import org.apache.http.params.HttpParams;
*
* @since 4.0
*/
-public class CookiePolicy {
+public final class CookiePolicy {
- private static Map SPECS = Collections.synchronizedMap(new HashMap());
+ public final static CookiePolicy DEFAULT = new CookiePolicy();
- private CookiePolicy() {
+ private final Map registeredSpecs;
+
+ public CookiePolicy() {
+ super();
+ this.registeredSpecs = new LinkedHashMap();
}
/**
* Registers a {@link CookieSpecFactory} 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 CookieSpecFactory}
+ * If a specification with the given name already exists it will be overridden.
+ * This nameis the same one used to retrieve the {@link CookieSpecFactory}
* from {@link #getCookieSpec(String)}.
*
- * @param id the identifier for this specification
+ * @param name the identifier for this specification
* @param factory the {@link CookieSpecFactory} class to register
*
* @see #getCookieSpec(String)
*/
- public static void register(final String id, final CookieSpecFactory factory) {
- if (id == null) {
- throw new IllegalArgumentException("Id may not be null");
+ public synchronized void register(final String name, final CookieSpecFactory factory) {
+ if (name == null) {
+ throw new IllegalArgumentException("Name may not be null");
}
if (factory == null) {
throw new IllegalArgumentException("Cookie spec factory may not be null");
}
- SPECS.put(id.toLowerCase(), factory);
+ registeredSpecs.put(name.toLowerCase(), factory);
}
/**
* Unregisters the {@link CookieSpecFactory} with the given ID.
*
- * @param id the ID of the {@link CookieSpec cookie specification} to unregister
+ * @param name the identifier of the {@link CookieSpec cookie specification} to unregister
*/
- public static void unregister(final String id) {
+ public synchronized void unregister(final String id) {
if (id == null) {
throw new IllegalArgumentException("Id may not be null");
}
- SPECS.remove(id.toLowerCase());
+ registeredSpecs.remove(id.toLowerCase());
}
/**
* Gets the {@link CookieSpec cookie specification} with the given ID.
*
- * @param id the {@link CookieSpec cookie specification} ID
+ * @param name the {@link CookieSpec cookie specification} identifier
* @param params the {@link HttpParams HTTP parameters} for the cookie
* specification.
*
* @return {@link CookieSpec cookie specification}
*
- * @throws IllegalStateException if a policy with the ID cannot be found
+ * @throws IllegalStateException if a policy with the given name cannot be found
*/
- public static CookieSpec getCookieSpec(final String id, final HttpParams params)
+ public synchronized CookieSpec getCookieSpec(final String name, final HttpParams params)
throws IllegalStateException {
- if (id == null) {
- throw new IllegalArgumentException("Id may not be null");
+ if (name == null) {
+ throw new IllegalArgumentException("Name may not be null");
}
- CookieSpecFactory factory = (CookieSpecFactory) SPECS.get(id.toLowerCase());
+ CookieSpecFactory factory = (CookieSpecFactory) registeredSpecs.get(name.toLowerCase());
if (factory != null) {
return factory.newInstance(params);
} else {
- throw new IllegalStateException("Unsupported cookie spec " + id);
+ throw new IllegalStateException("Unsupported cookie spec: " + name);
}
}
/**
* Gets the {@link CookieSpec cookie specification} based on the given
- * HTTP parameters. The cookie specification ID will be obtained from
+ * HTTP parameters. The cookie specification name will be obtained from
* the HTTP parameters.
*
* @param params the {@link HttpParams HTTP parameters} for the cookie
@@ -122,11 +127,11 @@ public class CookiePolicy {
*
* @return {@link CookieSpec cookie specification}
*
- * @throws IllegalStateException if a policy with the ID cannot be found
+ * @throws IllegalStateException if a policy with the given name cannot be found
*
* @see CookieSpecParams#getCookiePolicy(HttpParams)
*/
- public static CookieSpec getCookieSpec(final HttpParams params)
+ public CookieSpec getCookieSpec(final HttpParams params)
throws IllegalStateException {
if (params == null) {
throw new IllegalArgumentException("HTTP parameters may not be null");
@@ -135,29 +140,30 @@ public class CookiePolicy {
}
/**
- * Gets the {@link CookieSpec cookie specification} with the given ID.
+ * Gets the {@link CookieSpec cookie specification} with the given name.
*
- * @param id the {@link CookieSpec cookie specification} ID
+ * @param name the {@link CookieSpec cookie specification} identifier
*
* @return {@link CookieSpec cookie specification}
*
- * @throws IllegalStateException if a policy with the ID cannot be found
+ * @throws IllegalStateException if a policy with the given name cannot be found
*/
- public static CookieSpec getCookieSpec(final String id)
+ public synchronized CookieSpec getCookieSpec(final String name)
throws IllegalStateException {
- return getCookieSpec(id, null);
+ return getCookieSpec(name, null);
}
/**
- * Obtains the currently registered cookie policy names.
+ * Obtains a list containing names of all registered {@link CookieSpec cookie
+ * specs} in their default order.
*
* Note that the DEFAULT policy (if present) is likely to be the same
* as one of the other policies, but does not have to be.
*
- * @return array of registered cookie policy names
+ * @return list of registered cookie spec names
*/
- public static String[] getRegisteredCookieSpecs(){
- return (String[]) SPECS.keySet().toArray(new String [SPECS.size()]);
+ public synchronized List getSpecNames(){
+ return new ArrayList(registeredSpecs.keySet());
}
}
diff --git a/src/test/org/apache/http/conn/TestScheme.java b/src/test/org/apache/http/conn/TestScheme.java
index 84f0741a9..034da4909 100644
--- a/src/test/org/apache/http/conn/TestScheme.java
+++ b/src/test/org/apache/http/conn/TestScheme.java
@@ -31,7 +31,7 @@
package org.apache.http.conn;
-import java.util.Iterator;
+import java.util.List;
import junit.framework.Test;
import junit.framework.TestCase;
@@ -140,9 +140,9 @@ public class TestScheme extends TestCase {
public void testIterator() {
SchemeRegistry schmreg = new SchemeRegistry();
- Iterator iter = schmreg.getSchemeNames();
- assertNotNull(iter);
- assertFalse(iter.hasNext());
+ List names = schmreg.getSchemeNames();
+ assertNotNull(names);
+ assertTrue(names.isEmpty());
Scheme http = new Scheme
("http", PlainSocketFactory.getSocketFactory(), 80);
@@ -152,14 +152,13 @@ public class TestScheme extends TestCase {
schmreg.register(http);
schmreg.register(https);
- iter = schmreg.getSchemeNames();
- assertNotNull(iter);
- assertTrue(iter.hasNext());
+ names = schmreg.getSchemeNames();
+ assertNotNull(names);
+ assertFalse(names.isEmpty());
boolean flaghttp = false;
boolean flaghttps = false;
- String name = (String) iter.next();
- assertTrue(iter.hasNext());
+ String name = (String) names.get(0);
if ("http".equals(name))
flaghttp = true;
@@ -169,12 +168,10 @@ public class TestScheme extends TestCase {
fail("unexpected name in iterator: " + name);
assertNotNull(schmreg.get(name));
- iter.remove();
- assertTrue(iter.hasNext());
+ schmreg.unregister(name);
assertNull(schmreg.get(name));
- name = (String) iter.next();
- assertFalse(iter.hasNext());
+ name = (String) names.get(1);
if ("http".equals(name)) {
if (flaghttp) fail("name 'http' found twice");
diff --git a/src/test/org/apache/http/cookie/TestCookiePolicy.java b/src/test/org/apache/http/cookie/TestCookiePolicy.java
index d775a8cab..8a357f625 100644
--- a/src/test/org/apache/http/cookie/TestCookiePolicy.java
+++ b/src/test/org/apache/http/cookie/TestCookiePolicy.java
@@ -30,6 +30,8 @@
package org.apache.http.cookie;
+import java.util.List;
+
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
@@ -61,93 +63,101 @@ public class TestCookiePolicy extends TestCase {
}
public void testRegisterUnregisterCookieSpecFactory() {
- String[] specs = CookiePolicy.getRegisteredCookieSpecs();
- assertNotNull(specs);
- assertEquals(0, specs.length);
+ CookiePolicy registry = CookiePolicy.DEFAULT;
+ List names = registry.getSpecNames();
+ assertNotNull(names);
+ assertEquals(0, names.size());
- CookiePolicy.register(CookieSpecParams.BROWSER_COMPATIBILITY,
+ registry.register(CookieSpecParams.BROWSER_COMPATIBILITY,
new BrowserCompatSpecFactory());
- CookiePolicy.register(CookieSpecParams.NETSCAPE,
+ registry.register(CookieSpecParams.NETSCAPE,
new NetscapeDraftSpecFactory());
- CookiePolicy.register(CookieSpecParams.RFC_2109,
+ registry.register(CookieSpecParams.RFC_2109,
new RFC2109SpecFactory());
- CookiePolicy.register(CookieSpecParams.RFC_2109,
+ registry.register(CookieSpecParams.RFC_2109,
new RFC2109SpecFactory());
+ registry.register(CookieSpecParams.NETSCAPE,
+ new NetscapeDraftSpecFactory());
- specs = CookiePolicy.getRegisteredCookieSpecs();
- assertNotNull(specs);
- assertEquals(3, specs.length);
+ names = registry.getSpecNames();
+ assertNotNull(names);
+ assertEquals(3, names.size());
+ assertEquals(CookieSpecParams.BROWSER_COMPATIBILITY, (String) names.get(0));
+ assertEquals(CookieSpecParams.NETSCAPE, (String) names.get(1));
+ assertEquals(CookieSpecParams.RFC_2109, (String) names.get(2));
- CookiePolicy.unregister(CookieSpecParams.NETSCAPE);
- CookiePolicy.unregister(CookieSpecParams.NETSCAPE);
- CookiePolicy.unregister(CookieSpecParams.RFC_2109);
- CookiePolicy.unregister(CookieSpecParams.BROWSER_COMPATIBILITY);
- CookiePolicy.unregister("whatever");
+ registry.unregister(CookieSpecParams.NETSCAPE);
+ registry.unregister(CookieSpecParams.NETSCAPE);
+ registry.unregister(CookieSpecParams.RFC_2109);
+ registry.unregister(CookieSpecParams.BROWSER_COMPATIBILITY);
+ registry.unregister("whatever");
- specs = CookiePolicy.getRegisteredCookieSpecs();
- assertNotNull(specs);
- assertEquals(0, specs.length);
+ names = registry.getSpecNames();
+ assertNotNull(names);
+ assertEquals(0, names.size());
}
public void testGetNewCookieSpec() {
- CookiePolicy.register(CookieSpecParams.BROWSER_COMPATIBILITY,
+ CookiePolicy registry = CookiePolicy.DEFAULT;
+ registry.register(CookieSpecParams.BROWSER_COMPATIBILITY,
new BrowserCompatSpecFactory());
- CookiePolicy.register(CookieSpecParams.NETSCAPE,
+ registry.register(CookieSpecParams.NETSCAPE,
new NetscapeDraftSpecFactory());
- CookiePolicy.register(CookieSpecParams.RFC_2109,
+ registry.register(CookieSpecParams.RFC_2109,
new RFC2109SpecFactory());
- assertNotNull(CookiePolicy.getCookieSpec(CookieSpecParams.NETSCAPE));
- assertNotNull(CookiePolicy.getCookieSpec(CookieSpecParams.RFC_2109));
- assertNotNull(CookiePolicy.getCookieSpec(CookieSpecParams.BROWSER_COMPATIBILITY));
+ assertNotNull(registry.getCookieSpec(CookieSpecParams.NETSCAPE));
+ assertNotNull(registry.getCookieSpec(CookieSpecParams.RFC_2109));
+ assertNotNull(registry.getCookieSpec(CookieSpecParams.BROWSER_COMPATIBILITY));
try {
- CookiePolicy.getCookieSpec("whatever");
+ registry.getCookieSpec("whatever");
fail("IllegalStateException should have been thrown");
} catch (IllegalStateException ex) {
// expected
}
HttpParams params = new BasicHttpParams();
- assertNotNull(CookiePolicy.getCookieSpec(CookieSpecParams.NETSCAPE, params));
- assertNotNull(CookiePolicy.getCookieSpec(CookieSpecParams.RFC_2109, params));
- assertNotNull(CookiePolicy.getCookieSpec(CookieSpecParams.BROWSER_COMPATIBILITY, params));
+ assertNotNull(registry.getCookieSpec(CookieSpecParams.NETSCAPE, params));
+ assertNotNull(registry.getCookieSpec(CookieSpecParams.RFC_2109, params));
+ assertNotNull(registry.getCookieSpec(CookieSpecParams.BROWSER_COMPATIBILITY, params));
try {
- CookiePolicy.getCookieSpec("whatever", params);
+ registry.getCookieSpec("whatever", params);
fail("IllegalStateException should have been thrown");
} catch (IllegalStateException ex) {
// expected
}
CookieSpecParams.setCookiePolicy(params, CookieSpecParams.BROWSER_COMPATIBILITY);
- CookieSpec cookiespec = CookiePolicy.getCookieSpec(params);
+ CookieSpec cookiespec = registry.getCookieSpec(params);
assertTrue(cookiespec instanceof BrowserCompatSpec);
}
public void testInvalidInput() {
+ CookiePolicy registry = CookiePolicy.DEFAULT;
try {
- CookiePolicy.register(null, null);
+ registry.register(null, null);
fail("IllegalArgumentException should have been thrown");
} catch (IllegalArgumentException ex) {
// expected
}
try {
- CookiePolicy.register("whatever", null);
+ registry.register("whatever", null);
fail("IllegalArgumentException should have been thrown");
} catch (IllegalArgumentException ex) {
// expected
}
try {
- CookiePolicy.unregister(null);
+ registry.unregister(null);
fail("IllegalArgumentException should have been thrown");
} catch (IllegalArgumentException ex) {
// expected
}
try {
- CookiePolicy.getCookieSpec((String)null);
+ registry.getCookieSpec((String)null);
fail("IllegalArgumentException should have been thrown");
} catch (IllegalArgumentException ex) {
// expected
}
try {
- CookiePolicy.getCookieSpec((HttpParams)null);
+ registry.getCookieSpec((HttpParams)null);
fail("IllegalArgumentException should have been thrown");
} catch (IllegalArgumentException ex) {
// expected