diff options
author | Remigiusz Janeczek <remigiusz.janeczek@nokia.com> | 2020-02-27 16:15:29 +0100 |
---|---|---|
committer | Remigiusz Janeczek <remigiusz.janeczek@nokia.com> | 2020-02-28 09:45:00 +0100 |
commit | cbe239fb79ee16e1aabfd0d2be286b41fa8941eb (patch) | |
tree | 16997bb13410724d97d69ddc7fb57dd633d242a3 /certServiceClient/src/main/java | |
parent | 7dc94694ec75ce738ecb2e98508c57eadc1d81fa (diff) |
Refactor access to env variables in certservice-client
Issue-ID: AAF-996
Signed-off-by: Remigiusz Janeczek <remigiusz.janeczek@nokia.com>
Change-Id: Ib11998a72e3addefd16587cda5e10fb787e99a62
Diffstat (limited to 'certServiceClient/src/main/java')
6 files changed, 54 insertions, 63 deletions
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<String> 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<String> getUrlToCertService() { + return readEnv(ClientConfigurationEnvs.REQUEST_URL); + } + + public Optional<String> getRequestTimeOut() { + return readEnv(ClientConfigurationEnvs.REQUEST_TIMEOUT); } - public String getRequestTimeOut() { - return requestTimeOut; + public Optional<String> getOutputPath() { + return readEnv(ClientConfigurationEnvs.OUTPUT_PATH); } - public String getOutputPath() { - return outputPath; + public Optional<String> getCaName() { + return readEnv(ClientConfigurationEnvs.CA_NAME); } - public String getCaName() { - return caName; + private Optional<String> 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<String> getCommonName() { + return readEnv(CsrConfigurationEnvs.COMMON_NAME); } - public String getCommonName() { - return commonName; + public Optional<String> getOrganization() { + return readEnv(CsrConfigurationEnvs.ORGANIZATION); } - public String getOrganization() { - return organization; + public Optional<String> getOrganizationUnit() { + return readEnv(CsrConfigurationEnvs.ORGANIZATION_UNIT); } - public String getOrganizationUnit() { - return organizationUnit; + public Optional<String> getLocation() { + return readEnv(CsrConfigurationEnvs.LOCATION); } - public String getLocation() { - return location; + public Optional<String> getState() { + return readEnv(CsrConfigurationEnvs.STATE); } - public String getState() { - return state; + public Optional<String> getCountry() { + return readEnv(CsrConfigurationEnvs.COUNTRY); } - public String getCountry() { - return country; + public Optional<String> getSubjectAlternativesName() { + return readEnv(CsrConfigurationEnvs.SANS); } - public String getSubjectAlternativesName() { - return subjectAlternativesName; + private Optional<String> 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<Csr CsrConfiguration configuration = new CsrConfiguration(); - Optional.ofNullable(envsForCsr.getCommonName()).filter(EnvValidationUtils::isEnvExists) + envsForCsr.getCommonName() .filter(EnvValidationUtils::isCommonNameValid) .map(configuration::setCommonName) .orElseThrow(() -> 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; |