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