From b655b67e38d98c3478a3a47407ee90410d4442bb Mon Sep 17 00:00:00 2001 From: Krishnajinka Date: Thu, 16 Aug 2018 18:14:51 +0900 Subject: Use builder for http and jersey client Sonar issue with more than 7 params in a method. Use builder object instead of params. Rework2 for fixing review comments. Add modifications copyright Issue-ID: POLICY-1017 Change-Id: Ib0fa692f8da770dcba06158d6e6cafbed2969c27 Signed-off-by: Krishnajinka --- .../endpoints/http/client/HttpClientFactory.java | 33 ++++++++++----- .../http/client/internal/JerseyClient.java | 47 +++++++++++----------- 2 files changed, 46 insertions(+), 34 deletions(-) (limited to 'policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http') diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClientFactory.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClientFactory.java index c002402d..ca10680b 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClientFactory.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClientFactory.java @@ -3,6 +3,7 @@ * policy-endpoints * ================================================================================ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2018 Samsung Electronics Co., Ltd. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,6 +29,8 @@ import java.util.HashMap; import java.util.List; import java.util.Properties; +import org.apache.commons.lang3.StringUtils; +import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams; import org.onap.policy.common.endpoints.http.client.internal.JerseyClient; import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties; import org.slf4j.Logger; @@ -41,8 +44,7 @@ public interface HttpClientFactory { /** * Build and http client with the following parameters. */ - public HttpClient build(String name, boolean https, boolean selfSignedCerts, String hostname, int port, - String baseUrl, String userName, String password, boolean managed) + public HttpClient build(BusTopicParams busTopicParams) throws KeyManagementException, NoSuchAlgorithmException; /** @@ -89,18 +91,17 @@ class IndexedHttpClientFactory implements HttpClientFactory { protected HashMap clients = new HashMap<>(); @Override - public synchronized HttpClient build(String name, boolean https, boolean selfSignedCerts, String hostname, int port, - String baseUrl, String userName, String password, boolean managed) + public synchronized HttpClient build(BusTopicParams busTopicParams) throws KeyManagementException, NoSuchAlgorithmException { - if (clients.containsKey(name)) { - return clients.get(name); + if (clients.containsKey(busTopicParams.getClientName())) { + return clients.get(busTopicParams.getClientName()); } JerseyClient client = - new JerseyClient(name, https, selfSignedCerts, hostname, port, baseUrl, userName, password); + new JerseyClient(busTopicParams); - if (managed) { - clients.put(name, client); + if (busTopicParams.isManaged()) { + clients.put(busTopicParams.getClientName(), client); } return client; @@ -122,7 +123,7 @@ class IndexedHttpClientFactory implements HttpClientFactory { String httpsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + clientName + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX); boolean https = false; - if (httpsString != null && !httpsString.isEmpty()) { + if (StringUtils.isNotBlank(httpsString)) { https = Boolean.parseBoolean(httpsString); } @@ -160,7 +161,17 @@ class IndexedHttpClientFactory implements HttpClientFactory { try { HttpClient client = - this.build(clientName, https, https, hostName, port, baseUrl, userName, password, managed); + this.build(BusTopicParams.builder() + .clientName(clientName) + .useHttps(https) + .allowSelfSignedCerts(https) + .hostname(hostName) + .port(port) + .basePath(baseUrl) + .userName(userName) + .password(password) + .managed(managed) + .build()); clientList.add(client); } catch (Exception e) { logger.error("http-client-factory: cannot build client {}", clientName, e); diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/internal/JerseyClient.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/internal/JerseyClient.java index 1fc00bfe..c227071c 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/internal/JerseyClient.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/internal/JerseyClient.java @@ -3,6 +3,7 @@ * policy-endpoints * ================================================================================ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2018 Samsung Electronics Co., Ltd. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -36,6 +37,7 @@ import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.core.Response; import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; +import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams; import org.onap.policy.common.endpoints.http.client.HttpClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -64,43 +66,42 @@ public class JerseyClient implements HttpClient { /** * Constructor. * - * @param name the name - * @param https is it https or not - * @param selfSignedCerts are there self signed certs - * @param hostname the hostname - * @param port port being used - * @param basePath base context - * @param userName user - * @param password password - * + * name the name + * https is it https or not + * selfSignedCerts are there self signed certs + * hostname the hostname + * port port being used + * basePath base context + * userName user + * password password + * @param busTopicParams Input parameters object * @throws KeyManagementException key exception * @throws NoSuchAlgorithmException no algorithm exception */ - public JerseyClient(String name, boolean https, boolean selfSignedCerts, String hostname, int port, String basePath, - String userName, String password) throws KeyManagementException, NoSuchAlgorithmException { + public JerseyClient(BusTopicParams busTopicParams) throws KeyManagementException, NoSuchAlgorithmException { super(); - if (name == null || name.isEmpty()) { + if (busTopicParams.isClientNameInvalid()) { throw new IllegalArgumentException("Name must be provided"); } - if (hostname == null || hostname.isEmpty()) { + if (busTopicParams.isHostnameInvalid()) { throw new IllegalArgumentException("Hostname must be provided"); } - if (port <= 0 && port >= 65535) { - throw new IllegalArgumentException("Invalid Port provided: " + port); + if (busTopicParams.isPortInvalid()) { + throw new IllegalArgumentException("Invalid Port provided: " + busTopicParams.getPort()); } - this.name = name; - this.https = https; - this.hostname = hostname; - this.port = port; - this.basePath = basePath; - this.userName = userName; - this.password = password; - this.selfSignedCerts = selfSignedCerts; + this.name = busTopicParams.getClientName(); + this.https = busTopicParams.isUseHttps(); + this.hostname = busTopicParams.getHostname(); + this.port = busTopicParams.getPort(); + this.basePath = busTopicParams.getBasePath(); + this.userName = busTopicParams.getUserName(); + this.password = busTopicParams.getPassword(); + this.selfSignedCerts = busTopicParams.isAllowSelfSignedCerts(); StringBuilder tmpBaseUrl = new StringBuilder(); if (this.https) { -- cgit 1.2.3-korg