001package org.eclipse.aether.internal.impl;
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.regex.Pattern;
023
024import javax.inject.Named;
025import javax.inject.Singleton;
026
027import org.eclipse.aether.RepositorySystemSession;
028import org.eclipse.aether.impl.OfflineController;
029import org.eclipse.aether.repository.RemoteRepository;
030import org.eclipse.aether.transfer.RepositoryOfflineException;
031import org.eclipse.aether.util.ConfigUtils;
032
033/**
034 * 
035 */
036@Singleton
037@Named
038public class DefaultOfflineController
039    implements OfflineController
040{
041
042    static final String CONFIG_PROP_OFFLINE_PROTOCOLS = "aether.offline.protocols";
043
044    static final String CONFIG_PROP_OFFLINE_HOSTS = "aether.offline.hosts";
045
046    private static final Pattern SEP = Pattern.compile( "\\s*,\\s*" );
047
048    public DefaultOfflineController()
049    {
050        // enables default constructor
051    }
052
053    public void checkOffline( RepositorySystemSession session, RemoteRepository repository )
054        throws RepositoryOfflineException
055    {
056        if ( isOfflineProtocol( session, repository ) || isOfflineHost( session, repository ) )
057        {
058            return;
059        }
060
061        throw new RepositoryOfflineException( repository );
062    }
063
064    private boolean isOfflineProtocol( RepositorySystemSession session, RemoteRepository repository )
065    {
066        String[] protocols = getConfig( session, CONFIG_PROP_OFFLINE_PROTOCOLS );
067        if ( protocols != null )
068        {
069            String protocol = repository.getProtocol();
070            if ( protocol.length() > 0 )
071            {
072                for ( String p : protocols )
073                {
074                    if ( p.equalsIgnoreCase( protocol ) )
075                    {
076                        return true;
077                    }
078                }
079            }
080        }
081        return false;
082    }
083
084    private boolean isOfflineHost( RepositorySystemSession session, RemoteRepository repository )
085    {
086        String[] hosts = getConfig( session, CONFIG_PROP_OFFLINE_HOSTS );
087        if ( hosts != null )
088        {
089            String host = repository.getHost();
090            if ( host.length() > 0 )
091            {
092                for ( String h : hosts )
093                {
094                    if ( h.equalsIgnoreCase( host ) )
095                    {
096                        return true;
097                    }
098                }
099            }
100        }
101        return false;
102    }
103
104    private String[] getConfig( RepositorySystemSession session, String key )
105    {
106        String value = ConfigUtils.getString( session, "", key ).trim();
107        if ( value.length() <= 0 )
108        {
109            return null;
110        }
111        return SEP.split( value );
112    }
113
114}