1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.repository;
20
21 import java.net.MalformedURLException;
22 import java.net.URL;
23 import java.util.List;
24
25 import org.apache.maven.RepositoryUtils;
26 import org.apache.maven.artifact.repository.ArtifactRepository;
27 import org.apache.maven.settings.Mirror;
28 import org.codehaus.plexus.component.annotations.Component;
29 import org.codehaus.plexus.util.StringUtils;
30
31
32
33
34 @Component(role = MirrorSelector.class)
35 public class DefaultMirrorSelector implements MirrorSelector {
36
37 private static final String WILDCARD = "*";
38
39 private static final String EXTERNAL_WILDCARD = "external:*";
40
41 private static final String EXTERNAL_HTTP_WILDCARD = "external:http:*";
42
43 public Mirror getMirror(ArtifactRepository repository, List<Mirror> mirrors) {
44 String repoId = repository.getId();
45
46 if (repoId != null && mirrors != null) {
47 for (Mirror mirror : mirrors) {
48 if (repoId.equals(mirror.getMirrorOf()) && matchesLayout(repository, mirror)) {
49 return mirror;
50 }
51 }
52
53 for (Mirror mirror : mirrors) {
54 if (matchPattern(repository, mirror.getMirrorOf()) && matchesLayout(repository, mirror)) {
55 return mirror;
56 }
57 }
58 }
59
60 return null;
61 }
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 static boolean matchPattern(ArtifactRepository originalRepository, String pattern) {
78 boolean result = false;
79 String originalId = originalRepository.getId();
80
81
82 if (WILDCARD.equals(pattern) || pattern.equals(originalId)) {
83 result = true;
84 } else {
85
86 String[] repos = pattern.split(",");
87 for (String repo : repos) {
88 repo = repo.trim();
89
90 if (repo.length() > 1 && repo.startsWith("!")) {
91 if (repo.substring(1).equals(originalId)) {
92
93 result = false;
94 break;
95 }
96 }
97
98 else if (repo.equals(originalId)) {
99 result = true;
100 break;
101 }
102
103 else if (EXTERNAL_WILDCARD.equals(repo) && isExternalRepo(originalRepository)) {
104 result = true;
105
106 }
107
108 else if (EXTERNAL_HTTP_WILDCARD.equals(repo) && isExternalHttpRepo(originalRepository)) {
109 result = true;
110
111 } else if (WILDCARD.equals(repo)) {
112 result = true;
113
114 }
115 }
116 }
117 return result;
118 }
119
120
121
122
123
124
125
126 static boolean isExternalRepo(ArtifactRepository originalRepository) {
127 try {
128 URL url = new URL(originalRepository.getUrl());
129 return !(isLocal(url.getHost()) || url.getProtocol().equals("file"));
130 } catch (MalformedURLException e) {
131
132 return false;
133 }
134 }
135
136 private static boolean isLocal(String host) {
137 return "localhost".equals(host) || "127.0.0.1".equals(host);
138 }
139
140
141
142
143
144
145
146 static boolean isExternalHttpRepo(ArtifactRepository originalRepository) {
147 try {
148 URL url = new URL(originalRepository.getUrl());
149 return ("http".equalsIgnoreCase(url.getProtocol())
150 || "dav".equalsIgnoreCase(url.getProtocol())
151 || "dav:http".equalsIgnoreCase(url.getProtocol())
152 || "dav+http".equalsIgnoreCase(url.getProtocol()))
153 && !isLocal(url.getHost());
154 } catch (MalformedURLException e) {
155
156 return false;
157 }
158 }
159
160 static boolean matchesLayout(ArtifactRepository repository, Mirror mirror) {
161 return matchesLayout(RepositoryUtils.getLayout(repository), mirror.getMirrorOfLayouts());
162 }
163
164
165
166
167
168
169
170
171
172 static boolean matchesLayout(String repoLayout, String mirrorLayout) {
173 boolean result = false;
174
175
176 if (StringUtils.isEmpty(mirrorLayout) || WILDCARD.equals(mirrorLayout)) {
177 result = true;
178 } else if (mirrorLayout.equals(repoLayout)) {
179 result = true;
180 } else {
181
182 String[] layouts = mirrorLayout.split(",");
183 for (String layout : layouts) {
184
185 if (layout.length() > 1 && layout.startsWith("!")) {
186 if (layout.substring(1).equals(repoLayout)) {
187
188 result = false;
189 break;
190 }
191 }
192
193 else if (layout.equals(repoLayout)) {
194 result = true;
195 break;
196 } else if (WILDCARD.equals(layout)) {
197 result = true;
198
199 }
200 }
201 }
202
203 return result;
204 }
205 }