View Javadoc
1   package org.apache.maven.repository;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.net.MalformedURLException;
23  import java.net.URL;
24  import java.util.List;
25  
26  import org.apache.maven.RepositoryUtils;
27  import org.apache.maven.artifact.repository.ArtifactRepository;
28  import org.apache.maven.settings.Mirror;
29  import org.codehaus.plexus.component.annotations.Component;
30  import org.codehaus.plexus.util.StringUtils;
31  
32  /**
33   * DefaultMirrorSelector
34   */
35  @Component( role = MirrorSelector.class )
36  public class DefaultMirrorSelector
37      implements MirrorSelector
38  {
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      {
48          String repoId = repository.getId();
49  
50          if ( repoId != null && mirrors != null )
51          {
52              for ( Mirror mirror : mirrors )
53              {
54                  if ( repoId.equals( mirror.getMirrorOf() ) && matchesLayout( repository, mirror ) )
55                  {
56                      return mirror;
57                  }
58              }
59  
60              for ( Mirror mirror : mirrors )
61              {
62                  if ( matchPattern( repository, mirror.getMirrorOf() ) && matchesLayout( repository, mirror ) )
63                  {
64                      return mirror;
65                  }
66              }
67          }
68  
69          return null;
70      }
71  
72      /**
73       * This method checks if the pattern matches the originalRepository. Valid patterns:
74       * <ul>
75       * <li>{@code *} = everything,</li>
76       * <li>{@code external:*} = everything not on the localhost and not file based,</li>
77       * <li>{@code external:http:*} = any repository not on the localhost using HTTP,</li>
78       * <li>{@code repo,repo1} = {@code repo} or {@code repo1},</li>
79       * <li>{@code *,!repo1} = everything except {@code repo1}.</li>
80       * </ul>
81       *
82       * @param originalRepository to compare for a match.
83       * @param pattern used for match. Currently only '*' is supported.
84       * @return true if the repository is a match to this pattern.
85       */
86      static boolean matchPattern( ArtifactRepository originalRepository, String pattern )
87      {
88          boolean result = false;
89          String originalId = originalRepository.getId();
90  
91          // simple checks first to short circuit processing below.
92          if ( WILDCARD.equals( pattern ) || pattern.equals( originalId ) )
93          {
94              result = true;
95          }
96          else
97          {
98              // process the list
99              String[] repos = pattern.split( "," );
100             for ( String repo : repos )
101             {
102                 repo = repo.trim();
103                 // see if this is a negative match
104                 if ( repo.length() > 1 && repo.startsWith( "!" ) )
105                 {
106                     if ( repo.substring( 1 ).equals( originalId ) )
107                     {
108                         // explicitly exclude. Set result and stop processing.
109                         result = false;
110                         break;
111                     }
112                 }
113                 // check for exact match
114                 else if ( repo.equals( originalId ) )
115                 {
116                     result = true;
117                     break;
118                 }
119                 // check for external:*
120                 else if ( EXTERNAL_WILDCARD.equals( repo ) && isExternalRepo( originalRepository ) )
121                 {
122                     result = true;
123                     // don't stop processing in case a future segment explicitly excludes this repo
124                 }
125                 // check for external:http:*
126                 else if ( EXTERNAL_HTTP_WILDCARD.equals( repo ) && isExternalHttpRepo( originalRepository ) )
127                 {
128                     result = true;
129                     // don't stop processing in case a future segment explicitly excludes this repo
130                 }
131                 else if ( WILDCARD.equals( repo ) )
132                 {
133                     result = true;
134                     // don't stop processing in case a future segment explicitly excludes this repo
135                 }
136             }
137         }
138         return result;
139     }
140 
141     /**
142      * Checks the URL to see if this repository refers to an external repository
143      *
144      * @param originalRepository
145      * @return true if external.
146      */
147     static boolean isExternalRepo( ArtifactRepository originalRepository )
148     {
149         try
150         {
151             URL url = new URL( originalRepository.getUrl() );
152             return !( isLocal( url.getHost() ) || url.getProtocol().equals( "file" ) );
153         }
154         catch ( MalformedURLException e )
155         {
156             // bad url just skip it here. It should have been validated already, but the wagon lookup will deal with it
157             return false;
158         }
159     }
160 
161     private static boolean isLocal( String host )
162     {
163         return "localhost".equals( host ) || "127.0.0.1".equals( host );
164     }
165 
166     /**
167      * Checks the URL to see if this repository refers to a non-localhost repository using HTTP.
168      *
169      * @param originalRepository
170      * @return true if external.
171      */
172     static boolean isExternalHttpRepo( ArtifactRepository originalRepository )
173     {
174         try
175         {
176             URL url = new URL( originalRepository.getUrl() );
177             return ( "http".equalsIgnoreCase( url.getProtocol() ) || "dav".equalsIgnoreCase( url.getProtocol() )
178                 || "dav:http".equalsIgnoreCase( url.getProtocol() )
179                 || "dav+http".equalsIgnoreCase( url.getProtocol() ) ) && !isLocal( url.getHost() );
180         }
181         catch ( MalformedURLException e )
182         {
183             // bad url just skip it here. It should have been validated already, but the wagon lookup will deal with it
184             return false;
185         }
186     }
187 
188    static boolean matchesLayout( ArtifactRepository repository, Mirror mirror )
189     {
190         return matchesLayout( RepositoryUtils.getLayout( repository ), mirror.getMirrorOfLayouts() );
191     }
192 
193     /**
194      * Checks whether the layouts configured for a mirror match with the layout of the repository.
195      *
196      * @param repoLayout The layout of the repository, may be {@code null}.
197      * @param mirrorLayout The layouts supported by the mirror, may be {@code null}.
198      * @return {@code true} if the layouts associated with the mirror match the layout of the original repository,
199      *         {@code false} otherwise.
200      */
201     static boolean matchesLayout( String repoLayout, String mirrorLayout )
202     {
203         boolean result = false;
204 
205         // simple checks first to short circuit processing below.
206         if ( StringUtils.isEmpty( mirrorLayout ) || WILDCARD.equals( mirrorLayout ) )
207         {
208             result = true;
209         }
210         else if ( mirrorLayout.equals( repoLayout ) )
211         {
212             result = true;
213         }
214         else
215         {
216             // process the list
217             String[] layouts = mirrorLayout.split( "," );
218             for ( String layout : layouts )
219             {
220                 // see if this is a negative match
221                 if ( layout.length() > 1 && layout.startsWith( "!" ) )
222                 {
223                     if ( layout.substring( 1 ).equals( repoLayout ) )
224                     {
225                         // explicitly exclude. Set result and stop processing.
226                         result = false;
227                         break;
228                     }
229                 }
230                 // check for exact match
231                 else if ( layout.equals( repoLayout ) )
232                 {
233                     result = true;
234                     break;
235                 }
236                 else if ( WILDCARD.equals( layout ) )
237                 {
238                     result = true;
239                     // don't stop processing in case a future segment explicitly excludes this repo
240                 }
241             }
242         }
243 
244         return result;
245     }
246 
247 }