001package org.apache.commons.jcs3.auxiliary.remote.http.client;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.IOException;
023
024import org.apache.commons.jcs3.log.Log;
025import org.apache.commons.jcs3.log.LogManager;
026import org.apache.http.HttpResponse;
027import org.apache.http.HttpVersion;
028import org.apache.http.client.HttpClient;
029import org.apache.http.client.config.CookieSpecs;
030import org.apache.http.client.config.RequestConfig;
031import org.apache.http.client.methods.HttpUriRequest;
032import org.apache.http.client.methods.RequestBuilder;
033import org.apache.http.impl.client.HttpClientBuilder;
034
035/**
036 * This class simply configures the http multithreaded connection manager.
037 * <p>
038 * This is abstract because it can do anything. Child classes can overwrite whatever they want.
039 */
040public abstract class AbstractHttpClient
041{
042    /** The client */
043    private final HttpClient httpClient;
044
045    /** The protocol version */
046    private final HttpVersion httpVersion;
047
048    /** Configuration settings. */
049    private final RemoteHttpCacheAttributes remoteHttpCacheAttributes;
050
051    /** The Logger. */
052    private static final Log log = LogManager.getLog( AbstractHttpClient.class );
053
054    /**
055     * Sets the default Properties File and Heading, and creates the HttpClient and connection
056     * manager.
057     * <p>
058     * @param remoteHttpCacheAttributes
059     */
060    public AbstractHttpClient( final RemoteHttpCacheAttributes remoteHttpCacheAttributes )
061    {
062        this.remoteHttpCacheAttributes = remoteHttpCacheAttributes;
063
064        final String httpVersion = getRemoteHttpCacheAttributes().getHttpVersion();
065        if ( "1.1".equals( httpVersion ) )
066        {
067            this.httpVersion = HttpVersion.HTTP_1_1;
068        }
069        else if ( "1.0".equals( httpVersion ) )
070        {
071            this.httpVersion = HttpVersion.HTTP_1_0;
072        }
073        else
074        {
075            log.warn( "Unrecognized value for 'httpVersion': [{0}], defaulting to 1.1",
076                    httpVersion );
077            this.httpVersion = HttpVersion.HTTP_1_1;
078        }
079
080        final HttpClientBuilder builder = HttpClientBuilder.create();
081        configureClient(builder);
082        this.httpClient = builder.build();
083    }
084
085    /**
086     * Configures the http client.
087     *
088     * @param builder client builder to configure
089     */
090    protected void configureClient(final HttpClientBuilder builder)
091    {
092        if ( getRemoteHttpCacheAttributes().getMaxConnectionsPerHost() > 0 )
093        {
094            builder.setMaxConnTotal(getRemoteHttpCacheAttributes().getMaxConnectionsPerHost());
095            builder.setMaxConnPerRoute(getRemoteHttpCacheAttributes().getMaxConnectionsPerHost());
096        }
097
098        builder.setDefaultRequestConfig(RequestConfig.custom()
099                .setConnectTimeout(getRemoteHttpCacheAttributes().getConnectionTimeoutMillis())
100                .setSocketTimeout(getRemoteHttpCacheAttributes().getSocketTimeoutMillis())
101                // By default we instruct HttpClient to ignore cookies.
102                .setCookieSpec(CookieSpecs.IGNORE_COOKIES)
103                .build());
104    }
105
106    /**
107     * Execute the web service call
108     * <p>
109     * @param builder builder for the post request
110     *
111     * @return the call response
112     *
113     * @throws IOException on i/o error
114     */
115    protected final HttpResponse doWebserviceCall( final RequestBuilder builder )
116        throws IOException
117    {
118        preProcessWebserviceCall( builder.setVersion(httpVersion) );
119        final HttpUriRequest request = builder.build();
120        final HttpResponse httpResponse = this.httpClient.execute( request );
121        postProcessWebserviceCall( request, httpResponse );
122
123        return httpResponse;
124    }
125
126    /**
127     * Called before the execute call on the client.
128     * <p>
129     * @param requestBuilder http method request builder
130     *
131     * @throws IOException
132     */
133    protected abstract void preProcessWebserviceCall( RequestBuilder requestBuilder )
134        throws IOException;
135
136    /**
137     * Called after the execute call on the client.
138     * <p>
139     * @param request http request
140     * @param httpState result of execution
141     *
142     * @throws IOException
143     */
144    protected abstract void postProcessWebserviceCall( HttpUriRequest request, HttpResponse httpState )
145        throws IOException;
146
147    /**
148     * @return the remoteHttpCacheAttributes
149     */
150    protected RemoteHttpCacheAttributes getRemoteHttpCacheAttributes()
151    {
152        return remoteHttpCacheAttributes;
153    }
154}