View Javadoc

1   /*
2    * $Id: MuleHostConfiguration.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.http;
12  
13  import org.apache.commons.httpclient.HostConfiguration;
14  import org.apache.commons.httpclient.HttpHost;
15  import org.apache.commons.httpclient.URI;
16  import org.apache.commons.httpclient.URIException;
17  import org.apache.commons.httpclient.protocol.Protocol;
18  
19  /**
20   * Subclass of httpclient's {@link HostConfiguration} that retains its {@link Protocol} when
21   * a new host is set via the URI.
22   * 
23   * It looks like we're not the only ones who stumbled over the HostConfiguration behaviour, see
24   * http://issues.apache.org/jira/browse/HTTPCLIENT-634
25   */
26  public class MuleHostConfiguration extends HostConfiguration
27  {
28      
29      public MuleHostConfiguration()
30      {
31          super();
32      }
33      
34      public MuleHostConfiguration(HostConfiguration hostConfig)
35      {
36          super(hostConfig);
37      }
38  
39      @Override
40      public synchronized void setHost(URI uri)
41      {
42          try
43          {
44              Protocol original = getProtocol();
45      
46              if (uri.getScheme().equals(original.getScheme()))
47              {
48                  Protocol newProtocol = new Protocol(uri.getScheme(), original.getSocketFactory(),
49                      original.getDefaultPort());
50      
51                      super.setHost(uri.getHost(), uri.getPort(), newProtocol);
52              }
53              else
54              {
55                  Protocol protoByName = Protocol.getProtocol(uri.getScheme());
56                  super.setHost(uri.getHost(), uri.getPort(), protoByName);
57              }
58          }
59          catch (URIException uriException)
60          {
61              throw new IllegalArgumentException(uriException);
62          }
63      }
64  
65      @Override
66      public synchronized void setHost(HttpHost host)
67      {
68          Protocol newProtocol = cloneProtocolKeepingSocketFactory(host.getProtocol());
69          
70          HttpHost hostCopy = new HttpHost(host.getHostName(), host.getPort(), newProtocol);
71          super.setHost(hostCopy);
72      }
73  
74      @Override
75      public synchronized void setHost(String host, int port, String protocolName)
76      {
77          Protocol protoByName = Protocol.getProtocol(protocolName);
78          Protocol newProtocol = cloneProtocolKeepingSocketFactory(protoByName);        
79  
80          super.setHost(host, port, newProtocol);
81      }
82  
83      @Override
84      @SuppressWarnings("deprecation")
85      public synchronized void setHost(String host, String virtualHost, int port, Protocol protocol)
86      {
87          Protocol newProtocol = cloneProtocolKeepingSocketFactory(protocol);        
88          super.setHost(host, virtualHost, port, newProtocol);
89      }
90      
91      @Override
92      public synchronized void setHost(String host, int port)
93      {
94          super.setHost(host, port, getProtocol());
95      }
96      
97      @Override
98      public synchronized void setHost(String host)
99      {
100         super.setHost(host, getPort(), getProtocol());
101     }
102 
103     private Protocol cloneProtocolKeepingSocketFactory(Protocol protocol)
104     {
105         Protocol original = getProtocol();
106         if (protocol.getScheme().equals(original.getScheme()))
107         {
108             // the protocol is the same, create a copy of it but keep the original socket factory
109             return new Protocol(protocol.getScheme(), original.getSocketFactory(), 
110                 protocol.getDefaultPort());
111         }
112         return protocol;
113     }
114 
115     @Override
116     public Object clone()
117     {
118         return new MuleHostConfiguration(this);
119     }
120 
121 }
122 
123