View Javadoc

1   /*
2    * $Id: HttpConnector.java 7976 2007-08-21 14:26:13Z dirk.olmes $
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  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      private String proxyHostname = null;
76  
77      private int proxyPort = HttpConstants.DEFAULT_HTTP_PORT;
78  
79      private String proxyUsername = null;
80  
81      private String proxyPassword = null;
82  
83      private String cookieSpec;
84  
85      private boolean enableCookies = false;
86  
87      protected HttpConnectionManager clientConnectionManager;
88  
89  
90      //@Override
91      protected void doInitialise() throws InitialisationException
92      {
93          super.doInitialise();
94          if(clientConnectionManager==null)
95          {
96              clientConnectionManager = new MultiThreadedHttpConnectionManager();
97              HttpConnectionManagerParams params = new HttpConnectionManagerParams();
98              if(getSendBufferSize()!= INT_VALUE_NOT_SET ) params.setSendBufferSize(getSendBufferSize());
99              if(getReceiveBufferSize()!= INT_VALUE_NOT_SET ) params.setReceiveBufferSize(getReceiveBufferSize());
100             if(getSendTimeout()!= INT_VALUE_NOT_SET ) params.setSoTimeout(getSendTimeout());
101             if(getSendSocketLinger()!= INT_VALUE_NOT_SET ) params.setLinger(getSendSocketLinger());
102 
103             params.setTcpNoDelay(isSendTcpNoDelay());
104             params.setMaxTotalConnections(getDispatcherThreadingProfile().getMaxThreadsActive());
105             params.setDefaultMaxConnectionsPerHost(getDispatcherThreadingProfile().getMaxThreadsActive());
106             
107             clientConnectionManager.setParams(params);
108         }
109     }
110 
111     /**
112      * @see UMOConnector#registerListener(UMOComponent, UMOEndpoint)
113      */
114     public UMOMessageReceiver registerListener(UMOComponent component, UMOEndpoint endpoint) throws Exception
115     {
116         if (endpoint != null)
117         {
118             Map endpointProperties = endpoint.getProperties();
119             if (endpointProperties != null)
120             {
121                 // normalize properties for HTTP
122                 Map newProperties = new HashMap(endpointProperties.size());
123                 for (Iterator entries = endpointProperties.entrySet().iterator(); entries.hasNext();)
124                 {
125                     Map.Entry entry = (Map.Entry)entries.next();
126                     Object key = entry.getKey();
127                     Object normalizedKey = HttpConstants.ALL_HEADER_NAMES.get(key);
128                     if (normalizedKey != null)
129                     {
130                         // normalized property exists
131                         key = normalizedKey;
132                     }
133                     newProperties.put(key, entry.getValue());
134                 }
135                 // set normalized properties back on the endpoint
136                 endpoint.setProperties(newProperties);
137             }
138         }
139         // proceed as usual
140         return super.registerListener(component, endpoint);
141     }
142 
143     /**
144      * The method determines the key used to store the receiver against.
145      * 
146      * @param component the component for which the endpoint is being registered
147      * @param endpoint the endpoint being registered for the component
148      * @return the key to store the newly created receiver against
149      */
150     protected Object getReceiverKey(UMOComponent component, UMOEndpoint endpoint)
151     {
152         String key = endpoint.getEndpointURI().toString();
153         int i = key.indexOf('?');
154         if (i > -1)
155         {
156             key = key.substring(0, i);
157         }
158         return key;
159     }
160 
161     /**
162      * @see org.mule.umo.provider.UMOConnector#getProtocol()
163      */
164     public String getProtocol()
165     {
166         return "http";
167     }
168 
169     /**
170      * @return
171      */
172     public String getProxyHostname()
173     {
174         return proxyHostname;
175     }
176 
177     /**
178      * @return
179      */
180     public String getProxyPassword()
181     {
182         return proxyPassword;
183     }
184 
185     /**
186      * @return
187      */
188     public int getProxyPort()
189     {
190         return proxyPort;
191     }
192 
193     /**
194      * @return
195      */
196     public String getProxyUsername()
197     {
198         return proxyUsername;
199     }
200 
201     /**
202      * @param host
203      */
204     public void setProxyHostname(String host)
205     {
206         proxyHostname = host;
207     }
208 
209     /**
210      * @param string
211      */
212     public void setProxyPassword(String string)
213     {
214         proxyPassword = string;
215     }
216 
217     /**
218      * @param port
219      */
220     public void setProxyPort(int port)
221     {
222         proxyPort = port;
223     }
224 
225     /**
226      * @param string
227      */
228     public void setProxyUsername(String string)
229     {
230         proxyUsername = string;
231     }
232 
233     public Map getReceivers()
234     {
235         return this.receivers;
236     }
237 
238     public String getCookieSpec()
239     {
240         return cookieSpec;
241     }
242 
243     public void setCookieSpec(String cookieSpec)
244     {
245         if (!(cookieSpec.equalsIgnoreCase(COOKIE_SPEC_NETSCAPE) && cookieSpec.equalsIgnoreCase(COOKIE_SPEC_RFC2109)))
246         {
247             throw new IllegalArgumentException(
248                 CoreMessages.propertyHasInvalidValue("cookieSpec", cookieSpec).toString());
249         }
250         this.cookieSpec = cookieSpec;
251     }
252 
253     public boolean isEnableCookies()
254     {
255         return enableCookies;
256     }
257 
258     public void setEnableCookies(boolean enableCookies)
259     {
260         this.enableCookies = enableCookies;
261     }
262 
263 
264     public HttpConnectionManager getClientConnectionManager()
265     {
266         return clientConnectionManager;
267     }
268 
269     public void setClientConnectionManager(HttpConnectionManager clientConnectionManager)
270     {
271         this.clientConnectionManager = clientConnectionManager;
272     }
273 }