001package org.apache.commons.jcs.auxiliary.remote.http.client;
002
003import java.io.IOException;
004
005/*
006 * Licensed to the Apache Software Foundation (ASF) under one
007 * or more contributor license agreements.  See the NOTICE file
008 * distributed with this work for additional information
009 * regarding copyright ownership.  The ASF licenses this file
010 * to you under the Apache License, Version 2.0 (the
011 * "License"); you may not use this file except in compliance
012 * with the License.  You may obtain a copy of the License at
013 *
014 *   http://www.apache.org/licenses/LICENSE-2.0
015 *
016 * Unless required by applicable law or agreed to in writing,
017 * software distributed under the License is distributed on an
018 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
019 * KIND, either express or implied.  See the License for the
020 * specific language governing permissions and limitations
021 * under the License.
022 */
023
024import org.apache.commons.httpclient.HttpClient;
025import org.apache.commons.httpclient.HttpMethod;
026import org.apache.commons.httpclient.HttpState;
027import org.apache.commons.httpclient.HttpVersion;
028import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
029import org.apache.commons.httpclient.cookie.CookiePolicy;
030import org.apache.commons.logging.Log;
031import org.apache.commons.logging.LogFactory;
032
033/**
034 * This class simply configures the http multithreaded connection manager.
035 * <p>
036 * This is abstract because it can do anything. Child classes can overwrite whatever they want.
037 */
038public abstract class AbstractHttpClient
039{
040    /** The connection manager. */
041    private MultiThreadedHttpConnectionManager connectionManager;
042
043    /** The client */
044    private HttpClient httpClient;
045
046    /** Configuration settings. */
047    private RemoteHttpCacheAttributes remoteHttpCacheAttributes;
048
049    /** The Logger. */
050    private static final Log log = LogFactory.getLog( AbstractHttpClient.class );
051
052    /**
053     * Sets the default Properties File and Heading, and creates the HttpClient and connection
054     * manager.
055     * <p>
056     * @param remoteHttpCacheAttributes
057     */
058    public AbstractHttpClient( RemoteHttpCacheAttributes remoteHttpCacheAttributes )
059    {
060        this.remoteHttpCacheAttributes = remoteHttpCacheAttributes;
061        this.connectionManager = new MultiThreadedHttpConnectionManager();
062        this.httpClient = new HttpClient(this.connectionManager);
063
064        configureClient();
065    }
066
067    /**
068     * Configures the http client.
069     */
070    protected void configureClient()
071    {
072        if ( getRemoteHttpCacheAttributes().getMaxConnectionsPerHost() > 0 )
073        {
074            this.connectionManager.getParams()
075                .setMaxTotalConnections(getRemoteHttpCacheAttributes().getMaxConnectionsPerHost());
076            this.connectionManager.getParams()
077                .setDefaultMaxConnectionsPerHost(getRemoteHttpCacheAttributes().getMaxConnectionsPerHost());
078        }
079
080        this.connectionManager.getParams().setSoTimeout( getRemoteHttpCacheAttributes().getSocketTimeoutMillis() );
081
082        String httpVersion = getRemoteHttpCacheAttributes().getHttpVersion();
083        if ( httpVersion != null )
084        {
085            if ( "1.1".equals( httpVersion ) )
086            {
087                this.httpClient.getParams().setParameter( "http.protocol.version", HttpVersion.HTTP_1_1 );
088            }
089            else if ( "1.0".equals( httpVersion ) )
090            {
091                this.httpClient.getParams().setParameter( "http.protocol.version", HttpVersion.HTTP_1_0 );
092            }
093            else
094            {
095                log.warn( "Unrecognized value for 'httpVersion': [" + httpVersion + "]" );
096            }
097        }
098
099        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}