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.Inject;
22 import javax.inject.Named;
23 import javax.inject.Singleton;
24
25 import java.net.URI;
26 import java.net.URISyntaxException;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.Collections;
30 import java.util.List;
31 import java.util.Set;
32 import java.util.stream.Collectors;
33
34 import org.eclipse.aether.RepositorySystemSession;
35 import org.eclipse.aether.artifact.Artifact;
36 import org.eclipse.aether.internal.impl.checksum.DefaultChecksumAlgorithmFactorySelector;
37 import org.eclipse.aether.metadata.Metadata;
38 import org.eclipse.aether.repository.RemoteRepository;
39 import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactory;
40 import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactorySelector;
41 import org.eclipse.aether.spi.connector.layout.RepositoryLayout;
42 import org.eclipse.aether.spi.connector.layout.RepositoryLayoutFactory;
43 import org.eclipse.aether.transfer.NoRepositoryLayoutException;
44 import org.eclipse.aether.util.ConfigUtils;
45
46 import static java.util.Objects.requireNonNull;
47
48
49
50
51 @Singleton
52 @Named("maven2")
53 public final class Maven2RepositoryLayoutFactory implements RepositoryLayoutFactory {
54
55 public static final String CONFIG_PROP_CHECKSUMS_ALGORITHMS = "aether.checksums.algorithms";
56
57 private static final String DEFAULT_CHECKSUMS_ALGORITHMS = "SHA-1,MD5";
58
59 public static final String CONFIG_PROP_OMIT_CHECKSUMS_FOR_EXTENSIONS =
60 "aether.checksums.omitChecksumsForExtensions";
61
62 private static final String DEFAULT_OMIT_CHECKSUMS_FOR_EXTENSIONS = ".asc,.sigstore";
63
64 private float priority;
65
66 private final ChecksumAlgorithmFactorySelector checksumAlgorithmFactorySelector;
67
68 public float getPriority() {
69 return priority;
70 }
71
72
73
74
75 @Deprecated
76 public Maven2RepositoryLayoutFactory() {
77 this(new DefaultChecksumAlgorithmFactorySelector());
78 }
79
80 @Inject
81 public Maven2RepositoryLayoutFactory(ChecksumAlgorithmFactorySelector checksumAlgorithmFactorySelector) {
82 this.checksumAlgorithmFactorySelector = requireNonNull(checksumAlgorithmFactorySelector);
83 }
84
85
86
87
88
89
90
91 public Maven2RepositoryLayoutFactory setPriority(float priority) {
92 this.priority = priority;
93 return this;
94 }
95
96 public RepositoryLayout newInstance(RepositorySystemSession session, RemoteRepository repository)
97 throws NoRepositoryLayoutException {
98 requireNonNull(session, "session cannot be null");
99 requireNonNull(repository, "repository cannot be null");
100 if (!"default".equals(repository.getContentType())) {
101 throw new NoRepositoryLayoutException(repository);
102 }
103
104 List<ChecksumAlgorithmFactory> checksumsAlgorithms = checksumAlgorithmFactorySelector.selectList(
105 ConfigUtils.parseCommaSeparatedUniqueNames(ConfigUtils.getString(
106 session, DEFAULT_CHECKSUMS_ALGORITHMS, CONFIG_PROP_CHECKSUMS_ALGORITHMS)));
107
108
109 Set<String> omitChecksumsForExtensions = Arrays.stream(ConfigUtils.getString(
110 session,
111 DEFAULT_OMIT_CHECKSUMS_FOR_EXTENSIONS,
112 CONFIG_PROP_OMIT_CHECKSUMS_FOR_EXTENSIONS)
113 .split(","))
114 .filter(s -> s != null && !s.trim().isEmpty())
115 .collect(Collectors.toSet());
116
117
118 if (omitChecksumsForExtensions.stream().anyMatch(s -> !s.startsWith("."))) {
119 throw new IllegalArgumentException(String.format(
120 "The configuration %s contains illegal values: %s (all entries must start with '.' (dot))",
121 CONFIG_PROP_OMIT_CHECKSUMS_FOR_EXTENSIONS, omitChecksumsForExtensions));
122 }
123
124 return new Maven2RepositoryLayout(
125 checksumAlgorithmFactorySelector, checksumsAlgorithms, omitChecksumsForExtensions);
126 }
127
128 private static class Maven2RepositoryLayout implements RepositoryLayout {
129 private final ChecksumAlgorithmFactorySelector checksumAlgorithmFactorySelector;
130
131 private final List<ChecksumAlgorithmFactory> configuredChecksumAlgorithms;
132
133 private final Set<String> extensionsWithoutChecksums;
134
135 private Maven2RepositoryLayout(
136 ChecksumAlgorithmFactorySelector checksumAlgorithmFactorySelector,
137 List<ChecksumAlgorithmFactory> configuredChecksumAlgorithms,
138 Set<String> extensionsWithoutChecksums) {
139 this.checksumAlgorithmFactorySelector = requireNonNull(checksumAlgorithmFactorySelector);
140 this.configuredChecksumAlgorithms = Collections.unmodifiableList(configuredChecksumAlgorithms);
141 this.extensionsWithoutChecksums = requireNonNull(extensionsWithoutChecksums);
142 }
143
144 private URI toUri(String path) {
145 try {
146 return new URI(null, null, path, null);
147 } catch (URISyntaxException e) {
148 throw new IllegalStateException(e);
149 }
150 }
151
152 @Override
153 public List<ChecksumAlgorithmFactory> getChecksumAlgorithmFactories() {
154 return configuredChecksumAlgorithms;
155 }
156
157 @Override
158 public boolean hasChecksums(Artifact artifact) {
159 String artifactExtension = artifact.getExtension();
160 for (String extensionWithoutChecksums : extensionsWithoutChecksums) {
161 if (artifactExtension.endsWith(extensionWithoutChecksums)) {
162 return false;
163 }
164 }
165 return true;
166 }
167
168 @Override
169 public URI getLocation(Artifact artifact, boolean upload) {
170 StringBuilder path = new StringBuilder(128);
171
172 path.append(artifact.getGroupId().replace('.', '/')).append('/');
173
174 path.append(artifact.getArtifactId()).append('/');
175
176 path.append(artifact.getBaseVersion()).append('/');
177
178 path.append(artifact.getArtifactId()).append('-').append(artifact.getVersion());
179
180 if (artifact.getClassifier().length() > 0) {
181 path.append('-').append(artifact.getClassifier());
182 }
183
184 if (artifact.getExtension().length() > 0) {
185 path.append('.').append(artifact.getExtension());
186 }
187
188 return toUri(path.toString());
189 }
190
191 @Override
192 public URI getLocation(Metadata metadata, boolean upload) {
193 StringBuilder path = new StringBuilder(128);
194
195 if (metadata.getGroupId().length() > 0) {
196 path.append(metadata.getGroupId().replace('.', '/')).append('/');
197
198 if (metadata.getArtifactId().length() > 0) {
199 path.append(metadata.getArtifactId()).append('/');
200
201 if (metadata.getVersion().length() > 0) {
202 path.append(metadata.getVersion()).append('/');
203 }
204 }
205 }
206
207 path.append(metadata.getType());
208
209 return toUri(path.toString());
210 }
211
212 @Override
213 public List<ChecksumLocation> getChecksumLocations(Artifact artifact, boolean upload, URI location) {
214 if (!hasChecksums(artifact) || isChecksum(artifact.getExtension())) {
215 return Collections.emptyList();
216 }
217 return getChecksumLocations(location);
218 }
219
220 @Override
221 public List<ChecksumLocation> getChecksumLocations(Metadata metadata, boolean upload, URI location) {
222 return getChecksumLocations(location);
223 }
224
225 private List<ChecksumLocation> getChecksumLocations(URI location) {
226 List<ChecksumLocation> checksumLocations = new ArrayList<>(configuredChecksumAlgorithms.size());
227 for (ChecksumAlgorithmFactory checksumAlgorithmFactory : configuredChecksumAlgorithms) {
228 checksumLocations.add(ChecksumLocation.forLocation(location, checksumAlgorithmFactory));
229 }
230 return checksumLocations;
231 }
232
233 private boolean isChecksum(String extension) {
234 return checksumAlgorithmFactorySelector.isChecksumExtension(extension);
235 }
236 }
237 }