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