FileDocCategorySizeDatePackage
SetProxy.javaAPI DocApache Ant 1.709306Wed Dec 13 06:16:24 GMT 2006org.apache.tools.ant.taskdefs.optional.net

SetProxy

public class SetProxy extends org.apache.tools.ant.Task
Sets Java's web proxy properties, so that tasks and code run in the same JVM can have through-the-firewall access to remote web sites, and remote ftp sites. You can nominate an http and ftp proxy, or a socks server, reset the server settings, or do nothing at all.

Examples

<setproxy/>
do nothing
<setproxy proxyhost="firewall"/>
set the proxy to firewall:80
<setproxy proxyhost="firewall" proxyport="81"/>
set the proxy to firewall:81
<setproxy proxyhost=""/>
stop using the http proxy; don't change the socks settings
<setproxy socksproxyhost="socksy"/>
use socks via socksy:1080
<setproxy socksproxyhost=""/>
stop using the socks server.

You can set a username and password for http with the proxyHost and proxyPassword attributes. On Java1.4 and above these can also be used against SOCKS5 servers.

see
java 1.5 network property list
since
Ant 1.5
ant.task
category="network"

Fields Summary
protected String
proxyHost
proxy details
protected int
proxyPort
name of proxy port
private String
socksProxyHost
socks host.
private int
socksProxyPort
Socks proxy port. Default is 1080.
private String
nonProxyHosts
list of non proxy hosts
private String
proxyUser
user for http only
private String
proxyPassword
password for http only
Constructors Summary
Methods Summary
public voidapplyWebProxySettings()
if the proxy port and host settings are not null, then the settings get applied these settings last beyond the life of the object and apply to all network connections Relevant docs: buglist #4183340

        boolean settingsChanged = false;
        boolean enablingProxy = false;
        Properties sysprops = System.getProperties();
        if (proxyHost != null) {
            settingsChanged = true;
            if (proxyHost.length() != 0) {
                traceSettingInfo();
                enablingProxy = true;
                sysprops.put(ProxySetup.HTTP_PROXY_HOST, proxyHost);
                String portString = Integer.toString(proxyPort);
                sysprops.put(ProxySetup.HTTP_PROXY_PORT, portString);
                sysprops.put(ProxySetup.HTTPS_PROXY_HOST, proxyHost);
                sysprops.put(ProxySetup.HTTPS_PROXY_PORT, portString);
                sysprops.put(ProxySetup.FTP_PROXY_HOST, proxyHost);
                sysprops.put(ProxySetup.FTP_PROXY_PORT, portString);
                if (nonProxyHosts != null) {
                    sysprops.put(ProxySetup.HTTP_NON_PROXY_HOSTS, nonProxyHosts);
                    sysprops.put(ProxySetup.HTTPS_NON_PROXY_HOSTS, nonProxyHosts);
                    sysprops.put(ProxySetup.FTP_NON_PROXY_HOSTS, nonProxyHosts);
                }
                if (proxyUser != null) {
                    sysprops.put(ProxySetup.HTTP_PROXY_USERNAME, proxyUser);
                    sysprops.put(ProxySetup.HTTP_PROXY_PASSWORD, proxyPassword);
                }
            } else {
                log("resetting http proxy", Project.MSG_VERBOSE);
                sysprops.remove(ProxySetup.HTTP_PROXY_HOST);
                sysprops.remove(ProxySetup.HTTP_PROXY_PORT);
                sysprops.remove(ProxySetup.HTTP_PROXY_USERNAME);
                sysprops.remove(ProxySetup.HTTP_PROXY_PASSWORD);
                sysprops.remove(ProxySetup.HTTPS_PROXY_HOST);
                sysprops.remove(ProxySetup.HTTPS_PROXY_PORT);
                sysprops.remove(ProxySetup.FTP_PROXY_HOST);
                sysprops.remove(ProxySetup.FTP_PROXY_PORT);
            }
        }

        //socks
        if (socksProxyHost != null) {
            settingsChanged = true;
            if (socksProxyHost.length() != 0) {
                enablingProxy = true;
                sysprops.put(ProxySetup.SOCKS_PROXY_HOST, socksProxyHost);
                sysprops.put(ProxySetup.SOCKS_PROXY_PORT, Integer.toString(socksProxyPort));
                if (proxyUser != null) {
                    //this may be a java1.4 thingy only
                    sysprops.put(ProxySetup.SOCKS_PROXY_USERNAME, proxyUser);
                    sysprops.put(ProxySetup.SOCKS_PROXY_PASSWORD, proxyPassword);
                }

            } else {
                log("resetting socks proxy", Project.MSG_VERBOSE);
                sysprops.remove(ProxySetup.SOCKS_PROXY_HOST);
                sysprops.remove(ProxySetup.SOCKS_PROXY_PORT);
                sysprops.remove(ProxySetup.SOCKS_PROXY_USERNAME);
                sysprops.remove(ProxySetup.SOCKS_PROXY_PASSWORD);
            }
        }

        if (proxyUser != null) {
            if (enablingProxy) {
                Authenticator.setDefault(new ProxyAuth(proxyUser,
                                                       proxyPassword));
            } else if (settingsChanged) {
                Authenticator.setDefault(new ProxyAuth("", ""));
            }
        }
    
public voidexecute()
Does the work.

exception
BuildException thrown in unrecoverable error.

        applyWebProxySettings();
    
public voidsetNonProxyHosts(java.lang.String nonProxyHosts)
A list of hosts to bypass the proxy on. These should be separated with the vertical bar character '|'. Only in Java 1.4 does ftp use this list. e.g. fozbot.corp.sun.com|*.eng.sun.com

param
nonProxyHosts lists of hosts to talk direct to

        this.nonProxyHosts = nonProxyHosts;
    
public voidsetProxyHost(java.lang.String hostname)
the HTTP/ftp proxy host. Set this to "" for the http proxy option to be disabled

param
hostname the new proxy hostname


                               
        
        proxyHost = hostname;
    
public voidsetProxyPassword(java.lang.String proxyPassword)
Set the password for the proxy. Used only if the proxyUser is set.

param
proxyPassword password to go with the username
since
Ant1.6

        this.proxyPassword = proxyPassword;
    
public voidsetProxyPort(int port)
the HTTP/ftp proxy port number; default is 80

param
port port number of the proxy

        proxyPort = port;
    
public voidsetProxyUser(java.lang.String proxyUser)
set the proxy user. Probably requires a password to accompany this setting. Default=""

param
proxyUser username
since
Ant1.6

        this.proxyUser = proxyUser;
    
public voidsetSocksProxyHost(java.lang.String host)
The name of a Socks server. Set to "" to turn socks proxying off.

param
host The new SocksProxyHost value

        this.socksProxyHost = host;
    
public voidsetSocksProxyPort(int port)
Set the ProxyPort for socks connections. The default value is 1080

param
port The new SocksProxyPort value

        this.socksProxyPort = port;
    
private voidtraceSettingInfo()
list out what is going on

        log("Setting proxy to "
                + (proxyHost != null ? proxyHost : "''")
                + ":" + proxyPort,
                Project.MSG_VERBOSE);