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;
25 import org.openecomp.sdc.be.config.ConfigurationManager;
26 import org.openecomp.sdc.be.config.Configuration.ToscaValidatorsConfig;
27 import org.openecomp.sdc.be.model.DataTypeDefinition;
28 import org.openecomp.sdc.common.util.ValidationUtils;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 public class StringValidator implements PropertyTypeValidator {
34 public static final int DEFAULT_STRING_MAXIMUM_LENGTH = 2500;
36 private static int STRING_MAXIMUM_LENGTH = DEFAULT_STRING_MAXIMUM_LENGTH;
38 private static Logger log = LoggerFactory.getLogger(StringValidator.class.getName());
40 private static StringValidator stringValidator = new StringValidator();
42 private StringValidator() {
43 if (ConfigurationManager.getConfigurationManager() != null) {
44 ToscaValidatorsConfig toscaValidators = ConfigurationManager.getConfigurationManager().getConfiguration()
45 .getToscaValidators();
46 log.debug("toscaValidators= {}", toscaValidators);
47 if (toscaValidators != null) {
48 Integer stringMaxLength = toscaValidators.getStringMaxLength();
49 if (stringMaxLength != null) {
50 STRING_MAXIMUM_LENGTH = stringMaxLength;
56 public static StringValidator getInstance() {
57 return stringValidator;
61 public boolean isValid(String value, String innerType, Map<String, DataTypeDefinition> allDataTypes) {
63 if (value == null || true == value.isEmpty()) {
67 if (value.length() > STRING_MAXIMUM_LENGTH) {
68 log.debug("parameter String length {} is higher than configured({})", value.length(), STRING_MAXIMUM_LENGTH);
71 String converted = ValidationUtils.removeNoneUtf8Chars(value);
72 boolean isValid = ValidationUtils.validateIsAscii(converted);
74 if (false == isValid && log.isDebugEnabled()) {
75 log.debug("parameter String value {} is not an ascii string.", value.substring(0, Math.min(value.length(), 20)));
82 public boolean isValid(String value, String innerType) {
83 return isValid(value, innerType, null);