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