2 * Copyright © 2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.sdc.workflow.api.validation;
19 import java.util.Objects;
20 import java.util.regex.Matcher;
21 import java.util.regex.Pattern;
22 import javax.validation.ConstraintValidator;
23 import javax.validation.ConstraintValidatorContext;
24 import org.onap.sdc.workflow.persistence.types.ActivitySpecParameter;
25 import org.springframework.beans.factory.annotation.Value;
26 import org.springframework.stereotype.Component;
28 @Component("ActivitySpecParameterNameValidator")
29 public class ActivitySpecParameterNameValidator implements ConstraintValidator<ValidName, ActivitySpecParameter> {
31 private String validationRegex;
32 private static final String defaultValidationRegex = "^\\S*$";
33 private String validationMessage;
34 private static final String defaultValidationMessage = "Input and Output names must not contain any spaces";
37 public boolean isValid(ActivitySpecParameter parameter, ConstraintValidatorContext context) {
38 Pattern pattern = Pattern.compile(validationRegex);
39 Matcher matcher = pattern.matcher(parameter.getName());
40 boolean isValid = matcher.find();
42 context.disableDefaultConstraintViolation();
43 context.buildConstraintViolationWithTemplate(validationMessage).addConstraintViolation();
48 @Value("${activitySpec.parameterName.validation:" + defaultValidationRegex + "}")
49 void setValidationRegex(String regex) {
50 if (Objects.isNull(regex) || regex.equals("")) {
51 Objects.requireNonNull(regex);
53 validationRegex = regex;
57 @Value("${activitySpec.parameterName.validationMessage:" + defaultValidationMessage + "}")
58 void setValidationMessage(String message) {
59 if (Objects.isNull(message) || message.equals("")) {
60 Objects.requireNonNull(message);
62 validationMessage = message;