From 67fcc6f6abb7904ecd4b4444fa23b355cf9fd4ae Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Mon, 24 Jun 2019 15:46:36 -0400 Subject: Fix some sonar issues in policy-endpoints Refactored various classes to reduce cyclomatic complexity. Introduced some endpoint utility classes to facilitate extraction and conversion of property values, and populating of common "builder" values. Change-Id: Ie1c91cd94cb54700dc9127f72780b4d94b82ec39 Issue-ID: POLICY-1791 Signed-off-by: Jim Hahn --- .../http/client/IndexedHttpClientFactory.java | 111 +++++-------- .../http/client/internal/JerseyClient.java | 42 ++--- .../server/IndexedHttpServletServerFactory.java | 182 +++++++++------------ 3 files changed, 145 insertions(+), 190 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/IndexedHttpClientFactory.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/IndexedHttpClientFactory.java index 5cc0071e..c2d0e400 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/IndexedHttpClientFactory.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/IndexedHttpClientFactory.java @@ -7,9 +7,9 @@ * 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. @@ -23,15 +23,14 @@ package org.onap.policy.common.endpoints.http.client; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; -import java.util.Arrays; 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.onap.policy.common.endpoints.utils.PropertyUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -70,78 +69,54 @@ class IndexedHttpClientFactory implements HttpClientFactory { ArrayList clientList = new ArrayList<>(); String clientNames = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES); - if (clientNames == null || clientNames.isEmpty()) { + if (StringUtils.isBlank(clientNames)) { return clientList; } - List clientNameList = new ArrayList<>(Arrays.asList(clientNames.split("\\s*,\\s*"))); - - for (String clientName : clientNameList) { - String httpsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." - + clientName + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX); - boolean https = false; - if (StringUtils.isNotBlank(httpsString)) { - https = Boolean.parseBoolean(httpsString); - } - - String hostName = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." - + clientName + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX); - - String servicePortString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES - + "." + clientName + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX); - int port; - try { - if (servicePortString == null || servicePortString.isEmpty()) { - continue; - } - port = Integer.parseInt(servicePortString); - } catch (NumberFormatException nfe) { - logger.error("http-client-factory: cannot parse port {}", servicePortString, nfe); - continue; - } - - String baseUrl = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." - + clientName + PolicyEndPointProperties.PROPERTY_HTTP_URL_SUFFIX); - - String userName = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." - + clientName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX); - - String password = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." - + clientName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX); - - final String classProv = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES - + "." + clientName + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER); - - String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." - + clientName + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX); - boolean managed = true; - if (managedString != null && !managedString.isEmpty()) { - managed = Boolean.parseBoolean(managedString); - } - - try { - HttpClient client = - this.build(BusTopicParams.builder() - .clientName(clientName) - .useHttps(https) - .allowSelfSignedCerts(https) - .hostname(hostName) - .port(port) - .basePath(baseUrl) - .userName(userName) - .password(password) - .managed(managed) - .serializationProvider(classProv) - .build()); - clientList.add(client); - } catch (Exception e) { - logger.error("http-client-factory: cannot build client {}", clientName, e); - } + for (String clientName : clientNames.split("\\s*,\\s*")) { + addClient(clientList, clientName, properties); } return clientList; } + private void addClient(ArrayList clientList, String clientName, Properties properties) { + String clientPrefix = PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + clientName; + + PropertyUtils props = new PropertyUtils(properties, clientPrefix, + (name, value, ex) -> + logger.warn("{}: {} {} is in invalid format for http client {} ", this, name, value, clientName)); + + int port = props.getInteger(PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, -1); + if (port < 0) { + logger.warn("No HTTP port for client in {}", clientName); + return; + } + + boolean https = props.getBoolean(PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX, false); + + try { + HttpClient client = this.build(BusTopicParams.builder() + .clientName(clientName) + .useHttps(https) + .allowSelfSignedCerts(https) + .hostname(props.getString(PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, null)) + .port(port) + .basePath(props.getString(PolicyEndPointProperties.PROPERTY_HTTP_URL_SUFFIX, null)) + .userName(props.getString(PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, + null)) + .password(props.getString(PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, + null)) + .managed(props.getBoolean(PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, true)) + .serializationProvider(props.getString( + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER, null)) + .build()); + clientList.add(client); + } catch (Exception e) { + logger.error("http-client-factory: cannot build client {}", clientName, e); + } + } + @Override public synchronized HttpClient get(String name) { if (clients.containsKey(name)) { 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 d4ccc494..4b73c5c4 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 @@ -33,6 +33,7 @@ import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.Entity; import javax.ws.rs.client.Invocation.Builder; import javax.ws.rs.core.Response; +import org.apache.commons.lang3.StringUtils; import org.glassfish.jersey.client.ClientProperties; import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams; @@ -84,8 +85,6 @@ public class JerseyClient implements HttpClient { public JerseyClient(BusTopicParams busTopicParams) throws KeyManagementException, NoSuchAlgorithmException, ClassNotFoundException { - super(); - if (busTopicParams.isClientNameInvalid()) { throw new IllegalArgumentException("Name must be provided"); } @@ -106,10 +105,23 @@ public class JerseyClient implements HttpClient { this.userName = busTopicParams.getUserName(); this.password = busTopicParams.getPassword(); this.selfSignedCerts = busTopicParams.isAllowSelfSignedCerts(); + this.client = detmClient(); + + if (!StringUtils.isBlank(this.userName) && !StringUtils.isBlank(this.password)) { + HttpAuthenticationFeature authFeature = HttpAuthenticationFeature.basic(userName, password); + this.client.register(authFeature); + } + + this.client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true"); + + registerSerProviders(busTopicParams.getSerializationProvider()); + + this.baseUrl = (this.https ? "https://" : "http://") + this.hostname + ":" + this.port + "/" + + (this.basePath == null ? "" : this.basePath); + } - StringBuilder tmpBaseUrl = new StringBuilder(); + private Client detmClient() throws NoSuchAlgorithmException, KeyManagementException { if (this.https) { - tmpBaseUrl.append("https://"); ClientBuilder clientBuilder; SSLContext sslContext = SSLContext.getInstance("TLSv1.2"); if (this.selfSignedCerts) { @@ -120,23 +132,11 @@ public class JerseyClient implements HttpClient { sslContext.init(null, null, null); clientBuilder = ClientBuilder.newBuilder().sslContext(sslContext); } - this.client = clientBuilder.build(); - } else { - tmpBaseUrl.append("http://"); - this.client = ClientBuilder.newClient(); - } + return clientBuilder.build(); - if (this.userName != null && !this.userName.isEmpty() && this.password != null && !this.password.isEmpty()) { - HttpAuthenticationFeature authFeature = HttpAuthenticationFeature.basic(userName, password); - this.client.register(authFeature); + } else { + return ClientBuilder.newClient(); } - - registerSerProviders(busTopicParams.getSerializationProvider()); - - this.client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true"); - - this.baseUrl = tmpBaseUrl.append(this.hostname).append(":").append(this.port).append("/") - .append((this.basePath == null) ? "" : this.basePath).toString(); } /** @@ -146,7 +146,7 @@ public class JerseyClient implements HttpClient { * @throws ClassNotFoundException if the serialization provider cannot be found */ private void registerSerProviders(String serializationProvider) throws ClassNotFoundException { - String providers = (serializationProvider == null || serializationProvider.isEmpty() + String providers = (StringUtils.isBlank(serializationProvider) ? JERSEY_DEFAULT_SERIALIZATION_PROVIDER : serializationProvider); for (String prov : providers.split(",")) { this.client.register(Class.forName(prov)); @@ -155,7 +155,7 @@ public class JerseyClient implements HttpClient { @Override public Response get(String path) { - if (path != null && !path.isEmpty()) { + if (!StringUtils.isBlank(path)) { return this.client.target(this.baseUrl).path(path).request().get(); } else { return this.client.target(this.baseUrl).request().get(); diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/IndexedHttpServletServerFactory.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/IndexedHttpServletServerFactory.java index b2c49eae..517ad208 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/IndexedHttpServletServerFactory.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/IndexedHttpServletServerFactory.java @@ -7,9 +7,9 @@ * 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. @@ -21,12 +21,13 @@ package org.onap.policy.common.endpoints.http.server; import java.util.ArrayList; -import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Properties; +import org.apache.commons.lang3.StringUtils; import org.onap.policy.common.endpoints.http.server.internal.JettyJerseyServer; import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties; +import org.onap.policy.common.endpoints.utils.PropertyUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -75,132 +76,111 @@ class IndexedHttpServletServerFactory implements HttpServletServerFactory { ArrayList serviceList = new ArrayList<>(); String serviceNames = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES); - if (serviceNames == null || serviceNames.isEmpty()) { + if (StringUtils.isBlank(serviceNames)) { logger.warn("No topic for HTTP Service: {}", properties); return serviceList; } - List serviceNameList = Arrays.asList(serviceNames.split(SPACES_COMMA_SPACES)); - - for (String serviceName : serviceNameList) { - String servicePortString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES - + "." + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX); - - int servicePort; - try { - if (servicePortString == null || servicePortString.isEmpty()) { - if (logger.isWarnEnabled()) { - logger.warn("No HTTP port for service in {}", serviceName); - } - continue; - } - servicePort = Integer.parseInt(servicePortString); - } catch (NumberFormatException nfe) { - if (logger.isWarnEnabled()) { - logger.warn("No HTTP port for service in {}", serviceName); - } - continue; - } + for (String serviceName : serviceNames.split(SPACES_COMMA_SPACES)) { + addService(serviceList, serviceName, properties); + } - final String hostName = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." - + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX); + return serviceList; + } - final String contextUriPath = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES - + "." + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_CONTEXT_URIPATH_SUFFIX); + private void addService(ArrayList serviceList, String serviceName, Properties properties) { - final String userName = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." - + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX); + String servicePrefix = PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + serviceName; - final String password = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." - + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX); + PropertyUtils props = new PropertyUtils(properties, servicePrefix, + (name, value, ex) -> logger + .warn("{}: {} {} is in invalid format for http service {} ", this, name, value, serviceName)); - final String authUriPath = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES - + "." + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_URIPATH_SUFFIX); + int servicePort = props.getInteger(PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, -1); + if (servicePort < 0) { + logger.warn("No HTTP port for service in {}", serviceName); + return; + } - final String restClasses = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES - + "." + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX); + final String hostName = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, null); + final String contextUriPath = + props.getString(PolicyEndPointProperties.PROPERTY_HTTP_CONTEXT_URIPATH_SUFFIX, null); + boolean managed = props.getBoolean(PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, true); + boolean swagger = props.getBoolean(PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX, false); + boolean https = props.getBoolean(PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX, false); - final String filterClasses = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES - + "." + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_FILTER_CLASSES_SUFFIX); + // create the service + HttpServletServer service = build(serviceName, https, hostName, servicePort, contextUriPath, swagger, managed); - final String restPackages = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES - + "." + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_REST_PACKAGES_SUFFIX); + // configure the service + setSerializationProvider(props, service); + setAuthentication(props, service, contextUriPath); - final String restUriPath = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES - + "." + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_REST_URIPATH_SUFFIX); + final String restUriPath = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_REST_URIPATH_SUFFIX, null); - final String classProv = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES - + "." + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER); - - final String managedString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES - + "." + serviceName + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX); - boolean managed = true; - if (managedString != null && !managedString.isEmpty()) { - managed = Boolean.parseBoolean(managedString); - } + addFilterClasses(props, service, restUriPath); + addServletClasses(props, service, restUriPath); + addServletPackages(props, service, restUriPath); - String swaggerString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." - + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX); - boolean swagger = false; - if (swaggerString != null && !swaggerString.isEmpty()) { - swagger = Boolean.parseBoolean(swaggerString); - } + serviceList.add(service); + } - String httpsString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." - + serviceName + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX); - boolean https = false; - if (httpsString != null && !httpsString.isEmpty()) { - https = Boolean.parseBoolean(httpsString); - } + private void setSerializationProvider(PropertyUtils props, HttpServletServer service) { - String aafString = properties.getProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." - + serviceName + PolicyEndPointProperties.PROPERTY_AAF_SUFFIX); - boolean aaf = false; - if (aafString != null && !aafString.isEmpty()) { - aaf = Boolean.parseBoolean(aafString); - } + final String classProv = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER, null); - - HttpServletServer service = build(serviceName, https, hostName, servicePort, contextUriPath, swagger, - managed); + if (!StringUtils.isBlank(classProv)) { + service.setSerializationProvider(classProv); + } + } - if (classProv != null && !classProv.isEmpty()) { - service.setSerializationProvider(classProv); - } + private void setAuthentication(PropertyUtils props, HttpServletServer service, final String contextUriPath) { + /* authentication method either AAF or HTTP Basic Auth */ - /* authentication method either AAF or HTTP Basic Auth */ + boolean aaf = props.getBoolean(PolicyEndPointProperties.PROPERTY_AAF_SUFFIX, false); + final String userName = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, null); + final String password = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, null); + final String authUriPath = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_AUTH_URIPATH_SUFFIX, null); - if (aaf) { - service.setAafAuthentication(contextUriPath); - } else if (userName != null && !userName.isEmpty() && password != null && !password.isEmpty()) { - service.setBasicAuthentication(userName, password, authUriPath); - } + if (aaf) { + service.setAafAuthentication(contextUriPath); + } else if (!StringUtils.isBlank(userName) && !StringUtils.isBlank(password)) { + service.setBasicAuthentication(userName, password, authUriPath); + } + } - if (filterClasses != null && !filterClasses.isEmpty()) { - List filterClassesList = Arrays.asList(filterClasses.split(SPACES_COMMA_SPACES)); - for (String filterClass : filterClassesList) { - service.addFilterClass(restUriPath, filterClass); - } - } + private void addFilterClasses(PropertyUtils props, HttpServletServer service, final String restUriPath) { - if (restClasses != null && !restClasses.isEmpty()) { - List restClassesList = Arrays.asList(restClasses.split(SPACES_COMMA_SPACES)); - for (String restClass : restClassesList) { - service.addServletClass(restUriPath, restClass); - } - } + final String filterClasses = + props.getString(PolicyEndPointProperties.PROPERTY_HTTP_FILTER_CLASSES_SUFFIX, null); - if (restPackages != null && !restPackages.isEmpty()) { - List restPackageList = Arrays.asList(restPackages.split(SPACES_COMMA_SPACES)); - for (String restPackage : restPackageList) { - service.addServletPackage(restUriPath, restPackage); - } + if (!StringUtils.isBlank(filterClasses)) { + for (String filterClass : filterClasses.split(SPACES_COMMA_SPACES)) { + service.addFilterClass(restUriPath, filterClass); } + } + } + + private void addServletClasses(PropertyUtils props, HttpServletServer service, final String restUriPath) { + + final String restClasses = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX, null); - serviceList.add(service); + if (!StringUtils.isBlank(restClasses)) { + for (String restClass : restClasses.split(SPACES_COMMA_SPACES)) { + service.addServletClass(restUriPath, restClass); + } } + } - return serviceList; + private void addServletPackages(PropertyUtils props, HttpServletServer service, final String restUriPath) { + + final String restPackages = props.getString(PolicyEndPointProperties.PROPERTY_HTTP_REST_PACKAGES_SUFFIX, null); + + if (!StringUtils.isBlank(restPackages)) { + for (String restPackage : restPackages.split(SPACES_COMMA_SPACES)) { + service.addServletPackage(restUriPath, restPackage); + } + } } @Override -- cgit 1.2.3-korg