1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
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 * DefaultMirrorSelector
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 * This method checks if the pattern matches the originalRepository. Valid patterns:
69 * <ul>
70 * <li>{@code *} = everything,</li>
71 * <li>{@code external:*} = everything not on the localhost and not file based,</li>
72 * <li>{@code external:http:*} = any repository not on the localhost using HTTP,</li>
73 * <li>{@code repo,repo1} = {@code repo} or {@code repo1},</li>
74 * <li>{@code *,!repo1} = everything except {@code repo1}.</li>
75 * </ul>
76 *
77 * @param originalRepository to compare for a match.
78 * @param pattern used for match.
79 * @return true if the repository is a match to this pattern.
80 */
81 static boolean matchPattern(ArtifactRepository originalRepository, String pattern) {
82 boolean result = false;
83 String originalId = originalRepository.getId();
84
85 // simple checks first to short circuit processing below.
86 if (WILDCARD.equals(pattern) || pattern.equals(originalId)) {
87 result = true;
88 } else {
89 // process the list
90 String[] repos = pattern.split(",");
91 for (String repo : repos) {
92 repo = repo.trim();
93 // see if this is a negative match
94 if (repo.length() > 1 && repo.startsWith("!")) {
95 if (repo.substring(1).equals(originalId)) {
96 // explicitly exclude. Set result and stop processing.
97 result = false;
98 break;
99 }
100 }
101 // check for exact match
102 else if (repo.equals(originalId)) {
103 result = true;
104 break;
105 }
106 // check for external:*
107 else if (EXTERNAL_WILDCARD.equals(repo) && isExternalRepo(originalRepository)) {
108 result = true;
109 // don't stop processing in case a future segment explicitly excludes this repo
110 }
111 // check for external:http:*
112 else if (EXTERNAL_HTTP_WILDCARD.equals(repo) && isExternalHttpRepo(originalRepository)) {
113 result = true;
114 // don't stop processing in case a future segment explicitly excludes this repo
115 } else if (WILDCARD.equals(repo)) {
116 result = true;
117 // don't stop processing in case a future segment explicitly excludes this repo
118 }
119 }
120 }
121 return result;
122 }
123
124 /**
125 * Checks the URL to see if this repository refers to an external repository
126 *
127 * @param originalRepository
128 * @return true if external.
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 // bad url just skip it here. It should have been validated already, but the wagon lookup will deal with it
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 * Checks the URL to see if this repository refers to a non-localhost repository using HTTP.
146 *
147 * @param originalRepository
148 * @return true if external.
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 // bad url just skip it here. It should have been validated already, but the wagon lookup will deal with it
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 * Checks whether the layouts configured for a mirror match with the layout of the repository.
170 *
171 * @param repoLayout The layout of the repository, may be {@code null}.
172 * @param mirrorLayout The layouts supported by the mirror, may be {@code null}.
173 * @return {@code true} if the layouts associated with the mirror match the layout of the original repository,
174 * {@code false} otherwise.
175 */
176 static boolean matchesLayout(String repoLayout, String mirrorLayout) {
177 boolean result = false;
178
179 // simple checks first to short circuit processing below.
180 if ((mirrorLayout == null || mirrorLayout.isEmpty()) || WILDCARD.equals(mirrorLayout)) {
181 result = true;
182 } else if (mirrorLayout.equals(repoLayout)) {
183 result = true;
184 } else {
185 // process the list
186 String[] layouts = mirrorLayout.split(",");
187 for (String layout : layouts) {
188 // see if this is a negative match
189 if (layout.length() > 1 && layout.startsWith("!")) {
190 if (layout.substring(1).equals(repoLayout)) {
191 // explicitly exclude. Set result and stop processing.
192 result = false;
193 break;
194 }
195 }
196 // check for exact match
197 else if (layout.equals(repoLayout)) {
198 result = true;
199 break;
200 } else if (WILDCARD.equals(layout)) {
201 result = true;
202 // don't stop processing in case a future segment explicitly excludes this repo
203 }
204 }
205 }
206
207 return result;
208 }
209 }