01: package org.apache.commons.httpclient.contrib.ssl;
02:
03: import org.apache.commons.httpclient.HostConfiguration;
04: import org.apache.commons.httpclient.HttpHost;
05: import org.apache.commons.httpclient.HttpsURL;
06: import org.apache.commons.httpclient.protocol.Protocol;
07: import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
08:
09: /** A source of HttpHosts. */
10: public class HttpHostFactory {
11: /** The default factory. */
12: public static final HttpHostFactory DEFAULT = new HttpHostFactory(
13: null, // httpProtocol
14: new Protocol(
15: new String(HttpsURL.DEFAULT_SCHEME),
16: (ProtocolSocketFactory) new EasySSLProtocolSocketFactory(),
17: HttpsURL.DEFAULT_PORT));
18:
19: public HttpHostFactory(Protocol httpProtocol, Protocol httpsProtocol) {
20: this .httpProtocol = httpProtocol;
21: this .httpsProtocol = httpsProtocol;
22: }
23:
24: protected final Protocol httpProtocol;
25:
26: protected final Protocol httpsProtocol;
27:
28: /** Get a host for the given parameters. This method need not be thread-safe. */
29: public HttpHost getHost(HostConfiguration old, String scheme,
30: String host, int port) {
31: return new HttpHost(host, port, getProtocol(old, scheme, host,
32: port));
33: }
34:
35: /**
36: * Get a Protocol for the given parameters. The default implementation
37: * selects a protocol based only on the scheme. Subclasses can do fancier
38: * things, such as select SSL parameters based on the host or port. This
39: * method must not return null.
40: */
41: protected Protocol getProtocol(HostConfiguration old,
42: String scheme, String host, int port) {
43: final Protocol oldProtocol = old.getProtocol();
44: if (oldProtocol != null) {
45: final String oldScheme = oldProtocol.getScheme();
46: if (oldScheme == scheme
47: || (oldScheme != null && oldScheme
48: .equalsIgnoreCase(scheme))) {
49: // The old protocol has the desired scheme.
50: return oldProtocol; // Retain it.
51: }
52: }
53: Protocol newProtocol = (scheme != null && scheme.toLowerCase()
54: .endsWith("s")) ? httpsProtocol : httpProtocol;
55: if (newProtocol == null) {
56: newProtocol = Protocol.getProtocol(scheme);
57: }
58: return newProtocol;
59: }
60:
61: }
|