1 18 package org.apache.tools.ant.taskdefs.optional.net; 19 20 import java.net.Authenticator ; 21 import java.net.PasswordAuthentication ; 22 import java.util.Properties ; 23 import org.apache.tools.ant.BuildException; 24 import org.apache.tools.ant.Project; 25 import org.apache.tools.ant.Task; 26 import org.apache.tools.ant.util.ProxySetup; 27 28 58 public class SetProxy extends Task { 59 60 64 protected String proxyHost = null; 65 66 69 protected int proxyPort = 80; 70 71 73 76 private String socksProxyHost = null; 77 78 81 private int socksProxyPort = 1080; 82 83 84 87 private String nonProxyHosts = null; 88 89 92 private String proxyUser = null; 93 94 97 private String proxyPassword = null; 98 99 105 public void setProxyHost(String hostname) { 106 proxyHost = hostname; 107 } 108 109 110 115 public void setProxyPort(int port) { 116 proxyPort = port; 117 } 118 119 125 public void setSocksProxyHost(String host) { 126 this.socksProxyHost = host; 127 } 128 129 130 135 public void setSocksProxyPort(int port) { 136 this.socksProxyPort = port; 137 } 138 139 140 147 public void setNonProxyHosts(String nonProxyHosts) { 148 this.nonProxyHosts = nonProxyHosts; 149 } 150 151 157 public void setProxyUser(String proxyUser) { 158 this.proxyUser = proxyUser; 159 } 160 161 166 public void setProxyPassword(String proxyPassword) { 167 this.proxyPassword = proxyPassword; 168 } 169 170 176 177 public void applyWebProxySettings() { 178 boolean settingsChanged = false; 179 boolean enablingProxy = false; 180 Properties sysprops = System.getProperties(); 181 if (proxyHost != null) { 182 settingsChanged = true; 183 if (proxyHost.length() != 0) { 184 traceSettingInfo(); 185 enablingProxy = true; 186 sysprops.put(ProxySetup.HTTP_PROXY_HOST, proxyHost); 187 String portString = Integer.toString(proxyPort); 188 sysprops.put(ProxySetup.HTTP_PROXY_PORT, portString); 189 sysprops.put(ProxySetup.HTTPS_PROXY_HOST, proxyHost); 190 sysprops.put(ProxySetup.HTTPS_PROXY_PORT, portString); 191 sysprops.put(ProxySetup.FTP_PROXY_HOST, proxyHost); 192 sysprops.put(ProxySetup.FTP_PROXY_PORT, portString); 193 if (nonProxyHosts != null) { 194 sysprops.put(ProxySetup.HTTP_NON_PROXY_HOSTS, nonProxyHosts); 195 sysprops.put(ProxySetup.HTTPS_NON_PROXY_HOSTS, nonProxyHosts); 196 sysprops.put(ProxySetup.FTP_NON_PROXY_HOSTS, nonProxyHosts); 197 } 198 if (proxyUser != null) { 199 sysprops.put(ProxySetup.HTTP_PROXY_USERNAME, proxyUser); 200 sysprops.put(ProxySetup.HTTP_PROXY_PASSWORD, proxyPassword); 201 } 202 } else { 203 log("resetting http proxy", Project.MSG_VERBOSE); 204 sysprops.remove(ProxySetup.HTTP_PROXY_HOST); 205 sysprops.remove(ProxySetup.HTTP_PROXY_PORT); 206 sysprops.remove(ProxySetup.HTTP_PROXY_USERNAME); 207 sysprops.remove(ProxySetup.HTTP_PROXY_PASSWORD); 208 sysprops.remove(ProxySetup.HTTPS_PROXY_HOST); 209 sysprops.remove(ProxySetup.HTTPS_PROXY_PORT); 210 sysprops.remove(ProxySetup.FTP_PROXY_HOST); 211 sysprops.remove(ProxySetup.FTP_PROXY_PORT); 212 } 213 } 214 215 if (socksProxyHost != null) { 217 settingsChanged = true; 218 if (socksProxyHost.length() != 0) { 219 enablingProxy = true; 220 sysprops.put(ProxySetup.SOCKS_PROXY_HOST, socksProxyHost); 221 sysprops.put(ProxySetup.SOCKS_PROXY_PORT, Integer.toString(socksProxyPort)); 222 if (proxyUser != null) { 223 sysprops.put(ProxySetup.SOCKS_PROXY_USERNAME, proxyUser); 225 sysprops.put(ProxySetup.SOCKS_PROXY_PASSWORD, proxyPassword); 226 } 227 228 } else { 229 log("resetting socks proxy", Project.MSG_VERBOSE); 230 sysprops.remove(ProxySetup.SOCKS_PROXY_HOST); 231 sysprops.remove(ProxySetup.SOCKS_PROXY_PORT); 232 sysprops.remove(ProxySetup.SOCKS_PROXY_USERNAME); 233 sysprops.remove(ProxySetup.SOCKS_PROXY_PASSWORD); 234 } 235 } 236 237 if (proxyUser != null) { 238 if (enablingProxy) { 239 Authenticator.setDefault(new ProxyAuth(proxyUser, 240 proxyPassword)); 241 } else if (settingsChanged) { 242 Authenticator.setDefault(new ProxyAuth("", "")); 243 } 244 } 245 } 246 247 250 private void traceSettingInfo() { 251 log("Setting proxy to " 252 + (proxyHost != null ? proxyHost : "''") 253 + ":" + proxyPort, 254 Project.MSG_VERBOSE); 255 } 256 257 262 public void execute() throws BuildException { 263 applyWebProxySettings(); 264 } 265 266 269 private static final class ProxyAuth extends Authenticator { 270 private PasswordAuthentication auth; 271 272 private ProxyAuth(String user, String pass) { 273 auth = new PasswordAuthentication (user, pass.toCharArray()); 274 } 275 276 protected PasswordAuthentication getPasswordAuthentication() { 277 return auth; 278 } 279 } 280 } 281 282 | Popular Tags |