06818c9d6e481298e56cd7a15c52ef9752bf5d94
[sdc/sdc-workflow-designer.git] /
1 /*
2  * Copyright © 2018 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.sdc.workflow.api.validation;
18
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;
27
28 @Component("ActivitySpecParameterNameValidator")
29 public class ActivitySpecParameterNameValidator implements ConstraintValidator<ValidName, ActivitySpecParameter> {
30
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";
35
36     @Override
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();
41         if (!isValid) {
42             context.disableDefaultConstraintViolation();
43             context.buildConstraintViolationWithTemplate(validationMessage).addConstraintViolation();
44         }
45         return isValid;
46     }
47
48     @Value("${activitySpec.parameterName.validation:" + defaultValidationRegex + "}")
49     void setValidationRegex(String regex) {
50         if (Objects.isNull(regex) || regex.equals("")) {
51             Objects.requireNonNull(regex);
52         } else {
53             validationRegex = regex;
54         }
55     }
56
57     @Value("${activitySpec.parameterName.validationMessage:" + defaultValidationMessage + "}")
58     void setValidationMessage(String message) {
59         if (Objects.isNull(message) || message.equals("")) {
60             Objects.requireNonNull(message);
61         } else {
62             validationMessage = message;
63         }
64     }
65 }