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