1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.internal.impl;
20
21 import javax.inject.Named;
22 import javax.inject.Singleton;
23
24 import org.eclipse.aether.artifact.Artifact;
25 import org.eclipse.aether.metadata.Metadata;
26
27 import static java.util.Objects.requireNonNull;
28 import static org.eclipse.aether.util.PathUtils.validateArtifactComponents;
29 import static org.eclipse.aether.util.PathUtils.validateMetadataComponents;
30
31
32
33
34
35
36 @Singleton
37 @Named
38 public final class DefaultLocalPathComposer implements LocalPathComposer {
39 @Override
40 public String getPathForArtifact(Artifact artifact, boolean local) {
41 requireNonNull(artifact);
42 validateArtifactComponents(artifact);
43
44 StringBuilder path = new StringBuilder(128);
45
46 path.append(artifact.getGroupId().replace('.', '/')).append('/');
47
48 path.append(artifact.getArtifactId()).append('/');
49
50 path.append(artifact.getBaseVersion()).append('/');
51
52 path.append(artifact.getArtifactId()).append('-');
53 if (local) {
54 path.append(artifact.getBaseVersion());
55 } else {
56 path.append(artifact.getVersion());
57 }
58
59 if (!artifact.getClassifier().isEmpty()) {
60 path.append('-').append(artifact.getClassifier());
61 }
62
63 if (!artifact.getExtension().isEmpty()) {
64 path.append('.').append(artifact.getExtension());
65 }
66
67 return requireContainedPath(path.toString());
68 }
69
70 @Override
71 public String getPathForMetadata(Metadata metadata, String repositoryKey) {
72 requireNonNull(metadata);
73 requireNonNull(repositoryKey);
74 validateMetadataComponents(metadata);
75
76 StringBuilder path = new StringBuilder(128);
77
78 if (!metadata.getGroupId().isEmpty()) {
79 path.append(metadata.getGroupId().replace('.', '/')).append('/');
80
81 if (!metadata.getArtifactId().isEmpty()) {
82 path.append(metadata.getArtifactId()).append('/');
83
84 if (!metadata.getVersion().isEmpty()) {
85 path.append(metadata.getVersion()).append('/');
86 }
87 }
88 }
89
90 path.append(insertRepositoryKey(metadata.getType(), repositoryKey));
91
92 return requireContainedPath(path.toString());
93 }
94
95
96
97
98
99
100
101 private static String requireContainedPath(String path) {
102 if (path.startsWith("/")
103 || path.contains("//")
104 || path.equals("..")
105 || path.startsWith("../")
106 || path.endsWith("/..")
107 || path.contains("/../")) {
108 throw new IllegalArgumentException(
109 "Invalid coordinates: composed local repository path is not a valid relative path: " + path);
110 }
111 return path;
112 }
113
114 private String insertRepositoryKey(String metadataType, String repositoryKey) {
115 if (metadataType.contains("/") && !metadataType.endsWith("/")) {
116 int lastSlash = metadataType.lastIndexOf('/');
117 return metadataType.substring(0, lastSlash + 1)
118 + insertRepositoryKey(metadataType.substring(lastSlash + 1), repositoryKey);
119 } else {
120 String result;
121 int idx = metadataType.indexOf('.');
122 if (idx < 0) {
123 result = metadataType + '-' + repositoryKey;
124 } else {
125 result = metadataType.substring(0, idx) + '-' + repositoryKey + metadataType.substring(idx);
126 }
127 return result;
128 }
129 }
130 }