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.repository.internal.relocation;
20  
21  import javax.inject.Named;
22  import javax.inject.Singleton;
23  
24  import org.apache.maven.model.DistributionManagement;
25  import org.apache.maven.model.Model;
26  import org.apache.maven.model.Relocation;
27  import org.apache.maven.repository.internal.MavenArtifactRelocationSource;
28  import org.apache.maven.repository.internal.RelocatedArtifact;
29  import org.eclipse.aether.RepositorySystemSession;
30  import org.eclipse.aether.artifact.Artifact;
31  import org.eclipse.aether.resolution.ArtifactDescriptorException;
32  import org.eclipse.aether.resolution.ArtifactDescriptorResult;
33  import org.eclipse.sisu.Priority;
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  
37  /**
38   * Relocation source from standard distribution management. This is the "one and only" relocation implementation that
39   * existed in Maven 3 land, uses POM distributionManagement/relocation.
40   * <p>
41   * Note: this component should kick-in last regarding relocations.
42   *
43   * @since 3.10.0
44   */
45  @Singleton
46  @Named(DistributionManagementArtifactRelocationSource.NAME)
47  @Priority(5)
48  public final class DistributionManagementArtifactRelocationSource implements MavenArtifactRelocationSource {
49      public static final String NAME = "distributionManagement";
50      private static final Logger LOGGER = LoggerFactory.getLogger(DistributionManagementArtifactRelocationSource.class);
51  
52      @Override
53      public Artifact relocatedTarget(
54              RepositorySystemSession session, ArtifactDescriptorResult artifactDescriptorResult, Model model)
55              throws ArtifactDescriptorException {
56          DistributionManagement distMgmt = model.getDistributionManagement();
57          if (distMgmt != null) {
58              Relocation relocation = distMgmt.getRelocation();
59              if (relocation != null) {
60                  validateCoordinateComponent(relocation.getGroupId(), "groupId", artifactDescriptorResult);
61                  validateCoordinateComponent(relocation.getArtifactId(), "artifactId", artifactDescriptorResult);
62                  validateCoordinateComponent(relocation.getVersion(), "version", artifactDescriptorResult);
63                  Artifact result = new RelocatedArtifact(
64                          artifactDescriptorResult.getRequest().getArtifact(),
65                          relocation.getGroupId(),
66                          relocation.getArtifactId(),
67                          null,
68                          null,
69                          relocation.getVersion(),
70                          relocation.getMessage());
71                  LOGGER.debug(
72                          "The artifact {} has been relocated to {}: {}",
73                          artifactDescriptorResult.getRequest().getArtifact(),
74                          result,
75                          relocation.getMessage());
76                  return result;
77              }
78          }
79          return null;
80      }
81  
82      /**
83       * Checks that a relocation coordinate component is usable as an artifact coordinate. Components outside
84       * the coordinate character set are rejected so that only well-formed coordinates enter resolution.
85       */
86      private static void validateCoordinateComponent(
87              String value, String component, ArtifactDescriptorResult artifactDescriptorResult)
88              throws ArtifactDescriptorException {
89          if (value == null || value.isEmpty()) {
90              return; // component is not relocated: the original artifact's value is kept
91          }
92          if (isInvalidCoordinateComponent(value)) {
93              IllegalArgumentException cause = new IllegalArgumentException("Invalid relocation " + component + " '"
94                      + value + "' in artifact descriptor for "
95                      + artifactDescriptorResult.getRequest().getArtifact()
96                      + ": not a valid artifact coordinate component");
97              artifactDescriptorResult.addException(cause);
98              throw new ArtifactDescriptorException(artifactDescriptorResult, cause.getMessage(), cause);
99          }
100     }
101 
102     private static boolean isInvalidCoordinateComponent(String value) {
103         if ("..".equals(value) || value.contains("/") || value.contains("\\") || value.contains(":")) {
104             return true;
105         }
106         for (int i = 0; i < value.length(); i++) {
107             if (Character.isISOControl(value.charAt(i))) {
108                 return true;
109             }
110         }
111         return false;
112     }
113 }