Search in sources :

Example 1 with ProxyHandler

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;
        }
    };
}
Also used : ProxyHandler(aQute.bnd.service.url.ProxyHandler) InetSocketAddress(java.net.InetSocketAddress) URL(java.net.URL) Proxy(java.net.Proxy) Glob(aQute.libg.glob.Glob) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) PasswordAuthentication(java.net.PasswordAuthentication)

Aggregations

ProxyHandler (aQute.bnd.service.url.ProxyHandler)1 Glob (aQute.libg.glob.Glob)1 InetSocketAddress (java.net.InetSocketAddress)1 PasswordAuthentication (java.net.PasswordAuthentication)1 Proxy (java.net.Proxy)1 SocketAddress (java.net.SocketAddress)1 URL (java.net.URL)1