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 java.io.IOException;
22 import java.io.UncheckedIOException;
23 import java.nio.file.Path;
24 import java.util.List;
25 import java.util.Map;
26
27 import org.eclipse.aether.RepositorySystemSession;
28 import org.eclipse.aether.artifact.Artifact;
29 import org.eclipse.aether.repository.ArtifactRepository;
30 import org.eclipse.aether.spi.checksums.TrustedChecksumsSource;
31 import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactory;
32 import org.eclipse.aether.util.ConfigUtils;
33 import org.eclipse.aether.util.DirectoryUtils;
34
35 import static java.util.Objects.requireNonNull;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 abstract class FileTrustedChecksumsSourceSupport implements TrustedChecksumsSource {
57 private static final String CONFIG_PROP_PREFIX = "aether.trustedChecksumsSource.";
58
59 private static final String CONF_NAME_BASEDIR = "basedir";
60
61 private static final String CONF_NAME_ORIGIN_AWARE = "originAware";
62
63
64
65
66 static final String LOCAL_REPO_PREFIX_DIR = ".checksums";
67
68 private final String name;
69
70 FileTrustedChecksumsSourceSupport(String name) {
71 this.name = requireNonNull(name);
72 }
73
74
75
76
77
78 @Override
79 public Map<String, String> getTrustedArtifactChecksums(
80 RepositorySystemSession session,
81 Artifact artifact,
82 ArtifactRepository artifactRepository,
83 List<ChecksumAlgorithmFactory> checksumAlgorithmFactories) {
84 requireNonNull(session, "session is null");
85 requireNonNull(artifact, "artifact is null");
86 requireNonNull(artifactRepository, "artifactRepository is null");
87 requireNonNull(checksumAlgorithmFactories, "checksumAlgorithmFactories is null");
88 if (isEnabled(session)) {
89 return requireNonNull(
90 doGetTrustedArtifactChecksums(session, artifact, artifactRepository, checksumAlgorithmFactories));
91 }
92 return null;
93 }
94
95
96
97
98
99 @Override
100 public Writer getTrustedArtifactChecksumsWriter(RepositorySystemSession session) {
101 requireNonNull(session, "session is null");
102 if (isEnabled(session)) {
103 return doGetTrustedArtifactChecksumsWriter(session);
104 }
105 return null;
106 }
107
108
109
110
111 protected abstract Map<String, String> doGetTrustedArtifactChecksums(
112 RepositorySystemSession session,
113 Artifact artifact,
114 ArtifactRepository artifactRepository,
115 List<ChecksumAlgorithmFactory> checksumAlgorithmFactories);
116
117
118
119
120 protected Writer doGetTrustedArtifactChecksumsWriter(RepositorySystemSession session) {
121 return null;
122 }
123
124
125
126
127 protected String configPropKey(String name) {
128 requireNonNull(name);
129 return CONFIG_PROP_PREFIX + this.name + "." + name;
130 }
131
132
133
134
135
136
137 protected boolean isEnabled(RepositorySystemSession session) {
138 return ConfigUtils.getBoolean(session, false, CONFIG_PROP_PREFIX + this.name);
139 }
140
141
142
143
144
145
146 protected boolean isOriginAware(RepositorySystemSession session) {
147 return ConfigUtils.getBoolean(session, true, configPropKey(CONF_NAME_ORIGIN_AWARE));
148 }
149
150
151
152
153
154
155
156
157
158
159 protected Path getBasedir(RepositorySystemSession session, boolean mayCreate) {
160 try {
161 return DirectoryUtils.resolveDirectory(
162 session, LOCAL_REPO_PREFIX_DIR, configPropKey(CONF_NAME_BASEDIR), mayCreate);
163 } catch (IOException e) {
164 throw new UncheckedIOException(e);
165 }
166 }
167 }