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