KickJava   Java API By Example, From Geeks To Geeks.

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


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.protocol.Protocol;
6
7 /**
8  * A kind of HostConfiguration that can retain its Protocol when its host name
9  * or port changes. HttpClient may clone its HostConfigurationWithStickyProtocol
10  * and change the host URL, without changing the specialized Protocol.
11  * <p>
12  * This is useful for integrating a specialized Protocol or SocketFactory; for
13  * example, a SecureSocketFactory that authenticates via SSL. Use
14  * HttpClient.setHostConfiguration to install a
15  * HostConfigurationWithStickyProtocol that contains the specialized Protocol or
16  * SocketFactory.
17  * <p>
18  * An alternative is to use Protocol.registerProtocol to register a specialized
19  * Protocol. But that has drawbacks: it makes it hard to integrate modules (e.g.
20  * web applications in a servlet container) with different strategies, because
21  * they share the specialized Protocol (Protocol.PROTOCOLS is static). Also, it
22  * can't handle multiple socket factories for the same host and port, since the
23  * URL path isn't a parameter to Protocol.getProtocol or socket factory methods.
24  *
25  * @author John Kristian
26  */

27 public class HostConfigurationWithStickyProtocol extends HostConfiguration
28 {
29     public HostConfigurationWithStickyProtocol()
30     {
31     }
32
33     public HostConfigurationWithStickyProtocol(HostConfiguration hostConfiguration)
34     {
35         super(hostConfiguration);
36     }
37
38     public Object JavaDoc clone()
39     {
40         return new HostConfigurationWithStickyProtocol(this);
41     }
42
43     public synchronized void setHost(String JavaDoc host, int port, String JavaDoc scheme)
44     {
45         setHost(new HttpHost(host, port, getNewProtocol(host, port, scheme)));
46     }
47
48     /**
49      * Select a Protocol to be used for the given host, port and scheme. The
50      * current Protocol may be selected, if appropriate. This method need not be
51      * thread-safe; the caller must synchronize if necessary.
52      * <p>
53      * This implementation returns the current Protocol if it has the given
54      * scheme; otherwise it returns the Protocol registered for that scheme.
55      */

56     protected Protocol getNewProtocol(String JavaDoc host, int port, String JavaDoc scheme)
57     {
58         final Protocol oldProtocol = getProtocol();
59         if (oldProtocol != null) {
60             final String JavaDoc oldScheme = oldProtocol.getScheme();
61             if (oldScheme == scheme || (oldScheme != null && oldScheme.equalsIgnoreCase(scheme))) {
62                 // The old {rotocol has the desired scheme.
63
return oldProtocol; // Retain it.
64
}
65         }
66         return Protocol.getProtocol(scheme);
67     }
68
69 }
70
Popular Tags