20
20
21
21
import lombok .*;
22
22
import org .apache .http .*;
23
- import org .apache .http .client .*;
24
23
import org .apache .http .client .config .*;
25
24
import org .apache .http .config .*;
25
+ import org .apache .http .conn .socket .*;
26
+ import org .apache .http .conn .ssl .*;
27
+ import org .apache .http .conn .util .*;
26
28
import org .apache .http .impl .client .*;
29
+ import org .apache .http .impl .conn .*;
27
30
import org .apache .http .impl .nio .client .*;
31
+ import org .apache .http .impl .nio .conn .*;
28
32
import org .apache .http .impl .nio .reactor .*;
33
+ import org .apache .http .nio .conn .*;
34
+ import org .apache .http .nio .conn .ssl .*;
35
+ import org .apache .http .ssl .SSLContexts ;
29
36
import org .zalando .logbook .httpclient .*;
30
- import ru .art .http .client .constants .*;
31
37
import ru .art .http .client .exception .*;
32
38
import ru .art .http .client .interceptor .*;
33
39
import ru .art .http .client .model .*;
@@ -58,6 +64,8 @@ public interface HttpClientModuleConfiguration extends HttpModuleConfiguration {
58
64
59
65
int getMaxConnectionsTotal ();
60
66
67
+ int getValidateAfterInactivityMillis ();
68
+
61
69
CloseableHttpClient getClient ();
62
70
63
71
CloseableHttpAsyncClient getAsynchronousClient ();
@@ -103,8 +111,10 @@ default HttpCommunicationTargetConfiguration getCommunicationTargetConfiguration
103
111
104
112
@ Getter
105
113
class HttpClientModuleDefaultConfiguration extends HttpModuleDefaultConfiguration implements HttpClientModuleConfiguration {
114
+ private static HostnameVerifier ALLOW_ALL = (hostName , session ) -> true ;
106
115
int maxConnectionsPerRoute = DEFAULT_MAX_CONNECTIONS_PER_ROUTE ;
107
116
int maxConnectionsTotal = DEFAULT_MAX_CONNECTIONS_TOTAL ;
117
+ int validateAfterInactivityMillis = DEFAULT_VALIDATE_AFTER_INACTIVITY_MILLIS ;
108
118
private final RequestConfig requestConfig = RequestConfig .DEFAULT ;
109
119
private final SocketConfig socketConfig = SocketConfig .DEFAULT ;
110
120
private final ConnectionConfig connectionConfig = ConnectionConfig .DEFAULT ;
@@ -132,25 +142,8 @@ class HttpClientModuleDefaultConfiguration extends HttpModuleDefaultConfiguratio
132
142
@ SuppressWarnings ({"Duplicates" , "WeakerAccess" })
133
143
protected CloseableHttpAsyncClient createAsyncHttpClient () {
134
144
HttpAsyncClientBuilder clientBuilder = HttpAsyncClients .custom ()
135
- .setMaxConnPerRoute (getMaxConnectionsPerRoute ())
136
- .setMaxConnTotal (getMaxConnectionsTotal ())
137
- .setDefaultRequestConfig (getRequestConfig ())
138
- .setDefaultIOReactorConfig (getIoReactorConfig ())
139
- .setDefaultConnectionConfig (getConnectionConfig ());
140
- if (isSsl ()) {
141
- try {
142
- if (disableSslHostNameVerification ) {
143
- HostnameVerifier allowAll = (hostName , session ) -> true ;
144
- clientBuilder .setSSLHostnameVerifier (allowAll );
145
- }
146
- clientBuilder .setSSLContext (custom ()
147
- .loadKeyMaterial (loadKeyStore (), getSslKeyStorePassword ().toCharArray ())
148
- .build ());
149
- } catch (Throwable throwable ) {
150
- throw new HttpClientException (HTTP_SSL_CONFIGURATION_FAILED , throwable );
151
- }
152
- }
153
- clientBuilder .addInterceptorFirst (new LogbookHttpRequestInterceptor (getLogbook ()));
145
+ .setConnectionManager (createAsyncConnectionManager ())
146
+ .addInterceptorFirst (new LogbookHttpRequestInterceptor (getLogbook ()));
154
147
CloseableHttpAsyncClient client = httpClientModuleState ().registerClient (clientBuilder .build ());
155
148
client .start ();
156
149
return client ;
@@ -165,21 +158,74 @@ protected CloseableHttpClient createHttpClient() {
165
158
.setDefaultConnectionConfig (getConnectionConfig ())
166
159
.setDefaultSocketConfig (getSocketConfig ())
167
160
.addInterceptorFirst (new LogbookHttpRequestInterceptor (getLogbook ()))
168
- .addInterceptorLast (new LogbookHttpResponseInterceptor ());
161
+ .addInterceptorLast (new LogbookHttpResponseInterceptor ())
162
+ .setConnectionManager (createConnectionManager ());
163
+ return httpClientModuleState ().registerClient (clientBuilder .build ());
164
+ }
165
+
166
+ private PoolingHttpClientConnectionManager createConnectionManager () {
167
+ HostnameVerifier hostnameVerifier = new DefaultHostnameVerifier (PublicSuffixMatcherLoader .getDefault ());
168
+ SSLContext sslContext = null ;
169
169
if (isSsl ()) {
170
170
try {
171
- if ( disableSslHostNameVerification ) {
172
- HostnameVerifier allowAll = ( hostName , session ) -> true ;
173
- clientBuilder . setSSLHostnameVerifier ( allowAll ) ;
171
+ sslContext = custom (). loadKeyMaterial ( loadKeyStore (), getSslKeyStorePassword (). toCharArray ()). build ();
172
+ if ( isDisableSslHostNameVerification ()) {
173
+ hostnameVerifier = ALLOW_ALL ;
174
174
}
175
- clientBuilder .setSSLContext (custom ()
176
- .loadKeyMaterial (loadKeyStore (), getSslKeyStorePassword ().toCharArray ())
177
- .build ());
178
175
} catch (Throwable throwable ) {
179
176
throw new HttpClientException (HTTP_SSL_CONFIGURATION_FAILED , throwable );
180
177
}
181
178
}
182
- return httpClientModuleState ().registerClient (clientBuilder .build ());
179
+
180
+ SSLConnectionSocketFactory sslSocketFactory = isNull (sslContext )
181
+ ? new SSLConnectionSocketFactory (org .apache .http .ssl .SSLContexts .createDefault (), hostnameVerifier )
182
+ : new SSLConnectionSocketFactory (sslContext , null , null , hostnameVerifier );
183
+
184
+ Registry <ConnectionSocketFactory > registry = RegistryBuilder .<ConnectionSocketFactory >create ()
185
+ .register (HTTP_SCHEME , PlainConnectionSocketFactory .getSocketFactory ())
186
+ .register (HTTPS_SCHEME , sslSocketFactory )
187
+ .build ();
188
+ PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager (registry );
189
+ manager .setDefaultSocketConfig (getSocketConfig ());
190
+ manager .setDefaultConnectionConfig (getConnectionConfig ());
191
+ manager .setMaxTotal (getMaxConnectionsTotal ());
192
+ manager .setDefaultMaxPerRoute (getMaxConnectionsPerRoute ());
193
+ manager .setValidateAfterInactivity (getValidateAfterInactivityMillis ());
194
+ return manager ;
195
+ }
196
+
197
+ private PoolingNHttpClientConnectionManager createAsyncConnectionManager () {
198
+ HostnameVerifier hostnameVerifier = new DefaultHostnameVerifier (PublicSuffixMatcherLoader .getDefault ());
199
+ SSLContext sslContext = null ;
200
+ if (isSsl ()) {
201
+ try {
202
+ sslContext = custom ().loadKeyMaterial (loadKeyStore (), getSslKeyStorePassword ().toCharArray ()).build ();
203
+ if (isDisableSslHostNameVerification ()) {
204
+ hostnameVerifier = ALLOW_ALL ;
205
+ }
206
+ } catch (Throwable throwable ) {
207
+ throw new HttpClientException (HTTP_SSL_CONFIGURATION_FAILED , throwable );
208
+ }
209
+ }
210
+
211
+ SSLIOSessionStrategy sslSessionStrategy = isNull (sslContext )
212
+ ? new SSLIOSessionStrategy (SSLContexts .createDefault (), hostnameVerifier )
213
+ : new SSLIOSessionStrategy (sslContext , null , null , hostnameVerifier );
214
+
215
+ Registry <SchemeIOSessionStrategy > registry = RegistryBuilder .<SchemeIOSessionStrategy >create ()
216
+ .register (HTTP_SCHEME , NoopIOSessionStrategy .INSTANCE )
217
+ .register (HTTPS_SCHEME , sslSessionStrategy )
218
+ .build ();
219
+ PoolingNHttpClientConnectionManager manager = null ;
220
+ try {
221
+ manager = new PoolingNHttpClientConnectionManager (new DefaultConnectingIOReactor (getIoReactorConfig ()), registry );
222
+ } catch (Throwable throwable ) {
223
+ throw new HttpClientException (HTTP_ASYNC_CONFIGURATION_FAILED , throwable );
224
+ }
225
+ manager .setDefaultConnectionConfig (getConnectionConfig ());
226
+ manager .setMaxTotal (getMaxConnectionsTotal ());
227
+ manager .setDefaultMaxPerRoute (getMaxConnectionsPerRoute ());
228
+ return manager ;
183
229
}
184
230
185
231
private KeyStore loadKeyStore () {
0 commit comments