View Javadoc
1   package org.apache.commons.jcs.auxiliary.remote.http.client;
2   
3   import java.io.IOException;
4   
5   /*
6    * Licensed to the Apache Software Foundation (ASF) under one
7    * or more contributor license agreements.  See the NOTICE file
8    * distributed with this work for additional information
9    * regarding copyright ownership.  The ASF licenses this file
10   * to you under the Apache License, Version 2.0 (the
11   * "License"); you may not use this file except in compliance
12   * with the License.  You may obtain a copy of the License at
13   *
14   *   http://www.apache.org/licenses/LICENSE-2.0
15   *
16   * Unless required by applicable law or agreed to in writing,
17   * software distributed under the License is distributed on an
18   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19   * KIND, either express or implied.  See the License for the
20   * specific language governing permissions and limitations
21   * under the License.
22   */
23  
24  import org.apache.commons.httpclient.HttpClient;
25  import org.apache.commons.httpclient.HttpMethod;
26  import org.apache.commons.httpclient.HttpState;
27  import org.apache.commons.httpclient.HttpVersion;
28  import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
29  import org.apache.commons.httpclient.cookie.CookiePolicy;
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  
33  /**
34   * This class simply configures the http multithreaded connection manager.
35   * <p>
36   * This is abstract because it can do anything. Child classes can overwrite whatever they want.
37   */
38  public abstract class AbstractHttpClient
39  {
40      /** The connection manager. */
41      private MultiThreadedHttpConnectionManager connectionManager;
42  
43      /** The client */
44      private HttpClient httpClient;
45  
46      /** Configuration settings. */
47      private RemoteHttpCacheAttributes remoteHttpCacheAttributes;
48  
49      /** The Logger. */
50      private static final Log log = LogFactory.getLog( AbstractHttpClient.class );
51  
52      /**
53       * Sets the default Properties File and Heading, and creates the HttpClient and connection
54       * manager.
55       * <p>
56       * @param remoteHttpCacheAttributes
57       */
58      public AbstractHttpClient( RemoteHttpCacheAttributes remoteHttpCacheAttributes )
59      {
60          this.remoteHttpCacheAttributes = remoteHttpCacheAttributes;
61          this.connectionManager = new MultiThreadedHttpConnectionManager();
62          this.httpClient = new HttpClient(this.connectionManager);
63  
64          configureClient();
65      }
66  
67      /**
68       * Configures the http client.
69       */
70      protected void configureClient()
71      {
72          if ( getRemoteHttpCacheAttributes().getMaxConnectionsPerHost() > 0 )
73          {
74              this.connectionManager.getParams()
75                  .setMaxTotalConnections(getRemoteHttpCacheAttributes().getMaxConnectionsPerHost());
76              this.connectionManager.getParams()
77                  .setDefaultMaxConnectionsPerHost(getRemoteHttpCacheAttributes().getMaxConnectionsPerHost());
78          }
79  
80          this.connectionManager.getParams().setSoTimeout( getRemoteHttpCacheAttributes().getSocketTimeoutMillis() );
81  
82          String httpVersion = getRemoteHttpCacheAttributes().getHttpVersion();
83          if ( httpVersion != null )
84          {
85              if ( "1.1".equals( httpVersion ) )
86              {
87                  this.httpClient.getParams().setParameter( "http.protocol.version", HttpVersion.HTTP_1_1 );
88              }
89              else if ( "1.0".equals( httpVersion ) )
90              {
91                  this.httpClient.getParams().setParameter( "http.protocol.version", HttpVersion.HTTP_1_0 );
92              }
93              else
94              {
95                  log.warn( "Unrecognized value for 'httpVersion': [" + httpVersion + "]" );
96              }
97          }
98  
99          this.connectionManager.getParams()
100             .setConnectionTimeout(getRemoteHttpCacheAttributes().getConnectionTimeoutMillis());
101 
102         // By default we instruct HttpClient to ignore cookies.
103         this.httpClient.getParams().setCookiePolicy( CookiePolicy.IGNORE_COOKIES );
104     }
105 
106     /**
107      * Extracted method that can be overwritten to do additional things to the post before the call
108      * is made.
109      * <p>
110      * @param post the post that is about to get executed.
111      * @throws IOException on i/o error
112      */
113     protected final void doWebserviceCall( HttpMethod post )
114         throws IOException
115     {
116         HttpState httpState = preProcessWebserviceCall( post );
117         this.httpClient.executeMethod( null, post, httpState );
118         postProcessWebserviceCall( post, httpState );
119     }
120 
121     /**
122      * Called before the executeMethod on the client.
123      * <p>
124      * @param post http method
125      * @return HttpState
126      * @throws IOException
127      */
128     protected abstract HttpState preProcessWebserviceCall( HttpMethod post )
129         throws IOException;
130 
131     /**
132      * Called after the executeMethod on the client.
133      * <p>
134      * @param post http method
135      * @param httpState state
136      * @throws IOException
137      */
138     protected abstract void postProcessWebserviceCall( HttpMethod post, HttpState httpState )
139         throws IOException;
140 
141     /**
142      * @return the remoteHttpCacheAttributes
143      */
144     protected RemoteHttpCacheAttributes getRemoteHttpCacheAttributes()
145     {
146         return remoteHttpCacheAttributes;
147     }
148 }