1 package com.atlassian.sal.core.net;
2
3 import org.apache.commons.lang.StringUtils;
4 import org.apache.http.HttpException;
5 import org.apache.http.HttpHost;
6 import org.apache.http.HttpRequest;
7 import org.apache.http.impl.conn.DefaultRoutePlanner;
8 import org.apache.http.protocol.HttpContext;
9
10 public class ProxyRoutePlanner extends DefaultRoutePlanner
11 {
12 private static final String NON_PROXY_WILDCARD = "*";
13
14 private final HttpHost proxy;
15 private final String[] nonProxyHosts;
16
17 public ProxyRoutePlanner(final ProxyConfig proxyConfig)
18 {
19 super(null);
20 this.proxy = new HttpHost(proxyConfig.getHost(), proxyConfig.getPort());
21 this.nonProxyHosts = proxyConfig.getNonProxyHosts();
22 }
23
24 @Override
25 protected HttpHost determineProxy(final HttpHost target, final HttpRequest request, final HttpContext context)
26 throws HttpException
27 {
28 return shouldBeProxied(target.getHostName()) ? proxy : null;
29 }
30
31 private boolean shouldBeProxied(final String host)
32 {
33 if (StringUtils.isBlank(host))
34 {
35 return false;
36 }
37
38 for (final String nonProxyHost : nonProxyHosts)
39 {
40 if (nonProxyHost.startsWith(NON_PROXY_WILDCARD))
41 {
42 if (host.endsWith(nonProxyHost.substring(1)))
43 {
44 return false;
45 }
46 }
47 else if (host.equals(nonProxyHost))
48 {
49 return false;
50 }
51 }
52
53 return true;
54 }
55
56 }