176f05a25c1a5eab0592f4804ac7999580066b20
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / config / HttpClientFactory.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.openecomp.sdc.config;
21
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;
41
42 public class HttpClientFactory {
43
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 Logger log = Logger.getLogger(HttpClientFactory.class);
49     private static final UserTokenHandler userTokenHandler = context -> null;
50
51     private HttpClientConnectionManager createConnectionManager() {
52         SSLConnectionSocketFactory sslsf = getSslConnectionSocketFactory();
53         Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
54             .register(Constants.HTTP, PlainConnectionSocketFactory.getSocketFactory()).register(Constants.HTTPS, sslsf).build();
55         PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager(registry);
56         manager.setMaxTotal(DEFAULT_CONNECTION_POOL_SIZE);
57         manager.setDefaultMaxPerRoute(DEFAULT_MAX_CONNECTION_PER_ROUTE);
58         manager.setValidateAfterInactivity(VALIDATE_CONNECTION_AFTER_INACTIVITY_MS);
59         SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(CONNECT_TIMEOUT_MS).build();
60         manager.setDefaultSocketConfig(socketConfig);
61         return manager;
62     }
63
64     private SSLConnectionSocketFactory getSslConnectionSocketFactory() {
65         return new SSLConnectionSocketFactory(SSLContexts.createSystemDefault());
66     }
67
68     /*
69     The difference between this client factory and the one in common api,
70     is that this one returns an apache httpclient instance, rather than a custom created custom.
71     */
72     public CloseableHttpClient createHttpClient() {
73         int connectTimeoutMs = 5000;
74         int readTimeoutMs = 10000;
75         HttpClientConnectionManager connManager = createConnectionManager();
76         HttpClientConfig httpClientConfig = new HttpClientConfig(new Timeouts(connectTimeoutMs, readTimeoutMs));
77         HttpClientConfigImmutable immutableHttpClientConfig = new HttpClientConfigImmutable(httpClientConfig);
78         RequestConfig requestConfig = createClientTimeoutConfiguration(immutableHttpClientConfig);
79         return HttpClients.custom().setConnectionManager(connManager).setDefaultRequestConfig(requestConfig).setUserTokenHandler(userTokenHandler)
80             .setRetryHandler(resolveRetryHandler(immutableHttpClientConfig)).build();
81     }
82
83     private RequestConfig createClientTimeoutConfiguration(HttpClientConfigImmutable config) {
84         return RequestConfig.custom().setConnectTimeout(config.getConnectTimeoutMs()).setSocketTimeout(config.getReadTimeoutMs())
85             .setConnectionRequestTimeout(config.getConnectPoolTimeoutMs()).build();
86     }
87
88     private HttpRequestRetryHandler resolveRetryHandler(HttpClientConfigImmutable config) {
89         return config.getNumOfRetries() > 0 ? config.getRetryHandler() : null;
90     }
91 }