Coverage Report - org.mule.providers.http.HttpConnector
 
Classes in this File Line Coverage Branch Coverage Complexity
HttpConnector
67%
45/67
62%
15/24
1.632
 
 1  
 /*
 2  
  * $Id: HttpConnector.java 8074 2007-08-27 18:41:12Z aperepel $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.providers.http;
 12  
 
 13  
 import org.mule.config.i18n.CoreMessages;
 14  
 import org.mule.providers.tcp.TcpConnector;
 15  
 import org.mule.umo.UMOComponent;
 16  
 import org.mule.umo.endpoint.UMOEndpoint;
 17  
 import org.mule.umo.lifecycle.InitialisationException;
 18  
 import org.mule.umo.provider.UMOConnector;
 19  
 import org.mule.umo.provider.UMOMessageReceiver;
 20  
 
 21  
 import java.util.HashMap;
 22  
 import java.util.Iterator;
 23  
 import java.util.Map;
 24  
 
 25  
 import org.apache.commons.httpclient.HttpConnectionManager;
 26  
 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
 27  
 import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
 28  
 
 29  
 /**
 30  
  * <code>HttpConnector</code> provides a way of receiving and sending http requests
 31  
  * and responses. The UMOConnector itself handles dispatching http requests. The
 32  
  * <code>HttpMessageReceiver</code> handles the receiving requests and processing
 33  
  * of headers This endpoint recognises the following properties - <p/>
 34  
  * <ul>
 35  
  * <li>hostname - The hostname to send and receive http requests</li>
 36  
  * <li>port - The port to listen on. The industry standard is 80 and if this propert
 37  
  * is not set it will default to 80</li>
 38  
  * <li>proxyHostname - If you access the web through a proxy, this holds the server
 39  
  * address</li>
 40  
  * <li>proxyPort - The port the proxy is configured on</li>
 41  
  * <li>proxyUsername - If the proxy requires authentication supply a username</li>
 42  
  * <li>proxyPassword - If the proxy requires authentication supply a password</li>
 43  
  * </ul>
 44  
  * 
 45  
  */
 46  
 
 47  90
 public class HttpConnector extends TcpConnector
 48  
 {
 49  
 
 50  
     /**
 51  
      * Event property to pass back the status for the response
 52  
      */
 53  
     public static final String HTTP_STATUS_PROPERTY = "http.status";
 54  
     public static final String HTTP_VERSION_PROPERTY = "http.version";
 55  
     public static final String HTTP_CUSTOM_HEADERS_MAP_PROPERTY = "http.custom.headers";
 56  
     public static final String HTTP_METHOD_PROPERTY = "http.method";
 57  
     public static final String HTTP_REQUEST_PROPERTY = "http.request";
 58  
 
 59  
     /**
 60  
      * Allows the user to set a {@link org.apache.commons.httpclient.params.HttpMethodParams} object in the client
 61  
      * request to be set on the HttpMethod request object
 62  
      */
 63  
     public static final String HTTP_PARAMS_PROPERTY = "http.params";
 64  
     public static final String HTTP_GET_BODY_PARAM_PROPERTY = "http.get.body.param";
 65  
     public static final String DEFAULT_HTTP_GET_BODY_PARAM_PROPERTY = "body";
 66  
     public static final String HTTP_POST_BODY_PARAM_PROPERTY = "http.post.body.param";
 67  
 
 68  
     public static final String HTTP_COOKIE_SPEC_PROPERTY = "cookieSpec";
 69  
     public static final String HTTP_COOKIES_PROPERTY = "cookies";
 70  
     public static final String HTTP_ENABLE_COOKIES_PROPERTY = "enableCookies";
 71  
 
 72  
     public static final String COOKIE_SPEC_NETSCAPE = "netscape";
 73  
     public static final String COOKIE_SPEC_RFC2109 = "rcf2109";
 74  
 
 75  90
     private String proxyHostname = null;
 76  
 
 77  90
     private int proxyPort = HttpConstants.DEFAULT_HTTP_PORT;
 78  
 
 79  90
     private String proxyUsername = null;
 80  
 
 81  90
     private String proxyPassword = null;
 82  
 
 83  
     private String cookieSpec;
 84  
 
 85  90
     private boolean enableCookies = false;
 86  
 
 87  
     protected HttpConnectionManager clientConnectionManager;
 88  
 
 89  
 
 90  
     //@Override
 91  
     protected void doInitialise() throws InitialisationException
 92  
     {
 93  88
         super.doInitialise();
 94  88
         if (clientConnectionManager == null)
 95  
         {
 96  84
             clientConnectionManager = new MultiThreadedHttpConnectionManager();
 97  84
             HttpConnectionManagerParams params = new HttpConnectionManagerParams();
 98  84
             if (getSendBufferSize() != INT_VALUE_NOT_SET)
 99  
             {
 100  2
                 params.setSendBufferSize(getSendBufferSize());
 101  
             }
 102  84
             if (getReceiveBufferSize() != INT_VALUE_NOT_SET)
 103  
             {
 104  0
                 params.setReceiveBufferSize(getReceiveBufferSize());
 105  
             }
 106  84
             if (getSendTimeout() != INT_VALUE_NOT_SET)
 107  
             {
 108  0
                 params.setSoTimeout(getSendTimeout());
 109  
             }
 110  84
             if (getSendSocketLinger() != INT_VALUE_NOT_SET)
 111  
             {
 112  0
                 params.setLinger(getSendSocketLinger());
 113  
             }
 114  
 
 115  84
             params.setTcpNoDelay(isSendTcpNoDelay());
 116  84
             params.setMaxTotalConnections(getDispatcherThreadingProfile().getMaxThreadsActive());
 117  84
             params.setDefaultMaxConnectionsPerHost(getDispatcherThreadingProfile().getMaxThreadsActive());
 118  
 
 119  84
             clientConnectionManager.setParams(params);
 120  
         }
 121  88
     }
 122  
 
 123  
     /**
 124  
      * @see UMOConnector#registerListener(UMOComponent, UMOEndpoint)
 125  
      */
 126  
     public UMOMessageReceiver registerListener(UMOComponent component, UMOEndpoint endpoint) throws Exception
 127  
     {
 128  84
         if (endpoint != null)
 129  
         {
 130  76
             Map endpointProperties = endpoint.getProperties();
 131  76
             if (endpointProperties != null)
 132  
             {
 133  
                 // normalize properties for HTTP
 134  76
                 Map newProperties = new HashMap(endpointProperties.size());
 135  76
                 for (Iterator entries = endpointProperties.entrySet().iterator(); entries.hasNext();)
 136  
                 {
 137  4
                     Map.Entry entry = (Map.Entry) entries.next();
 138  4
                     Object key = entry.getKey();
 139  4
                     Object normalizedKey = HttpConstants.ALL_HEADER_NAMES.get(key);
 140  4
                     if (normalizedKey != null)
 141  
                     {
 142  
                         // normalized property exists
 143  2
                         key = normalizedKey;
 144  
                     }
 145  4
                     newProperties.put(key, entry.getValue());
 146  4
                 }
 147  
                 // set normalized properties back on the endpoint
 148  76
                 endpoint.setProperties(newProperties);
 149  
             }
 150  
         }
 151  
         // proceed as usual
 152  84
         return super.registerListener(component, endpoint);
 153  
     }
 154  
 
 155  
     /**
 156  
      * The method determines the key used to store the receiver against.
 157  
      * 
 158  
      * @param component the component for which the endpoint is being registered
 159  
      * @param endpoint the endpoint being registered for the component
 160  
      * @return the key to store the newly created receiver against
 161  
      */
 162  
     protected Object getReceiverKey(UMOComponent component, UMOEndpoint endpoint)
 163  
     {
 164  284
         String key = endpoint.getEndpointURI().toString();
 165  284
         int i = key.indexOf('?');
 166  284
         if (i > -1)
 167  
         {
 168  0
             key = key.substring(0, i);
 169  
         }
 170  284
         return key;
 171  
     }
 172  
 
 173  
     /**
 174  
      * @see org.mule.umo.provider.UMOConnector#getProtocol()
 175  
      */
 176  
     public String getProtocol()
 177  
     {
 178  528
         return "http";
 179  
     }
 180  
 
 181  
     /**
 182  
      * @return
 183  
      */
 184  
     public String getProxyHostname()
 185  
     {
 186  126
         return proxyHostname;
 187  
     }
 188  
 
 189  
     /**
 190  
      * @return
 191  
      */
 192  
     public String getProxyPassword()
 193  
     {
 194  0
         return proxyPassword;
 195  
     }
 196  
 
 197  
     /**
 198  
      * @return
 199  
      */
 200  
     public int getProxyPort()
 201  
     {
 202  0
         return proxyPort;
 203  
     }
 204  
 
 205  
     /**
 206  
      * @return
 207  
      */
 208  
     public String getProxyUsername()
 209  
     {
 210  46
         return proxyUsername;
 211  
     }
 212  
 
 213  
     /**
 214  
      * @param host
 215  
      */
 216  
     public void setProxyHostname(String host)
 217  
     {
 218  0
         proxyHostname = host;
 219  0
     }
 220  
 
 221  
     /**
 222  
      * @param string
 223  
      */
 224  
     public void setProxyPassword(String string)
 225  
     {
 226  0
         proxyPassword = string;
 227  0
     }
 228  
 
 229  
     /**
 230  
      * @param port
 231  
      */
 232  
     public void setProxyPort(int port)
 233  
     {
 234  0
         proxyPort = port;
 235  0
     }
 236  
 
 237  
     /**
 238  
      * @param string
 239  
      */
 240  
     public void setProxyUsername(String string)
 241  
     {
 242  0
         proxyUsername = string;
 243  0
     }
 244  
 
 245  
     public Map getReceivers()
 246  
     {
 247  6
         return this.receivers;
 248  
     }
 249  
 
 250  
     public String getCookieSpec()
 251  
     {
 252  136
         return cookieSpec;
 253  
     }
 254  
 
 255  
     public void setCookieSpec(String cookieSpec)
 256  
     {
 257  0
         if (!(COOKIE_SPEC_NETSCAPE.equalsIgnoreCase(cookieSpec) || COOKIE_SPEC_RFC2109.equalsIgnoreCase(cookieSpec)))
 258  
         {
 259  0
             throw new IllegalArgumentException(
 260  
                 CoreMessages.propertyHasInvalidValue("cookieSpec", cookieSpec).toString());
 261  
         }
 262  0
         this.cookieSpec = cookieSpec;
 263  0
     }
 264  
 
 265  
     public boolean isEnableCookies()
 266  
     {
 267  136
         return enableCookies;
 268  
     }
 269  
 
 270  
     public void setEnableCookies(boolean enableCookies)
 271  
     {
 272  0
         this.enableCookies = enableCookies;
 273  0
     }
 274  
 
 275  
 
 276  
     public HttpConnectionManager getClientConnectionManager()
 277  
     {
 278  48
         return clientConnectionManager;
 279  
     }
 280  
 
 281  
     public void setClientConnectionManager(HttpConnectionManager clientConnectionManager)
 282  
     {
 283  0
         this.clientConnectionManager = clientConnectionManager;
 284  0
     }
 285  
 }