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