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.Collection; 024import java.util.HashMap; 025import java.util.List; 026import java.util.Locale; 027import java.util.Map; 028import static java.util.Objects.requireNonNull; 029import java.util.StringTokenizer; 030import java.util.regex.Pattern; 031 032import org.eclipse.aether.repository.Proxy; 033import org.eclipse.aether.repository.ProxySelector; 034import org.eclipse.aether.repository.RemoteRepository; 035 036/** 037 * A simple proxy selector that selects the first matching proxy from a list of configured proxies. 038 */ 039public final class DefaultProxySelector 040 implements ProxySelector 041{ 042 043 private List<ProxyDef> proxies = new ArrayList<ProxyDef>(); 044 045 /** 046 * Adds the specified proxy definition to the selector. Proxy definitions are ordered, the first matching proxy for 047 * a given repository will be used. 048 * 049 * @param proxy The proxy definition to add, must not be {@code null}. 050 * @param nonProxyHosts The list of (case-insensitive) host names to exclude from proxying, may be {@code null}. 051 * @return This proxy selector for chaining, never {@code null}. 052 */ 053 public DefaultProxySelector add( Proxy proxy, String nonProxyHosts ) 054 { 055 requireNonNull( proxy, "proxy cannot be null" ); 056 proxies.add( new ProxyDef( proxy, nonProxyHosts ) ); 057 058 return this; 059 } 060 061 public Proxy getProxy( RemoteRepository repository ) 062 { 063 Map<String, ProxyDef> candidates = new HashMap<String, ProxyDef>(); 064 065 String host = repository.getHost(); 066 for ( ProxyDef proxy : proxies ) 067 { 068 if ( !proxy.nonProxyHosts.isNonProxyHost( host ) ) 069 { 070 String key = proxy.proxy.getType().toLowerCase( Locale.ENGLISH ); 071 if ( !candidates.containsKey( key ) ) 072 { 073 candidates.put( key, proxy ); 074 } 075 } 076 } 077 078 String protocol = repository.getProtocol().toLowerCase( Locale.ENGLISH ); 079 080 if ( "davs".equals( protocol ) ) 081 { 082 protocol = "https"; 083 } 084 else if ( "dav".equals( protocol ) ) 085 { 086 protocol = "http"; 087 } 088 else if ( protocol.startsWith( "dav:" ) ) 089 { 090 protocol = protocol.substring( "dav:".length() ); 091 } 092 093 ProxyDef proxy = candidates.get( protocol ); 094 if ( proxy == null && "https".equals( protocol ) ) 095 { 096 proxy = candidates.get( "http" ); 097 } 098 099 return ( proxy != null ) ? proxy.proxy : null; 100 } 101 102 static class NonProxyHosts 103 { 104 105 private final Pattern[] patterns; 106 107 public NonProxyHosts( String nonProxyHosts ) 108 { 109 List<Pattern> patterns = new ArrayList<Pattern>(); 110 if ( nonProxyHosts != null ) 111 { 112 for ( StringTokenizer tokenizer = new StringTokenizer( nonProxyHosts, "|" ); tokenizer.hasMoreTokens(); ) 113 { 114 String pattern = tokenizer.nextToken(); 115 pattern = pattern.replace( ".", "\\." ).replace( "*", ".*" ); 116 patterns.add( Pattern.compile( pattern, Pattern.CASE_INSENSITIVE ) ); 117 } 118 } 119 this.patterns = patterns.toArray( new Pattern[patterns.size()] ); 120 } 121 122 boolean isNonProxyHost( String host ) 123 { 124 if ( host != null ) 125 { 126 for ( Pattern pattern : patterns ) 127 { 128 if ( pattern.matcher( host ).matches() ) 129 { 130 return true; 131 } 132 } 133 } 134 return false; 135 } 136 137 } 138 139 static class ProxyDef 140 { 141 142 final Proxy proxy; 143 144 final NonProxyHosts nonProxyHosts; 145 146 public ProxyDef( Proxy proxy, String nonProxyHosts ) 147 { 148 this.proxy = proxy; 149 this.nonProxyHosts = new NonProxyHosts( nonProxyHosts ); 150 } 151 152 } 153 154}