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