2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.openecomp.sdc.config;
22 import org.apache.http.client.HttpRequestRetryHandler;
23 import org.apache.http.client.UserTokenHandler;
24 import org.apache.http.client.config.RequestConfig;
25 import org.apache.http.config.Registry;
26 import org.apache.http.config.RegistryBuilder;
27 import org.apache.http.config.SocketConfig;
28 import org.apache.http.conn.HttpClientConnectionManager;
29 import org.apache.http.conn.socket.ConnectionSocketFactory;
30 import org.apache.http.conn.socket.PlainConnectionSocketFactory;
31 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
32 import org.apache.http.impl.client.CloseableHttpClient;
33 import org.apache.http.impl.client.HttpClients;
34 import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
35 import org.apache.http.ssl.SSLContexts;
36 import org.openecomp.sdc.common.api.Constants;
37 import org.openecomp.sdc.common.http.client.api.HttpClientConfigImmutable;
38 import org.openecomp.sdc.common.http.config.HttpClientConfig;
39 import org.openecomp.sdc.common.http.config.Timeouts;
40 import org.openecomp.sdc.common.log.wrappers.Logger;
42 public class HttpClientFactory {
44 private static final int DEFAULT_CONNECTION_POOL_SIZE = 30;
45 private static final int DEFAULT_MAX_CONNECTION_PER_ROUTE = 5;
46 private static final int VALIDATE_CONNECTION_AFTER_INACTIVITY_MS = 10000;
47 private static final int CONNECT_TIMEOUT_MS = 15000;
48 private static final UserTokenHandler userTokenHandler = context -> null;
50 private HttpClientConnectionManager createConnectionManager() {
51 SSLConnectionSocketFactory sslsf = getSslConnectionSocketFactory();
52 Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
53 .register(Constants.HTTP, PlainConnectionSocketFactory.getSocketFactory()).register(Constants.HTTPS, sslsf).build();
54 PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager(registry);
55 manager.setMaxTotal(DEFAULT_CONNECTION_POOL_SIZE);
56 manager.setDefaultMaxPerRoute(DEFAULT_MAX_CONNECTION_PER_ROUTE);
57 manager.setValidateAfterInactivity(VALIDATE_CONNECTION_AFTER_INACTIVITY_MS);
58 SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(CONNECT_TIMEOUT_MS).build();
59 manager.setDefaultSocketConfig(socketConfig);
63 private SSLConnectionSocketFactory getSslConnectionSocketFactory() {
64 return new SSLConnectionSocketFactory(SSLContexts.createSystemDefault());
68 The difference between this client factory and the one in common api,
69 is that this one returns an apache httpclient instance, rather than a custom created custom.
71 public CloseableHttpClient createHttpClient() {
72 int connectTimeoutMs = 5000;
73 int readTimeoutMs = 10000;
74 HttpClientConnectionManager connManager = createConnectionManager();
75 HttpClientConfig httpClientConfig = new HttpClientConfig(new Timeouts(connectTimeoutMs, readTimeoutMs));
76 HttpClientConfigImmutable immutableHttpClientConfig = new HttpClientConfigImmutable(httpClientConfig);
77 RequestConfig requestConfig = createClientTimeoutConfiguration(immutableHttpClientConfig);
78 return HttpClients.custom().setConnectionManager(connManager).setDefaultRequestConfig(requestConfig).setUserTokenHandler(userTokenHandler)
79 .setRetryHandler(resolveRetryHandler(immutableHttpClientConfig)).build();
82 private RequestConfig createClientTimeoutConfiguration(HttpClientConfigImmutable config) {
83 return RequestConfig.custom().setConnectTimeout(config.getConnectTimeoutMs()).setSocketTimeout(config.getReadTimeoutMs())
84 .setConnectionRequestTimeout(config.getConnectPoolTimeoutMs()).build();
87 private HttpRequestRetryHandler resolveRetryHandler(HttpClientConfigImmutable config) {
88 return config.getNumOfRetries() > 0 ? config.getRetryHandler() : null;