1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
39
40
41
42
43
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
84
85
86 private static void validateCoordinateComponent(
87 String value, String component, ArtifactDescriptorResult artifactDescriptorResult)
88 throws ArtifactDescriptorException {
89 if (value == null || value.isEmpty()) {
90 return;
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 }