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