diff options
author | Pawel <pawel.kasperkiewicz@nokia.com> | 2021-02-12 14:20:40 +0100 |
---|---|---|
committer | Pawel <pawel.kasperkiewicz@nokia.com> | 2021-02-12 14:53:45 +0100 |
commit | 0f93dcadfdb93b8251009f84567652a4675dfd5b (patch) | |
tree | 11cab9afa7c217216ad53a5ec75178e37b304af3 /rest-services/http-client | |
parent | 600abbb83d270a0bed69ab539476829b93d9b37a (diff) |
Refactor class RxHttpClientFactory
Issue-ID: DCAEGEN2-1483
Signed-off-by: Pawel <pawel.kasperkiewicz@nokia.com>
Change-Id: Ic35eba7198bfaf1b83ae0107952bf274772b3f35
Diffstat (limited to 'rest-services/http-client')
2 files changed, 109 insertions, 44 deletions
diff --git a/rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/HttpClientFactory.java b/rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/HttpClientFactory.java new file mode 100644 index 00000000..e64e7cb4 --- /dev/null +++ b/rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/HttpClientFactory.java @@ -0,0 +1,88 @@ +/* + * ============LICENSE_START==================================== + * DCAEGEN2-SERVICES-SDK + * ========================================================= + * Copyright (C) 2021 Nokia. All rights reserved. + * ========================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END===================================== + */ + +package org.onap.dcaegen2.services.sdk.rest.services.adapters.http; + +import io.netty.handler.ssl.SslContext; +import org.jetbrains.annotations.NotNull; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.config.ConnectionPoolConfig; +import org.onap.dcaegen2.services.sdk.security.ssl.SecurityKeys; +import org.onap.dcaegen2.services.sdk.security.ssl.SslFactory; +import org.onap.dcaegen2.services.sdk.security.ssl.TrustStoreKeys; +import reactor.netty.http.client.HttpClient; +import reactor.netty.resources.ConnectionProvider; + +class HttpClientFactory { + + private static final SslFactory SSL_FACTORY = new SslFactory(); + + private HttpClientFactory() { + } + + static HttpClient create(){ + return HttpClient.create(); + } + + static HttpClient create(ConnectionPoolConfig connectionPoolConfig){ + return HttpClient.create(createConnectionProvider(connectionPoolConfig)); + } + + static HttpClient create(SecurityKeys securityKeys){ + final SslContext sslContext = SSL_FACTORY.createSecureClientContext(securityKeys); + return HttpClient.create().secure(sslContextSpec -> sslContextSpec.sslContext(sslContext)); + } + + static HttpClient create(SecurityKeys securityKeys, ConnectionPoolConfig connectionPoolConfig){ + final SslContext sslContext = SSL_FACTORY.createSecureClientContext(securityKeys); + final ConnectionProvider connectionProvider = createConnectionProvider(connectionPoolConfig); + return HttpClient.create(connectionProvider).secure(sslContextSpec -> sslContextSpec.sslContext(sslContext)); + } + + static HttpClient create(TrustStoreKeys trustStoreKeys){ + final SslContext sslContext = SSL_FACTORY.createSecureClientContext(trustStoreKeys); + return HttpClient.create().secure(sslContextSpec -> sslContextSpec.sslContext(sslContext)); + } + + static HttpClient create(TrustStoreKeys trustStoreKeys, ConnectionPoolConfig connectionPoolConfig){ + final SslContext sslContext = SSL_FACTORY.createSecureClientContext(trustStoreKeys); + final ConnectionProvider connectionProvider = createConnectionProvider(connectionPoolConfig); + return HttpClient.create(connectionProvider).secure(sslContextSpec -> sslContextSpec.sslContext(sslContext)); + } + + static HttpClient createInsecure() { + final SslContext context = SSL_FACTORY.createInsecureClientContext(); + return HttpClient.create().secure(sslContextSpec -> sslContextSpec.sslContext(context)); + } + + static HttpClient createInsecure(ConnectionPoolConfig connectionPoolConfig) { + final SslContext context = SSL_FACTORY.createInsecureClientContext(); + final ConnectionProvider connectionProvider = createConnectionProvider(connectionPoolConfig); + return HttpClient.create(connectionProvider).secure(sslContextSpec -> sslContextSpec.sslContext(context)); + } + + @NotNull + private static ConnectionProvider createConnectionProvider(ConnectionPoolConfig connectionPoolConfig) { + return ConnectionProvider.builder("fixed") + .maxConnections(connectionPoolConfig.connectionPool()) + .maxIdleTime(connectionPoolConfig.maxIdleTime()) + .maxLifeTime(connectionPoolConfig.maxLifeTime()) + .build(); + } +} diff --git a/rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/RxHttpClientFactory.java b/rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/RxHttpClientFactory.java index 0567b1b4..90b8ff16 100644 --- a/rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/RxHttpClientFactory.java +++ b/rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/RxHttpClientFactory.java @@ -20,16 +20,11 @@ package org.onap.dcaegen2.services.sdk.rest.services.adapters.http; -import io.netty.handler.ssl.SslContext; import io.vavr.control.Option; -import org.jetbrains.annotations.NotNull; -import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.config.ConnectionPoolConfig; import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.config.RxHttpClientConfig; import org.onap.dcaegen2.services.sdk.security.ssl.SecurityKeys; -import org.onap.dcaegen2.services.sdk.security.ssl.SslFactory; import org.onap.dcaegen2.services.sdk.security.ssl.TrustStoreKeys; import reactor.netty.http.client.HttpClient; -import reactor.netty.resources.ConnectionProvider; /** * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a> @@ -37,60 +32,51 @@ import reactor.netty.resources.ConnectionProvider; */ public final class RxHttpClientFactory { - private static final SslFactory SSL_FACTORY = new SslFactory(); - private RxHttpClientFactory() { } public static RxHttpClient create() { - return new RxHttpClient(HttpClient.create()); + return new RxHttpClient(HttpClientFactory.create()); } - public static RxHttpClient create(RxHttpClientConfig config){ - return Option.of(config.connectionPool()) - .map(RxHttpClientFactory::createConnectionProvider) - .map(provider -> createWithConfig(HttpClient.create(provider), config)) - .getOrElse(createWithConfig(HttpClient.create(), config)); + public static RxHttpClient create(RxHttpClientConfig config) { + HttpClient httpClient = Option.of(config.connectionPool()) + .map(HttpClientFactory::create) + .getOrElse(HttpClientFactory::create); + return createWithConfig(httpClient, config); } public static RxHttpClient create(SecurityKeys securityKeys) { - final SslContext context = SSL_FACTORY.createSecureClientContext(securityKeys); - return create(context); + return new RxHttpClient(HttpClientFactory.create(securityKeys)); } public static RxHttpClient create(SecurityKeys securityKeys, RxHttpClientConfig config) { - final SslContext context = SSL_FACTORY.createSecureClientContext(securityKeys); - return create(context, config); + HttpClient httpClient = Option.of(config.connectionPool()) + .map(connectionPoolConfig -> HttpClientFactory.create(securityKeys, connectionPoolConfig)) + .getOrElse(() -> HttpClientFactory.create(securityKeys)); + return createWithConfig(httpClient, config); } public static RxHttpClient create(TrustStoreKeys trustStoreKeys) { - final SslContext context = SSL_FACTORY.createSecureClientContext(trustStoreKeys); - return create(context); + return new RxHttpClient(HttpClientFactory.create(trustStoreKeys)); } public static RxHttpClient create(TrustStoreKeys trustStoreKeys, RxHttpClientConfig config) { - final SslContext context = SSL_FACTORY.createSecureClientContext(trustStoreKeys); - return create(context, config); + HttpClient httpClient = Option.of(config.connectionPool()) + .map(connectionPoolConfig -> HttpClientFactory.create(trustStoreKeys, connectionPoolConfig)) + .getOrElse(() -> HttpClientFactory.create(trustStoreKeys)); + return createWithConfig(httpClient, config); } public static RxHttpClient createInsecure() { - final SslContext context = SSL_FACTORY.createInsecureClientContext(); - return create(context); + return new RxHttpClient(HttpClientFactory.createInsecure()); } public static RxHttpClient createInsecure(RxHttpClientConfig config) { - final SslContext context = SSL_FACTORY.createInsecureClientContext(); - return create(context, config); - } - - private static RxHttpClient create(@NotNull SslContext sslContext) { - HttpClient secure = HttpClient.create().secure(sslContextSpec -> sslContextSpec.sslContext(sslContext)); - return new RxHttpClient(secure); - } - - private static RxHttpClient create(@NotNull SslContext sslContext, RxHttpClientConfig config) { - HttpClient secure = HttpClient.create().secure(sslContextSpec -> sslContextSpec.sslContext(sslContext)); - return createWithConfig(secure, config); + HttpClient httpClient = Option.of(config.connectionPool()) + .map(HttpClientFactory::createInsecure) + .getOrElse(HttpClientFactory::createInsecure); + return createWithConfig(httpClient, config); } private static RxHttpClient createWithConfig(HttpClient httpClient, RxHttpClientConfig config) { @@ -98,13 +84,4 @@ public final class RxHttpClientFactory { .map(retryConfig -> new RxHttpClient(httpClient, retryConfig)) .getOrElse(() -> new RxHttpClient(httpClient)); } - - @NotNull - private static ConnectionProvider createConnectionProvider(ConnectionPoolConfig connectionPoolConfig) { - return ConnectionProvider.builder("fixed") - .maxConnections(connectionPoolConfig.connectionPool()) - .maxIdleTime(connectionPoolConfig.maxIdleTime()) - .maxLifeTime(connectionPoolConfig.maxLifeTime()) - .build(); - } } |