a0403de8ff53699332997786219aaf358b1dc7ec
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / common / util / ValidationUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.openecomp.sdc.common.util;
21
22 import com.google.common.base.CharMatcher;
23 import java.util.Arrays;
24 import java.util.HashSet;
25 import java.util.LinkedHashSet;
26 import java.util.List;
27 import java.util.Set;
28 import java.util.regex.Pattern;
29 import org.apache.commons.collections.CollectionUtils;
30 import org.apache.commons.lang3.StringUtils;
31 import org.apache.commons.lang3.math.NumberUtils;
32 import org.apache.commons.text.StringEscapeUtils;
33 import org.apache.commons.text.WordUtils;
34 import org.apache.commons.validator.routines.UrlValidator;
35 import org.jsoup.Jsoup;
36 import org.jsoup.safety.Safelist;
37
38 public class ValidationUtils {
39
40     public static final Integer COMPONENT_NAME_MAX_LENGTH = 1024;
41     public static final Pattern COMPONENT_NAME_PATTERN = Pattern.compile("^[\\w][\\w \\.\\-\\_\\:\\+]{0," + (COMPONENT_NAME_MAX_LENGTH - 1) + "}$");
42     public static final Integer ADDITIONAL_INFORMATION_KEY_MAX_LENGTH = 50;
43     public static final Pattern ADDITIONAL_INFORMATION_KEY_PATTERN = Pattern.compile("^[\\w\\s\\.\\-\\_]{1," + COMPONENT_NAME_MAX_LENGTH + "}$");
44     public static final Integer RSI_NAME_MAX_LENGTH = 1024;
45     public static final Pattern RSI_NAME_PATTERN = Pattern.compile("^[\\w \\s\\.\\-\\_\\:\\+]{1," + RSI_NAME_MAX_LENGTH + "}$");
46     public static final Integer COMMENT_MAX_LENGTH = 256;
47     public static final Integer ICON_MAX_LENGTH = 25;
48     public static final Pattern ICON_PATTERN = Pattern.compile("^[\\w\\-]{1," + ICON_MAX_LENGTH + "}$");
49     public static final Integer PROJECT_CODE_MAX_LEGTH = 50;
50     public static final Pattern PROJECT_CODE_PATTERN = Pattern.compile("^[\\s\\w_.-]{5,50}$");
51     // USER_ID format : aannnX (where a=a-z or A-Z, n=0-9, and X=a-z,A-Z, or 0-9)
52     public static final Integer CONNTACT_ID_MAX_LENGTH = 50;
53     public static final Pattern CONTACT_ID_PATTERN = Pattern.compile("^[\\s\\w_.-]{1,50}$");
54     public static final Pattern OCTET_PATTERN = Pattern.compile("%[a-fA-F0-9]{2}");
55     public static final Pattern NONE_UTF8_PATTERN = Pattern.compile("[^\\x00-\\x7F]+");
56     public static final Pattern URL_INVALIDE_PATTERN = Pattern.compile("[,#?&@$<>~^`\\\\\\[\\]{}|\")(*!+=;%]+");// ,#?&@$<>~^`\\[]{}|")(*!
57     public static final Pattern ENGLISH_PATTERN = Pattern.compile("^[\\p{Graph}\\x20]+$");
58     public static final Pattern COMMENT_PATTERN = Pattern.compile("^[\\u0000-\\u00BF]{1,1024}$");
59     public static final Pattern SERVICE_METADATA_PATTERN = Pattern
60         .compile("^[\\x20-\\x21\\x23-\\x29\\x2B-\\x2E\\x30-\\x39\\x3B\\x3D\\x40-\\x5B\\x5D-\\x7B\\x7D-\\xFF]{1,256}");
61     public static final Integer COMPONENT_DESCRIPTION_MAX_LENGTH = 1024;
62     public static final Integer SERVICE_TYPE_MAX_LENGTH = 256;
63     public static final Integer SERVICE_ROLE_MAX_LENGTH = 256;
64     public static final Integer SERVICE_FUNCTION_MAX_LENGTH = 256;
65     public static final Integer SERVICE_NAMING_POLICY_MAX_SIZE = 100;
66     public static final Integer TAG_MAX_LENGTH = 1024;
67     public static final Integer TAG_LIST_MAX_LENGTH = 1024;
68     public static final Integer VENDOR_NAME_MAX_LENGTH = 60;
69     public static final Pattern VENDOR_NAME_PATTERN = Pattern
70         .compile("^[\\x20-\\x21\\x23-\\x29\\x2B-\\x2E\\x30-\\x39\\x3B\\x3D\\x40-\\x5B\\x5D-\\x7B\\x7D-\\xFF]+$");
71     public static final Integer VENDOR_RELEASE_MAX_LENGTH = 25;
72     public static final Pattern VENDOR_RELEASE_PATTERN = Pattern
73         .compile("^[\\x20-\\x21\\x23-\\x29\\x2B-\\x2E\\x30-\\x39\\x3B\\x3D\\x40-\\x5B\\x5D-\\x7B\\x7D-\\xFF]+$");
74     public static final Integer RESOURCE_VENDOR_MODEL_NUMBER_MAX_LENGTH = 65;
75     public static final Pattern CLEAN_FILENAME_PATTERN = Pattern.compile("[\\x00-\\x1f\\x80-\\x9f\\x5c/<?>\\*:|\"/]+");
76     public static final Pattern YANG_MODULE_3GPP_PATTERN = Pattern.compile("^(_3gpp).*$");
77     public static final Pattern DASH_PATTERN = Pattern.compile("[-]+");
78     public static final Pattern UNDERSCORE_PATTERN = Pattern.compile("[_]+");
79     public static final Pattern PLUS_PATTERN = Pattern.compile("[+]+");
80     public static final Pattern SPACE_PATTERN = Pattern.compile("[ ]+");
81     public static final Pattern AMP_PATTERN = Pattern.compile("[&]+");
82     public static final Pattern DOT_PATTERN = Pattern.compile("[\\.]+");
83     public static final Pattern APOST_PATTERN = Pattern.compile("[']+");
84     public static final Pattern HASHTAG_PATTERN = Pattern.compile("[#]+");
85     public static final Pattern EQUAL_PATTERN = Pattern.compile("[=]+");
86     public static final Pattern COLON_PATTERN = Pattern.compile("[:]+");
87     public static final Pattern AT_PATTERN = Pattern.compile("[@]+");
88     public static final Pattern AND_PATTERN = Pattern.compile(" [aA][Nn][Dd] ");
89     public static final Pattern COST_PATTERN = Pattern.compile("^[0-9]{1,5}\\.[0-9]{1,3}$");
90     public static final Pattern ARTIFACT_LABEL_PATTERN = Pattern.compile("^[a-zA-Z0-9 \\-@+]+$");
91     public static final Integer ARTIFACT_LABEL_LENGTH = 255;
92     public static final Pattern ARTIFACT_DISPLAY_NAME_PATTERN = Pattern.compile("^[a-zA-Z0-9][a-zA-Z0-9 &\\.'#=:@_\\-+]+$");
93     public static final Pattern CATEGORY_LABEL_PATTERN = Pattern.compile("^[a-zA-Z0-9][a-zA-Z0-9 &\\.'#=:@_\\-+]+$");
94     public static final Integer CATEGORY_LABEL_MIN_LENGTH = 3;
95     public static final Integer CATEGORY_LABEL_MAX_LENGTH = 25;
96     public static final Pattern COMPONENT_NAME_DELIMETER_PATTERN = Pattern.compile("[\\.\\-\\_]+");
97     public static final Pattern COMPONENT_INCTANCE_NAME_DELIMETER_PATTERN = Pattern.compile("[\\.\\-]+");
98     public static final Pattern PRODUCT_NAME_DELIMETER_PATTERN = Pattern.compile("[\\.\\-\\_&=#@':\\[\\]\\+]+");
99     public static final Integer CONSUMER_NAME_MAX_LENGTH = 255;
100     public static final Pattern CONSUMER_NAME_PATTERN = Pattern.compile("^[\\w]+[\\w\\.\\-]*$");
101     public static final Integer CONSUMER_SALT_LENGTH = 32;
102     public static final Integer CONSUMER_PASSWORD_LENGTH = 64;
103     public static final Pattern CONSUMER_PASS_SALT_PATTERN = Pattern.compile("^[a-z0-9]+$");
104     public static final Pattern FLOAT_PATTERN = Pattern.compile("^[\\d]+[\\.]{1}[\\d]+$");
105     public static final Pattern CERTIFIED_VERSION_PATTERN = Pattern.compile("^[1-9][0-9]*\\.0$");
106     public static final Pattern MINOR_VERSION_PATTERN = Pattern.compile("^0\\.[1-9][0-9]*$");
107     public static final Pattern TAGS_PATTERN = Pattern.compile("<[^><]*>");
108     public static final Pattern TAG_PATTERN = Pattern.compile("^[\\s\\w_.-]{1,1024}$");
109     public static final Integer ARTIFACT_NAME_LENGTH = 255;
110     public static final Integer API_URL_LENGTH = 100;
111     public static final Integer ARTIFACT_DESCRIPTION_MAX_LENGTH = 256;
112     public static final Integer PRODUCT_FULL_NAME_MIN_LENGTH = 4;
113     public static final Integer PRODUCT_FULL_NAME_MAX_LENGTH = 100;
114     public static final Integer FORWARDING_PATH_NAME_MAX_LENGTH = 100;
115     public static final Pattern FORWARDING_PATH_NAME_PATTERN = Pattern
116         .compile("^[\\w][\\w \\.\\-\\_\\:\\+]{0," + (FORWARDING_PATH_NAME_MAX_LENGTH - 1) + "}$");
117     public static final Integer POLICY_MAX_LENGTH = 1024;
118     public static final Pattern POLICY_NAME_PATTERN = Pattern.compile("^[\\w][\\w \\.\\-\\_\\:\\+]{0," + (POLICY_MAX_LENGTH - 1) + "}$");
119     private static final Set<String> CATEGORY_CONJUNCTIONS = new HashSet<>(Arrays.asList("of", "to", "for", "as", "a", "an", "the"));
120
121     private ValidationUtils() {
122     }
123
124     public static boolean validateArtifactLabel(String label) {
125         return ARTIFACT_LABEL_PATTERN.matcher(label).matches();
126     }
127
128     public static boolean validateArtifactDisplayName(String displayName) {
129         return ARTIFACT_DISPLAY_NAME_PATTERN.matcher(displayName).matches();
130     }
131
132     public static String cleanUpText(String text) {
133         text = removeNoneUtf8Chars(text);
134         text = normaliseWhitespace(text);
135         text = stripOctets(text);
136         text = removeHtmlTagsOnly(text);
137         return text;
138     }
139
140     public static boolean validateTagPattern(String tag) {
141         return TAG_PATTERN.matcher(tag).matches();
142     }
143
144     public static boolean validateServiceMetadata(String metadataField) {
145         return SERVICE_METADATA_PATTERN.matcher(metadataField).matches();
146     }
147
148     public static boolean validateCommentPattern(String comment) {
149         return COMMENT_PATTERN.matcher(comment).matches();
150     }
151
152     public static boolean validateCategoryDisplayNameFormat(String label) {
153         boolean res = true;
154         if (label != null) {
155             label = label.trim();
156             res = CATEGORY_LABEL_PATTERN.matcher(label).matches();
157         }
158         return res;
159     }
160
161     public static String normalizeCategoryName4Display(String str) {
162         if (str != null) {
163             str = str.trim();
164             str = DASH_PATTERN.matcher(str).replaceAll("-");
165             str = UNDERSCORE_PATTERN.matcher(str).replaceAll("_");
166             str = AMP_PATTERN.matcher(str).replaceAll("&");
167             str = PLUS_PATTERN.matcher(str).replaceAll("+");
168             str = DOT_PATTERN.matcher(str).replaceAll(".");
169             str = APOST_PATTERN.matcher(str).replaceAll("'");
170             str = HASHTAG_PATTERN.matcher(str).replaceAll("#");
171             str = EQUAL_PATTERN.matcher(str).replaceAll("=");
172             str = COLON_PATTERN.matcher(str).replaceAll(":");
173             str = AT_PATTERN.matcher(str).replaceAll("@");
174             str = normaliseWhitespace(str);
175             str = AND_PATTERN.matcher(str).replaceAll(" & ");
176             // Case normalizing
177             StringBuilder sb = new StringBuilder();
178             String[] split = str.split(" ");
179             for (int i = 0; i < split.length; i++) {
180                 String splitted = split[i];
181                 String lowerCase = splitted.toLowerCase();
182                 // BANK OF AMERICA --> BANK of AMERICA ("of" is lowercased), but
183
184                 // OF BANK OF AMERICA --> OF BANK of AMERICA (first "OF" is not
185
186                 // lowercased because it's first word)
187
188                 // Agreed with Ella, 26/11/15
189                 if ((i > 0) && CATEGORY_CONJUNCTIONS.contains(lowerCase)) {
190                     sb.append(lowerCase);
191                 } else {
192                     sb.append(WordUtils.capitalize(splitted));
193                 }
194                 sb.append(" ");
195             }
196             str = sb.toString().trim();
197         }
198         return str;
199     }
200
201     public static String normalizeCategoryName4Uniqueness(String str) {
202         return str.toLowerCase();
203     }
204
205     public static boolean validateCategoryDisplayNameLength(String label) {
206         return (label != null && label.length() >= CATEGORY_LABEL_MIN_LENGTH && label.length() <= CATEGORY_LABEL_MAX_LENGTH);
207     }
208
209     public static boolean validateProductFullNameLength(String fullName) {
210         return (fullName != null && fullName.length() >= PRODUCT_FULL_NAME_MIN_LENGTH && fullName.length() <= PRODUCT_FULL_NAME_MAX_LENGTH);
211     }
212
213     public static boolean validateArtifactLabelLength(String label) {
214         return label.length() > 0 && label.length() <= ARTIFACT_LABEL_LENGTH;
215     }
216
217     public static boolean validateResourceInstanceNameLength(String resourceInstanceName) {
218         return resourceInstanceName.length() <= RSI_NAME_MAX_LENGTH;
219     }
220
221     public static boolean validateResourceInstanceName(String resourceInstanceName) {
222         return RSI_NAME_PATTERN.matcher(resourceInstanceName).matches();
223     }
224
225     public static boolean validateUrlLength(String url) {
226         return url.length() <= API_URL_LENGTH;
227     }
228
229     public static boolean validateArtifactNameLength(String artifactName) {
230         return (artifactName.length() <= ARTIFACT_NAME_LENGTH && artifactName.length() > 0);
231     }
232
233     public static boolean validateComponentNamePattern(String componentName) {
234         return COMPONENT_NAME_PATTERN.matcher(componentName).matches();
235     }
236
237     public static boolean validateComponentNameLength(String componentName) {
238         return componentName.length() <= COMPONENT_NAME_MAX_LENGTH;
239     }
240
241     public static boolean validateIcon(String icon) {
242         return ICON_PATTERN.matcher(icon).matches();
243     }
244
245     public static boolean validateIconLength(String icon) {
246         return icon.length() <= ICON_MAX_LENGTH;
247     }
248
249     public static boolean validateProjectCode(String projectCode) {
250         return PROJECT_CODE_PATTERN.matcher(projectCode).matches();
251     }
252
253     public static boolean validateProjectCodeLegth(String projectCode) {
254         return projectCode.length() <= PROJECT_CODE_MAX_LEGTH;
255     }
256
257     public static boolean validateContactId(String contactId) {
258         return CONTACT_ID_PATTERN.matcher(contactId).matches();
259     }
260
261     public static boolean validateCost(String cost) {
262         return COST_PATTERN.matcher(cost).matches();
263     }
264
265     public static String removeHtmlTags(String str) {
266         return Jsoup.clean(str, Safelist.none());
267     }
268
269     public static String removeAllTags(String htmlText) {
270         return TAGS_PATTERN.matcher(htmlText).replaceAll("").trim();
271     }
272
273     public static String normaliseWhitespace(String str) {
274         StringBuilder sb = new StringBuilder(str.length());
275         appendNormalisedWhitespace(sb, str, false);
276         return sb.toString();
277     }
278
279     private static void appendNormalisedWhitespace(StringBuilder accum, String string, boolean stripLeading) {
280         boolean lastWasWhite = false;
281         boolean reachedNonWhite = false;
282         int len = string.length();
283         int c;
284         for (int i = 0; i < len; i += Character.charCount(c)) {
285             c = string.codePointAt(i);
286             if (isWhitespace(c)) {
287                 if ((stripLeading && !reachedNonWhite) || lastWasWhite) {
288                     continue;
289                 }
290                 accum.append(' ');
291                 lastWasWhite = true;
292             } else {
293                 accum.appendCodePoint(c);
294                 lastWasWhite = false;
295                 reachedNonWhite = true;
296             }
297         }
298     }
299
300     private static boolean isWhitespace(int c) {
301         return c == ' ';
302     }
303
304     public static String stripOctets(String str) {
305         return OCTET_PATTERN.matcher(str).replaceAll("");
306     }
307
308     public static String removeNoneUtf8Chars(String input) {
309         return NONE_UTF8_PATTERN.matcher(input).replaceAll("");
310     }
311
312     public static boolean validateIsEnglish(String input) {
313         return ENGLISH_PATTERN.matcher(input).matches();
314     }
315
316     public static boolean validateIsAscii(String input) {
317         return CharMatcher.ascii().matchesAllOf(input);
318     }
319
320     public static String convertHtmlTagsToEntities(String input) {
321         return StringEscapeUtils.escapeHtml4(input);
322     }
323
324     public static List<String> removeDuplicateFromList(List<String> list) {
325         Set<String> hs = new LinkedHashSet<>(list);
326         list.clear();
327         list.addAll(hs);
328         return list;
329     }
330
331     public static boolean validateTagLength(String tag) {
332         if (tag != null) {
333             return tag.length() <= TAG_MAX_LENGTH;
334         }
335         return false;
336     }
337
338     public static boolean validateTagListLength(int tagListLength) {
339         return tagListLength <= TAG_LIST_MAX_LENGTH;
340     }
341
342     public static boolean validateDescriptionLength(String description) {
343         return description.length() <= COMPONENT_DESCRIPTION_MAX_LENGTH;
344     }
345
346     public static boolean validateStringNotEmpty(String value) {
347         if ((value == null) || (value.isEmpty())) {
348             return false;
349         }
350         return true;
351     }
352
353     public static boolean validateListNotEmpty(List<?> list) {
354         if ((list == null) || (list.isEmpty())) {
355             return false;
356         }
357         return true;
358     }
359
360     public static boolean validateVendorName(String vendorName) {
361         return VENDOR_NAME_PATTERN.matcher(vendorName).matches();
362     }
363
364     public static boolean validateVendorNameLength(String vendorName) {
365         return vendorName.length() <= VENDOR_NAME_MAX_LENGTH;
366     }
367
368     public static boolean validateResourceVendorModelNumberLength(String resourceVendorModelNumber) {
369         return resourceVendorModelNumber.length() <= RESOURCE_VENDOR_MODEL_NUMBER_MAX_LENGTH;
370     }
371
372     public static boolean validateVendorRelease(String vendorRelease) {
373         return VENDOR_RELEASE_PATTERN.matcher(vendorRelease).matches();
374     }
375
376     public static boolean validateVendorReleaseLength(String vendorRelease) {
377         return vendorRelease.length() <= VENDOR_RELEASE_MAX_LENGTH;
378     }
379
380     public static boolean validateServiceTypeLength(String serviceType) {
381         return serviceType.length() <= SERVICE_TYPE_MAX_LENGTH;
382     }
383
384     public static boolean validateServiceRoleLength(String serviceRole) {
385         return serviceRole.length() <= SERVICE_ROLE_MAX_LENGTH;
386     }
387
388     public static boolean validateServiceFunctionLength(String serviceFunction) {
389         return serviceFunction.length() <= SERVICE_FUNCTION_MAX_LENGTH;
390     }
391
392     public static boolean validateServiceNamingPolicyLength(String namingPolicy) {
393         return namingPolicy.length() <= SERVICE_NAMING_POLICY_MAX_SIZE;
394     }
395
396     public static boolean hasBeenCertified(String version) {
397         return NumberUtils.toDouble(version) >= 1;
398     }
399
400     public static String normaliseComponentName(String name) {
401         String[] split = splitComponentName(name);
402         StringBuilder sb = new StringBuilder();
403         for (String splitElement : split) {
404             sb.append(splitElement);
405         }
406         return sb.toString();
407     }
408
409     public static String normalizeComponentInstanceName(String name) {
410         String[] split = splitComponentInstanceName(name);
411         StringBuilder sb = new StringBuilder();
412         for (String splitElement : split) {
413             sb.append(splitElement);
414         }
415         return sb.toString();
416     }
417
418     private static String[] splitComponentName(String name) {
419         String normalizedName = name.toLowerCase();
420         normalizedName = COMPONENT_NAME_DELIMETER_PATTERN.matcher(normalizedName).replaceAll(" ");
421         return normalizedName.split(" ");
422     }
423
424     private static String[] splitComponentInstanceName(String name) {
425         String normalizedName = name.toLowerCase();
426         normalizedName = COMPONENT_INCTANCE_NAME_DELIMETER_PATTERN.matcher(normalizedName).replaceAll(" ");
427         return normalizedName.split(" ");
428     }
429
430     public static String convertToSystemName(String name) {
431         String[] split = splitComponentName(name);
432         StringBuilder sb = new StringBuilder();
433         for (String splitElement : split) {
434             String capitalize = WordUtils.capitalize(splitElement);
435             sb.append(capitalize);
436         }
437         return sb.toString();
438     }
439
440     public static String normalizeFileName(String filename) {
441         return cleanFileName(filename);
442     }
443
444     private static String cleanFileName(String str) {
445         str = CLEAN_FILENAME_PATTERN.matcher(str).replaceAll("");
446         str = normaliseWhitespace(str);
447         str = SPACE_PATTERN.matcher(str).replaceAll("-");
448         str = DASH_PATTERN.matcher(str).replaceAll("-");
449         if (!YANG_MODULE_3GPP_PATTERN.matcher(str).matches()) {
450             str = StringUtils.strip(str, "-_ .");
451         }
452         return str;
453     }
454
455     public static boolean validateUrl(String url) {
456         UrlValidator urlValidator = new UrlValidator();
457         if (!urlValidator.isValid(url)) {
458             return false;
459         }
460         if (NONE_UTF8_PATTERN.matcher(url).find()) {
461             return false;
462         }
463         if (URL_INVALIDE_PATTERN.matcher(url).find()) {
464             return false;
465         }
466         return true;
467     }
468
469     public static String cleanArtifactDisplayName(String strIn) {
470         String str = DASH_PATTERN.matcher(strIn).replaceAll("-");
471         str = UNDERSCORE_PATTERN.matcher(str).replaceAll("_");
472         str = PLUS_PATTERN.matcher(str).replaceAll("+");
473         str = normaliseWhitespace(str);
474         str = str.trim();
475         return str;
476     }
477
478     public static String normalizeArtifactLabel(String strIn) {
479         String str = DASH_PATTERN.matcher(strIn).replaceAll("");
480         str = UNDERSCORE_PATTERN.matcher(str).replaceAll("");
481         str = PLUS_PATTERN.matcher(str).replaceAll("");
482         str = SPACE_PATTERN.matcher(str).replaceAll("");
483         str = DOT_PATTERN.matcher(str).replaceAll("");
484         str = str.toLowerCase();
485         return str;
486     }
487
488     public static boolean validateAdditionalInformationKeyName(String str) {
489         return ADDITIONAL_INFORMATION_KEY_PATTERN.matcher(str).matches();
490     }
491
492     public static String normalizeAdditionalInformation(String str) {
493         if (str != null) {
494             str = DASH_PATTERN.matcher(str).replaceAll("-");
495             str = UNDERSCORE_PATTERN.matcher(str).replaceAll("_");
496             str = normaliseWhitespace(str);
497         }
498         return str;
499     }
500
501     public static boolean validateLength(String str, int length) {
502         if (str == null) {
503             return true;
504         }
505         return str.length() <= length;
506     }
507
508     public static boolean validateConsumerName(String consumerName) {
509         return CONSUMER_NAME_PATTERN.matcher(consumerName).matches();
510     }
511
512     public static boolean isUTF8Str(String str) {
513         if (NONE_UTF8_PATTERN.matcher(str).find()) {
514             return false;
515         }
516         return true;
517     }
518
519     public static boolean validateConsumerPassSalt(String consumerSalt) {
520         return CONSUMER_PASS_SALT_PATTERN.matcher(consumerSalt).matches();
521     }
522
523     public static boolean isFloatNumber(String number) {
524         return FLOAT_PATTERN.matcher(number).matches();
525     }
526
527     public static boolean validateCertifiedVersion(String version) {
528         return (version != null && CERTIFIED_VERSION_PATTERN.matcher(version).matches());
529     }
530
531     public static boolean validateMinorVersion(String version) {
532         return (version != null && MINOR_VERSION_PATTERN.matcher(version).matches());
533     }
534
535     public static boolean validateCategoryIconNotEmpty(List<String> categoryIcons) {
536         return CollectionUtils.isEmpty(categoryIcons);
537     }
538
539     public static String normaliseProductName(String name) {
540         String[] split = splitComponentName(PRODUCT_NAME_DELIMETER_PATTERN, name);
541         StringBuilder sb = new StringBuilder();
542         for (String splitElement : split) {
543             sb.append(splitElement);
544         }
545         return sb.toString();
546     }
547
548     private static String[] splitComponentName(Pattern pattern, String name) {
549         String normalizedName = name.toLowerCase();
550         normalizedName = pattern.matcher(normalizedName).replaceAll(" ");
551         return normalizedName.split(" ");
552     }
553
554     public static String removeHtmlTagsOnly(String htmlText) {
555         return HtmlCleaner.stripHtml(htmlText, false);
556     }
557
558     public static boolean validateForwardingPathNamePattern(String forwardingPathName) {
559         return FORWARDING_PATH_NAME_PATTERN.matcher(forwardingPathName).matches();
560     }
561
562     public static String sanitizeInputString(String input) {
563         if (StringUtils.isNotEmpty(input)) {
564             input = ValidationUtils.removeNoneUtf8Chars(input);
565             input = ValidationUtils.removeHtmlTags(input);
566             input = ValidationUtils.normaliseWhitespace(input);
567             input = ValidationUtils.stripOctets(input);
568         }
569         return input;
570     }
571 }