View Javadoc
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 java.net.MalformedURLException;
22  import java.net.URL;
23  import java.util.List;
24  
25  import org.apache.maven.RepositoryUtils;
26  import org.apache.maven.artifact.repository.ArtifactRepository;
27  import org.apache.maven.settings.Mirror;
28  import org.codehaus.plexus.component.annotations.Component;
29  import org.codehaus.plexus.util.StringUtils;
30  
31  /**
32   * DefaultMirrorSelector
33   */
34  @Component(role = MirrorSelector.class)
35  public class DefaultMirrorSelector implements MirrorSelector {
36  
37      private static final String WILDCARD = "*";
38  
39      private static final String EXTERNAL_WILDCARD = "external:*";
40  
41      private static final String EXTERNAL_HTTP_WILDCARD = "external:http:*";
42  
43      public Mirror getMirror(ArtifactRepository repository, List<Mirror> mirrors) {
44          String repoId = repository.getId();
45  
46          if (repoId != null && mirrors != null) {
47              for (Mirror mirror : mirrors) {
48                  if (repoId.equals(mirror.getMirrorOf()) && matchesLayout(repository, mirror)) {
49                      return mirror;
50                  }
51              }
52  
53              for (Mirror mirror : mirrors) {
54                  if (matchPattern(repository, mirror.getMirrorOf()) && matchesLayout(repository, mirror)) {
55                      return mirror;
56                  }
57              }
58          }
59  
60          return null;
61      }
62  
63      /**
64       * This method checks if the pattern matches the originalRepository. Valid patterns:
65       * <ul>
66       * <li>{@code *} = everything,</li>
67       * <li>{@code external:*} = everything not on the localhost and not file based,</li>
68       * <li>{@code external:http:*} = any repository not on the localhost using HTTP,</li>
69       * <li>{@code repo,repo1} = {@code repo} or {@code repo1},</li>
70       * <li>{@code *,!repo1} = everything except {@code repo1}.</li>
71       * </ul>
72       *
73       * @param originalRepository to compare for a match.
74       * @param pattern used for match. Currently only '*' is supported.
75       * @return true if the repository is a match to this pattern.
76       */
77      static boolean matchPattern(ArtifactRepository originalRepository, String pattern) {
78          boolean result = false;
79          String originalId = originalRepository.getId();
80  
81          // simple checks first to short circuit processing below.
82          if (WILDCARD.equals(pattern) || pattern.equals(originalId)) {
83              result = true;
84          } else {
85              // process the list
86              String[] repos = pattern.split(",");
87              for (String repo : repos) {
88                  repo = repo.trim();
89                  // see if this is a negative match
90                  if (repo.length() > 1 && repo.startsWith("!")) {
91                      if (repo.substring(1).equals(originalId)) {
92                          // explicitly exclude. Set result and stop processing.
93                          result = false;
94                          break;
95                      }
96                  }
97                  // check for exact match
98                  else if (repo.equals(originalId)) {
99                      result = true;
100                     break;
101                 }
102                 // check for external:*
103                 else if (EXTERNAL_WILDCARD.equals(repo) && isExternalRepo(originalRepository)) {
104                     result = true;
105                     // don't stop processing in case a future segment explicitly excludes this repo
106                 }
107                 // check for external:http:*
108                 else if (EXTERNAL_HTTP_WILDCARD.equals(repo) && isExternalHttpRepo(originalRepository)) {
109                     result = true;
110                     // don't stop processing in case a future segment explicitly excludes this repo
111                 } else if (WILDCARD.equals(repo)) {
112                     result = true;
113                     // don't stop processing in case a future segment explicitly excludes this repo
114                 }
115             }
116         }
117         return result;
118     }
119 
120     /**
121      * Checks the URL to see if this repository refers to an external repository
122      *
123      * @param originalRepository
124      * @return true if external.
125      */
126     static boolean isExternalRepo(ArtifactRepository originalRepository) {
127         try {
128             URL url = new URL(originalRepository.getUrl());
129             return !(isLocal(url.getHost()) || url.getProtocol().equals("file"));
130         } catch (MalformedURLException e) {
131             // bad url just skip it here. It should have been validated already, but the wagon lookup will deal with it
132             return false;
133         }
134     }
135 
136     private static boolean isLocal(String host) {
137         return "localhost".equals(host) || "127.0.0.1".equals(host);
138     }
139 
140     /**
141      * Checks the URL to see if this repository refers to a non-localhost repository using HTTP.
142      *
143      * @param originalRepository
144      * @return true if external.
145      */
146     static boolean isExternalHttpRepo(ArtifactRepository originalRepository) {
147         try {
148             URL url = new URL(originalRepository.getUrl());
149             return ("http".equalsIgnoreCase(url.getProtocol())
150                             || "dav".equalsIgnoreCase(url.getProtocol())
151                             || "dav:http".equalsIgnoreCase(url.getProtocol())
152                             || "dav+http".equalsIgnoreCase(url.getProtocol()))
153                     && !isLocal(url.getHost());
154         } catch (MalformedURLException e) {
155             // bad url just skip it here. It should have been validated already, but the wagon lookup will deal with it
156             return false;
157         }
158     }
159 
160     static boolean matchesLayout(ArtifactRepository repository, Mirror mirror) {
161         return matchesLayout(RepositoryUtils.getLayout(repository), mirror.getMirrorOfLayouts());
162     }
163 
164     /**
165      * Checks whether the layouts configured for a mirror match with the layout of the repository.
166      *
167      * @param repoLayout The layout of the repository, may be {@code null}.
168      * @param mirrorLayout The layouts supported by the mirror, may be {@code null}.
169      * @return {@code true} if the layouts associated with the mirror match the layout of the original repository,
170      *         {@code false} otherwise.
171      */
172     static boolean matchesLayout(String repoLayout, String mirrorLayout) {
173         boolean result = false;
174 
175         // simple checks first to short circuit processing below.
176         if (StringUtils.isEmpty(mirrorLayout) || WILDCARD.equals(mirrorLayout)) {
177             result = true;
178         } else if (mirrorLayout.equals(repoLayout)) {
179             result = true;
180         } else {
181             // process the list
182             String[] layouts = mirrorLayout.split(",");
183             for (String layout : layouts) {
184                 // see if this is a negative match
185                 if (layout.length() > 1 && layout.startsWith("!")) {
186                     if (layout.substring(1).equals(repoLayout)) {
187                         // explicitly exclude. Set result and stop processing.
188                         result = false;
189                         break;
190                     }
191                 }
192                 // check for exact match
193                 else if (layout.equals(repoLayout)) {
194                     result = true;
195                     break;
196                 } else if (WILDCARD.equals(layout)) {
197                     result = true;
198                     // don't stop processing in case a future segment explicitly excludes this repo
199                 }
200             }
201         }
202 
203         return result;
204     }
205 }