001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.taskdefs.optional.net;
019:
020: import java.net.Authenticator;
021: import java.net.PasswordAuthentication;
022: import java.util.Properties;
023: import org.apache.tools.ant.BuildException;
024: import org.apache.tools.ant.Project;
025: import org.apache.tools.ant.Task;
026: import org.apache.tools.ant.util.ProxySetup;
027:
028: /**
029: * Sets Java's web proxy properties, so that tasks and code run in
030: * the same JVM can have through-the-firewall access to remote web sites,
031: * and remote ftp sites.
032: * You can nominate an http and ftp proxy, or a socks server, reset the server
033: * settings, or do nothing at all.
034: * <p>
035: * Examples
036: * <pre><setproxy/></pre>
037: * do nothing
038: * <pre><setproxy proxyhost="firewall"/></pre>
039: * set the proxy to firewall:80
040: * <pre><setproxy proxyhost="firewall" proxyport="81"/></pre>
041: * set the proxy to firewall:81
042: * <pre><setproxy proxyhost=""/></pre>
043: * stop using the http proxy; don't change the socks settings
044: * <pre><setproxy socksproxyhost="socksy"/></pre>
045: * use socks via socksy:1080
046: * <pre><setproxy socksproxyhost=""/></pre>
047: * stop using the socks server.
048: * <p>
049: * You can set a username and password for http with the <tt>proxyHost</tt>
050: * and <tt>proxyPassword</tt> attributes. On Java1.4 and above these can also be
051: * used against SOCKS5 servers.
052: * </p>
053: * @see <a href="http://java.sun.com/j2se/1.5.0/docs/guide/net/properties.html">
054: * java 1.5 network property list</a>
055: *@since Ant 1.5
056: * @ant.task category="network"
057: */
058: public class SetProxy extends Task {
059:
060: // CheckStyle:VisibilityModifier OFF - bc
061: /**
062: * proxy details
063: */
064: protected String proxyHost = null;
065:
066: /**
067: * name of proxy port
068: */
069: protected int proxyPort = 80;
070:
071: // CheckStyle:VisibilityModifier ON
072:
073: /**
074: * socks host.
075: */
076: private String socksProxyHost = null;
077:
078: /**
079: * Socks proxy port. Default is 1080.
080: */
081: private int socksProxyPort = 1080;
082:
083: /**
084: * list of non proxy hosts
085: */
086: private String nonProxyHosts = null;
087:
088: /**
089: * user for http only
090: */
091: private String proxyUser = null;
092:
093: /**
094: * password for http only
095: */
096: private String proxyPassword = null;
097:
098: /**
099: * the HTTP/ftp proxy host. Set this to "" for the http proxy
100: * option to be disabled
101: *
102: * @param hostname the new proxy hostname
103: */
104: public void setProxyHost(String hostname) {
105: proxyHost = hostname;
106: }
107:
108: /**
109: * the HTTP/ftp proxy port number; default is 80
110: *
111: * @param port port number of the proxy
112: */
113: public void setProxyPort(int port) {
114: proxyPort = port;
115: }
116:
117: /**
118: * The name of a Socks server. Set to "" to turn socks
119: * proxying off.
120: *
121: * @param host The new SocksProxyHost value
122: */
123: public void setSocksProxyHost(String host) {
124: this .socksProxyHost = host;
125: }
126:
127: /**
128: * Set the ProxyPort for socks connections. The default value is 1080
129: *
130: * @param port The new SocksProxyPort value
131: */
132: public void setSocksProxyPort(int port) {
133: this .socksProxyPort = port;
134: }
135:
136: /**
137: * A list of hosts to bypass the proxy on. These should be separated
138: * with the vertical bar character '|'. Only in Java 1.4 does ftp use
139: * this list.
140: * e.g. fozbot.corp.sun.com|*.eng.sun.com
141: * @param nonProxyHosts lists of hosts to talk direct to
142: */
143: public void setNonProxyHosts(String nonProxyHosts) {
144: this .nonProxyHosts = nonProxyHosts;
145: }
146:
147: /**
148: * set the proxy user. Probably requires a password to accompany this
149: * setting. Default=""
150: * @param proxyUser username
151: * @since Ant1.6
152: */
153: public void setProxyUser(String proxyUser) {
154: this .proxyUser = proxyUser;
155: }
156:
157: /**
158: * Set the password for the proxy. Used only if the proxyUser is set.
159: * @param proxyPassword password to go with the username
160: * @since Ant1.6
161: */
162: public void setProxyPassword(String proxyPassword) {
163: this .proxyPassword = proxyPassword;
164: }
165:
166: /**
167: * if the proxy port and host settings are not null, then the settings
168: * get applied these settings last beyond the life of the object and
169: * apply to all network connections
170: * Relevant docs: buglist #4183340
171: */
172:
173: public void applyWebProxySettings() {
174: boolean settingsChanged = false;
175: boolean enablingProxy = false;
176: Properties sysprops = System.getProperties();
177: if (proxyHost != null) {
178: settingsChanged = true;
179: if (proxyHost.length() != 0) {
180: traceSettingInfo();
181: enablingProxy = true;
182: sysprops.put(ProxySetup.HTTP_PROXY_HOST, proxyHost);
183: String portString = Integer.toString(proxyPort);
184: sysprops.put(ProxySetup.HTTP_PROXY_PORT, portString);
185: sysprops.put(ProxySetup.HTTPS_PROXY_HOST, proxyHost);
186: sysprops.put(ProxySetup.HTTPS_PROXY_PORT, portString);
187: sysprops.put(ProxySetup.FTP_PROXY_HOST, proxyHost);
188: sysprops.put(ProxySetup.FTP_PROXY_PORT, portString);
189: if (nonProxyHosts != null) {
190: sysprops.put(ProxySetup.HTTP_NON_PROXY_HOSTS,
191: nonProxyHosts);
192: sysprops.put(ProxySetup.HTTPS_NON_PROXY_HOSTS,
193: nonProxyHosts);
194: sysprops.put(ProxySetup.FTP_NON_PROXY_HOSTS,
195: nonProxyHosts);
196: }
197: if (proxyUser != null) {
198: sysprops.put(ProxySetup.HTTP_PROXY_USERNAME,
199: proxyUser);
200: sysprops.put(ProxySetup.HTTP_PROXY_PASSWORD,
201: proxyPassword);
202: }
203: } else {
204: log("resetting http proxy", Project.MSG_VERBOSE);
205: sysprops.remove(ProxySetup.HTTP_PROXY_HOST);
206: sysprops.remove(ProxySetup.HTTP_PROXY_PORT);
207: sysprops.remove(ProxySetup.HTTP_PROXY_USERNAME);
208: sysprops.remove(ProxySetup.HTTP_PROXY_PASSWORD);
209: sysprops.remove(ProxySetup.HTTPS_PROXY_HOST);
210: sysprops.remove(ProxySetup.HTTPS_PROXY_PORT);
211: sysprops.remove(ProxySetup.FTP_PROXY_HOST);
212: sysprops.remove(ProxySetup.FTP_PROXY_PORT);
213: }
214: }
215:
216: //socks
217: if (socksProxyHost != null) {
218: settingsChanged = true;
219: if (socksProxyHost.length() != 0) {
220: enablingProxy = true;
221: sysprops.put(ProxySetup.SOCKS_PROXY_HOST,
222: socksProxyHost);
223: sysprops.put(ProxySetup.SOCKS_PROXY_PORT, Integer
224: .toString(socksProxyPort));
225: if (proxyUser != null) {
226: //this may be a java1.4 thingy only
227: sysprops.put(ProxySetup.SOCKS_PROXY_USERNAME,
228: proxyUser);
229: sysprops.put(ProxySetup.SOCKS_PROXY_PASSWORD,
230: proxyPassword);
231: }
232:
233: } else {
234: log("resetting socks proxy", Project.MSG_VERBOSE);
235: sysprops.remove(ProxySetup.SOCKS_PROXY_HOST);
236: sysprops.remove(ProxySetup.SOCKS_PROXY_PORT);
237: sysprops.remove(ProxySetup.SOCKS_PROXY_USERNAME);
238: sysprops.remove(ProxySetup.SOCKS_PROXY_PASSWORD);
239: }
240: }
241:
242: if (proxyUser != null) {
243: if (enablingProxy) {
244: Authenticator.setDefault(new ProxyAuth(proxyUser,
245: proxyPassword));
246: } else if (settingsChanged) {
247: Authenticator.setDefault(new ProxyAuth("", ""));
248: }
249: }
250: }
251:
252: /**
253: * list out what is going on
254: */
255: private void traceSettingInfo() {
256: log("Setting proxy to "
257: + (proxyHost != null ? proxyHost : "''") + ":"
258: + proxyPort, Project.MSG_VERBOSE);
259: }
260:
261: /**
262: * Does the work.
263: *
264: * @exception BuildException thrown in unrecoverable error.
265: */
266: public void execute() throws BuildException {
267: applyWebProxySettings();
268: }
269:
270: /**
271: * @since 1.6.3
272: */
273: private static final class ProxyAuth extends Authenticator {
274: private PasswordAuthentication auth;
275:
276: private ProxyAuth(String user, String pass) {
277: auth = new PasswordAuthentication(user, pass.toCharArray());
278: }
279:
280: protected PasswordAuthentication getPasswordAuthentication() {
281: return auth;
282: }
283: }
284: }
|