2 * ============LICENSE_START====================================
3 * DCAEGEN2-SERVICES-SDK
4 * =========================================================
5 * Copyright (C) 2019-2021 Nokia. 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.onap.dcaegen2.services.sdk.rest.services.adapters.http;
23 import io.netty.handler.ssl.SslContext;
24 import io.vavr.control.Option;
25 import org.jetbrains.annotations.NotNull;
26 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.config.ConnectionPoolConfig;
27 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.config.RxHttpClientConfig;
28 import org.onap.dcaegen2.services.sdk.security.ssl.SecurityKeys;
29 import org.onap.dcaegen2.services.sdk.security.ssl.SslFactory;
30 import org.onap.dcaegen2.services.sdk.security.ssl.TrustStoreKeys;
31 import reactor.netty.http.client.HttpClient;
32 import reactor.netty.resources.ConnectionProvider;
35 * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
38 public final class RxHttpClientFactory {
40 private static final SslFactory SSL_FACTORY = new SslFactory();
42 private RxHttpClientFactory() {
45 public static RxHttpClient create() {
46 return new RxHttpClient(HttpClient.create());
49 public static RxHttpClient create(RxHttpClientConfig config){
50 return Option.of(config.connectionPool())
51 .map(RxHttpClientFactory::createConnectionProvider)
52 .map(provider -> createWithConfig(HttpClient.create(provider), config))
53 .getOrElse(createWithConfig(HttpClient.create(), config));
56 public static RxHttpClient create(SecurityKeys securityKeys) {
57 final SslContext context = SSL_FACTORY.createSecureClientContext(securityKeys);
58 return create(context);
61 public static RxHttpClient create(SecurityKeys securityKeys, RxHttpClientConfig config) {
62 final SslContext context = SSL_FACTORY.createSecureClientContext(securityKeys);
63 return create(context, config);
66 public static RxHttpClient create(TrustStoreKeys trustStoreKeys) {
67 final SslContext context = SSL_FACTORY.createSecureClientContext(trustStoreKeys);
68 return create(context);
71 public static RxHttpClient create(TrustStoreKeys trustStoreKeys, RxHttpClientConfig config) {
72 final SslContext context = SSL_FACTORY.createSecureClientContext(trustStoreKeys);
73 return create(context, config);
76 public static RxHttpClient createInsecure() {
77 final SslContext context = SSL_FACTORY.createInsecureClientContext();
78 return create(context);
81 public static RxHttpClient createInsecure(RxHttpClientConfig config) {
82 final SslContext context = SSL_FACTORY.createInsecureClientContext();
83 return create(context, config);
86 private static RxHttpClient create(@NotNull SslContext sslContext) {
87 HttpClient secure = HttpClient.create().secure(sslContextSpec -> sslContextSpec.sslContext(sslContext));
88 return new RxHttpClient(secure);
91 private static RxHttpClient create(@NotNull SslContext sslContext, RxHttpClientConfig config) {
92 HttpClient secure = HttpClient.create().secure(sslContextSpec -> sslContextSpec.sslContext(sslContext));
93 return createWithConfig(secure, config);
96 private static RxHttpClient createWithConfig(HttpClient httpClient, RxHttpClientConfig config) {
97 return Option.of(config.retryConfig())
98 .map(retryConfig -> new RxHttpClient(httpClient, retryConfig))
99 .getOrElse(() -> new RxHttpClient(httpClient));
103 private static ConnectionProvider createConnectionProvider(ConnectionPoolConfig connectionPoolConfig) {
104 return ConnectionProvider.builder("fixed")
105 .maxConnections(connectionPoolConfig.connectionPool())
106 .maxIdleTime(connectionPoolConfig.maxIdleTime())
107 .maxLifeTime(connectionPoolConfig.maxLifeTime())