1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.util.repository;
20
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24
25 import org.eclipse.aether.repository.MirrorSelector;
26 import org.eclipse.aether.repository.RemoteRepository;
27
28 import static java.util.Objects.requireNonNull;
29
30
31
32
33 public final class DefaultMirrorSelector implements MirrorSelector {
34
35 private static final String WILDCARD = "*";
36
37 private static final String EXTERNAL_WILDCARD = "external:*";
38
39 private static final String EXTERNAL_HTTP_WILDCARD = "external:http:*";
40
41 private final List<MirrorDef> mirrors = new ArrayList<>();
42
43 @Deprecated
44 public DefaultMirrorSelector add(
45 String id, String url, String type, boolean repositoryManager, String mirrorOfIds, String mirrorOfTypes) {
46 return add(id, url, type, repositoryManager, false, mirrorOfIds, mirrorOfTypes);
47 }
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 public DefaultMirrorSelector add(
67 String id,
68 String url,
69 String type,
70 boolean repositoryManager,
71 boolean blocked,
72 String mirrorOfIds,
73 String mirrorOfTypes) {
74 mirrors.add(new MirrorDef(id, url, type, repositoryManager, blocked, mirrorOfIds, mirrorOfTypes));
75
76 return this;
77 }
78
79 public RemoteRepository getMirror(RemoteRepository repository) {
80 requireNonNull(repository, "repository cannot be null");
81 MirrorDef mirror = findMirror(repository);
82
83 if (mirror == null) {
84 return null;
85 }
86
87 RemoteRepository.Builder builder =
88 new RemoteRepository.Builder(mirror.id, repository.getContentType(), mirror.url);
89
90 builder.setRepositoryManager(mirror.repositoryManager);
91
92 builder.setBlocked(mirror.blocked);
93
94 if (mirror.type != null && mirror.type.length() > 0) {
95 builder.setContentType(mirror.type);
96 }
97
98 builder.setSnapshotPolicy(repository.getPolicy(true));
99 builder.setReleasePolicy(repository.getPolicy(false));
100
101 builder.setMirroredRepositories(Collections.singletonList(repository));
102
103 return builder.build();
104 }
105
106 private MirrorDef findMirror(RemoteRepository repository) {
107 String repoId = repository.getId();
108
109 if (repoId != null && !mirrors.isEmpty()) {
110 for (MirrorDef mirror : mirrors) {
111 if (repoId.equals(mirror.mirrorOfIds)
112 && matchesType(repository.getContentType(), mirror.mirrorOfTypes)) {
113 return mirror;
114 }
115 }
116
117 for (MirrorDef mirror : mirrors) {
118 if (matchPattern(repository, mirror.mirrorOfIds)
119 && matchesType(repository.getContentType(), mirror.mirrorOfTypes)) {
120 return mirror;
121 }
122 }
123 }
124
125 return null;
126 }
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142 static boolean matchPattern(RemoteRepository repository, String pattern) {
143 boolean result = false;
144 String originalId = repository.getId();
145
146
147 if (WILDCARD.equals(pattern) || pattern.equals(originalId)) {
148 result = true;
149 } else {
150
151 String[] repos = pattern.split(",");
152 for (String repo : repos) {
153
154 if (repo.length() > 1 && repo.startsWith("!")) {
155 if (repo.substring(1).equals(originalId)) {
156
157 result = false;
158 break;
159 }
160 }
161
162 else if (repo.equals(originalId)) {
163 result = true;
164 break;
165 }
166
167 else if (EXTERNAL_WILDCARD.equals(repo) && isExternalRepo(repository)) {
168 result = true;
169
170 }
171
172 else if (EXTERNAL_HTTP_WILDCARD.equals(repo) && isExternalHttpRepo(repository)) {
173 result = true;
174
175 } else if (WILDCARD.equals(repo)) {
176 result = true;
177
178 }
179 }
180 }
181 return result;
182 }
183
184
185
186
187
188
189
190 static boolean isExternalRepo(RemoteRepository repository) {
191 boolean local = isLocal(repository.getHost()) || "file".equalsIgnoreCase(repository.getProtocol());
192 return !local;
193 }
194
195 private static boolean isLocal(String host) {
196 return "localhost".equals(host) || "127.0.0.1".equals(host);
197 }
198
199
200
201
202
203
204
205 static boolean isExternalHttpRepo(RemoteRepository repository) {
206 return ("http".equalsIgnoreCase(repository.getProtocol())
207 || "dav".equalsIgnoreCase(repository.getProtocol())
208 || "dav:http".equalsIgnoreCase(repository.getProtocol())
209 || "dav+http".equalsIgnoreCase(repository.getProtocol()))
210 && !isLocal(repository.getHost());
211 }
212
213
214
215
216
217
218
219
220
221 static boolean matchesType(String repoType, String mirrorType) {
222 boolean result = false;
223
224
225 if (mirrorType == null || mirrorType.isEmpty() || WILDCARD.equals(mirrorType)) {
226 result = true;
227 } else if (mirrorType.equals(repoType)) {
228 result = true;
229 } else {
230
231 String[] layouts = mirrorType.split(",");
232 for (String layout : layouts) {
233
234 if (layout.length() > 1 && layout.startsWith("!")) {
235 if (layout.substring(1).equals(repoType)) {
236
237 result = false;
238 break;
239 }
240 }
241
242 else if (layout.equals(repoType)) {
243 result = true;
244 break;
245 } else if (WILDCARD.equals(layout)) {
246 result = true;
247
248 }
249 }
250 }
251
252 return result;
253 }
254
255 static class MirrorDef {
256
257 final String id;
258
259 final String url;
260
261 final String type;
262
263 final boolean repositoryManager;
264
265 final boolean blocked;
266
267 final String mirrorOfIds;
268
269 final String mirrorOfTypes;
270
271 MirrorDef(
272 String id,
273 String url,
274 String type,
275 boolean repositoryManager,
276 boolean blocked,
277 String mirrorOfIds,
278 String mirrorOfTypes) {
279 this.id = id;
280 this.url = url;
281 this.type = type;
282 this.repositoryManager = repositoryManager;
283 this.blocked = blocked;
284 this.mirrorOfIds = mirrorOfIds;
285 this.mirrorOfTypes = mirrorOfTypes;
286 }
287 }
288 }