X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=PolicyEngineUtils%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Futils%2FPolicyUtils.java;h=67210c81418f8a1308f9d1359aeacdb8a57bdc26;hb=06d41227e6e745cf9dfbe8df2b29fd67992060da;hp=51409b6f796653d2263ee08c9382d0afe24fe36a;hpb=a3ce785c96bdb32f7657286b9ef00542597a6e04;p=policy%2Fengine.git diff --git a/PolicyEngineUtils/src/main/java/org/onap/policy/utils/PolicyUtils.java b/PolicyEngineUtils/src/main/java/org/onap/policy/utils/PolicyUtils.java index 51409b6f7..67210c814 100644 --- a/PolicyEngineUtils/src/main/java/org/onap/policy/utils/PolicyUtils.java +++ b/PolicyEngineUtils/src/main/java/org/onap/policy/utils/PolicyUtils.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * PolicyEngineUtils * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2018 AT&T Intellectual Property. 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. @@ -31,6 +31,7 @@ import java.util.StringTokenizer; import java.util.regex.Matcher; import java.util.regex.Pattern; +import javax.xml.XMLConstants; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; @@ -53,12 +54,12 @@ import com.google.gson.JsonSyntaxException; public class PolicyUtils { private static final Logger LOGGER = FlexLogger.getLogger(PolicyUtils.class); - + public static final String CATCH_EXCEPTION = "PE500: An exception was caught."; public static final String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; private static final String PACKAGE_ERROR = "mismatched input '{' expecting one of the following tokens: '[package"; - private static final String SUCCESS = "success"; + public static final String SUCCESS = "success"; private PolicyUtils(){ // Private Constructor @@ -114,7 +115,7 @@ public class PolicyUtils { public static String[] decodeBasicEncoding(String encodedValue) throws UnsupportedEncodingException { if(encodedValue!=null && encodedValue.contains("Basic ")){ String encodedUserPassword = encodedValue.replaceFirst("Basic" + " ", ""); - String usernameAndPassword = null; + String usernameAndPassword; byte[] decodedBytes = Base64.getDecoder().decode(encodedUserPassword); usernameAndPassword = new String(decodedBytes, "UTF-8"); StringTokenizer tokenizer = new StringTokenizer(usernameAndPassword, ":"); @@ -156,6 +157,21 @@ public class PolicyUtils { return SUCCESS; } + /** + * Validate a field (accepts Dash) if it contains unacceptable policy input and return "success" if good. + * + * @param field + * @return + */ + public static String policySpecialCharWithDashValidator(String field){ + String error; + if ("".equals(field) || !field.matches("^[a-zA-Z0-9_-]*$")) { + error = "The Value in Required Field will allow only '{0-9}, {a-z}, {A-Z}, _, -' following set of Combinations"; + return error; + } + return SUCCESS; + } + /** * Validate the XACML description tag and return "success" if good. * @@ -180,7 +196,7 @@ public class PolicyUtils { * @return */ public static boolean containsNonAsciiEmptyChars(String value) { - return (value == null|| value.contains(" ") || "".equals(value.trim())|| !CharMatcher.ASCII.matchesAllOf((CharSequence) value))? true:false; + return (value == null || value.contains(" ") || "".equals(value.trim()) || !CharMatcher.ascii().matchesAllOf((CharSequence) value)) ? true : false; } /** @@ -194,7 +210,9 @@ public class PolicyUtils { return false; } for (char c : number.toCharArray()){ - if (!Character.isDigit(c)) return false; + if (!Character.isDigit(c)) { + return false; + } } return true; } @@ -276,10 +294,15 @@ public class PolicyUtils { * @return */ public static boolean isXMLValid(String data) { + if(data == null || data.isEmpty()){ + return false; + } SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(false); factory.setNamespaceAware(true); - try { + + try { + factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); SAXParser parser = factory.newSAXParser(); XMLReader reader = parser.getXMLReader(); reader.setErrorHandler(new XMLErrorHandler());