diff options
author | kjaniak <kornel.janiak@nokia.com> | 2020-03-12 12:16:46 +0100 |
---|---|---|
committer | kjaniak <kornel.janiak@nokia.com> | 2020-03-12 14:31:31 +0100 |
commit | fbd668ada3882bb3b866c0cfcce0a184f0178fe8 (patch) | |
tree | 18abd2218d7e10293743fc460263aa6392fb37fb /certServiceClient/src/main/java | |
parent | 3310d5240df5cfe07f1a9c3b312fa1d5f8f1238b (diff) |
Removal of EnvValidationUtils class and cleanup
Issue-ID: AAF-996
Change-Id: Iccbbf5d746ed5772a0327a30fa8bdbc57bc8fe46
Signed-off-by: kjaniak <kornel.janiak@nokia.com>
Diffstat (limited to 'certServiceClient/src/main/java')
4 files changed, 47 insertions, 81 deletions
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 deleted file mode 100644 index b0405274..00000000 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/EnvValidationUtils.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * aaf-certservice-client - * ================================================================================ - * Copyright (C) 2020 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.aaf.certservice.client.configuration; - -import java.util.regex.Pattern; - -public final class EnvValidationUtils { - - private EnvValidationUtils() {} - - public static Boolean isPathValid(String path) { - return path.matches("^/|(/[a-zA-Z0-9_-]+)+/?$"); - } - - public static Boolean isAlphaNumeric(String caName) { - return caName.matches("^[a-zA-Z0-9]*$"); - } - - public static Boolean isCountryValid(String country) { - return country.matches("^([A-Z][A-Z])$"); - } - - public static Boolean isCommonNameValid(String commonName) { - return !isSpecialCharsPresent(commonName) && - !isHttpProtocolsPresent(commonName) && - !isIpAddressPresent(commonName) && - !isPortNumberPresent(commonName); - } - - static Boolean isPortNumberPresent(String stringToCheck) { - return Pattern.compile(":[0-9]{1,5}").matcher(stringToCheck).find(); - } - - static Boolean isIpAddressPresent(String stringToCheck) { - return Pattern.compile("[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}").matcher(stringToCheck).find(); - } - - static Boolean isHttpProtocolsPresent(String stringToCheck) { - return Pattern.compile("[h][t][t][p][:][/][/]|[h][t][t][p][s][:][/][/]").matcher(stringToCheck).find(); - } - - public static Boolean isSpecialCharsPresent(String stringToCheck) { - return Pattern.compile("[~#@*$+%!()?/{}<>\\|_^]").matcher(stringToCheck).find(); - } -} diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/factory/AbstractConfigurationFactory.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/factory/AbstractConfigurationFactory.java index 2464cc58..a914891f 100644 --- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/factory/AbstractConfigurationFactory.java +++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/factory/AbstractConfigurationFactory.java @@ -24,6 +24,44 @@ import org.onap.aaf.certservice.client.configuration.exception.ClientConfigurati import org.onap.aaf.certservice.client.configuration.exception.CsrConfigurationException; import org.onap.aaf.certservice.client.configuration.model.ConfigurationModel; -public interface AbstractConfigurationFactory<T extends ConfigurationModel> { - T create() throws ClientConfigurationException, CsrConfigurationException; +import java.util.regex.Pattern; + +public abstract class AbstractConfigurationFactory<T extends ConfigurationModel> { + + abstract T create() throws ClientConfigurationException, CsrConfigurationException; + + public boolean isPathValid(String path) { + return path.matches("^/|(/[a-zA-Z0-9_-]+)+/?$"); + } + + public boolean isAlphaNumeric(String caName) { + return caName.matches("^[a-zA-Z0-9]*$"); + } + + public boolean isCountryValid(String country) { + return country.matches("^([A-Z][A-Z])$"); + } + + public boolean isCommonNameValid(String commonName) { + return !isSpecialCharsPresent(commonName) && + !isHttpProtocolsPresent(commonName) && + !isIpAddressPresent(commonName) && + !isPortNumberPresent(commonName); + } + + public boolean isSpecialCharsPresent(String stringToCheck) { + return Pattern.compile("[~#@*$+%!()?/{}<>\\|_^]").matcher(stringToCheck).find(); + } + + private boolean isPortNumberPresent(String stringToCheck) { + return Pattern.compile(":[0-9]{1,5}").matcher(stringToCheck).find(); + } + + private boolean isIpAddressPresent(String stringToCheck) { + return Pattern.compile("[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}").matcher(stringToCheck).find(); + } + + private boolean isHttpProtocolsPresent(String stringToCheck) { + return Pattern.compile("[h][t][t][p][:][/][/]|[h][t][t][p][s][:][/][/]").matcher(stringToCheck).find(); + } } 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 3bd15288..26a2b1b1 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 @@ -20,20 +20,15 @@ package org.onap.aaf.certservice.client.configuration.factory; - - -import java.util.Optional; import org.onap.aaf.certservice.client.configuration.ClientConfigurationEnvs; -import org.onap.aaf.certservice.client.configuration.EnvValidationUtils; import org.onap.aaf.certservice.client.configuration.EnvsForClient; import org.onap.aaf.certservice.client.configuration.exception.ClientConfigurationException; import org.onap.aaf.certservice.client.configuration.model.ClientConfiguration; -public class ClientConfigurationFactory implements AbstractConfigurationFactory<ClientConfiguration> { +public class ClientConfigurationFactory extends AbstractConfigurationFactory<ClientConfiguration> { private final EnvsForClient envsForClient; - public ClientConfigurationFactory(EnvsForClient envsForClient) { this.envsForClient = envsForClient; } @@ -50,12 +45,12 @@ public class ClientConfigurationFactory implements AbstractConfigurationFactory< .map(timeout -> configuration.setRequestTimeout(Integer.valueOf(timeout))); envsForClient.getOutputPath() - .filter(EnvValidationUtils::isPathValid) + .filter(this::isPathValid) .map(configuration::setCertsOutputPath) .orElseThrow(() -> new ClientConfigurationException(ClientConfigurationEnvs.OUTPUT_PATH + " is invalid.")); envsForClient.getCaName() - .filter(EnvValidationUtils::isAlphaNumeric) + .filter(this::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 1e5d5c53..a6e86187 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 @@ -21,14 +21,11 @@ package org.onap.aaf.certservice.client.configuration.factory; import org.onap.aaf.certservice.client.configuration.CsrConfigurationEnvs; -import org.onap.aaf.certservice.client.configuration.EnvValidationUtils; import org.onap.aaf.certservice.client.configuration.EnvsForCsr; import org.onap.aaf.certservice.client.configuration.exception.CsrConfigurationException; import org.onap.aaf.certservice.client.configuration.model.CsrConfiguration; -import java.util.Optional; - -public class CsrConfigurationFactory implements AbstractConfigurationFactory<CsrConfiguration> { +public class CsrConfigurationFactory extends AbstractConfigurationFactory<CsrConfiguration> { private final EnvsForCsr envsForCsr; @@ -37,19 +34,18 @@ public class CsrConfigurationFactory implements AbstractConfigurationFactory<Csr this.envsForCsr = envsForCsr; } - @Override public CsrConfiguration create() throws CsrConfigurationException { CsrConfiguration configuration = new CsrConfiguration(); envsForCsr.getCommonName() - .filter(EnvValidationUtils::isCommonNameValid) + .filter(this::isCommonNameValid) .map(configuration::setCommonName) .orElseThrow(() -> new CsrConfigurationException(CsrConfigurationEnvs.COMMON_NAME + " is invalid.")); envsForCsr.getOrganization() - .filter(org -> !EnvValidationUtils.isSpecialCharsPresent(org)) + .filter(org -> !isSpecialCharsPresent(org)) .map(configuration::setOrganization) .orElseThrow(() -> new CsrConfigurationException(CsrConfigurationEnvs.ORGANIZATION + " is invalid.")); @@ -58,7 +54,7 @@ public class CsrConfigurationFactory implements AbstractConfigurationFactory<Csr .orElseThrow(() -> new CsrConfigurationException(CsrConfigurationEnvs.STATE + " is invalid.")); envsForCsr.getCountry() - .filter(EnvValidationUtils::isCountryValid) + .filter(this::isCountryValid) .map(configuration::setCountry) .orElseThrow(() -> new CsrConfigurationException(CsrConfigurationEnvs.COUNTRY + " is invalid.")); |