2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.common.util;
23 import com.google.common.base.CharMatcher;
24 import org.apache.commons.collections.CollectionUtils;
25 import org.apache.commons.text.StringEscapeUtils;
26 import org.apache.commons.lang3.StringUtils;
27 import org.apache.commons.lang3.math.NumberUtils;
28 import org.apache.commons.text.WordUtils;
29 import org.apache.commons.validator.routines.UrlValidator;
30 import org.jsoup.Jsoup;
31 import org.jsoup.safety.Whitelist;
34 import java.util.regex.Pattern;
36 public class ValidationUtils {
38 private ValidationUtils() {
41 public static final Integer COMPONENT_NAME_MAX_LENGTH = 1024;
42 public static final Pattern COMPONENT_NAME_PATTERN = Pattern
43 .compile("^[\\w][\\w \\.\\-\\_\\:\\+]{0," + (COMPONENT_NAME_MAX_LENGTH-1) + "}$");
44 public static final Integer ADDITIONAL_INFORMATION_KEY_MAX_LENGTH = 50;
45 public static final Pattern ADDITIONAL_INFORMATION_KEY_PATTERN = Pattern
46 .compile("^[\\w\\s\\.\\-\\_]{1," + COMPONENT_NAME_MAX_LENGTH + "}$");
47 public static final Integer RSI_NAME_MAX_LENGTH = 1024;
48 public static final Pattern RSI_NAME_PATTERN = Pattern
49 .compile("^[\\w \\s\\.\\-\\_\\:\\+]{1," + RSI_NAME_MAX_LENGTH + "}$");
50 public static final Integer COMMENT_MAX_LENGTH = 256;
52 public static final Integer ICON_MAX_LENGTH = 25;
53 public static final Pattern ICON_PATTERN = Pattern.compile("^[\\w\\-]{1," + ICON_MAX_LENGTH + "}$");
54 public static final Integer PROJECT_CODE_MAX_LEGTH = 50;
55 public static final Pattern PROJECT_CODE_PATTERN = Pattern.compile("^[\\s\\w_.-]{5,50}$");
57 // USER_ID format : aannnX (where a=a-z or A-Z, n=0-9, and X=a-z,A-Z, or 0-9)
58 public static final Integer CONNTACT_ID_MAX_LENGTH = 50;
60 public static final Pattern CONTACT_ID_PATTERN = Pattern.compile("^[\\s\\w_.-]{1,50}$");
61 public static final Pattern OCTET_PATTERN = Pattern.compile("%[a-fA-F0-9]{2}");
62 public static final Pattern NONE_UTF8_PATTERN = Pattern.compile("[^\\x00-\\x7F]+");
63 public static final Pattern URL_INVALIDE_PATTERN = Pattern.compile("[,#?&@$<>~^`\\\\\\[\\]{}|\")(*!+=;%]+");// ,#?&@$<>~^`\\[]{}|")(*!
65 public static final Pattern ENGLISH_PATTERN = Pattern.compile("^[\\p{Graph}\\x20]+$");
66 public static final Pattern COMMENT_PATTERN = Pattern.compile("^[\\u0000-\\u00BF]{1,1024}$");
67 public static final Pattern SERVICE_METADATA_PATTERN = Pattern.compile("^[\\x20-\\x21\\x23-\\x29\\x2B-\\x2E\\x30-\\x39\\x3B\\x3D\\x40-\\x5B\\x5D-\\x7B\\x7D-\\xFF]{1,256}");
68 public static final Integer COMPONENT_DESCRIPTION_MAX_LENGTH = 1024;
69 public static final Integer SERVICE_TYPE_MAX_LENGTH = 256;
70 public static final Integer SERVICE_ROLE_MAX_LENGTH = 256;
71 public static final Integer SERVICE_FUNCTION_MAX_LENGTH = 256;
72 public static final Integer SERVICE_NAMING_POLICY_MAX_SIZE = 100;
74 public static final Integer TAG_MAX_LENGTH = 1024;
75 public static final Integer TAG_LIST_MAX_LENGTH = 1024;
76 public static final Integer VENDOR_NAME_MAX_LENGTH = 60;
77 public static final Pattern VENDOR_NAME_PATTERN = Pattern
78 .compile("^[\\x20-\\x21\\x23-\\x29\\x2B-\\x2E\\x30-\\x39\\x3B\\x3D\\x40-\\x5B\\x5D-\\x7B\\x7D-\\xFF]+$");
79 public static final Integer VENDOR_RELEASE_MAX_LENGTH = 25;
80 public static final Pattern VENDOR_RELEASE_PATTERN = Pattern
81 .compile("^[\\x20-\\x21\\x23-\\x29\\x2B-\\x2E\\x30-\\x39\\x3B\\x3D\\x40-\\x5B\\x5D-\\x7B\\x7D-\\xFF]+$");
82 public static final Integer RESOURCE_VENDOR_MODEL_NUMBER_MAX_LENGTH = 65;
84 public static final Pattern CLEAN_FILENAME_PATTERN = Pattern.compile("[\\x00-\\x1f\\x80-\\x9f\\x5c/<?>\\*:|\"/]+");
86 public static final Pattern DASH_PATTERN = Pattern.compile("[-]+");
87 public static final Pattern UNDERSCORE_PATTERN = Pattern.compile("[_]+");
88 public static final Pattern PLUS_PATTERN = Pattern.compile("[+]+");
89 public static final Pattern SPACE_PATTERN = Pattern.compile("[ ]+");
90 public static final Pattern AMP_PATTERN = Pattern.compile("[&]+");
91 public static final Pattern DOT_PATTERN = Pattern.compile("[\\.]+");
92 public static final Pattern APOST_PATTERN = Pattern.compile("[']+");
93 public static final Pattern HASHTAG_PATTERN = Pattern.compile("[#]+");
94 public static final Pattern EQUAL_PATTERN = Pattern.compile("[=]+");
95 public static final Pattern COLON_PATTERN = Pattern.compile("[:]+");
96 public static final Pattern AT_PATTERN = Pattern.compile("[@]+");
97 public static final Pattern AND_PATTERN = Pattern.compile(" [aA][Nn][Dd] ");
98 private static final Set<String> CATEGORY_CONJUNCTIONS = new HashSet<>(
99 Arrays.asList("of", "to", "for", "as", "a", "an", "the"));
101 public static final Pattern COST_PATTERN = Pattern.compile("^[0-9]{1,5}\\.[0-9]{1,3}$");
102 public static final Pattern ARTIFACT_LABEL_PATTERN = Pattern.compile("^[a-zA-Z0-9 \\-@+]+$");
103 public static final Integer ARTIFACT_LABEL_LENGTH = 255;
104 public static final Pattern ARTIFACT_DISPLAY_NAME_PATTERN = Pattern.compile("^[a-zA-Z0-9][a-zA-Z0-9 &\\.'#=:@_\\-+]+$");
105 public static final Pattern CATEGORY_LABEL_PATTERN = Pattern.compile("^[a-zA-Z0-9][a-zA-Z0-9 &\\.'#=:@_\\-+]+$");
106 public static final Integer CATEGORY_LABEL_MIN_LENGTH = 3;
107 public static final Integer CATEGORY_LABEL_MAX_LENGTH = 25;
109 public static final Pattern COMPONENT_NAME_DELIMETER_PATTERN = Pattern.compile("[\\.\\-\\_]+");
110 public static final Pattern COMPONENT_INCTANCE_NAME_DELIMETER_PATTERN = Pattern.compile("[\\.\\-]+");
111 public static final Pattern PRODUCT_NAME_DELIMETER_PATTERN = Pattern.compile("[\\.\\-\\_&=#@':\\[\\]\\+]+");
112 public static final Integer CONSUMER_NAME_MAX_LENGTH = 255;
114 public static final Pattern CONSUMER_NAME_PATTERN = Pattern.compile("^[\\w]+[\\w\\.\\-]*$");
115 public static final Integer CONSUMER_SALT_LENGTH = 32;
116 public static final Integer CONSUMER_PASSWORD_LENGTH = 64;
117 public static final Pattern CONSUMER_PASS_SALT_PATTERN = Pattern.compile("^[a-z0-9]+$");
118 public static final Pattern FLOAT_PATTERN = Pattern.compile("^[\\d]+[\\.]{1}[\\d]+$");
119 public static final Pattern CERTIFIED_VERSION_PATTERN = Pattern.compile("^[1-9][0-9]*\\.0$");
120 public static final Pattern MINOR_VERSION_PATTERN = Pattern.compile("^0\\.[1-9][0-9]*$");
121 public static final Pattern TAGS_PATTERN = Pattern.compile("<[^><]*>");
122 public static final Pattern TAG_PATTERN = Pattern.compile("^[\\s\\w_.-]{1,1024}$");
124 public static final Integer ARTIFACT_NAME_LENGTH = 255;
125 public static final Integer API_URL_LENGTH = 100;
126 public static final Integer ARTIFACT_DESCRIPTION_MAX_LENGTH = 256;
128 public static final Integer PRODUCT_FULL_NAME_MIN_LENGTH = 4;
129 public static final Integer PRODUCT_FULL_NAME_MAX_LENGTH = 100;
130 public static final Integer FORWARDING_PATH_NAME_MAX_LENGTH = 100;
131 public static final Pattern FORWARDING_PATH_NAME_PATTERN = Pattern.compile("^[\\w][\\w \\.\\-\\_\\:\\+]{0," + (FORWARDING_PATH_NAME_MAX_LENGTH-1) + "}$");
133 public static final Integer POLICY_MAX_LENGTH = 1024;
134 public static final Pattern POLICY_NAME_PATTERN = Pattern
135 .compile("^[\\w][\\w \\.\\-\\_\\:\\+]{0," + (POLICY_MAX_LENGTH-1) + "}$");
137 public static boolean validateArtifactLabel(String label) {
138 return ARTIFACT_LABEL_PATTERN.matcher(label).matches();
141 public static boolean validateArtifactDisplayName(String displayName) {
142 return ARTIFACT_DISPLAY_NAME_PATTERN.matcher(displayName).matches();
145 public static String cleanUpText(String text){
146 text = removeNoneUtf8Chars(text);
147 text = normaliseWhitespace(text);
148 text = stripOctets(text);
149 text = removeHtmlTagsOnly(text);
153 public static boolean validateTagPattern(String tag) {
154 return TAG_PATTERN.matcher(tag).matches();
157 public static boolean validateServiceMetadata(String metadataField) {
158 return SERVICE_METADATA_PATTERN.matcher(metadataField).matches();
161 public static boolean validateCommentPattern(String comment) {
162 return COMMENT_PATTERN.matcher(comment).matches();
165 public static boolean validateCategoryDisplayNameFormat(String label) {
168 label = label.trim();
169 res = CATEGORY_LABEL_PATTERN.matcher(label).matches();
174 public static String normalizeCategoryName4Display(String str) {
177 str = DASH_PATTERN.matcher(str).replaceAll("-");
178 str = UNDERSCORE_PATTERN.matcher(str).replaceAll("_");
179 str = AMP_PATTERN.matcher(str).replaceAll("&");
180 str = PLUS_PATTERN.matcher(str).replaceAll("+");
181 str = DOT_PATTERN.matcher(str).replaceAll(".");
182 str = APOST_PATTERN.matcher(str).replaceAll("'");
183 str = HASHTAG_PATTERN.matcher(str).replaceAll("#");
184 str = EQUAL_PATTERN.matcher(str).replaceAll("=");
185 str = COLON_PATTERN.matcher(str).replaceAll(":");
186 str = AT_PATTERN.matcher(str).replaceAll("@");
187 str = normaliseWhitespace(str);
188 str = AND_PATTERN.matcher(str).replaceAll(" & ");
191 StringBuilder sb = new StringBuilder();
192 String[] split = str.split(" ");
193 for (int i = 0; i < split.length; i++) {
194 String splitted = split[i];
195 String lowerCase = splitted.toLowerCase();
196 // BANK OF AMERICA --> BANK of AMERICA ("of" is lowercased), but
197 // OF BANK OF AMERICA --> OF BANK of AMERICA (first "OF" is not
198 // lowercased because it's first word)
199 // Agreed with Ella, 26/11/15
200 if ((i > 0) && CATEGORY_CONJUNCTIONS.contains(lowerCase)) {
201 sb.append(lowerCase);
203 sb.append(WordUtils.capitalize(splitted));
207 str = sb.toString().trim();
212 public static String normalizeCategoryName4Uniqueness(String str) {
213 return str.toLowerCase();
216 public static boolean validateCategoryDisplayNameLength(String label) {
217 return (label != null && label.length() >= CATEGORY_LABEL_MIN_LENGTH
218 && label.length() <= CATEGORY_LABEL_MAX_LENGTH);
221 public static boolean validateProductFullNameLength(String fullName) {
222 return (fullName != null && fullName.length() >= PRODUCT_FULL_NAME_MIN_LENGTH
223 && fullName.length() <= PRODUCT_FULL_NAME_MAX_LENGTH);
226 public static boolean validateArtifactLabelLength(String label) {
227 return label.length() > 0 && label.length() <= ARTIFACT_LABEL_LENGTH;
230 public static boolean validateResourceInstanceNameLength(String resourceInstanceName) {
231 return resourceInstanceName.length() <= RSI_NAME_MAX_LENGTH;
234 public static boolean validateResourceInstanceName(String resourceInstanceName) {
235 return RSI_NAME_PATTERN.matcher(resourceInstanceName).matches();
238 public static boolean validateUrlLength(String url) {
239 return url.length() <= API_URL_LENGTH;
242 public static boolean validateArtifactNameLength(String artifactName) {
243 return (artifactName.length() <= ARTIFACT_NAME_LENGTH && artifactName.length() > 0);
246 public static boolean validateComponentNamePattern(String componentName) {
247 return COMPONENT_NAME_PATTERN.matcher(componentName).matches();
250 public static boolean validateComponentNameLength(String componentName) {
251 return componentName.length() <= COMPONENT_NAME_MAX_LENGTH;
254 public static boolean validateIcon(String icon) {
255 return ICON_PATTERN.matcher(icon).matches();
258 public static boolean validateIconLength(String icon) {
259 return icon.length() <= ICON_MAX_LENGTH;
262 public static boolean validateProjectCode(String projectCode) {
263 return PROJECT_CODE_PATTERN.matcher(projectCode).matches();
266 public static boolean validateProjectCodeLegth(String projectCode) {
267 return projectCode.length() <= PROJECT_CODE_MAX_LEGTH;
270 public static boolean validateContactId(String contactId) {
271 return CONTACT_ID_PATTERN.matcher(contactId).matches();
274 public static boolean validateCost(String cost) {
275 return COST_PATTERN.matcher(cost).matches();
278 public static String removeHtmlTags(String str) {
279 return Jsoup.clean(str, Whitelist.none());
282 public static String removeAllTags(String htmlText) {
284 return TAGS_PATTERN.matcher(htmlText).replaceAll("").trim();
287 public static String normaliseWhitespace(String str) {
288 StringBuilder sb = new StringBuilder(str.length());
289 appendNormalisedWhitespace(sb, str, false);
290 return sb.toString();
293 private static void appendNormalisedWhitespace(StringBuilder accum, String string, boolean stripLeading) {
294 boolean lastWasWhite = false;
295 boolean reachedNonWhite = false;
297 int len = string.length();
299 for (int i = 0; i < len; i+= Character.charCount(c)) {
300 c = string.codePointAt(i);
301 if (isWhitespace(c)) {
302 if ((stripLeading && !reachedNonWhite) || lastWasWhite)
308 accum.appendCodePoint(c);
309 lastWasWhite = false;
310 reachedNonWhite = true;
315 private static boolean isWhitespace(int c){
319 public static String stripOctets(String str) {
320 return OCTET_PATTERN.matcher(str).replaceAll("");
323 public static String removeNoneUtf8Chars(String input) {
324 return NONE_UTF8_PATTERN.matcher(input).replaceAll("");
327 public static boolean validateIsEnglish(String input) {
328 return ENGLISH_PATTERN.matcher(input).matches();
331 public static boolean validateIsAscii(String input) {
333 return CharMatcher.ASCII.matchesAllOf(input);
336 public static String convertHtmlTagsToEntities(String input) {
337 return StringEscapeUtils.escapeHtml4(input);
340 public static List<String> removeDuplicateFromList(List<String> list) {
341 Set<String> hs = new LinkedHashSet<>(list);
348 public static boolean validateTagLength(String tag) {
350 return tag.length() <= TAG_MAX_LENGTH;
355 public static boolean validateTagListLength(int tagListLength) {
356 return tagListLength <= TAG_LIST_MAX_LENGTH;
359 public static boolean validateDescriptionLength(String description) {
360 return description.length() <= COMPONENT_DESCRIPTION_MAX_LENGTH;
363 public static boolean validateStringNotEmpty(String value) {
364 if ((value == null) || (value.isEmpty())) {
370 public static boolean validateListNotEmpty(List<?> list) {
371 if ((list == null) || (list.isEmpty())) {
377 public static boolean validateVendorName(String vendorName) {
378 return VENDOR_NAME_PATTERN.matcher(vendorName).matches();
381 public static boolean validateVendorNameLength(String vendorName) {
382 return vendorName.length() <= VENDOR_NAME_MAX_LENGTH;
385 public static boolean validateResourceVendorModelNumberLength(String resourceVendorModelNumber) {
386 return resourceVendorModelNumber.length() <= RESOURCE_VENDOR_MODEL_NUMBER_MAX_LENGTH;
389 public static boolean validateVendorRelease(String vendorRelease) {
390 return VENDOR_RELEASE_PATTERN.matcher(vendorRelease).matches();
393 public static boolean validateVendorReleaseLength(String vendorRelease) {
394 return vendorRelease.length() <= VENDOR_RELEASE_MAX_LENGTH;
397 public static boolean validateServiceTypeLength(String serviceType) {
398 return serviceType.length() <= SERVICE_TYPE_MAX_LENGTH;
401 public static boolean validateServiceRoleLength(String serviceRole) {
402 return serviceRole.length() <= SERVICE_ROLE_MAX_LENGTH;
405 public static boolean validateServiceFunctionLength(String serviceFunction) {
406 return serviceFunction.length() <= SERVICE_FUNCTION_MAX_LENGTH;
409 public static boolean validateServiceNamingPolicyLength(String namingPolicy) {
410 return namingPolicy.length() <= SERVICE_NAMING_POLICY_MAX_SIZE;
412 public static boolean hasBeenCertified(String version) {
413 return NumberUtils.toDouble(version) >= 1;
416 public static String normaliseComponentName(String name) {
417 String[] split = splitComponentName(name);
418 StringBuilder sb = new StringBuilder();
419 for (String splitElement : split) {
420 sb.append(splitElement);
422 return sb.toString();
426 public static String normalizeComponentInstanceName(String name) {
427 String[] split = splitComponentInstanceName(name);
428 StringBuilder sb = new StringBuilder();
429 for (String splitElement : split) {
430 sb.append(splitElement);
432 return sb.toString();
436 private static String[] splitComponentName(String name) {
437 String normalizedName = name.toLowerCase();
438 normalizedName = COMPONENT_NAME_DELIMETER_PATTERN.matcher(normalizedName).replaceAll(" ");
439 return normalizedName.split(" ");
442 private static String[] splitComponentInstanceName(String name) {
443 String normalizedName = name.toLowerCase();
444 normalizedName = COMPONENT_INCTANCE_NAME_DELIMETER_PATTERN.matcher(normalizedName).replaceAll(" ");
445 return normalizedName.split(" ");
448 public static String convertToSystemName(String name) {
449 String[] split = splitComponentName(name);
450 StringBuilder sb = new StringBuilder();
451 for (String splitElement : split) {
452 String capitalize = WordUtils.capitalize(splitElement);
453 sb.append(capitalize);
455 return sb.toString();
458 public static String normalizeFileName(String filename) {
459 return cleanFileName(filename);
463 private static String cleanFileName(String str) {
464 str = CLEAN_FILENAME_PATTERN.matcher(str).replaceAll("");
465 str = normaliseWhitespace(str);
466 str = SPACE_PATTERN.matcher(str).replaceAll("-");
467 str = DASH_PATTERN.matcher(str).replaceAll("-");
468 str = StringUtils.strip(str, "-_ .");
473 public static boolean validateUrl(String url) {
475 UrlValidator urlValidator = new UrlValidator();
476 if (!urlValidator.isValid(url)) {
479 if (NONE_UTF8_PATTERN.matcher(url).find()) {
483 if (URL_INVALIDE_PATTERN.matcher(url).find()) {
490 public static String cleanArtifactDisplayName(String strIn) {
491 String str = DASH_PATTERN.matcher(strIn).replaceAll("-");
492 str = UNDERSCORE_PATTERN.matcher(str).replaceAll("_");
493 str = PLUS_PATTERN.matcher(str).replaceAll("+");
494 str = normaliseWhitespace(str);
500 public static String normalizeArtifactLabel(String strIn) {
502 String str = DASH_PATTERN.matcher(strIn).replaceAll("");
503 str = UNDERSCORE_PATTERN.matcher(str).replaceAll("");
504 str = PLUS_PATTERN.matcher(str).replaceAll("");
505 str = SPACE_PATTERN.matcher(str).replaceAll("");
506 str = DOT_PATTERN.matcher(str).replaceAll("");
507 str = str.toLowerCase();
512 public static boolean validateAdditionalInformationKeyName(String str) {
513 return ADDITIONAL_INFORMATION_KEY_PATTERN.matcher(str).matches();
516 public static String normalizeAdditionalInformation(String str) {
518 str = DASH_PATTERN.matcher(str).replaceAll("-");
519 str = UNDERSCORE_PATTERN.matcher(str).replaceAll("_");
520 str = normaliseWhitespace(str);
525 public static boolean validateLength(String str, int length) {
529 return str.length() <= length;
532 public static boolean validateConsumerName(String consumerName) {
533 return CONSUMER_NAME_PATTERN.matcher(consumerName).matches();
536 public static boolean isUTF8Str(String str) {
537 if (NONE_UTF8_PATTERN.matcher(str).find()) {
543 public static boolean validateConsumerPassSalt(String consumerSalt) {
544 return CONSUMER_PASS_SALT_PATTERN.matcher(consumerSalt).matches();
547 public static boolean isFloatNumber(String number) {
548 return FLOAT_PATTERN.matcher(number).matches();
551 public static boolean validateCertifiedVersion(String version) {
552 return (version != null && CERTIFIED_VERSION_PATTERN.matcher(version).matches());
555 public static boolean validateMinorVersion(String version) {
556 return (version != null && MINOR_VERSION_PATTERN.matcher(version).matches());
559 public static boolean validateCategoryIconNotEmpty(List<String> categoryIcons) {
560 return CollectionUtils.isEmpty(categoryIcons);
563 public static String normaliseProductName(String name) {
564 String[] split = splitComponentName(PRODUCT_NAME_DELIMETER_PATTERN, name);
565 StringBuilder sb = new StringBuilder();
566 for (String splitElement : split) {
567 sb.append(splitElement);
569 return sb.toString();
573 private static String[] splitComponentName(Pattern pattern, String name) {
574 String normalizedName = name.toLowerCase();
575 normalizedName = pattern.matcher(normalizedName).replaceAll(" ");
576 return normalizedName.split(" ");
579 public static String removeHtmlTagsOnly(String htmlText) {
580 return HtmlCleaner.stripHtml(htmlText, false);
583 public static boolean validateForwardingPathNamePattern(String forwardingPathName) {
584 return FORWARDING_PATH_NAME_PATTERN.matcher(forwardingPathName).matches();