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