1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springbyexample.httpclient;
18
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.Map;
22
23 import org.apache.commons.httpclient.HttpClient;
24 import org.apache.commons.httpclient.HttpConnectionManager;
25 import org.apache.commons.httpclient.HttpMethod;
26 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
27 import org.apache.commons.httpclient.NameValuePair;
28 import org.apache.commons.httpclient.UsernamePasswordCredentials;
29 import org.apache.commons.httpclient.auth.AuthScope;
30 import org.apache.commons.httpclient.methods.GetMethod;
31 import org.apache.commons.httpclient.methods.PostMethod;
32 import org.springbyexample.httpclient.auth.Credentials;
33 import org.springbyexample.httpclient.auth.NTCredentials;
34 import org.springframework.beans.factory.DisposableBean;
35 import org.springframework.beans.factory.InitializingBean;
36 import org.springframework.util.Assert;
37
38
39
40
41
42
43 public abstract class AbstractHttpClientTemplate<T> implements InitializingBean, DisposableBean {
44
45 protected HttpClient client = null;
46 protected HttpConnectionManager connectionManager = null;
47 protected String defaultUri = null;
48 protected boolean authenticationPreemptive = false;
49 protected List<Credentials> lCredentials = new ArrayList<Credentials>();
50
51
52
53
54 public AbstractHttpClientTemplate() {}
55
56
57
58
59
60
61 public AbstractHttpClientTemplate(String defaultUri) {
62 this(defaultUri, false);
63 }
64
65
66
67
68
69
70
71
72 public AbstractHttpClientTemplate(String defaultUri, boolean init) {
73 this.defaultUri = defaultUri;
74
75 if (init) {
76 try {
77 afterPropertiesSet();
78 } catch(Exception e) {
79 throw new HttpAccessException(e.getMessage(), e);
80 }
81 }
82 }
83
84
85
86
87 public HttpClient getClient() {
88 return client;
89 }
90
91
92
93
94 public void setClient(HttpClient client) {
95 this.client = client;
96 }
97
98
99
100
101 public HttpConnectionManager getConnectionManager() {
102 return connectionManager;
103 }
104
105
106
107
108 public void setConnectionManager(HttpConnectionManager connectionManager) {
109 this.connectionManager = connectionManager;
110 }
111
112
113
114
115 public String getDefaultUri() {
116 return defaultUri;
117 }
118
119
120
121
122 public void setDefaultUri(String defaultUri) {
123 this.defaultUri = defaultUri;
124 }
125
126
127
128
129
130
131
132
133 public boolean isAuthenticationPreemptive() {
134 return authenticationPreemptive;
135 }
136
137
138
139
140
141
142
143
144 public void setAuthenticationPreemptive(boolean authenticationPreemptive) {
145 this.authenticationPreemptive = authenticationPreemptive;
146 }
147
148
149
150
151 public List<Credentials> getCredentials() {
152 return lCredentials;
153 }
154
155
156
157
158 public void setCredentials(List<Credentials> credentials) {
159 this.lCredentials = credentials;
160 }
161
162
163
164
165
166
167
168 public void afterPropertiesSet() throws Exception {
169
170 for (Credentials credentials : lCredentials) {
171 Assert.notNull(credentials.getAuthScopeHost());
172 Assert.isTrue(credentials.getAuthScopePort() > 0);
173 Assert.notNull(credentials.getUserName());
174 Assert.notNull(credentials.getPassword());
175
176 if (credentials instanceof NTCredentials) {
177 Assert.notNull(((NTCredentials)credentials).getHost());
178 Assert.notNull(((NTCredentials)credentials).getDomain());
179 }
180 }
181
182 if (client == null) {
183 if (connectionManager == null) {
184 connectionManager = new MultiThreadedHttpConnectionManager();
185 }
186
187 client = new HttpClient(connectionManager);
188 }
189
190 client.getParams().setAuthenticationPreemptive(authenticationPreemptive);
191
192 for (Credentials credentials : lCredentials) {
193 AuthScope authScope = new AuthScope(credentials.getAuthScopeHost(),
194 credentials.getAuthScopePort(),
195 AuthScope.ANY_REALM);
196
197 org.apache.commons.httpclient.Credentials httpCredentials = null;
198
199 if (credentials instanceof NTCredentials) {
200 httpCredentials = new org.apache.commons.httpclient.NTCredentials(
201 credentials.getUserName(),
202 credentials.getPassword(),
203 ((NTCredentials)credentials).getHost(),
204 ((NTCredentials)credentials).getDomain());
205 } else {
206 httpCredentials = new UsernamePasswordCredentials(credentials.getUserName(),
207 credentials.getPassword());
208 }
209
210 client.getState().setCredentials(authScope, httpCredentials);
211 }
212 }
213
214
215
216
217
218
219 public void destroy() throws Exception {
220 if (client.getHttpConnectionManager() instanceof MultiThreadedHttpConnectionManager) {
221 ((MultiThreadedHttpConnectionManager)client.getHttpConnectionManager()).shutdown();
222 }
223 }
224
225
226
227
228 public void executeGetMethod() {
229 executeGetMethod(defaultUri, null, null);
230 }
231
232
233
234
235
236
237 public void executeGetMethod(ResponseCallback<?> callback) {
238 executeGetMethod(defaultUri, null, callback);
239 }
240
241
242
243
244
245
246 public void executeGetMethod(Map<String, String> hParams) {
247 executeGetMethod(defaultUri, hParams, null);
248 }
249
250
251
252
253
254
255
256 public void executeGetMethod(Map<String, String> hParams, ResponseCallback<?> callback) {
257 executeGetMethod(defaultUri, hParams, callback);
258 }
259
260
261
262
263
264
265
266
267 public void executeGetMethod(String uri, Map<String, String> hParams) {
268 executeGetMethod(uri, hParams, null);
269 }
270
271
272
273
274
275
276
277
278
279 public void executeGetMethod(String uri, Map<String, String> hParams, ResponseCallback<?> callback) {
280 GetMethod get = new GetMethod(uri);
281
282 processHttpMethodParams(get, hParams);
283
284 processHttpMethod(get, callback);
285 }
286
287
288
289
290 public void executePostMethod() {
291 executePostMethod(defaultUri, null, null, null);
292 }
293
294
295
296
297
298
299 public void executePostMethod(ResponseCallback<?> callback) {
300 executePostMethod(defaultUri, null, null, callback);
301 }
302
303
304
305
306
307
308 public void executePostMethod(Map<String, String> hParams) {
309 executePostMethod(defaultUri, null, hParams, null);
310 }
311
312
313
314
315
316
317
318 public void executePostMethod(Map<String, String> hParams, ResponseCallback<?> callback) {
319 executePostMethod(defaultUri, null, hParams, callback);
320 }
321
322
323
324
325
326
327
328
329
330
331 public void executePostMethod(String uri,
332 T requestPayload, Map<String, String> hParams) {
333 executePostMethod(uri, requestPayload, hParams, null);
334 }
335
336
337
338
339
340
341
342
343
344
345
346 public abstract void executePostMethod(String uri,
347 T requestPayload, Map<String, String> hParams,
348 ResponseCallback<?> callback);
349
350
351
352
353
354
355
356
357 protected abstract void processHttpMethod(HttpMethod httpMethod, ResponseCallback<?> callback);
358
359
360
361
362
363
364
365 protected void processHttpMethodParams(HttpMethod httpMethod, Map<String, String> hParams) {
366 if (hParams != null) {
367 List<NameValuePair> lParams = new ArrayList<NameValuePair>();
368
369 for (Object key : hParams.keySet()) {
370 Object value = hParams.get(key);
371
372 lParams.add(new NameValuePair(key.toString(), value.toString()));
373 }
374
375 if (httpMethod instanceof GetMethod) {
376 ((GetMethod)httpMethod).setQueryString(lParams.toArray(new NameValuePair[] {}));
377 } else if (httpMethod instanceof PostMethod) {
378 ((PostMethod)httpMethod).setRequestBody(lParams.toArray(new NameValuePair[] {}));
379 }
380 }
381 }
382
383
384
385
386
387
388 protected void validateResponse(HttpMethod httpMethod) {
389 if (httpMethod.getStatusCode() >= 300) {
390 throw new HttpAccessException(
391 "Did not receive successful HTTP response: status code = " +
392 httpMethod.getStatusCode() +
393 ", status message = [" + httpMethod.getStatusText() + "]", httpMethod.getStatusCode());
394 }
395 }
396
397 }