View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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  }