View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.internal.aether;
20  
21  import javax.inject.Named;
22  import javax.inject.Singleton;
23  
24  import java.util.Locale;
25  import java.util.StringJoiner;
26  
27  import org.eclipse.aether.RepositorySystemSession;
28  import org.eclipse.aether.spi.validator.Validator;
29  import org.eclipse.aether.spi.validator.ValidatorFactory;
30  import org.eclipse.aether.util.ConfigUtils;
31  
32  @Named
33  @Singleton
34  public class MavenValidatorFactory implements ValidatorFactory {
35      /**
36       * Resolver validation control.
37       * Can be <code>default</code> (full validation), <code>mild</code> (only uninterpolated placeholders) or
38       * <code>off</code> (no validation, as in Maven 3.9.x).
39       * This configuration provides "escape hatch" for those projects, that are forced to use non-conformant solutions.
40       * Default value is {@code default}.
41       *
42       * @since 3.10.0
43       */
44      public static final String MAVEN_RESOLVER_VALIDATION = "maven.resolver.validation";
45  
46      public enum ValidationLevel {
47          DEFAULT,
48          MILD,
49          OFF;
50      }
51  
52      private final MavenValidator defaultValidator = new MavenValidator(true);
53      private final MavenValidator mildValidator = new MavenValidator(false);
54  
55      @Override
56      public Validator newInstance(RepositorySystemSession session) {
57          switch (getValidationLevel(session)) {
58              case DEFAULT:
59                  return defaultValidator;
60              case MILD:
61                  return mildValidator;
62              case OFF:
63                  return NOOP;
64              default:
65                  throw new IllegalArgumentException("Unknown validation level");
66          }
67      }
68  
69      private static ValidationLevel getValidationLevel(RepositorySystemSession session) {
70          String value = ConfigUtils.getString(session, ValidationLevel.DEFAULT.name(), MAVEN_RESOLVER_VALIDATION);
71          try {
72              return ValidationLevel.valueOf(value.trim().toUpperCase(Locale.ROOT));
73          } catch (IllegalArgumentException e) {
74              StringJoiner supported = new StringJoiner(", ");
75              for (ValidationLevel level : ValidationLevel.values()) {
76                  supported.add(level.name().toLowerCase(Locale.ROOT));
77              }
78              throw new IllegalArgumentException(
79                      "Invalid value '" + value + "' for " + MAVEN_RESOLVER_VALIDATION + ", supported values are: "
80                              + supported,
81                      e);
82          }
83      }
84  }