2 * ============LICENSE_START=======================================================
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
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=========================================================
21 package org.openecomp.sdc.common.http.client.api;
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;
33 public class HttpClientFactory {
35 private static final Logger LOGGER = Logger.getLogger(HttpClientFactory.class.getName());
36 private static final UserTokenHandler USER_TOKEN_HANDLER = context -> null;
37 private final HttpConnectionMngFactory connectionMngFactory;
39 HttpClientFactory(HttpConnectionMngFactory connectionMngFactory) {
40 this.connectionMngFactory = connectionMngFactory;
43 HttpClient createClient(String protocol, HttpClientConfigImmutable config) {
44 LOGGER.debug("Create {} client based on {}", protocol, config);
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(USER_TOKEN_HANDLER)
53 .setRetryHandler(resolveRetryHandler(config))
56 return new HttpClient(client, config);
59 private HttpRequestRetryHandler resolveRetryHandler(HttpClientConfigImmutable config) {
60 return config.getNumOfRetries() > 0 ? config.getRetryHandler() : null;
63 private RequestConfig createClientTimeoutConfiguration(HttpClientConfigImmutable config) {
64 return RequestConfig.custom()
65 .setConnectTimeout(config.getConnectTimeoutMs())
66 .setSocketTimeout(config.getReadTimeoutMs())
67 .setConnectionRequestTimeout(config.getConnectPoolTimeoutMs())