Refactor class RxHttpClientFactory 94/117794/3
authorPawel <pawel.kasperkiewicz@nokia.com>
Fri, 12 Feb 2021 13:20:40 +0000 (14:20 +0100)
committerPawel <pawel.kasperkiewicz@nokia.com>
Fri, 12 Feb 2021 13:53:45 +0000 (14:53 +0100)
Issue-ID: DCAEGEN2-1483
Signed-off-by: Pawel <pawel.kasperkiewicz@nokia.com>
Change-Id: Ic35eba7198bfaf1b83ae0107952bf274772b3f35

rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/HttpClientFactory.java [new file with mode: 0644]
rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/RxHttpClientFactory.java

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 (file)
index 0000000..e64e7cb
--- /dev/null
@@ -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();
+    }
+}
index 0567b1b..90b8ff1 100644 (file)
 
 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();
-    }
 }