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 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 * This method checks if the pattern matches the originalRepository. Valid patterns:
67 * <ul>
68 * <li>{@code *} = everything,</li>
69 * <li>{@code external:*} = everything not on the localhost and not file based,</li>
70 * <li>{@code external:http:*} = any repository not on the localhost using HTTP,</li>
71 * <li>{@code repo,repo1} = {@code repo} or {@code repo1},</li>
72 * <li>{@code *,!repo1} = everything except {@code repo1}.</li>
73 * </ul>
74 *
75 * @param originalRepository to compare for a match.
76 * @param pattern used for match.
77 * @return true if the repository is a match to this pattern.
78 */
79 static boolean matchPattern(ArtifactRepository originalRepository, String pattern) {
80 boolean result = false;
81 String originalId = originalRepository.getId();
82
83 // simple checks first to short circuit processing below.
84 if (WILDCARD.equals(pattern) || pattern.equals(originalId)) {
85 result = true;
86 } else {
87 // process the list
88 String[] repos = pattern.split(",");
89 for (String repo : repos) {
90 repo = repo.trim();
91 // see if this is a negative match
92 if (repo.length() > 1 && repo.startsWith("!")) {
93 if (repo.substring(1).equals(originalId)) {
94 // explicitly exclude. Set result and stop processing.
95 result = false;
96 break;
97 }
98 }
99 // check for exact match
100 else if (repo.equals(originalId)) {
101 result = true;
102 break;
103 }
104 // check for external:*
105 else if (EXTERNAL_WILDCARD.equals(repo) && isExternalRepo(originalRepository)) {
106 result = true;
107 // don't stop processing in case a future segment explicitly excludes this repo
108 }
109 // check for external:http:*
110 else if (EXTERNAL_HTTP_WILDCARD.equals(repo) && isExternalHttpRepo(originalRepository)) {
111 result = true;
112 // don't stop processing in case a future segment explicitly excludes this repo
113 } else if (WILDCARD.equals(repo)) {
114 result = true;
115 // don't stop processing in case a future segment explicitly excludes this repo
116 }
117 }
118 }
119 return result;
120 }
121
122 /**
123 * Checks the URL to see if this repository refers to an external repository
124 *
125 * @param originalRepository
126 * @return true if external.
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 // bad url just skip it here. It should have been validated already, but the wagon lookup will deal with it
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 * Checks the URL to see if this repository refers to a non-localhost repository using HTTP.
144 *
145 * @param originalRepository
146 * @return true if external.
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 // bad url just skip it here. It should have been validated already, but the wagon lookup will deal with it
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 * Checks whether the layouts configured for a mirror match with the layout of the repository.
168 *
169 * @param repoLayout The layout of the repository, may be {@code null}.
170 * @param mirrorLayout The layouts supported by the mirror, may be {@code null}.
171 * @return {@code true} if the layouts associated with the mirror match the layout of the original repository,
172 * {@code false} otherwise.
173 */
174 static boolean matchesLayout(String repoLayout, String mirrorLayout) {
175 boolean result = false;
176
177 // simple checks first to short circuit processing below.
178 if ((mirrorLayout == null || mirrorLayout.isEmpty()) || WILDCARD.equals(mirrorLayout)) {
179 result = true;
180 } else if (mirrorLayout.equals(repoLayout)) {
181 result = true;
182 } else {
183 // process the list
184 String[] layouts = mirrorLayout.split(",");
185 for (String layout : layouts) {
186 // see if this is a negative match
187 if (layout.length() > 1 && layout.startsWith("!")) {
188 if (layout.substring(1).equals(repoLayout)) {
189 // explicitly exclude. Set result and stop processing.
190 result = false;
191 break;
192 }
193 }
194 // check for exact match
195 else if (layout.equals(repoLayout)) {
196 result = true;
197 break;
198 } else if (WILDCARD.equals(layout)) {
199 result = true;
200 // don't stop processing in case a future segment explicitly excludes this repo
201 }
202 }
203 }
204
205 return result;
206 }
207 }