View Javadoc
1   /*
2    * Copyright 2001-2005 The Apache Software Foundation.
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  package org.apache.logging.log4j.catalog.api.plugins;
17  
18  import java.util.Arrays;
19  
20  import org.apache.logging.log4j.catalog.api.ConstraintType;
21  import org.apache.logging.log4j.core.config.plugins.Plugin;
22  import static org.apache.logging.log4j.catalog.api.util.StringUtils.appendNewline;
23  import static org.apache.logging.log4j.catalog.api.util.StringUtils.isBlank;
24  
25  /**
26   * Performs a case insensitive comparison to verify the value is in the list of acceptable values.
27   */
28  @Plugin(name = "anyCaseEnum", category = ConstraintType.CATEGORY)
29  public class CaseInsensitiveEnumConstraint implements ConstraintType {
30  
31      @Override
32      public void validate(boolean isRequestContext, String name, String value, String enums, StringBuilder error) {
33          if (!isBlank(enums) && !isBlank(value)) {
34              boolean result = Arrays.stream(enums.trim().split("\\s*,\\s*")).anyMatch(value::equalsIgnoreCase);
35              if (!result) {
36                  appendNewline(error);
37                  if (isRequestContext) {
38                      error.append("ThreadContext key ");
39                  }
40                  error.append(name).append(" does not match one of the values: ").append(enums);
41              }
42          }
43      }
44  }