01: package org.apache.commons.httpclient.contrib.ssl;
02:
03: import org.apache.commons.httpclient.HostConfiguration;
04: import org.apache.commons.httpclient.HttpURL;
05: import org.apache.commons.httpclient.protocol.Protocol;
06:
07: /**
08: * A kind of HostConfiguration that gets its Host from a factory. This is useful
09: * for integrating a specialized Protocol or SocketFactory; for example, a
10: * SecureSocketFactory that authenticates via SSL. Use
11: * HttpClient.setHostConfiguration to install a HostConfigurationWithHostFactory
12: * that contains the specialized HostFactory, Protocol or SocketFactory.
13: * <p>
14: * An alternative is to use Protocol.registerProtocol to register a specialized
15: * Protocol. But that has drawbacks: it makes it hard to integrate modules (e.g.
16: * web applications in a servlet container) with different strategies, because
17: * they share the specialized Protocol (Protocol.PROTOCOLS is static). And it
18: * can't support different Protocols for different hosts or ports (since the
19: * host and port aren't parameters to Protocol.getProtocol).
20: *
21: * @author John Kristian
22: */
23: class HostConfigurationWithHostFactory extends HostConfiguration {
24: public HostConfigurationWithHostFactory(HttpHostFactory factory) {
25: this .factory = factory;
26: }
27:
28: private HostConfigurationWithHostFactory(
29: HostConfigurationWithHostFactory that) {
30: super (that);
31: this .factory = that.factory;
32: }
33:
34: private final HttpHostFactory factory;
35:
36: public Object clone() {
37: return new HostConfigurationWithHostFactory(this );
38: }
39:
40: private static final String DEFAULT_SCHEME = new String(
41: HttpURL.DEFAULT_SCHEME);
42:
43: public void setHost(String host) {
44: setHost(host, Protocol.getProtocol(DEFAULT_SCHEME)
45: .getDefaultPort());
46: }
47:
48: public void setHost(final String host, int port) {
49: setHost(host, port, DEFAULT_SCHEME);
50: }
51:
52: public synchronized void setHost(String host, int port,
53: String scheme) {
54: setHost(factory.getHost(this, scheme, host, port));
55: }
56:
57: }
|