1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.bridge;
20
21 import java.io.File;
22 import java.net.MalformedURLException;
23 import java.net.URL;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.HashSet;
28 import java.util.LinkedHashMap;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Set;
32 import javax.inject.Inject;
33 import javax.inject.Named;
34 import javax.inject.Singleton;
35 import org.apache.maven.RepositoryUtils;
36 import org.apache.maven.artifact.Artifact;
37 import org.apache.maven.artifact.DefaultArtifact;
38 import org.apache.maven.artifact.InvalidRepositoryException;
39 import org.apache.maven.artifact.handler.ArtifactHandler;
40 import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
41 import org.apache.maven.artifact.repository.ArtifactRepository;
42 import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
43 import org.apache.maven.artifact.repository.Authentication;
44 import org.apache.maven.artifact.repository.MavenArtifactRepository;
45 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
46 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout2;
47 import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
48 import org.apache.maven.artifact.resolver.filter.ExclusionArtifactFilter;
49 import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
50 import org.apache.maven.artifact.versioning.VersionRange;
51 import org.apache.maven.execution.MavenExecutionRequest;
52 import org.apache.maven.model.Dependency;
53 import org.apache.maven.model.Plugin;
54 import org.apache.maven.repository.Proxy;
55 import org.apache.maven.repository.RepositorySystem;
56 import org.apache.maven.settings.Mirror;
57 import org.codehaus.plexus.util.StringUtils;
58 import org.eclipse.aether.RepositorySystemSession;
59 import org.eclipse.aether.repository.AuthenticationContext;
60 import org.eclipse.aether.repository.AuthenticationSelector;
61 import org.eclipse.aether.repository.ProxySelector;
62 import org.eclipse.aether.repository.RemoteRepository;
63
64
65
66
67 @Named("default")
68 @Singleton
69 public class MavenRepositorySystem {
70 private final ArtifactHandlerManager artifactHandlerManager;
71
72 private final Map<String, ArtifactRepositoryLayout> layouts;
73
74 @Inject
75 public MavenRepositorySystem(
76 ArtifactHandlerManager artifactHandlerManager, Map<String, ArtifactRepositoryLayout> layouts) {
77 this.artifactHandlerManager = artifactHandlerManager;
78 this.layouts = layouts;
79 }
80
81
82 public Artifact createArtifact(String groupId, String artifactId, String version, String scope, String type) {
83 return createArtifactX(groupId, artifactId, version, scope, type);
84 }
85
86
87 public Artifact createProjectArtifact(String groupId, String artifactId, String metaVersionId) {
88 return createProjectArtifactX(groupId, artifactId, metaVersionId);
89 }
90
91
92 public Artifact createDependencyArtifact(Dependency d) {
93 if (d.getVersion() == null) {
94 return null;
95 }
96
97 VersionRange versionRange;
98 try {
99 versionRange = VersionRange.createFromVersionSpec(d.getVersion());
100 } catch (InvalidVersionSpecificationException e) {
101 return null;
102 }
103
104 Artifact artifact = createDependencyArtifactX(
105 d.getGroupId(),
106 d.getArtifactId(),
107 versionRange,
108 d.getType(),
109 d.getClassifier(),
110 d.getScope(),
111 d.isOptional());
112
113 if (Artifact.SCOPE_SYSTEM.equals(d.getScope()) && d.getSystemPath() != null) {
114 artifact.setFile(new File(d.getSystemPath()));
115 }
116
117 if (!d.getExclusions().isEmpty()) {
118 artifact.setDependencyFilter(new ExclusionArtifactFilter(d.getExclusions()));
119 }
120
121 return artifact;
122 }
123
124
125 public Artifact createExtensionArtifact(String groupId, String artifactId, String version) {
126 VersionRange versionRange;
127 try {
128 versionRange = VersionRange.createFromVersionSpec(version);
129 } catch (InvalidVersionSpecificationException e) {
130 return null;
131 }
132
133 return createExtensionArtifactX(groupId, artifactId, versionRange);
134 }
135
136
137 public Artifact createParentArtifact(String groupId, String artifactId, String version) {
138 return createParentArtifactX(groupId, artifactId, version);
139 }
140
141
142 public Artifact createPluginArtifact(Plugin plugin) {
143 VersionRange versionRange;
144 try {
145 String version = plugin.getVersion();
146 if (StringUtils.isEmpty(version)) {
147 version = "RELEASE";
148 }
149 versionRange = VersionRange.createFromVersionSpec(version);
150 } catch (InvalidVersionSpecificationException e) {
151 return null;
152 }
153
154 return createPluginArtifactX(plugin.getGroupId(), plugin.getArtifactId(), versionRange);
155 }
156
157 public void injectMirror(List<ArtifactRepository> repositories, List<Mirror> mirrors) {
158 if (repositories != null && mirrors != null) {
159 for (ArtifactRepository repository : repositories) {
160 Mirror mirror = getMirror(repository, mirrors);
161 injectMirror(repository, mirror);
162 }
163 }
164 }
165
166 private Mirror getMirror(RepositorySystemSession session, ArtifactRepository repository) {
167 if (session != null) {
168 org.eclipse.aether.repository.MirrorSelector selector = session.getMirrorSelector();
169 if (selector != null) {
170 RemoteRepository repo = selector.getMirror(RepositoryUtils.toRepo(repository));
171 if (repo != null) {
172 Mirror mirror = new Mirror();
173 mirror.setId(repo.getId());
174 mirror.setUrl(repo.getUrl());
175 mirror.setLayout(repo.getContentType());
176 mirror.setBlocked(repo.isBlocked());
177 return mirror;
178 }
179 }
180 }
181 return null;
182 }
183
184 public void injectMirror(RepositorySystemSession session, List<ArtifactRepository> repositories) {
185 if (repositories != null && session != null) {
186 for (ArtifactRepository repository : repositories) {
187 Mirror mirror = getMirror(session, repository);
188 injectMirror(repository, mirror);
189 }
190 }
191 }
192
193 private void injectMirror(ArtifactRepository repository, Mirror mirror) {
194 if (mirror != null) {
195 ArtifactRepository original = createArtifactRepository(
196 repository.getId(),
197 repository.getUrl(),
198 repository.getLayout(),
199 repository.getSnapshots(),
200 repository.getReleases());
201
202 repository.setMirroredRepositories(Collections.singletonList(original));
203
204 repository.setId(mirror.getId());
205 repository.setUrl(mirror.getUrl());
206
207 if (StringUtils.isNotEmpty(mirror.getLayout())) {
208 repository.setLayout(getLayout(mirror.getLayout()));
209 }
210
211 repository.setBlocked(mirror.isBlocked());
212 }
213 }
214
215 private Authentication getAuthentication(RepositorySystemSession session, ArtifactRepository repository) {
216 if (session != null) {
217 AuthenticationSelector selector = session.getAuthenticationSelector();
218 if (selector != null) {
219 RemoteRepository repo = RepositoryUtils.toRepo(repository);
220 org.eclipse.aether.repository.Authentication auth = selector.getAuthentication(repo);
221 if (auth != null) {
222 repo = new RemoteRepository.Builder(repo)
223 .setAuthentication(auth)
224 .build();
225 AuthenticationContext authCtx = AuthenticationContext.forRepository(session, repo);
226 Authentication result = new Authentication(
227 authCtx.get(AuthenticationContext.USERNAME), authCtx.get(AuthenticationContext.PASSWORD));
228 result.setPrivateKey(authCtx.get(AuthenticationContext.PRIVATE_KEY_PATH));
229 result.setPassphrase(authCtx.get(AuthenticationContext.PRIVATE_KEY_PASSPHRASE));
230 authCtx.close();
231 return result;
232 }
233 }
234 }
235 return null;
236 }
237
238 public void injectAuthentication(RepositorySystemSession session, List<ArtifactRepository> repositories) {
239 if (repositories != null && session != null) {
240 for (ArtifactRepository repository : repositories) {
241 repository.setAuthentication(getAuthentication(session, repository));
242 }
243 }
244 }
245
246 private Proxy getProxy(RepositorySystemSession session, ArtifactRepository repository) {
247 if (session != null) {
248 ProxySelector selector = session.getProxySelector();
249 if (selector != null) {
250 RemoteRepository repo = RepositoryUtils.toRepo(repository);
251 org.eclipse.aether.repository.Proxy proxy = selector.getProxy(repo);
252 if (proxy != null) {
253 Proxy p = new Proxy();
254 p.setHost(proxy.getHost());
255 p.setProtocol(proxy.getType());
256 p.setPort(proxy.getPort());
257 if (proxy.getAuthentication() != null) {
258 repo = new RemoteRepository.Builder(repo)
259 .setProxy(proxy)
260 .build();
261 AuthenticationContext authCtx = AuthenticationContext.forProxy(session, repo);
262 p.setUserName(authCtx.get(AuthenticationContext.USERNAME));
263 p.setPassword(authCtx.get(AuthenticationContext.PASSWORD));
264 p.setNtlmDomain(authCtx.get(AuthenticationContext.NTLM_DOMAIN));
265 p.setNtlmHost(authCtx.get(AuthenticationContext.NTLM_WORKSTATION));
266 authCtx.close();
267 }
268 return p;
269 }
270 }
271 }
272 return null;
273 }
274
275 public void injectProxy(RepositorySystemSession session, List<ArtifactRepository> repositories) {
276 if (repositories != null && session != null) {
277 for (ArtifactRepository repository : repositories) {
278 repository.setProxy(getProxy(session, repository));
279 }
280 }
281 }
282
283 private ArtifactRepositoryLayout getLayout(String id) {
284 return layouts.get(id);
285 }
286
287
288
289
290
291 public static org.apache.maven.model.Repository fromSettingsRepository(
292 org.apache.maven.settings.Repository settingsRepository) {
293 org.apache.maven.model.Repository modelRepository = new org.apache.maven.model.Repository();
294 modelRepository.setId(settingsRepository.getId());
295 modelRepository.setLayout(settingsRepository.getLayout());
296 modelRepository.setName(settingsRepository.getName());
297 modelRepository.setUrl(settingsRepository.getUrl());
298 modelRepository.setReleases(fromSettingsRepositoryPolicy(settingsRepository.getReleases()));
299 modelRepository.setSnapshots(fromSettingsRepositoryPolicy(settingsRepository.getSnapshots()));
300 return modelRepository;
301 }
302
303 public static org.apache.maven.model.RepositoryPolicy fromSettingsRepositoryPolicy(
304 org.apache.maven.settings.RepositoryPolicy settingsRepositoryPolicy) {
305 org.apache.maven.model.RepositoryPolicy modelRepositoryPolicy = new org.apache.maven.model.RepositoryPolicy();
306 if (settingsRepositoryPolicy != null) {
307 modelRepositoryPolicy.setEnabled(settingsRepositoryPolicy.isEnabled());
308 modelRepositoryPolicy.setUpdatePolicy(settingsRepositoryPolicy.getUpdatePolicy());
309 modelRepositoryPolicy.setChecksumPolicy(settingsRepositoryPolicy.getChecksumPolicy());
310 }
311 return modelRepositoryPolicy;
312 }
313
314 public static ArtifactRepository buildArtifactRepository(org.apache.maven.settings.Repository repo)
315 throws InvalidRepositoryException {
316 return buildArtifactRepository(fromSettingsRepository(repo));
317 }
318
319 public static ArtifactRepository buildArtifactRepository(org.apache.maven.model.Repository repo)
320 throws InvalidRepositoryException {
321 if (repo != null) {
322 String id = repo.getId();
323
324 if (StringUtils.isEmpty(id)) {
325 throw new InvalidRepositoryException("Repository identifier missing", "");
326 }
327
328 String url = repo.getUrl();
329
330 if (StringUtils.isEmpty(url)) {
331 throw new InvalidRepositoryException("URL missing for repository " + id, id);
332 }
333
334 ArtifactRepositoryPolicy snapshots = buildArtifactRepositoryPolicy(repo.getSnapshots());
335
336 ArtifactRepositoryPolicy releases = buildArtifactRepositoryPolicy(repo.getReleases());
337
338 ArtifactRepositoryLayout layout = new DefaultRepositoryLayout();
339
340 return createArtifactRepository(id, url, layout, snapshots, releases);
341 } else {
342 return null;
343 }
344 }
345
346 public static ArtifactRepositoryPolicy buildArtifactRepositoryPolicy(
347 org.apache.maven.model.RepositoryPolicy policy) {
348 boolean enabled = true;
349
350 String updatePolicy = null;
351
352 String checksumPolicy = null;
353
354 if (policy != null) {
355 enabled = policy.isEnabled();
356
357 if (policy.getUpdatePolicy() != null) {
358 updatePolicy = policy.getUpdatePolicy();
359 }
360 if (policy.getChecksumPolicy() != null) {
361 checksumPolicy = policy.getChecksumPolicy();
362 }
363 }
364
365 return new ArtifactRepositoryPolicy(enabled, updatePolicy, checksumPolicy);
366 }
367
368 public ArtifactRepository createArtifactRepository(
369 String id,
370 String url,
371 String layoutId,
372 ArtifactRepositoryPolicy snapshots,
373 ArtifactRepositoryPolicy releases)
374 throws Exception {
375 ArtifactRepositoryLayout layout = layouts.get(layoutId);
376
377 checkLayout(id, layoutId, layout);
378
379 return createArtifactRepository(id, url, layout, snapshots, releases);
380 }
381
382 private void checkLayout(String repositoryId, String layoutId, ArtifactRepositoryLayout layout) throws Exception {
383 if (layout == null) {
384 throw new Exception(
385 String.format("Cannot find ArtifactRepositoryLayout instance for: %s %s", layoutId, repositoryId));
386 }
387 }
388
389 public static ArtifactRepository createArtifactRepository(
390 String id,
391 String url,
392 ArtifactRepositoryLayout repositoryLayout,
393 ArtifactRepositoryPolicy snapshots,
394 ArtifactRepositoryPolicy releases) {
395 if (snapshots == null) {
396 snapshots = new ArtifactRepositoryPolicy();
397 }
398
399 if (releases == null) {
400 releases = new ArtifactRepositoryPolicy();
401 }
402
403 ArtifactRepository repository;
404 if (repositoryLayout instanceof ArtifactRepositoryLayout2) {
405 repository = ((ArtifactRepositoryLayout2) repositoryLayout)
406 .newMavenArtifactRepository(id, url, snapshots, releases);
407 } else {
408 repository = new MavenArtifactRepository(id, url, repositoryLayout, snapshots, releases);
409 }
410
411 return repository;
412 }
413
414
415 private Artifact createArtifactX(String groupId, String artifactId, String version, String scope, String type) {
416 return createArtifactX(groupId, artifactId, version, scope, type, null, null);
417 }
418
419 private Artifact createDependencyArtifactX(
420 String groupId,
421 String artifactId,
422 VersionRange versionRange,
423 String type,
424 String classifier,
425 String scope,
426 boolean optional) {
427 return createArtifactX(groupId, artifactId, versionRange, type, classifier, scope, null, optional);
428 }
429
430 private Artifact createProjectArtifactX(String groupId, String artifactId, String version) {
431 return createProjectArtifactX(groupId, artifactId, version, null);
432 }
433
434 private Artifact createParentArtifactX(String groupId, String artifactId, String version) {
435 return createProjectArtifactX(groupId, artifactId, version);
436 }
437
438 private Artifact createPluginArtifactX(String groupId, String artifactId, VersionRange versionRange) {
439 return createArtifactX(groupId, artifactId, versionRange, "maven-plugin", null, Artifact.SCOPE_RUNTIME, null);
440 }
441
442 private Artifact createProjectArtifactX(String groupId, String artifactId, String version, String scope) {
443 return createArtifactX(groupId, artifactId, version, scope, "pom");
444 }
445
446 private Artifact createExtensionArtifactX(String groupId, String artifactId, VersionRange versionRange) {
447 return createArtifactX(groupId, artifactId, versionRange, "jar", null, Artifact.SCOPE_RUNTIME, null);
448 }
449
450 private Artifact createArtifactX(
451 String groupId,
452 String artifactId,
453 String version,
454 String scope,
455 String type,
456 String classifier,
457 String inheritedScope) {
458 VersionRange versionRange = null;
459 if (version != null) {
460 versionRange = VersionRange.createFromVersion(version);
461 }
462 return createArtifactX(groupId, artifactId, versionRange, type, classifier, scope, inheritedScope);
463 }
464
465 private Artifact createArtifactX(
466 String groupId,
467 String artifactId,
468 VersionRange versionRange,
469 String type,
470 String classifier,
471 String scope,
472 String inheritedScope) {
473 return createArtifactX(groupId, artifactId, versionRange, type, classifier, scope, inheritedScope, false);
474 }
475
476 @SuppressWarnings("checkstyle:parameternumber")
477 private Artifact createArtifactX(
478 String groupId,
479 String artifactId,
480 VersionRange versionRange,
481 String type,
482 String classifier,
483 String scope,
484 String inheritedScope,
485 boolean optional) {
486 String desiredScope = Artifact.SCOPE_RUNTIME;
487
488 if (inheritedScope == null) {
489 desiredScope = scope;
490 } else if (Artifact.SCOPE_TEST.equals(scope) || Artifact.SCOPE_PROVIDED.equals(scope)) {
491 return null;
492 } else if (Artifact.SCOPE_COMPILE.equals(scope) && Artifact.SCOPE_COMPILE.equals(inheritedScope)) {
493
494 desiredScope = Artifact.SCOPE_COMPILE;
495 }
496
497 if (Artifact.SCOPE_TEST.equals(inheritedScope)) {
498 desiredScope = Artifact.SCOPE_TEST;
499 }
500
501 if (Artifact.SCOPE_PROVIDED.equals(inheritedScope)) {
502 desiredScope = Artifact.SCOPE_PROVIDED;
503 }
504
505 if (Artifact.SCOPE_SYSTEM.equals(scope)) {
506
507 desiredScope = Artifact.SCOPE_SYSTEM;
508 }
509
510 ArtifactHandler handler = artifactHandlerManager.getArtifactHandler(type);
511
512 return new DefaultArtifact(
513 groupId, artifactId, versionRange, desiredScope, type, classifier, handler, optional);
514 }
515
516
517
518
519
520 public ArtifactRepository createDefaultRemoteRepository(MavenExecutionRequest request) throws Exception {
521 return createRepository(
522 RepositorySystem.DEFAULT_REMOTE_REPO_URL,
523 RepositorySystem.DEFAULT_REMOTE_REPO_ID,
524 true,
525 ArtifactRepositoryPolicy.UPDATE_POLICY_DAILY,
526 false,
527 ArtifactRepositoryPolicy.UPDATE_POLICY_DAILY,
528 ArtifactRepositoryPolicy.DEFAULT_CHECKSUM_POLICY);
529 }
530
531 public ArtifactRepository createRepository(
532 String url,
533 String repositoryId,
534 boolean releases,
535 String releaseUpdates,
536 boolean snapshots,
537 String snapshotUpdates,
538 String checksumPolicy)
539 throws Exception {
540 ArtifactRepositoryPolicy snapshotsPolicy =
541 new ArtifactRepositoryPolicy(snapshots, snapshotUpdates, checksumPolicy);
542
543 ArtifactRepositoryPolicy releasesPolicy =
544 new ArtifactRepositoryPolicy(releases, releaseUpdates, checksumPolicy);
545
546 return createArtifactRepository(repositoryId, url, "default", snapshotsPolicy, releasesPolicy);
547 }
548
549 public Set<String> getRepoIds(List<ArtifactRepository> repositories) {
550 Set<String> repoIds = new HashSet<>();
551
552 if (repositories != null) {
553 for (ArtifactRepository repository : repositories) {
554 repoIds.add(repository.getId());
555 }
556 }
557
558 return repoIds;
559 }
560
561
562
563
564
565
566
567
568 public List<ArtifactRepository> getEffectiveRepositories(List<ArtifactRepository> repositories) {
569 if (repositories == null) {
570 return null;
571 }
572
573 Map<String, List<ArtifactRepository>> reposByKey = new LinkedHashMap<>();
574
575 for (ArtifactRepository repository : repositories) {
576 String key = repository.getId();
577
578 List<ArtifactRepository> aliasedRepos = reposByKey.computeIfAbsent(key, k -> new ArrayList<>());
579
580 aliasedRepos.add(repository);
581 }
582
583 List<ArtifactRepository> effectiveRepositories = new ArrayList<>();
584
585 for (List<ArtifactRepository> aliasedRepos : reposByKey.values()) {
586 List<ArtifactRepository> mirroredRepos = new ArrayList<>();
587
588 List<ArtifactRepositoryPolicy> releasePolicies = new ArrayList<>(aliasedRepos.size());
589
590 for (ArtifactRepository aliasedRepo : aliasedRepos) {
591 releasePolicies.add(aliasedRepo.getReleases());
592 mirroredRepos.addAll(aliasedRepo.getMirroredRepositories());
593 }
594
595 ArtifactRepositoryPolicy releasePolicy = getEffectivePolicy(releasePolicies);
596
597 List<ArtifactRepositoryPolicy> snapshotPolicies = new ArrayList<>(aliasedRepos.size());
598
599 for (ArtifactRepository aliasedRepo : aliasedRepos) {
600 snapshotPolicies.add(aliasedRepo.getSnapshots());
601 }
602
603 ArtifactRepositoryPolicy snapshotPolicy = getEffectivePolicy(snapshotPolicies);
604
605 ArtifactRepository aliasedRepo = aliasedRepos.get(0);
606
607 ArtifactRepository effectiveRepository = createArtifactRepository(
608 aliasedRepo.getId(), aliasedRepo.getUrl(), aliasedRepo.getLayout(), snapshotPolicy, releasePolicy);
609
610 effectiveRepository.setAuthentication(aliasedRepo.getAuthentication());
611
612 effectiveRepository.setProxy(aliasedRepo.getProxy());
613
614 effectiveRepository.setMirroredRepositories(mirroredRepos);
615
616 effectiveRepository.setBlocked(aliasedRepo.isBlocked());
617
618 effectiveRepositories.add(effectiveRepository);
619 }
620
621 return effectiveRepositories;
622 }
623
624 private ArtifactRepositoryPolicy getEffectivePolicy(Collection<ArtifactRepositoryPolicy> policies) {
625 ArtifactRepositoryPolicy effectivePolicy = null;
626
627 for (ArtifactRepositoryPolicy policy : policies) {
628 if (effectivePolicy == null) {
629 effectivePolicy = new ArtifactRepositoryPolicy(policy);
630 } else {
631 effectivePolicy.merge(policy);
632 }
633 }
634
635 return effectivePolicy;
636 }
637
638 public ArtifactRepository createLocalRepository(MavenExecutionRequest request, File localRepository)
639 throws Exception {
640 return createRepository(
641 "file://" + localRepository.toURI().getRawPath(),
642 RepositorySystem.DEFAULT_LOCAL_REPO_ID,
643 true,
644 ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS,
645 true,
646 ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS,
647 ArtifactRepositoryPolicy.CHECKSUM_POLICY_IGNORE);
648 }
649
650 private static final String WILDCARD = "*";
651
652 private static final String EXTERNAL_WILDCARD = "external:*";
653
654 private static final String EXTERNAL_HTTP_WILDCARD = "external:http:*";
655
656 public static Mirror getMirror(ArtifactRepository repository, List<Mirror> mirrors) {
657 String repoId = repository.getId();
658
659 if (repoId != null && mirrors != null) {
660 for (Mirror mirror : mirrors) {
661 if (repoId.equals(mirror.getMirrorOf()) && matchesLayout(repository, mirror)) {
662 return mirror;
663 }
664 }
665
666 for (Mirror mirror : mirrors) {
667 if (matchPattern(repository, mirror.getMirrorOf()) && matchesLayout(repository, mirror)) {
668 return mirror;
669 }
670 }
671 }
672
673 return null;
674 }
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690 static boolean matchPattern(ArtifactRepository originalRepository, String pattern) {
691 boolean result = false;
692 String originalId = originalRepository.getId();
693
694
695 if (WILDCARD.equals(pattern) || pattern.equals(originalId)) {
696 result = true;
697 } else {
698
699 String[] repos = pattern.split(",");
700 for (String repo : repos) {
701
702 if (repo.length() > 1 && repo.startsWith("!")) {
703 if (repo.substring(1).equals(originalId)) {
704
705 result = false;
706 break;
707 }
708 }
709
710 else if (repo.equals(originalId)) {
711 result = true;
712 break;
713 }
714
715 else if (EXTERNAL_WILDCARD.equals(repo) && isExternalRepo(originalRepository)) {
716 result = true;
717
718 }
719
720 else if (EXTERNAL_HTTP_WILDCARD.equals(repo) && isExternalHttpRepo(originalRepository)) {
721 result = true;
722
723 } else if (WILDCARD.equals(repo)) {
724 result = true;
725
726 }
727 }
728 }
729 return result;
730 }
731
732
733
734
735
736
737
738 static boolean isExternalRepo(ArtifactRepository originalRepository) {
739 try {
740 URL url = new URL(originalRepository.getUrl());
741 return !(isLocal(url.getHost()) || url.getProtocol().equals("file"));
742 } catch (MalformedURLException e) {
743
744 return false;
745 }
746 }
747
748 private static boolean isLocal(String host) {
749 return "localhost".equals(host) || "127.0.0.1".equals(host);
750 }
751
752
753
754
755
756
757
758 static boolean isExternalHttpRepo(ArtifactRepository originalRepository) {
759 try {
760 URL url = new URL(originalRepository.getUrl());
761 return ("http".equalsIgnoreCase(url.getProtocol())
762 || "dav".equalsIgnoreCase(url.getProtocol())
763 || "dav:http".equalsIgnoreCase(url.getProtocol())
764 || "dav+http".equalsIgnoreCase(url.getProtocol()))
765 && !isLocal(url.getHost());
766 } catch (MalformedURLException e) {
767
768 return false;
769 }
770 }
771
772 static boolean matchesLayout(ArtifactRepository repository, Mirror mirror) {
773 return matchesLayout(RepositoryUtils.getLayout(repository), mirror.getMirrorOfLayouts());
774 }
775
776
777
778
779
780
781
782
783
784 static boolean matchesLayout(String repoLayout, String mirrorLayout) {
785 boolean result = false;
786
787
788 if (StringUtils.isEmpty(mirrorLayout) || WILDCARD.equals(mirrorLayout)) {
789 result = true;
790 } else if (mirrorLayout.equals(repoLayout)) {
791 result = true;
792 } else {
793
794 String[] layouts = mirrorLayout.split(",");
795 for (String layout : layouts) {
796
797 if (layout.length() > 1 && layout.startsWith("!")) {
798 if (layout.substring(1).equals(repoLayout)) {
799
800 result = false;
801 break;
802 }
803 }
804
805 else if (layout.equals(repoLayout)) {
806 result = true;
807 break;
808 } else if (WILDCARD.equals(layout)) {
809 result = true;
810
811 }
812 }
813 }
814
815 return result;
816 }
817 }