Added oparent to sdc main
[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
21 package org.openecomp.sdc.common.http.client.api;
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.conn.HttpClientConnectionManager;
27 import org.apache.http.impl.client.CloseableHttpClient;
28 import org.apache.http.impl.client.HttpClients;
29 import org.openecomp.sdc.common.api.Constants;
30 import org.openecomp.sdc.common.http.config.ClientCertificate;
31 import org.openecomp.sdc.common.log.wrappers.Logger;
32
33 public class HttpClientFactory {
34
35     private static final Logger logger = Logger.getLogger(HttpClientFactory.class.getName());
36     private static final UserTokenHandler userTokenHandler = context -> null;
37     private final HttpConnectionMngFactory connectionMngFactory;
38     
39     HttpClientFactory(HttpConnectionMngFactory connectionMngFactory) {
40         this.connectionMngFactory = connectionMngFactory;
41     }
42
43     HttpClient createClient(String protocol, HttpClientConfigImmutable config) {
44         logger.debug("Create {} client based on {}", protocol, config);
45
46         ClientCertificate clientCertificate = Constants.HTTPS.equals(protocol) ? config.getClientCertificate() : null; 
47         HttpClientConnectionManager connectionManager = connectionMngFactory.getOrCreate(clientCertificate);
48         RequestConfig requestConfig = createClientTimeoutConfiguration(config);
49         CloseableHttpClient client = HttpClients.custom()
50                     .setDefaultRequestConfig(requestConfig)
51                     .setConnectionManager(connectionManager)
52                     .setUserTokenHandler(userTokenHandler)
53                     .setRetryHandler(resolveRetryHandler(config))
54                     .build();
55
56         return new HttpClient(client, config);
57     }
58
59     private HttpRequestRetryHandler resolveRetryHandler(HttpClientConfigImmutable config) {
60         return config.getNumOfRetries() > 0 ? config.getRetryHandler() : null;
61     }
62         
63     private RequestConfig createClientTimeoutConfiguration(HttpClientConfigImmutable config) {
64         return RequestConfig.custom()
65                 .setConnectTimeout(config.getConnectTimeoutMs())
66                 .setSocketTimeout(config.getReadTimeoutMs())
67                 .setConnectionRequestTimeout(config.getConnectPoolTimeoutMs())
68                 .build();
69     }
70 }