Coverage Report - org.mule.transport.http.MuleHostConfiguration
 
Classes in this File Line Coverage Branch Coverage Complexity
MuleHostConfiguration
0%
0/35
0%
0/4
0
 
 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  0
         super();
 28  0
     }
 29  
     
 30  
     public MuleHostConfiguration(HostConfiguration hostConfig)
 31  
     {
 32  0
         super(hostConfig);
 33  0
     }
 34  
 
 35  
     @Override
 36  
     public synchronized void setHost(URI uri)
 37  
     {
 38  
         try
 39  
         {
 40  0
             Protocol original = getProtocol();
 41  
     
 42  0
             if (uri.getScheme().equals(original.getScheme()))
 43  
             {
 44  0
                 Protocol newProtocol = new Protocol(uri.getScheme(), original.getSocketFactory(),
 45  
                     original.getDefaultPort());
 46  
     
 47  0
                     super.setHost(uri.getHost(), uri.getPort(), newProtocol);
 48  0
             }
 49  
             else
 50  
             {
 51  0
                 Protocol protoByName = Protocol.getProtocol(uri.getScheme());
 52  0
                 super.setHost(uri.getHost(), uri.getPort(), protoByName);
 53  
             }
 54  
         }
 55  0
         catch (URIException uriException)
 56  
         {
 57  0
             throw new IllegalArgumentException(uriException);
 58  0
         }
 59  0
     }
 60  
 
 61  
     @Override
 62  
     public synchronized void setHost(HttpHost host)
 63  
     {
 64  0
         Protocol newProtocol = cloneProtocolKeepingSocketFactory(host.getProtocol());
 65  
         
 66  0
         HttpHost hostCopy = new HttpHost(host.getHostName(), host.getPort(), newProtocol);
 67  0
         super.setHost(hostCopy);
 68  0
     }
 69  
 
 70  
     @Override
 71  
     public synchronized void setHost(String host, int port, String protocolName)
 72  
     {
 73  0
         Protocol protoByName = Protocol.getProtocol(protocolName);
 74  0
         Protocol newProtocol = cloneProtocolKeepingSocketFactory(protoByName);        
 75  
 
 76  0
         super.setHost(host, port, newProtocol);
 77  0
     }
 78  
 
 79  
     @Override
 80  
     @SuppressWarnings("deprecation")
 81  
     public synchronized void setHost(String host, String virtualHost, int port, Protocol protocol)
 82  
     {
 83  0
         Protocol newProtocol = cloneProtocolKeepingSocketFactory(protocol);        
 84  0
         super.setHost(host, virtualHost, port, newProtocol);
 85  0
     }
 86  
     
 87  
     @Override
 88  
     public synchronized void setHost(String host, int port)
 89  
     {
 90  0
         super.setHost(host, port, getProtocol());
 91  0
     }
 92  
     
 93  
     @Override
 94  
     public synchronized void setHost(String host)
 95  
     {
 96  0
         super.setHost(host, getPort(), getProtocol());
 97  0
     }
 98  
 
 99  
     private Protocol cloneProtocolKeepingSocketFactory(Protocol protocol)
 100  
     {
 101  0
         Protocol original = getProtocol();
 102  0
         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  0
             return new Protocol(protocol.getScheme(), original.getSocketFactory(), 
 106  
                 protocol.getDefaultPort());
 107  
         }
 108  0
         return protocol;
 109  
     }
 110  
 
 111  
     @Override
 112  
     public Object clone()
 113  
     {
 114  0
         return new MuleHostConfiguration(this);
 115  
     }
 116  
 
 117  
 }
 118  
 
 119