1 package org.apache.commons.jcs3.auxiliary.remote.http.client;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
37
38
39
40 public abstract class AbstractHttpClient
41 {
42
43 private final HttpClient httpClient;
44
45
46 private final HttpVersion httpVersion;
47
48
49 private final RemoteHttpCacheAttributes remoteHttpCacheAttributes;
50
51
52 private static final Log log = LogManager.getLog( AbstractHttpClient.class );
53
54
55
56
57
58
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
87
88
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
102 .setCookieSpec(CookieSpecs.IGNORE_COOKIES)
103 .build());
104 }
105
106
107
108
109
110
111
112
113
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
128
129
130
131
132
133 protected abstract void preProcessWebserviceCall( RequestBuilder requestBuilder )
134 throws IOException;
135
136
137
138
139
140
141
142
143
144 protected abstract void postProcessWebserviceCall( HttpUriRequest request, HttpResponse httpState )
145 throws IOException;
146
147
148
149
150 protected RemoteHttpCacheAttributes getRemoteHttpCacheAttributes()
151 {
152 return remoteHttpCacheAttributes;
153 }
154 }