Index: C:/depot/Tools/commons-httpclient-trunk/src/contrib/org/apache/commons/httpclient/contrib/ssl/HostConfigurationWithHostFactory.java =================================================================== --- C:/depot/Tools/commons-httpclient-trunk/src/contrib/org/apache/commons/httpclient/contrib/ssl/HostConfigurationWithHostFactory.java (revision 0) +++ C:/depot/Tools/commons-httpclient-trunk/src/contrib/org/apache/commons/httpclient/contrib/ssl/HostConfigurationWithHostFactory.java (revision 0) @@ -0,0 +1,60 @@ +package org.apache.commons.httpclient.contrib.ssl; + +import org.apache.commons.httpclient.HostConfiguration; +import org.apache.commons.httpclient.HttpURL; +import org.apache.commons.httpclient.protocol.Protocol; + +/** + * A kind of HostConfiguration that gets its Host from a factory. This is useful + * for integrating a specialized Protocol or SocketFactory; for example, a + * SecureSocketFactory that authenticates via SSL. Use + * HttpClient.setHostConfiguration to install a HostConfigurationWithHostFactory + * that contains the specialized HostFactory, Protocol or SocketFactory. + *

+ * An alternative is to use Protocol.registerProtocol to register a specialized + * Protocol. But that has drawbacks: it makes it hard to integrate modules (e.g. + * web applications in a servlet container) with different strategies, because + * they share the specialized Protocol (Protocol.PROTOCOLS is static). And it + * can't support different Protocols for different hosts or ports (since the + * host and port aren't parameters to Protocol.getProtocol). + * + * @author John Kristian + */ +class HostConfigurationWithHostFactory extends HostConfiguration +{ + public HostConfigurationWithHostFactory(HttpHostFactory factory) + { + this.factory = factory; + } + + private HostConfigurationWithHostFactory(HostConfigurationWithHostFactory that) + { + super(that); + this.factory = that.factory; + } + + private final HttpHostFactory factory; + + public Object clone() + { + return new HostConfigurationWithHostFactory(this); + } + + private static final String DEFAULT_SCHEME = new String(HttpURL.DEFAULT_SCHEME); + + public void setHost(String host) + { + setHost(host, Protocol.getProtocol(DEFAULT_SCHEME).getDefaultPort()); + } + + public void setHost(final String host, int port) + { + setHost(host, port, DEFAULT_SCHEME); + } + + public synchronized void setHost(String host, int port, String scheme) + { + setHost(factory.getHost(this, scheme, host, port)); + } + +} Index: C:/depot/Tools/commons-httpclient-trunk/src/contrib/org/apache/commons/httpclient/contrib/ssl/HttpHostFactory.java =================================================================== --- C:/depot/Tools/commons-httpclient-trunk/src/contrib/org/apache/commons/httpclient/contrib/ssl/HttpHostFactory.java (revision 0) +++ C:/depot/Tools/commons-httpclient-trunk/src/contrib/org/apache/commons/httpclient/contrib/ssl/HttpHostFactory.java (revision 0) @@ -0,0 +1,58 @@ +package org.apache.commons.httpclient.contrib.ssl; + +import org.apache.commons.httpclient.HostConfiguration; +import org.apache.commons.httpclient.HttpHost; +import org.apache.commons.httpclient.HttpsURL; +import org.apache.commons.httpclient.protocol.Protocol; +import org.apache.commons.httpclient.protocol.ProtocolSocketFactory; + +/** A source of HttpHosts. */ +public class HttpHostFactory +{ + /** The default factory. */ + public static final HttpHostFactory DEFAULT = new HttpHostFactory(null, // httpProtocol + new Protocol(new String(HttpsURL.DEFAULT_SCHEME), + (ProtocolSocketFactory) new EasySSLProtocolSocketFactory(), + HttpsURL.DEFAULT_PORT)); + + public HttpHostFactory(Protocol httpProtocol, Protocol httpsProtocol) + { + this.httpProtocol = httpProtocol; + this.httpsProtocol = httpsProtocol; + } + + protected final Protocol httpProtocol; + + protected final Protocol httpsProtocol; + + /** Get a host for the given parameters. This method need not be thread-safe. */ + public HttpHost getHost(HostConfiguration old, String scheme, String host, int port) + { + return new HttpHost(host, port, getProtocol(old, scheme, host, port)); + } + + /** + * Get a Protocol for the given parameters. The default implementation + * selects a protocol based only on the scheme. Subclasses can do fancier + * things, such as select SSL parameters based on the host or port. This + * method must not return null. + */ + protected Protocol getProtocol(HostConfiguration old, String scheme, String host, int port) + { + final Protocol oldProtocol = old.getProtocol(); + if (oldProtocol != null) { + final String oldScheme = oldProtocol.getScheme(); + if (oldScheme == scheme || (oldScheme != null && oldScheme.equalsIgnoreCase(scheme))) { + // The old protocol has the desired scheme. + return oldProtocol; // Retain it. + } + } + Protocol newProtocol = (scheme != null && scheme.toLowerCase().endsWith("s")) ? httpsProtocol + : httpProtocol; + if (newProtocol == null) { + newProtocol = Protocol.getProtocol(scheme); + } + return newProtocol; + } + +} Index: C:/depot/Tools/commons-httpclient-trunk/src/contrib/org/apache/commons/httpclient/contrib/ssl/HostConfigurationWithStickyProtocol.java =================================================================== --- C:/depot/Tools/commons-httpclient-trunk/src/contrib/org/apache/commons/httpclient/contrib/ssl/HostConfigurationWithStickyProtocol.java (revision 509666) +++ C:/depot/Tools/commons-httpclient-trunk/src/contrib/org/apache/commons/httpclient/contrib/ssl/HostConfigurationWithStickyProtocol.java (working copy) @@ -1,69 +0,0 @@ -package org.apache.commons.httpclient.contrib.ssl; - -import org.apache.commons.httpclient.HostConfiguration; -import org.apache.commons.httpclient.HttpHost; -import org.apache.commons.httpclient.protocol.Protocol; - -/** - * A kind of HostConfiguration that can retain its Protocol when its host name - * or port changes. HttpClient may clone its HostConfigurationWithStickyProtocol - * and change the host URL, without changing the specialized Protocol. - *

- * This is useful for integrating a specialized Protocol or SocketFactory; for - * example, a SecureSocketFactory that authenticates via SSL. Use - * HttpClient.setHostConfiguration to install a - * HostConfigurationWithStickyProtocol that contains the specialized Protocol or - * SocketFactory. - *

- * An alternative is to use Protocol.registerProtocol to register a specialized - * Protocol. But that has drawbacks: it makes it hard to integrate modules (e.g. - * web applications in a servlet container) with different strategies, because - * they share the specialized Protocol (Protocol.PROTOCOLS is static). Also, it - * can't handle multiple socket factories for the same host and port, since the - * URL path isn't a parameter to Protocol.getProtocol or socket factory methods. - * - * @author John Kristian - */ -public class HostConfigurationWithStickyProtocol extends HostConfiguration -{ - public HostConfigurationWithStickyProtocol() - { - } - - public HostConfigurationWithStickyProtocol(HostConfiguration hostConfiguration) - { - super(hostConfiguration); - } - - public Object clone() - { - return new HostConfigurationWithStickyProtocol(this); - } - - public synchronized void setHost(String host, int port, String scheme) - { - setHost(new HttpHost(host, port, getNewProtocol(host, port, scheme))); - } - - /** - * Select a Protocol to be used for the given host, port and scheme. The - * current Protocol may be selected, if appropriate. This method need not be - * thread-safe; the caller must synchronize if necessary. - *

- * This implementation returns the current Protocol if it has the given - * scheme; otherwise it returns the Protocol registered for that scheme. - */ - protected Protocol getNewProtocol(String host, int port, String scheme) - { - final Protocol oldProtocol = getProtocol(); - if (oldProtocol != null) { - final String oldScheme = oldProtocol.getScheme(); - if (oldScheme == scheme || (oldScheme != null && oldScheme.equalsIgnoreCase(scheme))) { - // The old {rotocol has the desired scheme. - return oldProtocol; // Retain it. - } - } - return Protocol.getProtocol(scheme); - } - -}