Changelog:
* Made all methods in AuthPolicy and CookiePolicy non-static * API and internal implementation of AuthPolicy, CookiePolicy and SchemeRegistry made more consistent * Removed name iterator from the SchemeRegistry * Changed AuthPolicy to use auth scheme factories git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@527900 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
919eeb2933
commit
134b0bf0f9
|
@ -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.
|
||||
* <p>The following specifications are provided:
|
||||
* <ul>
|
||||
* <li><tt>Basic</tt>: Basic authentication scheme as defined in RFC2617
|
||||
* (considered inherently insecure, but most widely supported)
|
||||
* <li><tt>Digest</tt>: Digest authentication scheme as defined in RFC2617
|
||||
* <li><tt>NTLM</tt>: The NTLM scheme is a proprietary Microsoft Windows
|
||||
* Authentication protocol (considered to be the most secure among
|
||||
* currently supported authentication schemes)
|
||||
* </ul>
|
||||
*
|
||||
* @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
|
||||
* @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
|
||||
*
|
||||
* @version $Revision$
|
||||
* @since 3.0
|
||||
*/
|
||||
public abstract class AuthPolicy {
|
||||
public final class AuthPolicy {
|
||||
|
||||
private static final HashMap SCHEMES = new HashMap();
|
||||
private static final ArrayList SCHEME_LIST = new ArrayList();
|
||||
public final static AuthPolicy DEFAULT = new AuthPolicy();
|
||||
|
||||
private final Map registeredSchemes = new LinkedHashMap();
|
||||
|
||||
/**
|
||||
* The key used to look up the list of IDs of supported {@link AuthScheme
|
||||
* authentication schemes} in their order of preference. The scheme IDs are
|
||||
* stored in a {@link java.util.Collection} as {@link java.lang.String}s.
|
||||
*
|
||||
* <p>
|
||||
* If several schemes are returned in the <tt>WWW-Authenticate</tt>
|
||||
* or <tt>Proxy-Authenticate</tt> 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.
|
||||
* </p>
|
||||
*
|
||||
* @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)}.
|
||||
*
|
||||
* <p>
|
||||
* Please note that custom authentication preferences, if used, need to be updated accordingly
|
||||
* for the new {@link AuthScheme authentication scheme} to take effect.
|
||||
* </p>
|
||||
*
|
||||
* @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());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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
|
||||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.apache.http.auth;
|
||||
|
||||
import org.apache.http.params.HttpParams;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
|
||||
*
|
||||
* @since 4.0
|
||||
*/
|
||||
public interface AuthSchemeFactory {
|
||||
|
||||
AuthScheme newInstance(HttpParams params);
|
||||
|
||||
}
|
|
@ -30,16 +30,14 @@
|
|||
*/
|
||||
package org.apache.http.conn;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.apache.http.HttpHost;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* A set of supported protocol {@link Scheme schemes}.
|
||||
* Schemes are identified by lowercase names.
|
||||
|
@ -73,7 +71,8 @@ public final class SchemeRegistry {
|
|||
* Creates a new, empty scheme registry.
|
||||
*/
|
||||
public SchemeRegistry() {
|
||||
registeredSchemes = Collections.synchronizedMap(new HashMap());
|
||||
super();
|
||||
registeredSchemes = new LinkedHashMap();
|
||||
}
|
||||
|
||||
|
||||
|
@ -87,7 +86,7 @@ public final class SchemeRegistry {
|
|||
* @throws IllegalStateException
|
||||
* if the scheme with the given name is not registered
|
||||
*/
|
||||
public final Scheme getScheme(String name) {
|
||||
public synchronized final Scheme getScheme(String name) {
|
||||
Scheme found = get(name);
|
||||
if (found == null) {
|
||||
throw new IllegalStateException
|
||||
|
@ -108,7 +107,7 @@ public final class SchemeRegistry {
|
|||
* @throws IllegalStateException
|
||||
* if a scheme with the respective name is not registered
|
||||
*/
|
||||
public final Scheme getScheme(HttpHost host) {
|
||||
public synchronized final Scheme getScheme(HttpHost host) {
|
||||
if (host == null) {
|
||||
throw new IllegalArgumentException("Host must not be null.");
|
||||
}
|
||||
|
@ -124,7 +123,7 @@ public final class SchemeRegistry {
|
|||
* @return the scheme, or
|
||||
* <code>null</code> 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
|
||||
* <code>null</code> 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
|
||||
* <code>null</code> 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());
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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");
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue