1 package org.eclipse.aether.util.repository;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
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 * A simple mirror selector that selects mirrors based on repository identifiers.
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 * Adds the specified mirror to this selector.
55 *
56 * @param id The identifier of the mirror, must not be {@code null}.
57 * @param url The URL of the mirror, must not be {@code null}.
58 * @param type The content type of the mirror, must not be {@code null}.
59 * @param repositoryManager A flag whether the mirror is a repository manager or a simple server.
60 * @param blocked A flag whether the mirror is blocked from performing any download requests.
61 * @param mirrorOfIds The identifier(s) of remote repositories to mirror, must not be {@code null}. Multiple
62 * identifiers can be separated by comma and additionally the wildcards "*", "external:http:*" and
63 * "external:*" can be used to match all (external) repositories, prefixing a repo id with an
64 * exclamation mark allows to express an exclusion. For example "external:*,!central".
65 * @param mirrorOfTypes The content type(s) of remote repositories to mirror, may be {@code null} or empty to match
66 * any content type. Similar to the repo id specification, multiple types can be comma-separated, the
67 * wildcard "*" and the "!" negation syntax are supported. For example "*,!p2".
68 * @return This selector for chaining, never {@code null}.
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 * This method checks if the pattern matches the originalRepository. Valid patterns:
138 * <ul>
139 * <li>{@code *} = everything,</li>
140 * <li>{@code external:*} = everything not on the localhost and not file based,</li>
141 * <li>{@code external:http:*} = any repository not on the localhost using HTTP,</li>
142 * <li>{@code repo,repo1} = {@code repo} or {@code repo1},</li>
143 * <li>{@code *,!repo1} = everything except {@code repo1}.</li>
144 * </ul>
145 *
146 * @param repository to compare for a match.
147 * @param pattern used for match.
148 * @return true if the repository is a match to this pattern.
149 */
150 static boolean matchPattern( RemoteRepository repository, String pattern )
151 {
152 boolean result = false;
153 String originalId = repository.getId();
154
155 // simple checks first to short circuit processing below.
156 if ( WILDCARD.equals( pattern ) || pattern.equals( originalId ) )
157 {
158 result = true;
159 }
160 else
161 {
162 // process the list
163 String[] repos = pattern.split( "," );
164 for ( String repo : repos )
165 {
166 // see if this is a negative match
167 if ( repo.length() > 1 && repo.startsWith( "!" ) )
168 {
169 if ( repo.substring( 1 ).equals( originalId ) )
170 {
171 // explicitly exclude. Set result and stop processing.
172 result = false;
173 break;
174 }
175 }
176 // check for exact match
177 else if ( repo.equals( originalId ) )
178 {
179 result = true;
180 break;
181 }
182 // check for external:*
183 else if ( EXTERNAL_WILDCARD.equals( repo ) && isExternalRepo( repository ) )
184 {
185 result = true;
186 // don't stop processing in case a future segment explicitly excludes this repo
187 }
188 // check for external:http:*
189 else if ( EXTERNAL_HTTP_WILDCARD.equals( repo ) && isExternalHttpRepo( repository ) )
190 {
191 result = true;
192 // don't stop processing in case a future segment explicitly excludes this repo
193 }
194 else if ( WILDCARD.equals( repo ) )
195 {
196 result = true;
197 // don't stop processing in case a future segment explicitly excludes this repo
198 }
199 }
200 }
201 return result;
202 }
203
204 /**
205 * Checks the URL to see if this repository refers to an external repository.
206 *
207 * @param repository The repository to check, must not be {@code null}.
208 * @return {@code true} if external, {@code false} otherwise.
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 * Checks the URL to see if this repository refers to a non-localhost repository using HTTP.
223 *
224 * @param repository The repository to check, must not be {@code null}.
225 * @return {@code true} if external, {@code false} otherwise.
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 * Checks whether the types configured for a mirror match with the type of the repository.
238 *
239 * @param repoType The type of the repository, may be {@code null}.
240 * @param mirrorType The types supported by the mirror, may be {@code null}.
241 * @return {@code true} if the types associated with the mirror match the type of the original repository,
242 * {@code false} otherwise.
243 */
244 static boolean matchesType( String repoType, String mirrorType )
245 {
246 boolean result = false;
247
248 // simple checks first to short circuit processing below.
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 // process the list
260 String[] layouts = mirrorType.split( "," );
261 for ( String layout : layouts )
262 {
263 // see if this is a negative match
264 if ( layout.length() > 1 && layout.startsWith( "!" ) )
265 {
266 if ( layout.substring( 1 ).equals( repoType ) )
267 {
268 // explicitly exclude. Set result and stop processing.
269 result = false;
270 break;
271 }
272 }
273 // check for exact match
274 else if ( layout.equals( repoType ) )
275 {
276 result = true;
277 break;
278 }
279 else if ( WILDCARD.equals( layout ) )
280 {
281 result = true;
282 // don't stop processing in case a future segment explicitly excludes this repo
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 }