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.checksum;
20
21 import javax.inject.Inject;
22 import javax.inject.Named;
23 import javax.inject.Singleton;
24
25 import java.io.IOException;
26 import java.io.UncheckedIOException;
27 import java.nio.file.Files;
28 import java.nio.file.Path;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32
33 import org.eclipse.aether.RepositorySystemSession;
34 import org.eclipse.aether.artifact.Artifact;
35 import org.eclipse.aether.internal.impl.LocalPathComposer;
36 import org.eclipse.aether.repository.ArtifactRepository;
37 import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactory;
38 import org.eclipse.aether.spi.io.FileProcessor;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 import static java.util.Objects.requireNonNull;
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 @Singleton
62 @Named(SparseDirectoryTrustedChecksumsSource.NAME)
63 public final class SparseDirectoryTrustedChecksumsSource extends FileTrustedChecksumsSourceSupport {
64 public static final String NAME = "sparseDirectory";
65
66 private static final Logger LOGGER = LoggerFactory.getLogger(SparseDirectoryTrustedChecksumsSource.class);
67
68 private final FileProcessor fileProcessor;
69
70 private final LocalPathComposer localPathComposer;
71
72 @Inject
73 public SparseDirectoryTrustedChecksumsSource(FileProcessor fileProcessor, LocalPathComposer localPathComposer) {
74 super(NAME);
75 this.fileProcessor = requireNonNull(fileProcessor);
76 this.localPathComposer = requireNonNull(localPathComposer);
77 }
78
79 @Override
80 protected Map<String, String> doGetTrustedArtifactChecksums(
81 RepositorySystemSession session,
82 Artifact artifact,
83 ArtifactRepository artifactRepository,
84 List<ChecksumAlgorithmFactory> checksumAlgorithmFactories) {
85 final boolean originAware = isOriginAware(session);
86 final HashMap<String, String> checksums = new HashMap<>();
87 Path basedir = getBasedir(session, false);
88 if (Files.isDirectory(basedir)) {
89 for (ChecksumAlgorithmFactory checksumAlgorithmFactory : checksumAlgorithmFactories) {
90 Path checksumPath = basedir.resolve(
91 calculateArtifactPath(originAware, artifact, artifactRepository, checksumAlgorithmFactory));
92
93 if (!Files.isRegularFile(checksumPath)) {
94 LOGGER.debug(
95 "Artifact '{}' trusted checksum '{}' not found on path '{}'",
96 artifact,
97 checksumAlgorithmFactory.getName(),
98 checksumPath);
99 continue;
100 }
101
102 try {
103 String checksum = fileProcessor.readChecksum(checksumPath.toFile());
104 if (checksum != null) {
105 checksums.put(checksumAlgorithmFactory.getName(), checksum);
106 }
107 } catch (IOException e) {
108
109 LOGGER.warn(
110 "Could not read artifact '{}' trusted checksum on path '{}'", artifact, checksumPath, e);
111 throw new UncheckedIOException(e);
112 }
113 }
114 }
115 return checksums;
116 }
117
118 @Override
119 protected SparseDirectoryWriter doGetTrustedArtifactChecksumsWriter(RepositorySystemSession session) {
120 return new SparseDirectoryWriter(getBasedir(session, true), isOriginAware(session));
121 }
122
123 private String calculateArtifactPath(
124 boolean originAware,
125 Artifact artifact,
126 ArtifactRepository artifactRepository,
127 ChecksumAlgorithmFactory checksumAlgorithmFactory) {
128 String path = localPathComposer.getPathForArtifact(artifact, false) + "."
129 + checksumAlgorithmFactory.getFileExtension();
130 if (originAware) {
131 path = artifactRepository.getId() + "/" + path;
132 }
133 return path;
134 }
135
136 private class SparseDirectoryWriter implements Writer {
137 private final Path basedir;
138
139 private final boolean originAware;
140
141 private SparseDirectoryWriter(Path basedir, boolean originAware) {
142 this.basedir = basedir;
143 this.originAware = originAware;
144 }
145
146 @Override
147 public void addTrustedArtifactChecksums(
148 Artifact artifact,
149 ArtifactRepository artifactRepository,
150 List<ChecksumAlgorithmFactory> checksumAlgorithmFactories,
151 Map<String, String> trustedArtifactChecksums)
152 throws IOException {
153 for (ChecksumAlgorithmFactory checksumAlgorithmFactory : checksumAlgorithmFactories) {
154 Path checksumPath = basedir.resolve(
155 calculateArtifactPath(originAware, artifact, artifactRepository, checksumAlgorithmFactory));
156 String checksum = requireNonNull(trustedArtifactChecksums.get(checksumAlgorithmFactory.getName()));
157 fileProcessor.writeChecksum(checksumPath.toFile(), checksum);
158 }
159 }
160 }
161 }