2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2021 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 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END=========================================================
20 package org.o.ran.oam.nf.oam.adopter.app.http;
23 import java.io.IOException;
24 import java.security.KeyManagementException;
25 import java.security.KeyStoreException;
26 import java.security.NoSuchAlgorithmException;
27 import java.security.cert.CertificateException;
28 import javax.net.ssl.SSLContext;
29 import lombok.AccessLevel;
30 import lombok.NoArgsConstructor;
31 import org.apache.hc.client5.http.config.RequestConfig;
32 import org.apache.hc.client5.http.cookie.StandardCookieSpec;
33 import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
34 import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
35 import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager;
36 import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder;
37 import org.apache.hc.client5.http.ssl.ClientTlsStrategyBuilder;
38 import org.apache.hc.client5.http.ssl.NoopHostnameVerifier;
39 import org.apache.hc.core5.http2.HttpVersionPolicy;
40 import org.apache.hc.core5.ssl.SSLContextBuilder;
41 import org.apache.hc.core5.util.Timeout;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 @NoArgsConstructor(access = AccessLevel.PRIVATE)
46 public final class HttpCientFactory {
47 private static final Logger LOG = LoggerFactory.getLogger(HttpCientFactory.class);
50 * Generates a CloseableHttpAsyncClient.
52 public static CloseableHttpAsyncClient createClient(final String trustStore,
53 final String trustStorePassword, final Long conectionTimeout, final Long responseTimeout)
54 throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException, IOException,
55 CertificateException {
56 final var sslContext = getSslContext(new File(trustStore), trustStorePassword);
57 return trustTrustStore(sslContext, conectionTimeout, responseTimeout);
60 private static SSLContext getSslContext(final File trustStoreFilePath, final String trustStorePassword)
61 throws IOException, CertificateException, NoSuchAlgorithmException, KeyStoreException,
62 KeyManagementException {
63 return new SSLContextBuilder()
64 .loadTrustMaterial(trustStoreFilePath.toURI().toURL(), trustStorePassword.toCharArray())
68 private static CloseableHttpAsyncClient trustTrustStore(final SSLContext sslContext,
69 final Long conectionTimeout, final Long responseTimeout) {
70 LOG.info("Trust all certificates under truststore");
71 final PoolingAsyncClientConnectionManager connectionManager =
72 PoolingAsyncClientConnectionManagerBuilder.create().setTlsStrategy(
73 ClientTlsStrategyBuilder.create()
74 .setSslContext(sslContext)
75 .setHostnameVerifier(NoopHostnameVerifier.INSTANCE)
79 return HttpAsyncClients.custom()
80 .setConnectionManager(connectionManager)
81 .setDefaultRequestConfig(createDefaultRequestConfig(conectionTimeout, responseTimeout))
82 .setVersionPolicy(HttpVersionPolicy.NEGOTIATE)
86 private static RequestConfig createDefaultRequestConfig(final Long conectionTimeout, final Long responseTimeout) {
87 return RequestConfig.custom()
88 .setConnectTimeout(Timeout.ofSeconds(conectionTimeout))
89 .setResponseTimeout(Timeout.ofSeconds(responseTimeout))
90 .setCookieSpec(StandardCookieSpec.STRICT)