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 private static final String NON_PROXY_WILDCARD = "*";
12
13 private final HttpHost proxy;
14 private final String[] nonProxyHosts;
15
16 public ProxyRoutePlanner(final ProxyConfig proxyConfig) {
17 super(null);
18 this.proxy = new HttpHost(proxyConfig.getHost(), proxyConfig.getPort());
19 this.nonProxyHosts = proxyConfig.getNonProxyHosts();
20 }
21
22 @Override
23 protected HttpHost determineProxy(final HttpHost target, final HttpRequest request, final HttpContext context)
24 throws HttpException {
25 return shouldBeProxied(target.getHostName()) ? proxy : null;
26 }
27
28 private boolean shouldBeProxied(final String host) {
29 if (StringUtils.isBlank(host)) {
30 return false;
31 }
32
33 for (final String nonProxyHost : nonProxyHosts) {
34 if (nonProxyHost.startsWith(NON_PROXY_WILDCARD)) {
35 if (host.endsWith(nonProxyHost.substring(1))) {
36 return false;
37 }
38 } else if (host.equals(nonProxyHost)) {
39 return false;
40 }
41 }
42
43 return true;
44 }
45
46 }