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 org.eclipse.aether.artifact.Artifact;
22  import org.eclipse.aether.graph.Dependency;
23  import org.eclipse.aether.metadata.Metadata;
24  import org.eclipse.aether.repository.LocalRepository;
25  import org.eclipse.aether.spi.validator.Validator;
26  import org.eclipse.aether.util.PathUtils;
27  
28  /**
29   * Simplest Maven specific validator that is meant to prevent un-interpolated
30   * elements enter resolver; if it does, is most likely some bug.
31   * <p>
32   * Note: {@link org.eclipse.aether.repository.RemoteRepository} is not validated here,
33   * see <a href="https://github.com/apache/maven/issues/7398">GH-7398</a> for details,
34   * and to keep very same behavior as Maven 3 did so far. If you want more, upgrade to Maven 4 ;)
35   */
36  public class MavenValidator implements Validator {
37      private final boolean validatePathComponents;
38  
39      public MavenValidator(boolean validatePathComponents) {
40          this.validatePathComponents = validatePathComponents;
41      }
42  
43      protected boolean containsPlaceholder(String value) {
44          return value != null && value.contains("${");
45      }
46  
47      @Override
48      public void validateArtifact(Artifact artifact) throws IllegalArgumentException {
49          if (containsPlaceholder(artifact.getGroupId())
50                  || containsPlaceholder(artifact.getArtifactId())
51                  || containsPlaceholder(artifact.getVersion())
52                  || containsPlaceholder(artifact.getClassifier())
53                  || containsPlaceholder(artifact.getExtension())) {
54              throw new IllegalArgumentException("Not fully interpolated artifact " + artifact);
55          }
56          if (validatePathComponents) {
57              PathUtils.validateArtifactComponents(artifact);
58          }
59      }
60  
61      @Override
62      public void validateMetadata(Metadata metadata) throws IllegalArgumentException {
63          if (containsPlaceholder(metadata.getGroupId())
64                  || containsPlaceholder(metadata.getArtifactId())
65                  || containsPlaceholder(metadata.getVersion())
66                  || containsPlaceholder(metadata.getType())) {
67              throw new IllegalArgumentException("Not fully interpolated metadata " + metadata);
68          }
69          if (validatePathComponents) {
70              PathUtils.validateMetadataComponents(metadata);
71          }
72      }
73  
74      @Override
75      public void validateDependency(Dependency dependency) throws IllegalArgumentException {
76          Artifact artifact = dependency.getArtifact();
77          if (containsPlaceholder(artifact.getGroupId())
78                  || containsPlaceholder(artifact.getArtifactId())
79                  || containsPlaceholder(artifact.getVersion())
80                  || containsPlaceholder(artifact.getClassifier())
81                  || containsPlaceholder(artifact.getExtension())
82                  || containsPlaceholder(dependency.getScope())
83                  || dependency.getExclusions().stream()
84                          .anyMatch(e -> containsPlaceholder(e.getGroupId())
85                                  || containsPlaceholder(e.getArtifactId())
86                                  || containsPlaceholder(e.getClassifier())
87                                  || containsPlaceholder(e.getExtension()))) {
88              throw new IllegalArgumentException("Not fully interpolated dependency " + dependency);
89          }
90          if (validatePathComponents) {
91              PathUtils.validateArtifactComponents(artifact);
92          }
93      }
94  
95      @Override
96      public void validateLocalRepository(LocalRepository localRepository) throws IllegalArgumentException {
97          if (containsPlaceholder(localRepository.getBasePath().toString())
98                  || containsPlaceholder(localRepository.getContentType())
99                  || containsPlaceholder(localRepository.getId())) {
100             throw new IllegalArgumentException("Not fully interpolated local repository " + localRepository);
101         }
102     }
103 }