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