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