1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.portletbridge.portlet;
17
18 import org.apache.commons.httpclient.HostConfiguration;
19 import org.apache.commons.httpclient.HttpClient;
20 import org.apache.commons.httpclient.HttpMethodBase;
21 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
22 import org.portletbridge.ResourceException;
23
24 /***
25 * Default implementation of the httpclient template. Makes a call
26 * to the url specifies and then passes the result to the callback.
27 *
28 * @author JMcCrindle
29 */
30 public class DefaultHttpClientTemplate implements HttpClientTemplate {
31
32 private HttpClient httpClient = null;
33
34 /***
35 *
36 */
37 public DefaultHttpClientTemplate() {
38 httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
39 }
40
41 public Object service(HttpMethodBase method, HttpClientState state, HttpClientCallback callback) throws ResourceException {
42 try {
43 HostConfiguration hostConfiguration = new HostConfiguration();
44
45 if(state.getProxyHost() != null && state.getProxyHost().trim().length() > 0) {
46 hostConfiguration.setProxy(state.getProxyHost(), state.getProxyPort());
47 }
48 hostConfiguration.setHost(method.getURI());
49 int statusCode = httpClient.executeMethod(hostConfiguration, method, state.getHttpState());
50 return callback.doInHttpClient(statusCode, method);
51 } catch (ResourceException e) {
52 throw e;
53 } catch (Throwable e) {
54 throw new ResourceException("error.httpclient", e.getMessage(), e);
55 } finally {
56 method.releaseConnection();
57 }
58 }
59
60 }