Debian Codegraph @

src/org/apache/commons/jcs3/auxiliary/remote/http/client/AbstractHttpClient.java

raw symbols SCIP
1 package org.apache.commons.jcs3.auxiliary.remote.http.client;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.io.IOException;
23
24 import org.apache.commons.jcs3.log.Log;
25 import org.apache.commons.jcs3.log.LogManager;
26 import org.apache.http.HttpResponse;
27 import org.apache.http.HttpVersion;
28 import org.apache.http.client.HttpClient;
29 import org.apache.http.client.config.CookieSpecs;
30 import org.apache.http.client.config.RequestConfig;
31 import org.apache.http.client.methods.HttpUriRequest;
32 import org.apache.http.client.methods.RequestBuilder;
33 import org.apache.http.impl.client.HttpClientBuilder;
34
35 /**
36 * This class simply configures the http multithreaded connection manager.
37 * <p>
38 * This is abstract because it can do anything. Child classes can overwrite whatever they want.
39 */
40 public abstract class AbstractHttpClient
41 {
42 /** The client */
43 private final HttpClient httpClient;
44
45 /** The protocol version */
46 private final HttpVersion httpVersion;
47
48 /** Configuration settings. */
49 private final RemoteHttpCacheAttributes remoteHttpCacheAttributes;
50
51 /** The Logger. */
52 private static final Log log = LogManager.getLog( AbstractHttpClient.class );
53
54 /**
55 * Sets the default Properties File and Heading, and creates the HttpClient and connection
56 * manager.
57 * <p>
58 * @param remoteHttpCacheAttributes
59 */
60 public AbstractHttpClient( final RemoteHttpCacheAttributes remoteHttpCacheAttributes )
61 {
62 this.remoteHttpCacheAttributes = remoteHttpCacheAttributes;
63
64 final String httpVersion = getRemoteHttpCacheAttributes().getHttpVersion();
65 if ( "1.1".equals( httpVersion ) )
66 {
67 this.httpVersion = HttpVersion.HTTP_1_1;
68 }
69 else if ( "1.0".equals( httpVersion ) )
70 {
71 this.httpVersion = HttpVersion.HTTP_1_0;
72 }
73 else
74 {
75 log.warn( "Unrecognized value for 'httpVersion': [{0}], defaulting to 1.1",
76 httpVersion );
77 this.httpVersion = HttpVersion.HTTP_1_1;
78 }
79
80 final HttpClientBuilder builder = HttpClientBuilder.create();
81 configureClient(builder);
82 this.httpClient = builder.build();
83 }
84
85 /**
86 * Configures the http client.
87 *
88 * @param builder client builder to configure
89 */
90 protected void configureClient(final HttpClientBuilder builder)
91 {
92 if ( getRemoteHttpCacheAttributes().getMaxConnectionsPerHost() > 0 )
93 {
94 builder.setMaxConnTotal(getRemoteHttpCacheAttributes().getMaxConnectionsPerHost());
95 builder.setMaxConnPerRoute(getRemoteHttpCacheAttributes().getMaxConnectionsPerHost());
96 }
97
98 builder.setDefaultRequestConfig(RequestConfig.custom()
99 .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 }