Catalog alignment
[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
21 package org.openecomp.sdc.config;
22
23 import org.apache.http.client.HttpRequestRetryHandler;
24 import org.apache.http.client.UserTokenHandler;
25 import org.apache.http.client.config.RequestConfig;
26 import org.apache.http.config.Registry;
27 import org.apache.http.config.RegistryBuilder;
28 import org.apache.http.config.SocketConfig;
29 import org.apache.http.conn.HttpClientConnectionManager;
30 import org.apache.http.conn.socket.ConnectionSocketFactory;
31 import org.apache.http.conn.socket.PlainConnectionSocketFactory;
32 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
33 import org.apache.http.impl.client.CloseableHttpClient;
34 import org.apache.http.impl.client.HttpClients;
35 import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
36 import org.apache.http.ssl.SSLContexts;
37 import org.openecomp.sdc.common.api.Constants;
38 import org.openecomp.sdc.common.http.client.api.HttpClientConfigImmutable;
39 import org.openecomp.sdc.common.http.config.HttpClientConfig;
40 import org.openecomp.sdc.common.http.config.Timeouts;
41 import org.openecomp.sdc.common.log.wrappers.Logger;
42
43 public class HttpClientFactory {
44
45     private static final int DEFAULT_CONNECTION_POOL_SIZE = 30;
46     private static final int DEFAULT_MAX_CONNECTION_PER_ROUTE = 5;
47     private static final int VALIDATE_CONNECTION_AFTER_INACTIVITY_MS = 10000;
48     private static final int CONNECT_TIMEOUT_MS = 15000;
49
50     private static final Logger log = Logger.getLogger(HttpClientFactory.class);
51     private static final UserTokenHandler userTokenHandler = context -> null;
52
53     private HttpClientConnectionManager createConnectionManager(){
54         SSLConnectionSocketFactory sslsf = getSslConnectionSocketFactory();
55
56         Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
57                 .register(Constants.HTTP, PlainConnectionSocketFactory.getSocketFactory())
58                 .register(Constants.HTTPS, sslsf).build();
59
60         PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager(registry);
61
62         manager.setMaxTotal(DEFAULT_CONNECTION_POOL_SIZE);
63         manager.setDefaultMaxPerRoute(DEFAULT_MAX_CONNECTION_PER_ROUTE);
64         manager.setValidateAfterInactivity(VALIDATE_CONNECTION_AFTER_INACTIVITY_MS);
65
66         SocketConfig socketConfig = SocketConfig.custom()
67                 .setSoTimeout(CONNECT_TIMEOUT_MS)
68                 .build();
69         manager.setDefaultSocketConfig(socketConfig);
70
71         return manager;
72     }
73
74     private SSLConnectionSocketFactory getSslConnectionSocketFactory() {
75         return new SSLConnectionSocketFactory(SSLContexts.createSystemDefault());
76     }
77
78
79     /*
80     The difference between this client factory and the one in common api,
81     is that this one returns an apache httpclient instance, rather than a custom created custom.
82     */
83     public CloseableHttpClient createHttpClient() {
84         int connectTimeoutMs = 5000;
85         int readTimeoutMs = 10000;
86         HttpClientConnectionManager connManager = createConnectionManager();
87         HttpClientConfig httpClientConfig = new HttpClientConfig(new Timeouts(connectTimeoutMs, readTimeoutMs));
88         HttpClientConfigImmutable immutableHttpClientConfig = new HttpClientConfigImmutable(httpClientConfig);
89         RequestConfig requestConfig = createClientTimeoutConfiguration(immutableHttpClientConfig);
90         return HttpClients.custom()
91                 .setConnectionManager(connManager)
92                 .setDefaultRequestConfig(requestConfig)
93                 .setUserTokenHandler(userTokenHandler)
94                 .setRetryHandler(resolveRetryHandler(immutableHttpClientConfig))
95                 .build();
96     }
97
98     private  RequestConfig createClientTimeoutConfiguration(HttpClientConfigImmutable config) {
99         return RequestConfig.custom()
100                 .setConnectTimeout(config.getConnectTimeoutMs())
101                 .setSocketTimeout(config.getReadTimeoutMs())
102                 .setConnectionRequestTimeout(config.getConnectPoolTimeoutMs())
103                 .build();
104     }
105
106     private HttpRequestRetryHandler resolveRetryHandler(HttpClientConfigImmutable config) {
107         return config.getNumOfRetries() > 0 ? config.getRetryHandler() : null;
108     }
109
110 }