use of aQute.bnd.service.url.ProxyHandler in project bnd by bndtools.
the class ConnectionSettings method createProxyHandler.
/**
* Create Proxy Handler from ProxyDTO
*/
public static ProxyHandler createProxyHandler(final ProxyDTO proxyDTO) {
return new ProxyHandler() {
Glob[] globs;
private ProxySetup proxySetup;
@Override
public ProxySetup forURL(URL url) throws Exception {
switch(proxyDTO.protocol) {
case DIRECT:
break;
case HTTP:
String scheme = url.getProtocol();
if (scheme.equalsIgnoreCase("http") || scheme.equalsIgnoreCase("https")) {
// ok
} else
return null;
break;
case SOCKS:
break;
default:
break;
}
String host = url.getHost();
if (host != null) {
if (isNonProxyHost(host))
return null;
}
if (proxySetup == null) {
proxySetup = new ProxySetup();
if (proxyDTO.username != null && proxyDTO.password != null)
proxySetup.authentication = new PasswordAuthentication(proxyDTO.username, proxyDTO.password.toCharArray());
SocketAddress socketAddress;
if (proxyDTO.host != null)
socketAddress = new InetSocketAddress(proxyDTO.host, proxyDTO.port);
else
socketAddress = new InetSocketAddress(proxyDTO.port);
proxySetup.proxy = new Proxy(proxyDTO.protocol, socketAddress);
}
return proxySetup;
}
public boolean isNonProxyHost(String host) {
Glob[] globs = getNonProxyHosts(proxyDTO);
for (Glob glob : globs) {
if (glob.matcher(host).matches())
return true;
}
return false;
}
public Glob[] getNonProxyHosts(final ProxyDTO proxyDTO) {
// not synchronized because conflicts only do some double work
if (globs == null) {
if (proxyDTO.nonProxyHosts == null)
globs = new Glob[0];
else {
String[] parts = proxyDTO.nonProxyHosts.split("\\s*\\|\\s*");
globs = new Glob[parts.length];
for (int i = 0; i < parts.length; i++) globs[i] = new Glob(parts[i]);
}
}
return globs;
}
};
}
Aggregations