iteman / win-proxy-selector

Win Proxy Selector is a ProxySelector implementation for Windows environment.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

win-proxy-selector

Win Proxy Selector is a ProxySelector implementation for Windows environment.

Win Proxy Selector is a fork of Proxy Vole, which is a Java library to auto detect the platform network proxy settings. Win Proxy Selector is currently dedicated to the Windows environment.

Note: This library is a fork of proxy-vole which in turn is the now dead proxy-vole project by Bernd Rosstauscher hosted at Google Code.

Introduction

The library provides a ProxySelector implementation which reads the proxy settings from the system config of Windows, IE config (both on the Windows Registory) and provides you a ready to use proxy selector.

Why a fork?

  • We want more greedy approach in finding the appropriate proxy server than that used in Proxy Vole (eg. tries provided fixed proxy address if failed to auto-detect one or the auto-detected proxy is not responding).

Usage

Using WinProxySelector

// Instantiate a WinProxySelector giving a fallback proxy selector (usually the system's default proxy selector).

ProxySelector myProxySelector = new WinProxySelector(ProxySelector.getDefault());

// Proxies can be got by invoking select() method with a URI you want to connect to.
List<Proxy> proxies = myProxySelector.select(new URI("http://www.fusions.co.jp"));

// You can also install this ProxySelector as the default ProxySelector for all connections.
ProxySelector.setDefault(proxySelector);

How to handle proxy authentication

Some proxy servers request a login from the user before they will allow any connections. Win Proxy Selector has no support to handle this automatically. In the Java communication framework, it is not a responsibility of a ProxySelector but an Authenticator to authenticate users. You need to install an authenticator in your Java program manually and e.g. ask the user in a dialog to enter the username and password.

Authenticator.setDefault(new Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
        if (getRequestorType() == RequestorType.PROXY) {
            return new PasswordAuthentication("proxy-user", "proxy-password".toCharArray());
        } else { 
            return super.getPasswordAuthentication();
        }
    }               
});

Choose the right proxy

Please be aware a Java ProxySelector returns a list of valid proxies for a given URL and sometimes simply choosing the first one is not good enough. Very often a check of the supported protocol is necessary.

The following code chooses the first HTTP/S proxy.

Proxy proxy = Proxy.NO_PROXY;

// Get list of proxies from default ProxySelector available for given URL
List<Proxy> proxies = null;
if (ProxySelector.getDefault() != null) {
    proxies = ProxySelector.getDefault().select(uri);
}

// Find first proxy for HTTP/S. Any DIRECT proxy in the list returned is only second choice
if (proxies != null) {
    loop: for (Proxy p : proxies) {
        switch (p.type()) {
        case HTTP:
            proxy = p;
            break loop;
        case DIRECT:
            proxy = p;
            break;
        }
    }
}

Logging

Win Proxy Selector allows you to use arbitrary logging framework. Install your logger which would redirect the logging output using Logger.setBackend() like this:

// Register MyLogger instance 
Logger.setBackend(new MyLogger());

About

Win Proxy Selector is a ProxySelector implementation for Windows environment.

License:Other


Languages

Language:Java 77.4%Language:HTML 14.1%Language:JavaScript 8.4%