1 package org.eclipse.aether.util.repository;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Locale;
26 import java.util.Map;
27 import static java.util.Objects.requireNonNull;
28 import java.util.StringTokenizer;
29 import java.util.regex.Pattern;
30
31 import org.eclipse.aether.repository.Proxy;
32 import org.eclipse.aether.repository.ProxySelector;
33 import org.eclipse.aether.repository.RemoteRepository;
34
35
36
37
38 public final class DefaultProxySelector
39 implements ProxySelector
40 {
41
42 private List<ProxyDef> proxies = new ArrayList<ProxyDef>();
43
44
45
46
47
48
49
50
51
52 public DefaultProxySelector add( Proxy proxy, String nonProxyHosts )
53 {
54 requireNonNull( proxy, "proxy cannot be null" );
55 proxies.add( new ProxyDef( proxy, nonProxyHosts ) );
56
57 return this;
58 }
59
60 public Proxy getProxy( RemoteRepository repository )
61 {
62 Map<String, ProxyDef> candidates = new HashMap<String, ProxyDef>();
63
64 String host = repository.getHost();
65 for ( ProxyDef proxy : proxies )
66 {
67 if ( !proxy.nonProxyHosts.isNonProxyHost( host ) )
68 {
69 String key = proxy.proxy.getType().toLowerCase( Locale.ENGLISH );
70 if ( !candidates.containsKey( key ) )
71 {
72 candidates.put( key, proxy );
73 }
74 }
75 }
76
77 String protocol = repository.getProtocol().toLowerCase( Locale.ENGLISH );
78
79 if ( "davs".equals( protocol ) )
80 {
81 protocol = "https";
82 }
83 else if ( "dav".equals( protocol ) )
84 {
85 protocol = "http";
86 }
87 else if ( protocol.startsWith( "dav:" ) )
88 {
89 protocol = protocol.substring( "dav:".length() );
90 }
91
92 ProxyDef proxy = candidates.get( protocol );
93 if ( proxy == null && "https".equals( protocol ) )
94 {
95 proxy = candidates.get( "http" );
96 }
97
98 return ( proxy != null ) ? proxy.proxy : null;
99 }
100
101 static class NonProxyHosts
102 {
103
104 private final Pattern[] patterns;
105
106 NonProxyHosts( String nonProxyHosts )
107 {
108 List<Pattern> patterns = new ArrayList<Pattern>();
109 if ( nonProxyHosts != null )
110 {
111 for ( StringTokenizer tokenizer = new StringTokenizer( nonProxyHosts, "|" ); tokenizer.hasMoreTokens(); )
112 {
113 String pattern = tokenizer.nextToken();
114 pattern = pattern.replace( ".", "\\." ).replace( "*", ".*" );
115 patterns.add( Pattern.compile( pattern, Pattern.CASE_INSENSITIVE ) );
116 }
117 }
118 this.patterns = patterns.toArray( new Pattern[patterns.size()] );
119 }
120
121 boolean isNonProxyHost( String host )
122 {
123 if ( host != null )
124 {
125 for ( Pattern pattern : patterns )
126 {
127 if ( pattern.matcher( host ).matches() )
128 {
129 return true;
130 }
131 }
132 }
133 return false;
134 }
135
136 }
137
138 static class ProxyDef
139 {
140
141 final Proxy proxy;
142
143 final NonProxyHosts nonProxyHosts;
144
145 ProxyDef( Proxy proxy, String nonProxyHosts )
146 {
147 this.proxy = proxy;
148 this.nonProxyHosts = new NonProxyHosts( nonProxyHosts );
149 }
150
151 }
152
153 }