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