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.be.model.tosca.validators;
23 import org.openecomp.sdc.be.config.Configuration.ToscaValidatorsConfig;
24 import org.openecomp.sdc.be.config.ConfigurationManager;
25 import org.openecomp.sdc.be.model.DataTypeDefinition;
26 import org.openecomp.sdc.common.log.wrappers.Logger;
27 import org.openecomp.sdc.common.util.ValidationUtils;
31 public class StringValidator implements PropertyTypeValidator {
33 public static final int DEFAULT_STRING_MAXIMUM_LENGTH = 2500;
35 private static int STRING_MAXIMUM_LENGTH = DEFAULT_STRING_MAXIMUM_LENGTH;
37 private static final Logger log = Logger.getLogger(StringValidator.class.getName());
39 private static StringValidator stringValidator = new StringValidator();
41 private StringValidator() {
42 if (ConfigurationManager.getConfigurationManager() != null) {
43 ToscaValidatorsConfig toscaValidators = ConfigurationManager.getConfigurationManager().getConfiguration()
44 .getToscaValidators();
45 log.debug("toscaValidators= {}", toscaValidators);
46 if (toscaValidators != null) {
47 Integer stringMaxLength = toscaValidators.getStringMaxLength();
48 if (stringMaxLength != null) {
49 STRING_MAXIMUM_LENGTH = stringMaxLength;
55 public static StringValidator getInstance() {
56 return stringValidator;
60 public boolean isValid(String value, String innerType, Map<String, DataTypeDefinition> allDataTypes) {
62 if (value == null || value.isEmpty()) {
66 if (value.length() > STRING_MAXIMUM_LENGTH) {
67 log.debug("parameter String length {} is higher than configured({})", value.length(), STRING_MAXIMUM_LENGTH);
70 String converted = ValidationUtils.removeNoneUtf8Chars(value);
71 boolean isValid = ValidationUtils.validateIsAscii(converted);
73 if (!isValid && log.isDebugEnabled()) {
74 log.debug("parameter String value {} is not an ascii string.", value.substring(0, Math.min(value.length(), 20)));
81 public boolean isValid(String value, String innerType) {
82 return isValid(value, innerType, null);