Author: olegk Date: Tue Mar 13 06:55:56 2007 New Revision: 517699 URL: http://svn.apache.org/viewvc?view=rev&rev=517699 Log: HTTPCLIENT-640: 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 Contributed by John Kristian <jkristian at netflix.com> Added: jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/ssl/HostConfigurationWithHostFactory.java (with props) jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/ssl/HttpHostFactory.java (with props) Added: jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/ssl/HostConfigurationWithHostFactory.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/ssl/HostConfigurationWithHostFactory.java?view=auto&rev=517699 ============================================================================== --- jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/ssl/HostConfigurationWithHostFactory.java (added) +++ jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/ssl/HostConfigurationWithHostFactory.java Tue Mar 13 06:55:56 2007 @@ -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. + * <p> + * 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)); + } + +} Propchange: jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/ssl/HostConfigurationWithHostFactory.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/ssl/HostConfigurationWithHostFactory.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/ssl/HttpHostFactory.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/ssl/HttpHostFactory.java?view=auto&rev=517699 ============================================================================== --- jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/ssl/HttpHostFactory.java (added) +++ jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/ssl/HttpHostFactory.java Tue Mar 13 06:55:56 2007 @@ -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; + } + +} Propchange: jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/ssl/HttpHostFactory.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/ssl/HttpHostFactory.java ------------------------------------------------------------------------------ svn:mime-type = text/plain --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]