Revert "Support SIP TLS"
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / common / http / client / api / HttpClientFactory.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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.common.http.client.api;
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.conn.HttpClientConnectionManager;
26 import org.apache.http.impl.client.CloseableHttpClient;
27 import org.apache.http.impl.client.HttpClients;
28 import org.openecomp.sdc.common.api.Constants;
29 import org.openecomp.sdc.common.http.config.ClientCertificate;
30 import org.openecomp.sdc.common.log.wrappers.Logger;
31
32 public class HttpClientFactory {
33
34     private static final Logger logger = Logger.getLogger(HttpClientFactory.class.getName());
35     private static final UserTokenHandler userTokenHandler = context -> null;
36     private final HttpConnectionMngFactory connectionMngFactory;
37
38     HttpClientFactory(HttpConnectionMngFactory connectionMngFactory) {
39         this.connectionMngFactory = connectionMngFactory;
40     }
41
42     HttpClient createClient(String protocol, HttpClientConfigImmutable config) {
43         logger.debug("Create {} client based on {}", protocol, config);
44         ClientCertificate clientCertificate = Constants.HTTPS.equals(protocol) ? config.getClientCertificate() : null;
45         HttpClientConnectionManager connectionManager = connectionMngFactory.getOrCreate(clientCertificate);
46         RequestConfig requestConfig = createClientTimeoutConfiguration(config);
47         CloseableHttpClient client = HttpClients.custom().setDefaultRequestConfig(requestConfig).setConnectionManager(connectionManager)
48             .setUserTokenHandler(userTokenHandler).setRetryHandler(resolveRetryHandler(config)).build();
49         return new HttpClient(client, config);
50     }
51
52     private HttpRequestRetryHandler resolveRetryHandler(HttpClientConfigImmutable config) {
53         return config.getNumOfRetries() > 0 ? config.getRetryHandler() : null;
54     }
55
56     private RequestConfig createClientTimeoutConfiguration(HttpClientConfigImmutable config) {
57         return RequestConfig.custom().setConnectTimeout(config.getConnectTimeoutMs()).setSocketTimeout(config.getReadTimeoutMs())
58             .setConnectionRequestTimeout(config.getConnectPoolTimeoutMs()).build();
59     }
60 }