001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.eclipse.aether.internal.impl; 020 021import javax.inject.Named; 022import javax.inject.Singleton; 023 024import java.util.regex.Pattern; 025 026import org.eclipse.aether.RepositorySystemSession; 027import org.eclipse.aether.impl.OfflineController; 028import org.eclipse.aether.repository.RemoteRepository; 029import org.eclipse.aether.transfer.RepositoryOfflineException; 030import org.eclipse.aether.util.ConfigUtils; 031 032import static java.util.Objects.requireNonNull; 033 034/** 035 * 036 */ 037@Singleton 038@Named 039public class DefaultOfflineController implements OfflineController { 040 041 static final String CONFIG_PROP_OFFLINE_PROTOCOLS = "aether.offline.protocols"; 042 043 static final String CONFIG_PROP_OFFLINE_HOSTS = "aether.offline.hosts"; 044 045 private static final Pattern SEP = Pattern.compile("\\s*,\\s*"); 046 047 public DefaultOfflineController() { 048 // enables default constructor 049 } 050 051 public void checkOffline(RepositorySystemSession session, RemoteRepository repository) 052 throws RepositoryOfflineException { 053 requireNonNull(session, "session cannot be null"); 054 requireNonNull(repository, "repository cannot be null"); 055 if (isOfflineProtocol(session, repository) || isOfflineHost(session, repository)) { 056 return; 057 } 058 059 throw new RepositoryOfflineException(repository); 060 } 061 062 private boolean isOfflineProtocol(RepositorySystemSession session, RemoteRepository repository) { 063 String[] protocols = getConfig(session, CONFIG_PROP_OFFLINE_PROTOCOLS); 064 if (protocols != null) { 065 String protocol = repository.getProtocol(); 066 if (protocol.length() > 0) { 067 for (String p : protocols) { 068 if (p.equalsIgnoreCase(protocol)) { 069 return true; 070 } 071 } 072 } 073 } 074 return false; 075 } 076 077 private boolean isOfflineHost(RepositorySystemSession session, RemoteRepository repository) { 078 String[] hosts = getConfig(session, CONFIG_PROP_OFFLINE_HOSTS); 079 if (hosts != null) { 080 String host = repository.getHost(); 081 if (host.length() > 0) { 082 for (String h : hosts) { 083 if (h.equalsIgnoreCase(host)) { 084 return true; 085 } 086 } 087 } 088 } 089 return false; 090 } 091 092 private String[] getConfig(RepositorySystemSession session, String key) { 093 String value = ConfigUtils.getString(session, "", key).trim(); 094 if (value.isEmpty()) { 095 return null; 096 } 097 return SEP.split(value); 098 } 099}