7ce3bb6c5bcc82d15d847d651bdccd9714fae113
[oom/platform/cert-service.git] / certService / src / main / java / org / onap / aaf / certservice / certification / configuration / validation / constraints / Cmpv2UrlValidator.java
1 /*
2  * ============LICENSE_START=======================================================
3  * PROJECT
4  * ================================================================================
5  * Copyright (C) 2020 Nokia. 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
21
22 package org.onap.aaf.certservice.certification.configuration.validation.constraints;
23
24 import org.onap.aaf.certservice.certification.configuration.validation.constraints.violations.PortNumberViolation;
25 import org.onap.aaf.certservice.certification.configuration.validation.constraints.violations.RequestTypeViolation;
26 import org.onap.aaf.certservice.certification.configuration.validation.constraints.violations.UrlServerViolation;
27
28 import javax.validation.ConstraintValidator;
29 import javax.validation.ConstraintValidatorContext;
30 import java.util.Arrays;
31 import java.util.List;
32 import java.util.concurrent.atomic.AtomicBoolean;
33
34 class Cmpv2UrlValidator implements ConstraintValidator<Cmpv2Url, String> {
35
36     private final List<UrlServerViolation> violations;
37
38     Cmpv2UrlValidator() {
39       this.violations = Arrays.asList(
40               new PortNumberViolation(),
41               new RequestTypeViolation()
42       );
43    }
44
45     @Override
46     public boolean isValid(String url, ConstraintValidatorContext context) {
47         AtomicBoolean isValid = new AtomicBoolean(true);
48         violations.forEach(violation -> {
49             if (!violation.validate(url)) {
50                 isValid.set(false);
51             }
52         });
53         return isValid.get();
54     }
55 }