View Javadoc
1   /*
2    * Copyright 2012-2025 CodeLibs Project and the Others.
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,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.validation;
17  
18  import org.codelibs.core.lang.StringUtil;
19  import org.codelibs.fess.util.ComponentUtil;
20  
21  import jakarta.validation.ConstraintDefinitionException;
22  import jakarta.validation.ConstraintValidator;
23  import jakarta.validation.ConstraintValidatorContext;
24  
25  /**
26   * Validator implementation for URI type constraints.
27   * This validator checks URI strings against specified protocol types.
28   */
29  public class UriTypeValidator implements ConstraintValidator<UriType, String> {
30  
31      /**
32       * Default constructor for UriTypeValidator.
33       */
34      public UriTypeValidator() {
35          // Default constructor
36      }
37  
38      private String[] protocols;
39  
40      @Override
41      public void initialize(final UriType uriType) {
42          protocols = switch (uriType.protocolType()) {
43          case WEB -> ComponentUtil.getProtocolHelper().getWebProtocols();
44          case FILE -> ComponentUtil.getProtocolHelper().getFileProtocols();
45          default -> throw new ConstraintDefinitionException("protocolType is empty or invalid: " + uriType.protocolType());
46          };
47      }
48  
49      @Override
50      public boolean isValid(final String value, final ConstraintValidatorContext context) {
51          if (StringUtil.isNotBlank(value)) {
52              return check(protocols, value);
53          }
54          return true;
55      }
56  
57      /**
58       * Checks if the given value matches any of the specified protocols.
59       *
60       * @param protocols the allowed protocols
61       * @param value the URI string to validate
62       * @return true if the value matches allowed protocols, false otherwise
63       */
64      protected static boolean check(final String[] protocols, final String value) {
65          final String[] paths = value.split("[\r\n]");
66          for (final String path : paths) {
67              if (StringUtil.isNotBlank(path) && !path.trim().startsWith("#")) {
68                  boolean flag = false;
69                  for (final String protocol : protocols) {
70                      if (path.trim().startsWith(protocol.trim())) {
71                          flag = true;
72                          break;
73                      }
74                  }
75                  if (!flag) {
76                      return false;
77                  }
78              }
79          }
80          return true;
81      }
82  
83      /**
84       * Enumeration of supported protocol types for URI validation.
85       */
86      public enum ProtocolType {
87          /**
88           * Web protocols (http, https, etc.).
89           */
90          WEB,
91          /**
92           * File protocols (file, etc.).
93           */
94          FILE;
95      }
96  }