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)
99 {
100 params.setSendBufferSize(getSendBufferSize());
101 }
102 if (getReceiveBufferSize() != INT_VALUE_NOT_SET)
103 {
104 params.setReceiveBufferSize(getReceiveBufferSize());
105 }
106 if (getSendTimeout() != INT_VALUE_NOT_SET)
107 {
108 params.setSoTimeout(getSendTimeout());
109 }
110 if (getSendSocketLinger() != INT_VALUE_NOT_SET)
111 {
112 params.setLinger(getSendSocketLinger());
113 }
114
115 params.setTcpNoDelay(isSendTcpNoDelay());
116 params.setMaxTotalConnections(getDispatcherThreadingProfile().getMaxThreadsActive());
117 params.setDefaultMaxConnectionsPerHost(getDispatcherThreadingProfile().getMaxThreadsActive());
118
119 clientConnectionManager.setParams(params);
120 }
121 }
122
123
124
125
126 public UMOMessageReceiver registerListener(UMOComponent component, UMOEndpoint endpoint) throws Exception
127 {
128 if (endpoint != null)
129 {
130 Map endpointProperties = endpoint.getProperties();
131 if (endpointProperties != null)
132 {
133
134 Map newProperties = new HashMap(endpointProperties.size());
135 for (Iterator entries = endpointProperties.entrySet().iterator(); entries.hasNext();)
136 {
137 Map.Entry entry = (Map.Entry) entries.next();
138 Object key = entry.getKey();
139 Object normalizedKey = HttpConstants.ALL_HEADER_NAMES.get(key);
140 if (normalizedKey != null)
141 {
142
143 key = normalizedKey;
144 }
145 newProperties.put(key, entry.getValue());
146 }
147
148 endpoint.setProperties(newProperties);
149 }
150 }
151
152 return super.registerListener(component, endpoint);
153 }
154
155
156
157
158
159
160
161
162 protected Object getReceiverKey(UMOComponent component, UMOEndpoint endpoint)
163 {
164 String key = endpoint.getEndpointURI().toString();
165 int i = key.indexOf('?');
166 if (i > -1)
167 {
168 key = key.substring(0, i);
169 }
170 return key;
171 }
172
173
174
175
176 public String getProtocol()
177 {
178 return "http";
179 }
180
181
182
183
184 public String getProxyHostname()
185 {
186 return proxyHostname;
187 }
188
189
190
191
192 public String getProxyPassword()
193 {
194 return proxyPassword;
195 }
196
197
198
199
200 public int getProxyPort()
201 {
202 return proxyPort;
203 }
204
205
206
207
208 public String getProxyUsername()
209 {
210 return proxyUsername;
211 }
212
213
214
215
216 public void setProxyHostname(String host)
217 {
218 proxyHostname = host;
219 }
220
221
222
223
224 public void setProxyPassword(String string)
225 {
226 proxyPassword = string;
227 }
228
229
230
231
232 public void setProxyPort(int port)
233 {
234 proxyPort = port;
235 }
236
237
238
239
240 public void setProxyUsername(String string)
241 {
242 proxyUsername = string;
243 }
244
245 public Map getReceivers()
246 {
247 return this.receivers;
248 }
249
250 public String getCookieSpec()
251 {
252 return cookieSpec;
253 }
254
255 public void setCookieSpec(String cookieSpec)
256 {
257 if (!(COOKIE_SPEC_NETSCAPE.equalsIgnoreCase(cookieSpec) || COOKIE_SPEC_RFC2109.equalsIgnoreCase(cookieSpec)))
258 {
259 throw new IllegalArgumentException(
260 CoreMessages.propertyHasInvalidValue("cookieSpec", cookieSpec).toString());
261 }
262 this.cookieSpec = cookieSpec;
263 }
264
265 public boolean isEnableCookies()
266 {
267 return enableCookies;
268 }
269
270 public void setEnableCookies(boolean enableCookies)
271 {
272 this.enableCookies = enableCookies;
273 }
274
275
276 public HttpConnectionManager getClientConnectionManager()
277 {
278 return clientConnectionManager;
279 }
280
281 public void setClientConnectionManager(HttpConnectionManager clientConnectionManager)
282 {
283 this.clientConnectionManager = clientConnectionManager;
284 }
285 }