001package org.eclipse.aether.util.repository;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 * 
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 * 
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.ArrayList;
023import java.util.Collections;
024import java.util.List;
025
026import org.eclipse.aether.repository.MirrorSelector;
027import org.eclipse.aether.repository.RemoteRepository;
028
029/**
030 * A simple mirror selector that selects mirrors based on repository identifiers.
031 */
032public final class DefaultMirrorSelector
033    implements MirrorSelector
034{
035
036    private static final String WILDCARD = "*";
037
038    private static final String EXTERNAL_WILDCARD = "external:*";
039
040    private final List<MirrorDef> mirrors = new ArrayList<MirrorDef>();
041
042    @Deprecated
043    public DefaultMirrorSelector add( String id, String url, String type, boolean repositoryManager,
044                                      String mirrorOfIds, String mirrorOfTypes )
045    {
046        return add( id, url, type, repositoryManager, false, mirrorOfIds, mirrorOfTypes );
047    }
048
049    /**
050     * Adds the specified mirror to this selector.
051     * 
052     * @param id The identifier of the mirror, must not be {@code null}.
053     * @param url The URL of the mirror, must not be {@code null}.
054     * @param type The content type of the mirror, must not be {@code null}.
055     * @param repositoryManager A flag whether the mirror is a repository manager or a simple server.
056     * @param blocked A flag whether the mirror blocks any download request.
057     * @param mirrorOfIds The identifier(s) of remote repositories to mirror, must not be {@code null}. Multiple
058     *            identifiers can be separated by comma and additionally the wildcards "*" and "external:*" can be used
059     *            to match all (external) repositories, prefixing a repo id with an exclamation mark allows to express
060     *            an exclusion. For example "external:*,!central".
061     * @param mirrorOfTypes The content type(s) of remote repositories to mirror, may be {@code null} or empty to match
062     *            any content type. Similar to the repo id specification, multiple types can be comma-separated, the
063     *            wildcard "*" and the "!" negation syntax are supported. For example "*,!p2".
064     * @return This selector for chaining, never {@code null}.
065     */
066    public DefaultMirrorSelector add( String id, String url, String type, boolean repositoryManager, boolean blocked,
067                                      String mirrorOfIds, String mirrorOfTypes )
068    {
069        mirrors.add( new MirrorDef( id, url, type, repositoryManager, blocked, mirrorOfIds, mirrorOfTypes ) );
070
071        return this;
072    }
073
074    public RemoteRepository getMirror( RemoteRepository repository )
075    {
076        MirrorDef mirror = findMirror( repository );
077
078        if ( mirror == null )
079        {
080            return null;
081        }
082
083        RemoteRepository.Builder builder =
084            new RemoteRepository.Builder( mirror.id, repository.getContentType(), mirror.url );
085
086        builder.setRepositoryManager( mirror.repositoryManager );
087
088        builder.setBlocked( mirror.blocked );
089
090        if ( mirror.type != null && mirror.type.length() > 0 )
091        {
092            builder.setContentType( mirror.type );
093        }
094
095        builder.setSnapshotPolicy( repository.getPolicy( true ) );
096        builder.setReleasePolicy( repository.getPolicy( false ) );
097
098        builder.setMirroredRepositories( Collections.singletonList( repository ) );
099
100        return builder.build();
101    }
102
103    private MirrorDef findMirror( RemoteRepository repository )
104    {
105        String repoId = repository.getId();
106
107        if ( repoId != null && !mirrors.isEmpty() )
108        {
109            for ( MirrorDef mirror : mirrors )
110            {
111                if ( repoId.equals( mirror.mirrorOfIds ) && matchesType( repository.getContentType(),
112                                                                         mirror.mirrorOfTypes ) )
113                {
114                    return mirror;
115                }
116            }
117
118            for ( MirrorDef mirror : mirrors )
119            {
120                if ( matchPattern( repository, mirror.mirrorOfIds ) && matchesType( repository.getContentType(),
121                                                                                    mirror.mirrorOfTypes ) )
122                {
123                    return mirror;
124                }
125            }
126        }
127
128        return null;
129    }
130
131    /**
132     * This method checks if the pattern matches the originalRepository. Valid patterns: * = everything external:* =
133     * everything not on the localhost and not file based. repo,repo1 = repo or repo1 *,!repo1 = everything except repo1
134     * 
135     * @param repository to compare for a match.
136     * @param pattern used for match. Currently only '*' is supported.
137     * @return true if the repository is a match to this pattern.
138     */
139    static boolean matchPattern( RemoteRepository repository, String pattern )
140    {
141        boolean result = false;
142        String originalId = repository.getId();
143
144        // simple checks first to short circuit processing below.
145        if ( WILDCARD.equals( pattern ) || pattern.equals( originalId ) )
146        {
147            result = true;
148        }
149        else
150        {
151            // process the list
152            String[] repos = pattern.split( "," );
153            for ( String repo : repos )
154            {
155                // see if this is a negative match
156                if ( repo.length() > 1 && repo.startsWith( "!" ) )
157                {
158                    if ( repo.substring( 1 ).equals( originalId ) )
159                    {
160                        // explicitly exclude. Set result and stop processing.
161                        result = false;
162                        break;
163                    }
164                }
165                // check for exact match
166                else if ( repo.equals( originalId ) )
167                {
168                    result = true;
169                    break;
170                }
171                // check for external:*
172                else if ( EXTERNAL_WILDCARD.equals( repo ) && isExternalRepo( repository ) )
173                {
174                    result = true;
175                    // don't stop processing in case a future segment explicitly excludes this repo
176                }
177                else if ( WILDCARD.equals( repo ) )
178                {
179                    result = true;
180                    // don't stop processing in case a future segment explicitly excludes this repo
181                }
182            }
183        }
184        return result;
185    }
186
187    /**
188     * Checks the URL to see if this repository refers to an external repository.
189     * 
190     * @param repository The repository to check, must not be {@code null}.
191     * @return {@code true} if external, {@code false} otherwise.
192     */
193    static boolean isExternalRepo( RemoteRepository repository )
194    {
195        boolean local =
196            "localhost".equals( repository.getHost() ) || "127.0.0.1".equals( repository.getHost() )
197                || "file".equalsIgnoreCase( repository.getProtocol() );
198        return !local;
199    }
200
201    /**
202     * Checks whether the types configured for a mirror match with the type of the repository.
203     * 
204     * @param repoType The type of the repository, may be {@code null}.
205     * @param mirrorType The types supported by the mirror, may be {@code null}.
206     * @return {@code true} if the types associated with the mirror match the type of the original repository,
207     *         {@code false} otherwise.
208     */
209    static boolean matchesType( String repoType, String mirrorType )
210    {
211        boolean result = false;
212
213        // simple checks first to short circuit processing below.
214        if ( mirrorType == null || mirrorType.length() <= 0 || WILDCARD.equals( mirrorType ) )
215        {
216            result = true;
217        }
218        else if ( mirrorType.equals( repoType ) )
219        {
220            result = true;
221        }
222        else
223        {
224            // process the list
225            String[] layouts = mirrorType.split( "," );
226            for ( String layout : layouts )
227            {
228                // see if this is a negative match
229                if ( layout.length() > 1 && layout.startsWith( "!" ) )
230                {
231                    if ( layout.substring( 1 ).equals( repoType ) )
232                    {
233                        // explicitly exclude. Set result and stop processing.
234                        result = false;
235                        break;
236                    }
237                }
238                // check for exact match
239                else if ( layout.equals( repoType ) )
240                {
241                    result = true;
242                    break;
243                }
244                else if ( WILDCARD.equals( layout ) )
245                {
246                    result = true;
247                    // don't stop processing in case a future segment explicitly excludes this repo
248                }
249            }
250        }
251
252        return result;
253    }
254
255    static class MirrorDef
256    {
257
258        final String id;
259
260        final String url;
261
262        final String type;
263
264        final boolean repositoryManager;
265
266        final boolean blocked;
267
268        final String mirrorOfIds;
269
270        final String mirrorOfTypes;
271
272        MirrorDef( String id, String url, String type, boolean repositoryManager, boolean blocked, String mirrorOfIds,
273                   String mirrorOfTypes )
274        {
275            this.id = id;
276            this.url = url;
277            this.type = type;
278            this.repositoryManager = repositoryManager;
279            this.blocked = blocked;
280            this.mirrorOfIds = mirrorOfIds;
281            this.mirrorOfTypes = mirrorOfTypes;
282        }
283
284    }
285
286}