mirror of
https://github.com/apache/httpcomponents-client.git
synced 2025-02-17 07:26:47 +00:00
new class SchemeSet, testcases included in TestScheme
git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@500800 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
787b02f73a
commit
026725e2ca
@ -49,6 +49,7 @@
|
|||||||
import org.apache.http.protocol.RequestUserAgent;
|
import org.apache.http.protocol.RequestUserAgent;
|
||||||
|
|
||||||
import org.apache.http.conn.Scheme;
|
import org.apache.http.conn.Scheme;
|
||||||
|
import org.apache.http.conn.SchemeSet;
|
||||||
import org.apache.http.conn.SocketFactory;
|
import org.apache.http.conn.SocketFactory;
|
||||||
import org.apache.http.conn.PlainSocketFactory;
|
import org.apache.http.conn.PlainSocketFactory;
|
||||||
import org.apache.http.conn.ClientConnectionManager;
|
import org.apache.http.conn.ClientConnectionManager;
|
||||||
@ -146,7 +147,7 @@ private final static void setup() {
|
|||||||
// Register the "http" protocol scheme, it is required
|
// Register the "http" protocol scheme, it is required
|
||||||
// by the default operator to look up socket factories.
|
// by the default operator to look up socket factories.
|
||||||
SocketFactory sf = PlainSocketFactory.getSocketFactory();
|
SocketFactory sf = PlainSocketFactory.getSocketFactory();
|
||||||
Scheme.registerScheme("http", new Scheme("http", sf, 80));
|
SchemeSet.DEFAULT.register(new Scheme("http", sf, 80));
|
||||||
|
|
||||||
// Prepare parameters.
|
// Prepare parameters.
|
||||||
// Since this example doesn't use the full core framework,
|
// Since this example doesn't use the full core framework,
|
||||||
|
@ -45,12 +45,15 @@
|
|||||||
import org.apache.http.protocol.HttpExecutionContext;
|
import org.apache.http.protocol.HttpExecutionContext;
|
||||||
|
|
||||||
import org.apache.http.conn.Scheme;
|
import org.apache.http.conn.Scheme;
|
||||||
|
import org.apache.http.conn.SchemeSet;
|
||||||
import org.apache.http.conn.SocketFactory;
|
import org.apache.http.conn.SocketFactory;
|
||||||
import org.apache.http.conn.PlainSocketFactory;
|
import org.apache.http.conn.PlainSocketFactory;
|
||||||
import org.apache.http.conn.HostConfiguration;
|
import org.apache.http.conn.HostConfiguration;
|
||||||
import org.apache.http.conn.ManagedClientConnection;
|
import org.apache.http.conn.ManagedClientConnection;
|
||||||
import org.apache.http.conn.ClientConnectionManager;
|
import org.apache.http.conn.ClientConnectionManager;
|
||||||
|
import org.apache.http.conn.ClientConnectionOperator;
|
||||||
import org.apache.http.conn.impl.ThreadSafeClientConnManager;
|
import org.apache.http.conn.impl.ThreadSafeClientConnManager;
|
||||||
|
import org.apache.http.conn.impl.DefaultClientConnectionOperator;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -77,6 +80,12 @@ public class ManagerConnectDirect {
|
|||||||
*/
|
*/
|
||||||
private static HttpParams defaultParameters = null;
|
private static HttpParams defaultParameters = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The scheme set.
|
||||||
|
* Instantiated in {@link #setup setup}.
|
||||||
|
*/
|
||||||
|
private static SchemeSet supportedSchemes;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main entry point to this example.
|
* Main entry point to this example.
|
||||||
@ -143,7 +152,13 @@ public final static void main(String[] args)
|
|||||||
|
|
||||||
|
|
||||||
private final static ClientConnectionManager createManager() {
|
private final static ClientConnectionManager createManager() {
|
||||||
return new ThreadSafeClientConnManager(getParams());
|
|
||||||
|
return new ThreadSafeClientConnManager(getParams()) {
|
||||||
|
//@@@ we need a better way to pass in the SchemeSet or operator
|
||||||
|
protected ClientConnectionOperator createConnectionOperator() {
|
||||||
|
return new DefaultClientConnectionOperator(supportedSchemes);
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -155,8 +170,9 @@ private final static void setup() {
|
|||||||
|
|
||||||
// Register the "http" protocol scheme, it is required
|
// Register the "http" protocol scheme, it is required
|
||||||
// by the default operator to look up socket factories.
|
// by the default operator to look up socket factories.
|
||||||
|
supportedSchemes = new SchemeSet();
|
||||||
SocketFactory sf = PlainSocketFactory.getSocketFactory();
|
SocketFactory sf = PlainSocketFactory.getSocketFactory();
|
||||||
Scheme.registerScheme("http", new Scheme("http", sf, 80));
|
supportedSchemes.register(new Scheme("http", sf, 80));
|
||||||
|
|
||||||
// Prepare parameters.
|
// Prepare parameters.
|
||||||
// Since this example doesn't use the full core framework,
|
// Since this example doesn't use the full core framework,
|
||||||
|
@ -45,13 +45,16 @@
|
|||||||
import org.apache.http.protocol.HttpExecutionContext;
|
import org.apache.http.protocol.HttpExecutionContext;
|
||||||
|
|
||||||
import org.apache.http.conn.Scheme;
|
import org.apache.http.conn.Scheme;
|
||||||
|
import org.apache.http.conn.SchemeSet;
|
||||||
import org.apache.http.conn.SocketFactory;
|
import org.apache.http.conn.SocketFactory;
|
||||||
import org.apache.http.conn.PlainSocketFactory;
|
import org.apache.http.conn.PlainSocketFactory;
|
||||||
import org.apache.http.conn.ssl.SSLSocketFactory;
|
import org.apache.http.conn.ssl.SSLSocketFactory;
|
||||||
import org.apache.http.conn.HostConfiguration;
|
import org.apache.http.conn.HostConfiguration;
|
||||||
import org.apache.http.conn.ManagedClientConnection;
|
import org.apache.http.conn.ManagedClientConnection;
|
||||||
import org.apache.http.conn.ClientConnectionManager;
|
import org.apache.http.conn.ClientConnectionManager;
|
||||||
|
import org.apache.http.conn.ClientConnectionOperator;
|
||||||
import org.apache.http.conn.impl.ThreadSafeClientConnManager;
|
import org.apache.http.conn.impl.ThreadSafeClientConnManager;
|
||||||
|
import org.apache.http.conn.impl.DefaultClientConnectionOperator;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -78,6 +81,12 @@ public class ManagerConnectProxy {
|
|||||||
*/
|
*/
|
||||||
private static HttpParams defaultParameters = null;
|
private static HttpParams defaultParameters = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The scheme set.
|
||||||
|
* Instantiated in {@link #setup setup}.
|
||||||
|
*/
|
||||||
|
private static SchemeSet supportedSchemes;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main entry point to this example.
|
* Main entry point to this example.
|
||||||
@ -170,7 +179,13 @@ public final static void main(String[] args)
|
|||||||
|
|
||||||
|
|
||||||
private final static ClientConnectionManager createManager() {
|
private final static ClientConnectionManager createManager() {
|
||||||
return new ThreadSafeClientConnManager(getParams());
|
|
||||||
|
return new ThreadSafeClientConnManager(getParams()) {
|
||||||
|
//@@@ we need a better way to pass in the SchemeSet or operator
|
||||||
|
protected ClientConnectionOperator createConnectionOperator() {
|
||||||
|
return new DefaultClientConnectionOperator(supportedSchemes);
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -182,10 +197,11 @@ private final static void setup() {
|
|||||||
|
|
||||||
// Register the "http" and "https" protocol schemes, they are
|
// Register the "http" and "https" protocol schemes, they are
|
||||||
// required by the default operator to look up socket factories.
|
// required by the default operator to look up socket factories.
|
||||||
|
supportedSchemes = new SchemeSet();
|
||||||
SocketFactory sf = PlainSocketFactory.getSocketFactory();
|
SocketFactory sf = PlainSocketFactory.getSocketFactory();
|
||||||
Scheme.registerScheme("http", new Scheme("http", sf, 80));
|
supportedSchemes.register(new Scheme("http", sf, 80));
|
||||||
sf = SSLSocketFactory.getSocketFactory();
|
sf = SSLSocketFactory.getSocketFactory();
|
||||||
Scheme.registerScheme("https", new Scheme("https", sf, 80));
|
supportedSchemes.register(new Scheme("https", sf, 80));
|
||||||
|
|
||||||
// Prepare parameters.
|
// Prepare parameters.
|
||||||
// Since this example doesn't use the full core framework,
|
// Since this example doesn't use the full core framework,
|
||||||
|
@ -45,6 +45,7 @@
|
|||||||
import org.apache.http.protocol.HttpExecutionContext;
|
import org.apache.http.protocol.HttpExecutionContext;
|
||||||
|
|
||||||
import org.apache.http.conn.Scheme;
|
import org.apache.http.conn.Scheme;
|
||||||
|
import org.apache.http.conn.SchemeSet;
|
||||||
import org.apache.http.conn.SocketFactory;
|
import org.apache.http.conn.SocketFactory;
|
||||||
import org.apache.http.conn.PlainSocketFactory;
|
import org.apache.http.conn.PlainSocketFactory;
|
||||||
import org.apache.http.conn.OperatedClientConnection;
|
import org.apache.http.conn.OperatedClientConnection;
|
||||||
@ -75,7 +76,13 @@ public class OperatorConnectDirect {
|
|||||||
* The default parameters.
|
* The default parameters.
|
||||||
* Instantiated in {@link #setup setup}.
|
* Instantiated in {@link #setup setup}.
|
||||||
*/
|
*/
|
||||||
private static HttpParams defaultParameters = null;
|
private static HttpParams defaultParameters;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The scheme set.
|
||||||
|
* Instantiated in {@link #setup setup}.
|
||||||
|
*/
|
||||||
|
private static SchemeSet supportedSchemes;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -123,7 +130,7 @@ public final static void main(String[] args)
|
|||||||
|
|
||||||
|
|
||||||
private final static ClientConnectionOperator createOperator() {
|
private final static ClientConnectionOperator createOperator() {
|
||||||
return new DefaultClientConnectionOperator();
|
return new DefaultClientConnectionOperator(supportedSchemes);
|
||||||
}
|
}
|
||||||
|
|
||||||
private final static OperatedClientConnection createConnection() {
|
private final static OperatedClientConnection createConnection() {
|
||||||
@ -139,8 +146,9 @@ private final static void setup() {
|
|||||||
|
|
||||||
// Register the "http" protocol scheme, it is required
|
// Register the "http" protocol scheme, it is required
|
||||||
// by the default operator to look up socket factories.
|
// by the default operator to look up socket factories.
|
||||||
|
supportedSchemes = new SchemeSet();
|
||||||
SocketFactory sf = PlainSocketFactory.getSocketFactory();
|
SocketFactory sf = PlainSocketFactory.getSocketFactory();
|
||||||
Scheme.registerScheme("http", new Scheme("http", sf, 80));
|
supportedSchemes.register(new Scheme("http", sf, 80));
|
||||||
|
|
||||||
// Prepare parameters.
|
// Prepare parameters.
|
||||||
// Since this example doesn't use the full core framework,
|
// Since this example doesn't use the full core framework,
|
||||||
|
@ -45,6 +45,7 @@
|
|||||||
import org.apache.http.protocol.HttpExecutionContext;
|
import org.apache.http.protocol.HttpExecutionContext;
|
||||||
|
|
||||||
import org.apache.http.conn.Scheme;
|
import org.apache.http.conn.Scheme;
|
||||||
|
import org.apache.http.conn.SchemeSet;
|
||||||
import org.apache.http.conn.SocketFactory;
|
import org.apache.http.conn.SocketFactory;
|
||||||
import org.apache.http.conn.PlainSocketFactory;
|
import org.apache.http.conn.PlainSocketFactory;
|
||||||
import org.apache.http.conn.ssl.SSLSocketFactory;
|
import org.apache.http.conn.ssl.SSLSocketFactory;
|
||||||
@ -78,6 +79,12 @@ public class OperatorConnectProxy {
|
|||||||
*/
|
*/
|
||||||
private static HttpParams defaultParameters = null;
|
private static HttpParams defaultParameters = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The scheme set.
|
||||||
|
* Instantiated in {@link #setup setup}.
|
||||||
|
*/
|
||||||
|
private static SchemeSet supportedSchemes;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main entry point to this example.
|
* Main entry point to this example.
|
||||||
@ -155,7 +162,7 @@ public final static void main(String[] args)
|
|||||||
|
|
||||||
|
|
||||||
private final static ClientConnectionOperator createOperator() {
|
private final static ClientConnectionOperator createOperator() {
|
||||||
return new DefaultClientConnectionOperator();
|
return new DefaultClientConnectionOperator(supportedSchemes);
|
||||||
}
|
}
|
||||||
|
|
||||||
private final static OperatedClientConnection createConnection() {
|
private final static OperatedClientConnection createConnection() {
|
||||||
@ -171,10 +178,11 @@ private final static void setup() {
|
|||||||
|
|
||||||
// Register the "http" and "https" protocol schemes, they are
|
// Register the "http" and "https" protocol schemes, they are
|
||||||
// required by the default operator to look up socket factories.
|
// required by the default operator to look up socket factories.
|
||||||
|
supportedSchemes = new SchemeSet();
|
||||||
SocketFactory sf = PlainSocketFactory.getSocketFactory();
|
SocketFactory sf = PlainSocketFactory.getSocketFactory();
|
||||||
Scheme.registerScheme("http", new Scheme("http", sf, 80));
|
supportedSchemes.register(new Scheme("http", sf, 80));
|
||||||
sf = SSLSocketFactory.getSocketFactory();
|
sf = SSLSocketFactory.getSocketFactory();
|
||||||
Scheme.registerScheme("https", new Scheme("https", sf, 80));
|
supportedSchemes.register(new Scheme("https", sf, 80));
|
||||||
|
|
||||||
// Prepare parameters.
|
// Prepare parameters.
|
||||||
// Since this example doesn't use the full core framework,
|
// Since this example doesn't use the full core framework,
|
||||||
|
@ -38,212 +38,185 @@
|
|||||||
import org.apache.http.util.LangUtils;
|
import org.apache.http.util.LangUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A class to encapsulate the specifics of a protocol scheme. This class also
|
* Encapsulates specifics of a protocol scheme such as "http" or "https".
|
||||||
* provides the ability to customize the set and characteristics of the
|
* Schemes are identified by lowercase names.
|
||||||
* schemes used.
|
* Supported schemes are typically collected in a {@link SchemeSet SchemeSet}.
|
||||||
*
|
*
|
||||||
* <p>One use case for modifying the default set of protocols would be to set a
|
* <p>
|
||||||
* custom SSL socket factory. This would look something like the following:
|
* For example, to configure support for "https://" URLs,
|
||||||
* <pre>
|
* you could write code like the following:
|
||||||
* Scheme myHTTPS = new Scheme( "https", new MySSLSocketFactory(), 443 );
|
* </p>
|
||||||
*
|
* <pre>
|
||||||
* Scheme.registerScheme( "https", myHTTPS );
|
* Scheme https = new Scheme("https", new MySecureSocketFactory(), 443);
|
||||||
|
* SchemeSet.DEFAULT.register(https);
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
|
* @author <a href="mailto:rolandw at apache.org">Roland Weber</a>
|
||||||
* @author Michael Becke
|
* @author Michael Becke
|
||||||
* @author Jeff Dever
|
* @author Jeff Dever
|
||||||
* @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
|
* @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
|
||||||
*
|
|
||||||
* @since 2.0
|
|
||||||
*/
|
*/
|
||||||
public class Scheme {
|
public final class Scheme {
|
||||||
|
|
||||||
/** The available schemes */
|
/** The name of this scheme, in lowercase. (e.g. http, https) */
|
||||||
private static final Map SCHEMES = Collections.synchronizedMap(new HashMap());
|
private final String name;
|
||||||
|
|
||||||
/**
|
|
||||||
* Registers a new scheme with the given identifier. If a scheme with
|
|
||||||
* the given ID already exists it will be overridden. This ID is the same
|
|
||||||
* one used to retrieve the scheme from getScheme(String).
|
|
||||||
*
|
|
||||||
* @param id the identifier for this scheme
|
|
||||||
* @param scheme the scheme to register
|
|
||||||
*
|
|
||||||
* @see #getScheme(String)
|
|
||||||
*/
|
|
||||||
public static void registerScheme(final String id, final Scheme scheme) {
|
|
||||||
if (id == null) {
|
|
||||||
throw new IllegalArgumentException("Id may not be null");
|
|
||||||
}
|
|
||||||
if (scheme == null) {
|
|
||||||
throw new IllegalArgumentException("Scheme may not be null");
|
|
||||||
}
|
|
||||||
SCHEMES.put(id, scheme);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Unregisters the scheme with the given ID.
|
|
||||||
*
|
|
||||||
* @param id the ID of the scheme to remove
|
|
||||||
*/
|
|
||||||
public static void unregisterScheme(final String id) {
|
|
||||||
if (id == null) {
|
|
||||||
throw new IllegalArgumentException("Id may not be null");
|
|
||||||
}
|
|
||||||
SCHEMES.remove(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the scheme with the given ID.
|
|
||||||
*
|
|
||||||
* @param id the scheme ID
|
|
||||||
*
|
|
||||||
* @return Scheme a scheme
|
|
||||||
*
|
|
||||||
* @throws IllegalStateException if a scheme with the ID cannot be found
|
|
||||||
*/
|
|
||||||
public static Scheme getScheme(String id)
|
|
||||||
throws IllegalStateException {
|
|
||||||
|
|
||||||
if (id == null) {
|
|
||||||
throw new IllegalArgumentException("id is null");
|
|
||||||
}
|
|
||||||
Scheme scheme = (Scheme) SCHEMES.get(id);
|
|
||||||
if (scheme == null) {
|
|
||||||
throw new IllegalStateException("Unsupported scheme: '" + id + "'");
|
|
||||||
}
|
|
||||||
return scheme;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** the scheme of this scheme (e.g. http, https) */
|
|
||||||
private String name;
|
|
||||||
|
|
||||||
/** The socket factory for this scheme */
|
/** The socket factory for this scheme */
|
||||||
private SocketFactory socketFactory;
|
private final SocketFactory socketFactory;
|
||||||
|
|
||||||
/** The default port for this scheme */
|
/** The default port for this scheme */
|
||||||
private int defaultPort;
|
private final int defaultPort;
|
||||||
|
|
||||||
/** True if this scheme allows for layered connections */
|
/** Indicates whether this scheme allows for layered connections */
|
||||||
private boolean layered;
|
private final boolean layered;
|
||||||
|
|
||||||
|
|
||||||
|
/** A string representation, for {@link #toString toString}. */
|
||||||
|
private String stringRep;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a new scheme.
|
* Creates a new scheme.
|
||||||
* Whether the created scheme allows for layered connections
|
* Whether the created scheme allows for layered connections
|
||||||
* depends on the class of <code>factory</code>.
|
* depends on the class of <code>factory</code>.
|
||||||
*
|
*
|
||||||
* @param name the scheme name (e.g. http, https)
|
* @param name the scheme name, for example "http".
|
||||||
* @param factory the factory for creating sockets for communication using
|
* The name will be converted to lowercase.
|
||||||
* this scheme
|
* @param factory the factory for creating sockets for communication
|
||||||
* @param defaultPort the port this scheme defaults to
|
* with this scheme
|
||||||
|
* @param port the default port for this scheme
|
||||||
*/
|
*/
|
||||||
public Scheme(final String name, final SocketFactory factory, int defaultPort) {
|
public Scheme(final String name,
|
||||||
|
final SocketFactory factory,
|
||||||
|
final int port) {
|
||||||
|
|
||||||
if (name == null) {
|
if (name == null) {
|
||||||
throw new IllegalArgumentException("Scheme name may not be null");
|
throw new IllegalArgumentException
|
||||||
|
("Scheme name may not be null");
|
||||||
}
|
}
|
||||||
if (factory == null) {
|
if (factory == null) {
|
||||||
throw new IllegalArgumentException("Socket factory may not be null");
|
throw new IllegalArgumentException
|
||||||
|
("Socket factory may not be null");
|
||||||
}
|
}
|
||||||
if (defaultPort <= 0) {
|
if ((port <= 0) || (port > 0xffff)) {
|
||||||
throw new IllegalArgumentException("Port is invalid: " + defaultPort);
|
throw new IllegalArgumentException
|
||||||
|
("Port is invalid: " + port);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.name = name;
|
this.name = name.toLowerCase();
|
||||||
this.socketFactory = factory;
|
this.socketFactory = factory;
|
||||||
this.defaultPort = defaultPort;
|
this.defaultPort = port;
|
||||||
this.layered = (factory instanceof SecureSocketFactory);
|
this.layered = (factory instanceof SecureSocketFactory);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the defaultPort.
|
* Obtains the default port.
|
||||||
* @return int
|
*
|
||||||
|
* @return the default port for this scheme
|
||||||
*/
|
*/
|
||||||
public int getDefaultPort() {
|
public final int getDefaultPort() {
|
||||||
return defaultPort;
|
return defaultPort;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the socketFactory. If secure the factory is a SecureSocketFactory.
|
* Obtains the socket factory.
|
||||||
* @return SocketFactory
|
* If this scheme is {@link #isLayered layered}, the factory implements
|
||||||
|
* {@link SecureSocketFactory SecureSocketFactory}.
|
||||||
|
*
|
||||||
|
* @return the socket factory for this scheme
|
||||||
*/
|
*/
|
||||||
public SocketFactory getSocketFactory() {
|
public final SocketFactory getSocketFactory() {
|
||||||
return socketFactory;
|
return socketFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the scheme.
|
* Obtains the scheme name.
|
||||||
* @return The scheme
|
*
|
||||||
|
* @return the name of this scheme, in lowercase
|
||||||
*/
|
*/
|
||||||
public String getName() {
|
public final String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Indicates whether this scheme allows for layered connections.
|
* Indicates whether this scheme allows for layered connections.
|
||||||
|
*
|
||||||
* @return <code>true</code> if layered connections are possible,
|
* @return <code>true</code> if layered connections are possible,
|
||||||
* <code>false</code> otherwise
|
* <code>false</code> otherwise
|
||||||
*/
|
*/
|
||||||
public boolean isLayered() {
|
public final boolean isLayered() {
|
||||||
return layered;
|
return layered;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resolves the correct port for this scheme. Returns the given port if
|
* Resolves the correct port for this scheme.
|
||||||
* valid or the default port otherwise.
|
* Returns the given port if it is valid, the default port otherwise.
|
||||||
*
|
*
|
||||||
* @param port the port to be resolved
|
* @param port the port to be resolved,
|
||||||
|
* a negative number to obtain the default port
|
||||||
*
|
*
|
||||||
* @return the given port or the defaultPort
|
* @return the given port or the defaultPort
|
||||||
*/
|
*/
|
||||||
public int resolvePort(int port) {
|
public final int resolvePort(int port) {
|
||||||
return port <= 0 ? getDefaultPort() : port;
|
return ((port <= 0) || (port > 0xffff)) ? defaultPort : port;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a string representation of this object.
|
* Return a string representation of this object.
|
||||||
* @return a string representation of this object.
|
*
|
||||||
|
* @return a human-readable string description of this scheme
|
||||||
*/
|
*/
|
||||||
public String toString() {
|
public final String toString() {
|
||||||
CharArrayBuffer buffer = new CharArrayBuffer(32);
|
if (stringRep == null) {
|
||||||
buffer.append(this.name);
|
CharArrayBuffer buffer = new CharArrayBuffer(32);
|
||||||
buffer.append(':');
|
buffer.append(this.name);
|
||||||
buffer.append(Integer.toString(this.defaultPort));
|
buffer.append(':');
|
||||||
return buffer.toString();
|
buffer.append(Integer.toString(this.defaultPort));
|
||||||
}
|
stringRep = buffer.toString();
|
||||||
|
|
||||||
/**
|
|
||||||
* Return true if the specified object equals this object.
|
|
||||||
* @param obj The object to compare against.
|
|
||||||
* @return true if the objects are equal.
|
|
||||||
*/
|
|
||||||
public boolean equals(Object obj) {
|
|
||||||
if (obj == null) return false;
|
|
||||||
if (this == obj) return true;
|
|
||||||
if (obj instanceof Scheme) {
|
|
||||||
Scheme p = (Scheme) obj;
|
|
||||||
return (
|
|
||||||
defaultPort == p.getDefaultPort()
|
|
||||||
&& name.equalsIgnoreCase(p.getName())
|
|
||||||
&& layered == p.isLayered()
|
|
||||||
&& socketFactory.equals(p.getSocketFactory()));
|
|
||||||
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
return stringRep;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a hash code for this object
|
* Compares this scheme to an object.
|
||||||
* @return The hash code.
|
*
|
||||||
|
* @param obj the object to compare with
|
||||||
|
*
|
||||||
|
* @return <code>true</code> iff the argument is equal to this scheme
|
||||||
|
*/
|
||||||
|
public final boolean equals(Object obj) {
|
||||||
|
if (obj == null) return false;
|
||||||
|
if (this == obj) return true;
|
||||||
|
if (!(obj instanceof Scheme)) return false;
|
||||||
|
|
||||||
|
Scheme s = (Scheme) obj;
|
||||||
|
return (name.equals(s.name) &&
|
||||||
|
defaultPort == s.defaultPort &&
|
||||||
|
layered == s.layered &&
|
||||||
|
socketFactory.equals(s.socketFactory)
|
||||||
|
);
|
||||||
|
} // equals
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Obtains a hash code for this scheme.
|
||||||
|
*
|
||||||
|
* @return the hash code
|
||||||
*/
|
*/
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
int hash = LangUtils.HASH_SEED;
|
int hash = LangUtils.HASH_SEED;
|
||||||
hash = LangUtils.hashCode(hash, this.defaultPort);
|
hash = LangUtils.hashCode(hash, this.defaultPort);
|
||||||
hash = LangUtils.hashCode(hash, this.name.toLowerCase());
|
hash = LangUtils.hashCode(hash, this.name);
|
||||||
hash = LangUtils.hashCode(hash, this.layered);
|
hash = LangUtils.hashCode(hash, this.layered);
|
||||||
hash = LangUtils.hashCode(hash, this.socketFactory);
|
hash = LangUtils.hashCode(hash, this.socketFactory);
|
||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
} // class Scheme
|
||||||
|
167
src/java/org/apache/http/conn/SchemeSet.java
Normal file
167
src/java/org/apache/http/conn/SchemeSet.java
Normal file
@ -0,0 +1,167 @@
|
|||||||
|
/*
|
||||||
|
* $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.conn;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A set of supported protocol {@link Scheme schemes}.
|
||||||
|
* Schemes are identified by lowercase names.
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:rolandw at apache.org">Roland Weber</a>
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* <!-- empty lines to avoid svn diff problems -->
|
||||||
|
* @version $Revision$ $Date$
|
||||||
|
*
|
||||||
|
* @since 4.0
|
||||||
|
*/
|
||||||
|
public final class SchemeSet {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A default scheme set.
|
||||||
|
* It is empty by default, but can be initialized by any application.
|
||||||
|
* The default scheme set should only be used by applications that
|
||||||
|
* know for sure that this class is NOT shared with any other applications.
|
||||||
|
* For example a Servlet, Portlet or EJB should not rely on being the
|
||||||
|
* only user of this class.
|
||||||
|
*/
|
||||||
|
public final static SchemeSet DEFAULT = new SchemeSet();
|
||||||
|
|
||||||
|
|
||||||
|
/** The available schemes in this set. */
|
||||||
|
private final Map registeredSchemes;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new, empty scheme set.
|
||||||
|
*/
|
||||||
|
public SchemeSet() {
|
||||||
|
registeredSchemes = Collections.synchronizedMap(new HashMap());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Obtains a scheme by name.
|
||||||
|
*
|
||||||
|
* @param name the name of the scheme to look up (in lowercase)
|
||||||
|
*
|
||||||
|
* @return the scheme, never <code>null</code>
|
||||||
|
*
|
||||||
|
* @throws IllegalStateException
|
||||||
|
* if the scheme with the given name is not registered
|
||||||
|
*/
|
||||||
|
public final Scheme getScheme(String name) {
|
||||||
|
Scheme found = get(name);
|
||||||
|
if (found == null) {
|
||||||
|
throw new IllegalStateException
|
||||||
|
("Scheme '"+name+"' not registered.");
|
||||||
|
}
|
||||||
|
return found;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Obtains a scheme by name, if registered.
|
||||||
|
*
|
||||||
|
* @param name the name of the scheme to look up (in lowercase)
|
||||||
|
*
|
||||||
|
* @return the scheme, or
|
||||||
|
* <code>null</code> if there is none by this name
|
||||||
|
*/
|
||||||
|
public final Scheme get(String name) {
|
||||||
|
if (name == null)
|
||||||
|
throw new IllegalArgumentException("Name must not be null.");
|
||||||
|
|
||||||
|
// leave it to the caller to use the correct name - all lowercase
|
||||||
|
//name = name.toLowerCase();
|
||||||
|
Scheme found = (Scheme) registeredSchemes.get(name);
|
||||||
|
return found;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers a scheme in this set.
|
||||||
|
* The scheme can later be retrieved by it's name
|
||||||
|
* using {@link #getScheme getScheme} or {@link #get get}.
|
||||||
|
*
|
||||||
|
* @param sch the scheme to register
|
||||||
|
*
|
||||||
|
* @return the scheme previously registered with that name, or
|
||||||
|
* <code>null</code> if none was registered
|
||||||
|
*/
|
||||||
|
public final Scheme register(Scheme sch) {
|
||||||
|
if (sch == null)
|
||||||
|
throw new IllegalArgumentException("Scheme must not be null.");
|
||||||
|
|
||||||
|
Scheme old = (Scheme) registeredSchemes.put(sch.getName(), sch);
|
||||||
|
return old;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unregisters a scheme from this set.
|
||||||
|
*
|
||||||
|
* @param name the name of the scheme to unregister (in lowercase)
|
||||||
|
*
|
||||||
|
* @return the unregistered scheme, or
|
||||||
|
* <code>null</code> if there was none
|
||||||
|
*/
|
||||||
|
public final Scheme unregister(String name) {
|
||||||
|
if (name == null)
|
||||||
|
throw new IllegalArgumentException("Name must not be null.");
|
||||||
|
|
||||||
|
// leave it to the caller to use the correct name - all lowercase
|
||||||
|
//name = name.toLowerCase();
|
||||||
|
Scheme gone = (Scheme) registeredSchemes.remove(name);
|
||||||
|
return gone;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Obtains the names of the registered schemes.
|
||||||
|
*
|
||||||
|
* @return iterator over the registered scheme names.
|
||||||
|
* The iterator supports {@link Iterator#remove remove()}.
|
||||||
|
*/
|
||||||
|
public final Iterator getSchemeNames() {
|
||||||
|
return registeredSchemes.keySet().iterator();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
} // class SchemeSet
|
||||||
|
|
@ -41,6 +41,7 @@
|
|||||||
import org.apache.http.protocol.HttpContext;
|
import org.apache.http.protocol.HttpContext;
|
||||||
|
|
||||||
import org.apache.http.conn.Scheme;
|
import org.apache.http.conn.Scheme;
|
||||||
|
import org.apache.http.conn.SchemeSet;
|
||||||
import org.apache.http.conn.SocketFactory;
|
import org.apache.http.conn.SocketFactory;
|
||||||
import org.apache.http.conn.SecureSocketFactory;
|
import org.apache.http.conn.SecureSocketFactory;
|
||||||
import org.apache.http.conn.OperatedClientConnection;
|
import org.apache.http.conn.OperatedClientConnection;
|
||||||
@ -50,7 +51,7 @@
|
|||||||
/**
|
/**
|
||||||
* Default implementation of a
|
* Default implementation of a
|
||||||
* {@link ClientConnectionOperator ClientConnectionOperator}.
|
* {@link ClientConnectionOperator ClientConnectionOperator}.
|
||||||
* It uses the {@link Scheme Scheme} class to look up
|
* It uses a {@link SchemeSet SchemeSet} to look up
|
||||||
* {@link SocketFactory SocketFactory} objects.
|
* {@link SocketFactory SocketFactory} objects.
|
||||||
*
|
*
|
||||||
* @author <a href="mailto:rolandw at apache.org">Roland Weber</a>
|
* @author <a href="mailto:rolandw at apache.org">Roland Weber</a>
|
||||||
@ -65,8 +66,28 @@ public class DefaultClientConnectionOperator
|
|||||||
implements ClientConnectionOperator {
|
implements ClientConnectionOperator {
|
||||||
|
|
||||||
|
|
||||||
|
/** The scheme set for looking up socket factories. */
|
||||||
|
protected SchemeSet schemeSet;
|
||||||
|
|
||||||
// public default constructor
|
|
||||||
|
/**
|
||||||
|
* Creates a new client connection operator.
|
||||||
|
* Uses {@link SchemeSet#DEFAULT SchemeSet.DEFAULT}
|
||||||
|
* as the scheme set.
|
||||||
|
*/
|
||||||
|
public DefaultClientConnectionOperator() {
|
||||||
|
this(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new client connection operator for the given scheme set.
|
||||||
|
*
|
||||||
|
* @param schemes the scheme set, or <code>null</code> to use
|
||||||
|
* {@link SchemeSet#DEFAULT SchemeSet.DEFAULT}
|
||||||
|
*/
|
||||||
|
public DefaultClientConnectionOperator(SchemeSet schemes) {
|
||||||
|
schemeSet = (schemes != null) ? schemes : SchemeSet.DEFAULT;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -96,9 +117,9 @@ public void openConnection(OperatedClientConnection conn,
|
|||||||
}
|
}
|
||||||
|
|
||||||
InetAddress local = null;
|
InetAddress local = null;
|
||||||
//@@@ TODO: deal with local address stuff from context
|
//@@@ TODO: deal with local address stuff (from context?)
|
||||||
|
|
||||||
final Scheme schm = Scheme.getScheme(target.getSchemeName());
|
final Scheme schm = schemeSet.getScheme(target.getSchemeName());
|
||||||
if (schm == null) {
|
if (schm == null) {
|
||||||
throw new IllegalArgumentException
|
throw new IllegalArgumentException
|
||||||
("Unknown scheme '" + target.getSchemeName() +
|
("Unknown scheme '" + target.getSchemeName() +
|
||||||
@ -148,7 +169,7 @@ public void updateSecureConnection(OperatedClientConnection conn,
|
|||||||
("Connection must be open.");
|
("Connection must be open.");
|
||||||
}
|
}
|
||||||
|
|
||||||
final Scheme schm = Scheme.getScheme(target.getSchemeName());
|
final Scheme schm = schemeSet.getScheme(target.getSchemeName());
|
||||||
if (schm == null) {
|
if (schm == null) {
|
||||||
throw new IllegalArgumentException
|
throw new IllegalArgumentException
|
||||||
("Unknown scheme '" + target.getSchemeName() +
|
("Unknown scheme '" + target.getSchemeName() +
|
||||||
|
@ -41,6 +41,7 @@
|
|||||||
import org.apache.http.conn.HttpConnectionManager;
|
import org.apache.http.conn.HttpConnectionManager;
|
||||||
import org.apache.http.conn.HttpHostConnection;
|
import org.apache.http.conn.HttpHostConnection;
|
||||||
import org.apache.http.conn.Scheme;
|
import org.apache.http.conn.Scheme;
|
||||||
|
import org.apache.http.conn.SchemeSet;
|
||||||
import org.apache.http.conn.SecureSocketFactory;
|
import org.apache.http.conn.SecureSocketFactory;
|
||||||
import org.apache.http.conn.SocketFactory;
|
import org.apache.http.conn.SocketFactory;
|
||||||
import org.apache.http.impl.SocketHttpClientConnection;
|
import org.apache.http.impl.SocketHttpClientConnection;
|
||||||
@ -130,7 +131,7 @@ public void open(final HttpParams params)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Determine the type of the connection
|
// Determine the type of the connection
|
||||||
Scheme scheme = Scheme.getScheme(host.getSchemeName());
|
Scheme scheme = SchemeSet.DEFAULT.getScheme(host.getSchemeName());
|
||||||
SocketFactory socketFactory = scheme.getSocketFactory();
|
SocketFactory socketFactory = scheme.getSocketFactory();
|
||||||
boolean secure = (socketFactory instanceof SecureSocketFactory);
|
boolean secure = (socketFactory instanceof SecureSocketFactory);
|
||||||
boolean proxied = (proxyHost != null);
|
boolean proxied = (proxyHost != null);
|
||||||
@ -150,10 +151,10 @@ public void open(final HttpParams params)
|
|||||||
port = scheme.getDefaultPort();
|
port = scheme.getDefaultPort();
|
||||||
}
|
}
|
||||||
if (secure && proxied) {
|
if (secure && proxied) {
|
||||||
scheme = Scheme.getScheme("http");
|
scheme = SchemeSet.DEFAULT.getScheme("http");
|
||||||
socketFactory = scheme.getSocketFactory();
|
socketFactory = scheme.getSocketFactory();
|
||||||
} else {
|
} else {
|
||||||
scheme = Scheme.getScheme(target.getSchemeName());
|
scheme = SchemeSet.DEFAULT.getScheme(target.getSchemeName());
|
||||||
}
|
}
|
||||||
socketFactory = scheme.getSocketFactory();
|
socketFactory = scheme.getSocketFactory();
|
||||||
Socket socket = socketFactory.connectSocket(
|
Socket socket = socketFactory.connectSocket(
|
||||||
@ -193,7 +194,7 @@ public void tunnelCreated(final HttpParams params)
|
|||||||
LOG.debug("Secure tunnel to " + host);
|
LOG.debug("Secure tunnel to " + host);
|
||||||
}
|
}
|
||||||
|
|
||||||
Scheme scheme = Scheme.getScheme(host.getSchemeName());
|
Scheme scheme = SchemeSet.DEFAULT.getScheme(host.getSchemeName());
|
||||||
SocketFactory socketFactory = scheme.getSocketFactory();
|
SocketFactory socketFactory = scheme.getSocketFactory();
|
||||||
boolean secure = (socketFactory instanceof SecureSocketFactory);
|
boolean secure = (socketFactory instanceof SecureSocketFactory);
|
||||||
boolean proxied = (proxyHost != null);
|
boolean proxied = (proxyHost != null);
|
||||||
|
@ -31,6 +31,8 @@
|
|||||||
|
|
||||||
package org.apache.http.conn;
|
package org.apache.http.conn;
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
import org.apache.http.conn.ssl.SSLSocketFactory;
|
import org.apache.http.conn.ssl.SSLSocketFactory;
|
||||||
|
|
||||||
import junit.framework.Test;
|
import junit.framework.Test;
|
||||||
@ -38,8 +40,9 @@
|
|||||||
import junit.framework.TestSuite;
|
import junit.framework.TestSuite;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unit tests for {@link Scheme}.
|
* Unit tests for {@link Scheme} and {@link SchemeSet}.
|
||||||
*
|
*
|
||||||
|
* @author <a href="mailto:rolandw at apache.org">Roland Weber</a>
|
||||||
* @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
|
* @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
|
||||||
*/
|
*/
|
||||||
public class TestScheme extends TestCase {
|
public class TestScheme extends TestCase {
|
||||||
@ -58,77 +61,153 @@ public static Test suite() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void testConstructor() {
|
public void testConstructor() {
|
||||||
Scheme http = new Scheme("http", PlainSocketFactory.getSocketFactory(), 80);
|
Scheme http = new Scheme
|
||||||
|
("http", PlainSocketFactory.getSocketFactory(), 80);
|
||||||
assertEquals("http", http.getName());
|
assertEquals("http", http.getName());
|
||||||
assertEquals(80, http.getDefaultPort());
|
assertEquals(80, http.getDefaultPort());
|
||||||
assertEquals(PlainSocketFactory.getSocketFactory(), http.getSocketFactory());
|
assertSame(PlainSocketFactory.getSocketFactory(),
|
||||||
|
http.getSocketFactory());
|
||||||
assertFalse(http.isLayered());
|
assertFalse(http.isLayered());
|
||||||
Scheme https = new Scheme("http", SSLSocketFactory.getSocketFactory(), 443);
|
Scheme https = new Scheme
|
||||||
assertEquals("http", https.getName());
|
("https", SSLSocketFactory.getSocketFactory(), 443);
|
||||||
|
assertEquals("https", https.getName());
|
||||||
assertEquals(443, https.getDefaultPort());
|
assertEquals(443, https.getDefaultPort());
|
||||||
assertEquals(SSLSocketFactory.getSocketFactory(), https.getSocketFactory());
|
assertSame(SSLSocketFactory.getSocketFactory(),
|
||||||
|
https.getSocketFactory());
|
||||||
assertTrue(https.isLayered());
|
assertTrue(https.isLayered());
|
||||||
|
|
||||||
|
Scheme hTtP = new Scheme
|
||||||
|
("hTtP", PlainSocketFactory.getSocketFactory(), 80);
|
||||||
|
assertEquals("http", hTtP.getName());
|
||||||
|
// the rest is no different from above
|
||||||
|
|
||||||
try {
|
try {
|
||||||
new Scheme(null, PlainSocketFactory.getSocketFactory(), 80);
|
new Scheme(null, PlainSocketFactory.getSocketFactory(), 80);
|
||||||
fail("IllegalArgumentException should have been thrown");
|
fail("IllegalArgumentException should have been thrown");
|
||||||
} catch (IllegalArgumentException ex) {
|
} catch (IllegalArgumentException ex) {
|
||||||
// expected
|
// expected
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
new Scheme("http", null, 80);
|
new Scheme("http", null, 80);
|
||||||
fail("IllegalArgumentException should have been thrown");
|
fail("IllegalArgumentException should have been thrown");
|
||||||
} catch (IllegalArgumentException ex) {
|
} catch (IllegalArgumentException ex) {
|
||||||
// expected
|
// expected
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
new Scheme("http", PlainSocketFactory.getSocketFactory(), -1);
|
new Scheme("http", PlainSocketFactory.getSocketFactory(), -1);
|
||||||
fail("IllegalArgumentException should have been thrown");
|
fail("IllegalArgumentException should have been thrown");
|
||||||
} catch (IllegalArgumentException ex) {
|
} catch (IllegalArgumentException ex) {
|
||||||
// expected
|
// expected
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
new Scheme("http", PlainSocketFactory.getSocketFactory(), 70000);
|
||||||
|
fail("IllegalArgumentException should have been thrown");
|
||||||
|
} catch (IllegalArgumentException ex) {
|
||||||
|
// expected
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testRegisterUnregister() {
|
public void testRegisterUnregister() {
|
||||||
Scheme http = new Scheme("http", PlainSocketFactory.getSocketFactory(), 80);
|
SchemeSet schmset = new SchemeSet();
|
||||||
Scheme https = new Scheme("http", SSLSocketFactory.getSocketFactory(), 443);
|
|
||||||
Scheme.registerScheme("http", http);
|
Scheme http = new Scheme
|
||||||
Scheme.registerScheme("https", https);
|
("http", PlainSocketFactory.getSocketFactory(), 80);
|
||||||
assertEquals(http, Scheme.getScheme("http"));
|
Scheme https = new Scheme
|
||||||
assertEquals(https, Scheme.getScheme("https"));
|
("https", SSLSocketFactory.getSocketFactory(), 443);
|
||||||
Scheme.unregisterScheme("http");
|
Scheme myhttp = new Scheme
|
||||||
Scheme.unregisterScheme("https");
|
("http", PlainSocketFactory.getSocketFactory(), 80);
|
||||||
|
|
||||||
|
assertNull(schmset.register(myhttp));
|
||||||
|
assertNull(schmset.register(https));
|
||||||
|
assertSame(myhttp, schmset.register(http));
|
||||||
|
assertSame(http, schmset.getScheme("http"));
|
||||||
|
assertSame(https, schmset.getScheme("https"));
|
||||||
|
|
||||||
|
schmset.unregister("http");
|
||||||
|
schmset.unregister("https");
|
||||||
|
|
||||||
|
assertNull(schmset.get("http")); // get() does not throw exception
|
||||||
try {
|
try {
|
||||||
Scheme.getScheme("http");
|
schmset.getScheme("http"); // getScheme() does throw exception
|
||||||
fail("IllegalStateException should have been thrown");
|
fail("IllegalStateException should have been thrown");
|
||||||
} catch (IllegalStateException ex) {
|
} catch (IllegalStateException ex) {
|
||||||
// expected
|
// expected
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void testIterator() {
|
||||||
|
SchemeSet schmset = new SchemeSet();
|
||||||
|
|
||||||
|
Iterator iter = schmset.getSchemeNames();
|
||||||
|
assertNotNull(iter);
|
||||||
|
assertFalse(iter.hasNext());
|
||||||
|
|
||||||
|
Scheme http = new Scheme
|
||||||
|
("http", PlainSocketFactory.getSocketFactory(), 80);
|
||||||
|
Scheme https = new Scheme
|
||||||
|
("https", SSLSocketFactory.getSocketFactory(), 443);
|
||||||
|
|
||||||
|
schmset.register(http);
|
||||||
|
schmset.register(https);
|
||||||
|
|
||||||
|
iter = schmset.getSchemeNames();
|
||||||
|
assertNotNull(iter);
|
||||||
|
assertTrue(iter.hasNext());
|
||||||
|
|
||||||
|
boolean flaghttp = false;
|
||||||
|
boolean flaghttps = false;
|
||||||
|
String name = (String) iter.next();
|
||||||
|
assertTrue(iter.hasNext());
|
||||||
|
|
||||||
|
if ("http".equals(name))
|
||||||
|
flaghttp = true;
|
||||||
|
else if ("https".equals(name))
|
||||||
|
flaghttps = true;
|
||||||
|
else
|
||||||
|
fail("unexpected name in iterator: " + name);
|
||||||
|
|
||||||
|
assertNotNull(schmset.get(name));
|
||||||
|
iter.remove();
|
||||||
|
assertTrue(iter.hasNext());
|
||||||
|
assertNull(schmset.get(name));
|
||||||
|
|
||||||
|
name = (String) iter.next();
|
||||||
|
assertFalse(iter.hasNext());
|
||||||
|
|
||||||
|
if ("http".equals(name)) {
|
||||||
|
if (flaghttp) fail("name 'http' found twice");
|
||||||
|
} else if ("https".equals(name)) {
|
||||||
|
if (flaghttps) fail("name 'https' found twice");
|
||||||
|
} else {
|
||||||
|
fail("unexpected name in iterator: " + name);
|
||||||
|
}
|
||||||
|
|
||||||
|
assertNotNull(schmset.get(name));
|
||||||
|
}
|
||||||
|
|
||||||
public void testIllegalRegisterUnregister() {
|
public void testIllegalRegisterUnregister() {
|
||||||
Scheme http = new Scheme("http", PlainSocketFactory.getSocketFactory(), 80);
|
SchemeSet schmset = new SchemeSet();
|
||||||
try {
|
try {
|
||||||
Scheme.registerScheme(null, http);
|
schmset.register(null);
|
||||||
fail("IllegalArgumentException should have been thrown");
|
fail("IllegalArgumentException should have been thrown");
|
||||||
} catch (IllegalArgumentException ex) {
|
} catch (IllegalArgumentException ex) {
|
||||||
// expected
|
// expected
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
Scheme.registerScheme("http", null);
|
schmset.unregister(null);
|
||||||
fail("IllegalArgumentException should have been thrown");
|
fail("IllegalArgumentException should have been thrown");
|
||||||
} catch (IllegalArgumentException ex) {
|
} catch (IllegalArgumentException ex) {
|
||||||
// expected
|
// expected
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
Scheme.unregisterScheme(null);
|
schmset.get(null);
|
||||||
fail("IllegalArgumentException should have been thrown");
|
fail("IllegalArgumentException should have been thrown");
|
||||||
} catch (IllegalArgumentException ex) {
|
} catch (IllegalArgumentException ex) {
|
||||||
// expected
|
// expected
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
Scheme.getScheme(null);
|
schmset.getScheme(null);
|
||||||
fail("IllegalArgumentException should have been thrown");
|
fail("IllegalArgumentException should have been thrown");
|
||||||
} catch (IllegalArgumentException ex) {
|
} catch (IllegalArgumentException ex) {
|
||||||
// expected
|
// expected
|
||||||
@ -136,23 +215,33 @@ public void testIllegalRegisterUnregister() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void testResolvePort() {
|
public void testResolvePort() {
|
||||||
Scheme http = new Scheme("http", PlainSocketFactory.getSocketFactory(), 80);
|
Scheme http = new Scheme
|
||||||
|
("http", PlainSocketFactory.getSocketFactory(), 80);
|
||||||
|
|
||||||
assertEquals(8080, http.resolvePort(8080));
|
assertEquals(8080, http.resolvePort(8080));
|
||||||
assertEquals(80, http.resolvePort(-1));
|
assertEquals(80, http.resolvePort(-1));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testHashCode() {
|
public void testHashCode() {
|
||||||
Scheme http = new Scheme("http", PlainSocketFactory.getSocketFactory(), 80);
|
Scheme http = new Scheme
|
||||||
Scheme myhttp = new Scheme("http", PlainSocketFactory.getSocketFactory(), 80);
|
("http", PlainSocketFactory.getSocketFactory(), 80);
|
||||||
Scheme https = new Scheme("http", SSLSocketFactory.getSocketFactory(), 443);
|
Scheme myhttp = new Scheme
|
||||||
|
("http", PlainSocketFactory.getSocketFactory(), 80);
|
||||||
|
Scheme https = new Scheme
|
||||||
|
("http", SSLSocketFactory.getSocketFactory(), 443);
|
||||||
|
|
||||||
assertTrue(http.hashCode() != https.hashCode());
|
assertTrue(http.hashCode() != https.hashCode());
|
||||||
assertTrue(http.hashCode() == myhttp.hashCode());
|
assertTrue(http.hashCode() == myhttp.hashCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testEquals() {
|
public void testEquals() {
|
||||||
Scheme http = new Scheme("http", PlainSocketFactory.getSocketFactory(), 80);
|
Scheme http = new Scheme
|
||||||
Scheme myhttp = new Scheme("http", PlainSocketFactory.getSocketFactory(), 80);
|
("http", PlainSocketFactory.getSocketFactory(), 80);
|
||||||
Scheme https = new Scheme("http", SSLSocketFactory.getSocketFactory(), 443);
|
Scheme myhttp = new Scheme
|
||||||
|
("http", PlainSocketFactory.getSocketFactory(), 80);
|
||||||
|
Scheme https = new Scheme
|
||||||
|
("http", SSLSocketFactory.getSocketFactory(), 443);
|
||||||
|
|
||||||
assertFalse(http.equals(https));
|
assertFalse(http.equals(https));
|
||||||
assertFalse(http.equals(null));
|
assertFalse(http.equals(null));
|
||||||
assertFalse(http.equals("http"));
|
assertFalse(http.equals("http"));
|
||||||
@ -160,9 +249,10 @@ public void testEquals() {
|
|||||||
assertTrue(http.equals(myhttp));
|
assertTrue(http.equals(myhttp));
|
||||||
assertFalse(http.equals(https));
|
assertFalse(http.equals(https));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testToString() {
|
public void testToString() {
|
||||||
Scheme http = new Scheme("http", PlainSocketFactory.getSocketFactory(), 80);
|
Scheme http = new Scheme
|
||||||
|
("http", PlainSocketFactory.getSocketFactory(), 80);
|
||||||
assertEquals("http:80", http.toString());
|
assertEquals("http:80", http.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user