From fbd668ada3882bb3b866c0cfcce0a184f0178fe8 Mon Sep 17 00:00:00 2001 From: kjaniak Date: Thu, 12 Mar 2020 12:16:46 +0100 Subject: Removal of EnvValidationUtils class and cleanup Issue-ID: AAF-996 Change-Id: Iccbbf5d746ed5772a0327a30fa8bdbc57bc8fe46 Signed-off-by: kjaniak --- .../client/configuration/EnvValidationUtils.java | 63 ---------------------- .../factory/AbstractConfigurationFactory.java | 42 ++++++++++++++- .../factory/ClientConfigurationFactory.java | 11 ++-- .../factory/CsrConfigurationFactory.java | 12 ++--- 4 files changed, 47 insertions(+), 81 deletions(-) delete mode 100644 certServiceClient/src/main/java/org/onap/aaf/certservice/client/configuration/EnvValidationUtils.java (limited to 'certServiceClient/src/main/java') 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 create() throws ClientConfigurationException, CsrConfigurationException; +import java.util.regex.Pattern; + +public abstract class AbstractConfigurationFactory { + + 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 { +public class ClientConfigurationFactory extends AbstractConfigurationFactory { 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 { +public class CsrConfigurationFactory extends AbstractConfigurationFactory { private final EnvsForCsr envsForCsr; @@ -37,19 +34,18 @@ public class CsrConfigurationFactory implements AbstractConfigurationFactory 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 new CsrConfigurationException(CsrConfigurationEnvs.STATE + " is invalid.")); envsForCsr.getCountry() - .filter(EnvValidationUtils::isCountryValid) + .filter(this::isCountryValid) .map(configuration::setCountry) .orElseThrow(() -> new CsrConfigurationException(CsrConfigurationEnvs.COUNTRY + " is invalid.")); -- cgit 1.2.3-korg