1
2
3
4
5
6
7
8
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public class HttpConnector extends TcpConnector
48 {
49
50
51
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
61
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
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
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
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
131 key = normalizedKey;
132 }
133 newProperties.put(key, entry.getValue());
134 }
135
136 endpoint.setProperties(newProperties);
137 }
138 }
139
140 return super.registerListener(component, endpoint);
141 }
142
143
144
145
146
147
148
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
163
164 public String getProtocol()
165 {
166 return "http";
167 }
168
169
170
171
172 public String getProxyHostname()
173 {
174 return proxyHostname;
175 }
176
177
178
179
180 public String getProxyPassword()
181 {
182 return proxyPassword;
183 }
184
185
186
187
188 public int getProxyPort()
189 {
190 return proxyPort;
191 }
192
193
194
195
196 public String getProxyUsername()
197 {
198 return proxyUsername;
199 }
200
201
202
203
204 public void setProxyHostname(String host)
205 {
206 proxyHostname = host;
207 }
208
209
210
211
212 public void setProxyPassword(String string)
213 {
214 proxyPassword = string;
215 }
216
217
218
219
220 public void setProxyPort(int port)
221 {
222 proxyPort = port;
223 }
224
225
226
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 }