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.
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.
- 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).
// 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);
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();
}
}
});
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;
}
}
}
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());