From cbe239fb79ee16e1aabfd0d2be286b41fa8941eb Mon Sep 17 00:00:00 2001 From: Remigiusz Janeczek Date: Thu, 27 Feb 2020 16:15:29 +0100 Subject: Refactor access to env variables in certservice-client Issue-ID: AAF-996 Signed-off-by: Remigiusz Janeczek Change-Id: Ib11998a72e3addefd16587cda5e10fb787e99a62 --- .../client/configuration/EnvProvider.java | 11 ++++- .../client/configuration/EnvValidationUtils.java | 4 -- .../client/configuration/EnvsForClient.java | 32 +++++++-------- .../client/configuration/EnvsForCsr.java | 48 +++++++++------------- .../factory/ClientConfigurationFactory.java | 8 ++-- .../factory/CsrConfigurationFactory.java | 14 +++---- 6 files changed, 54 insertions(+), 63 deletions(-) (limited to 'certServiceClient/src/main/java/org/onap') diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/EnvProvider.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/EnvProvider.java index beccd383..25a152bf 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/EnvProvider.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/EnvProvider.java @@ -19,8 +19,15 @@ */ package org.onap.aaf.certservice.client.configuration; +import java.util.Optional; + public class EnvProvider { - public String readEnvVariable(String envVariable) { - return System.getProperty(envVariable); + public Optional readEnvVariable(String envVariableName) { + return Optional.ofNullable(System.getenv(envVariableName)) + .filter(EnvProvider::isEnvPresent); + } + + private static Boolean isEnvPresent(String envValue) { + return !"".equals(envValue); } } diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/EnvValidationUtils.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/EnvValidationUtils.java index b87df5be..e65d688d 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/EnvValidationUtils.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/EnvValidationUtils.java @@ -34,10 +34,6 @@ public final class EnvValidationUtils { return caName.matches("^[a-zA-Z0-9]*$"); } - public static Boolean isEnvExists(String envValue) { - return envValue != null && !"".equals(envValue); - } - public static Boolean isCountryValid(String country) { return country.matches("^([A-Z][A-Z])$"); } diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/EnvsForClient.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/EnvsForClient.java index aad64f5b..1ce76370 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/EnvsForClient.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/EnvsForClient.java @@ -19,33 +19,31 @@ */ package org.onap.aaf.certservice.client.configuration; +import java.util.Optional; + public class EnvsForClient { - private static final EnvProvider envProvider = new EnvProvider(); - private final String urlToCertService; - private final String requestTimeOut; - private final String outputPath; - private final String caName; + private final EnvProvider envProvider = new EnvProvider(); public EnvsForClient() { - this.urlToCertService = envProvider.readEnvVariable(ClientConfigurationEnvs.REQUEST_URL.toString()); - this.requestTimeOut = envProvider.readEnvVariable(ClientConfigurationEnvs.REQUEST_TIMEOUT.toString()); - this.outputPath = envProvider.readEnvVariable(ClientConfigurationEnvs.OUTPUT_PATH.toString()); - this.caName = envProvider.readEnvVariable(ClientConfigurationEnvs.CA_NAME.toString()); } - public String getUrlToCertService() { - return urlToCertService; + public Optional getUrlToCertService() { + return readEnv(ClientConfigurationEnvs.REQUEST_URL); + } + + public Optional getRequestTimeOut() { + return readEnv(ClientConfigurationEnvs.REQUEST_TIMEOUT); } - public String getRequestTimeOut() { - return requestTimeOut; + public Optional getOutputPath() { + return readEnv(ClientConfigurationEnvs.OUTPUT_PATH); } - public String getOutputPath() { - return outputPath; + public Optional getCaName() { + return readEnv(ClientConfigurationEnvs.CA_NAME); } - public String getCaName() { - return caName; + private Optional readEnv(ClientConfigurationEnvs envName) { + return envProvider.readEnvVariable(envName.toString()); } } diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/EnvsForCsr.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/EnvsForCsr.java index 0c948d3f..77efc198 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/EnvsForCsr.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/EnvsForCsr.java @@ -19,52 +19,42 @@ */ package org.onap.aaf.certservice.client.configuration; +import java.util.Optional; public class EnvsForCsr { private final EnvProvider envProvider = new EnvProvider(); - private String commonName; - private String organization; - private String organizationUnit; - private String location; - private String state; - private String country; - private String subjectAlternativesName; - public EnvsForCsr() { - this.commonName = envProvider.readEnvVariable(CsrConfigurationEnvs.COMMON_NAME.toString()); - this.organization = envProvider.readEnvVariable(CsrConfigurationEnvs.ORGANIZATION.toString()); - this.organizationUnit = envProvider.readEnvVariable(CsrConfigurationEnvs.ORGANIZATION_UNIT.toString()); - this.location = envProvider.readEnvVariable(CsrConfigurationEnvs.LOCATION.toString()); - this.state = envProvider.readEnvVariable(CsrConfigurationEnvs.STATE.toString()); - this.country = envProvider.readEnvVariable(CsrConfigurationEnvs.COUNTRY.toString()); - this.subjectAlternativesName = envProvider.readEnvVariable(CsrConfigurationEnvs.SANS.toString()); + public EnvsForCsr() {} + + public Optional getCommonName() { + return readEnv(CsrConfigurationEnvs.COMMON_NAME); } - public String getCommonName() { - return commonName; + public Optional getOrganization() { + return readEnv(CsrConfigurationEnvs.ORGANIZATION); } - public String getOrganization() { - return organization; + public Optional getOrganizationUnit() { + return readEnv(CsrConfigurationEnvs.ORGANIZATION_UNIT); } - public String getOrganizationUnit() { - return organizationUnit; + public Optional getLocation() { + return readEnv(CsrConfigurationEnvs.LOCATION); } - public String getLocation() { - return location; + public Optional getState() { + return readEnv(CsrConfigurationEnvs.STATE); } - public String getState() { - return state; + public Optional getCountry() { + return readEnv(CsrConfigurationEnvs.COUNTRY); } - public String getCountry() { - return country; + public Optional getSubjectAlternativesName() { + return readEnv(CsrConfigurationEnvs.SANS); } - public String getSubjectAlternativesName() { - return subjectAlternativesName; + private Optional readEnv(CsrConfigurationEnvs envName) { + return envProvider.readEnvVariable(envName.toString()); } } diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/factory/ClientConfigurationFactory.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/factory/ClientConfigurationFactory.java index 96b1fb8b..b7ee5d32 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/factory/ClientConfigurationFactory.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/factory/ClientConfigurationFactory.java @@ -42,18 +42,18 @@ public class ClientConfigurationFactory implements AbstractConfigurationFactory< ClientConfiguration configuration = new ClientConfiguration(); - Optional.ofNullable(envsForClient.getUrlToCertService()).filter(EnvValidationUtils::isEnvExists) + envsForClient.getUrlToCertService() .map(configuration::setUrlToCertService); - Optional.ofNullable(envsForClient.getRequestTimeOut()).filter(EnvValidationUtils::isEnvExists) + envsForClient.getRequestTimeOut() .map(timeout -> configuration.setRequestTimeout(Integer.valueOf(timeout))); - Optional.ofNullable(envsForClient.getOutputPath()).filter(EnvValidationUtils::isEnvExists) + envsForClient.getOutputPath() .filter(EnvValidationUtils::isPathValid) .map(configuration::setCertsOutputPath) .orElseThrow(() -> new ClientConfigurationException(ClientConfigurationEnvs.OUTPUT_PATH + " is invalid.")); - Optional.ofNullable(envsForClient.getCaName()).filter(EnvValidationUtils::isEnvExists) + envsForClient.getCaName() .filter(EnvValidationUtils::isAlphaNumeric) .map(configuration::setCaName) .orElseThrow(() -> new ClientConfigurationException(ClientConfigurationEnvs.CA_NAME + " is invalid.")); diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/factory/CsrConfigurationFactory.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/factory/CsrConfigurationFactory.java index 61e1b3c3..1e5d5c53 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/factory/CsrConfigurationFactory.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/factory/CsrConfigurationFactory.java @@ -43,32 +43,32 @@ public class CsrConfigurationFactory implements AbstractConfigurationFactory new CsrConfigurationException(CsrConfigurationEnvs.COMMON_NAME + " is invalid.")); - Optional.ofNullable(envsForCsr.getOrganization()).filter(EnvValidationUtils::isEnvExists) + envsForCsr.getOrganization() .filter(org -> !EnvValidationUtils.isSpecialCharsPresent(org)) .map(configuration::setOrganization) .orElseThrow(() -> new CsrConfigurationException(CsrConfigurationEnvs.ORGANIZATION + " is invalid.")); - Optional.ofNullable(envsForCsr.getState()).filter(EnvValidationUtils::isEnvExists) + envsForCsr.getState() .map(configuration::setState) .orElseThrow(() -> new CsrConfigurationException(CsrConfigurationEnvs.STATE + " is invalid.")); - Optional.ofNullable(envsForCsr.getCountry()).filter(EnvValidationUtils::isEnvExists) + envsForCsr.getCountry() .filter(EnvValidationUtils::isCountryValid) .map(configuration::setCountry) .orElseThrow(() -> new CsrConfigurationException(CsrConfigurationEnvs.COUNTRY + " is invalid.")); - Optional.ofNullable(envsForCsr.getOrganizationUnit()).filter(EnvValidationUtils::isEnvExists) + envsForCsr.getOrganizationUnit() .map(configuration::setOrganizationUnit); - Optional.ofNullable(envsForCsr.getLocation()).filter(EnvValidationUtils::isEnvExists) + envsForCsr.getLocation() .map(configuration::setLocation); - Optional.ofNullable(envsForCsr.getSubjectAlternativesName()).filter(EnvValidationUtils::isEnvExists) + envsForCsr.getSubjectAlternativesName() .map(configuration::setSubjectAlternativeNames); return configuration; -- cgit 1.2.3-korg