KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > httpclient > contrib > ssl > HttpHostFactory


1 package org.apache.commons.httpclient.contrib.ssl;
2
3 import org.apache.commons.httpclient.HostConfiguration;
4 import org.apache.commons.httpclient.HttpHost;
5 import org.apache.commons.httpclient.HttpsURL;
6 import org.apache.commons.httpclient.protocol.Protocol;
7 import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
8
9 /** A source of HttpHosts. */
10 public class HttpHostFactory
11 {
12     /** The default factory. */
13     public static final HttpHostFactory DEFAULT = new HttpHostFactory(null, // httpProtocol
14
new Protocol(new String JavaDoc(HttpsURL.DEFAULT_SCHEME),
15                     (ProtocolSocketFactory) new EasySSLProtocolSocketFactory(),
16                     HttpsURL.DEFAULT_PORT));
17
18     public HttpHostFactory(Protocol httpProtocol, Protocol httpsProtocol)
19     {
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 JavaDoc scheme, String JavaDoc host, int port)
30     {
31         return new HttpHost(host, port, getProtocol(old, scheme, host, port));
32     }
33
34     /**
35      * Get a Protocol for the given parameters. The default implementation
36      * selects a protocol based only on the scheme. Subclasses can do fancier
37      * things, such as select SSL parameters based on the host or port. This
38      * method must not return null.
39      */

40     protected Protocol getProtocol(HostConfiguration old, String JavaDoc scheme, String JavaDoc host, int port)
41     {
42         final Protocol oldProtocol = old.getProtocol();
43         if (oldProtocol != null) {
44             final String JavaDoc oldScheme = oldProtocol.getScheme();
45             if (oldScheme == scheme || (oldScheme != null && oldScheme.equalsIgnoreCase(scheme))) {
46                 // The old protocol has the desired scheme.
47
return oldProtocol; // Retain it.
48
}
49         }
50         Protocol newProtocol = (scheme != null && scheme.toLowerCase().endsWith("s")) ? httpsProtocol
51                 : httpProtocol;
52         if (newProtocol == null) {
53             newProtocol = Protocol.getProtocol(scheme);
54         }
55         return newProtocol;
56     }
57
58 }
59
Popular Tags