Using ProxySelector to allow users to configure the proxy

Paulo Levi i30817 at gmail.com
Thu Apr 30 08:33:12 PDT 2009


Hi. I'm using my application on networks that have proxy, and others that do
not.

I'm trying to make a ProxySelector that will use the System proxy setting as
default (just pass the select request to the delegate), and if that fails,
showing a dialog to configure the proxy for this application instance and
retry.
I thought that it would be pretty easy just by creating a list on
ProxySelector.select that first has the delegate proxy settings and then
adding my lazy Proxy implementation that when requested the SocketAdress
would query the user (as a last resort).

The problem is that the returned connection for the delegate is DIRECT with
proxy = Proxy.NO_PROXY and that is handled specially in the SocksSocketImpl
class, and does NOT catch the thrown exceptions, unlike all the other
Proxies.
Any way i can fix this?

Alternativelly, any  way i can probe for the proxy to change my strategy
from fail - query to test once at init - ask proxy if neeed - use proxy if
needed?

JDK code on protected void connect(SocketAddress endpoint, int timeout)
throws IOException of SocksSocketImpl follows.

        iProxy = sel.select(uri).iterator();
        if (iProxy == null || !(iProxy.hasNext())) {
        super.connect(epoint, timeout);
        return;
        }
        while (iProxy.hasNext()) {
        p = iProxy.next();
        if (p == null || p == Proxy.NO_PROXY) {
            super.connect(epoint, timeout);
<-----------------------EXCEPTION HERE
            return;
        }
        if (p.type() != Proxy.Type.SOCKS)
            throw new SocketException("Unknown proxy type : " + p.type());
        if (!(p.address() instanceof InetSocketAddress))
            throw new SocketException("Unknow address type for proxy: " +
p);
        // Use getHostString() to avoid reverse lookups
        server = ((InetSocketAddress) p.address()).getHostString();
        port = ((InetSocketAddress) p.address()).getPort();

        // Connects to the SOCKS server
        try {
            privilegedConnect(server, port, timeout);
            // Worked, let's get outta here
            break;
        } catch (IOException e) {
            // Ooops, let's notify the ProxySelector
            sel.connectFailed(uri,p.address(),e);
            server = null;
            port = -1;
            savedExc = e;
            // Will continue the while loop and try the next proxy
        }
        }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/net-dev/attachments/20090430/04816562/attachment.html 


More information about the net-dev mailing list