package org.jboss.forge.shell.util;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Proxy.Type;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

/*
 * Implemented following the guide at:
 * http://docs.oracle.com/javase/6/docs/technotes/guides/net/proxies.html
 */
public class ForgeProxySelector extends ProxySelector {

    private ProxySelector defaultProxySelector;
    private ProxySettings proxySettings;

    public ForgeProxySelector(ProxySelector defaultProxySelector, ProxySettings proxySettings) {
        this.defaultProxySelector = defaultProxySelector;
        this.proxySettings = proxySettings;
    }

    @Override
    public List<Proxy> select(URI uri) {
        if (uri == null) {
            throw new IllegalArgumentException("URI can't be null.");
        }
        String protocol = uri.getScheme();
        if ("http".equalsIgnoreCase(protocol) || "https".equalsIgnoreCase(protocol)) {
            ArrayList<Proxy> result = new ArrayList<Proxy>();
            result.add(new Proxy(Type.HTTP, new InetSocketAddress(proxySettings.getProxyHost(), 
                    proxySettings.getProxyPort()))); 
            return result;
        }
        if (defaultProxySelector != null) {
            return defaultProxySelector.select(uri);
        } else {
            ArrayList<Proxy> result = new ArrayList<Proxy>();
            result.add(Proxy.NO_PROXY);
            return result;
        }
    }

    @Override
    public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
        defaultProxySelector.connectFailed(uri, sa, ioe);
    }

}

@@ -77,33 +77,106 @@
 ava.net.
-InetSocketAddress
+Authenticator;%0D%0Aimport java.net.InetSocketAddress;%0D%0Aimport java.net.PasswordAuthentication
 ;%0D%0Aimpor
@@ -1363,17 +1363,461 @@
 rt())));
- 
+%0D%0A            if (proxySettings.isAuthenticationSupported()) %7B%0D%0A                Authenticator.setDefault(new Authenticator() %7B%0D%0A                    protected PasswordAuthentication getPasswordAuthentication() %7B%0D%0A                        return new PasswordAuthentication(proxySettings.getProxyUserName(), %0D%0A                                proxySettings.getProxyPassword().toCharArray());%0D%0A                    %7D%0D%0A                %7D);%0D%0A            %7D
 %0D%0A      

Make sure that the proxy selector supports proxies requiring authentication as well