source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/taskdefs/optional/net/SetProxy.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 10.1 KB
Line 
1/*
2 * Copyright 2000-2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17package org.apache.tools.ant.taskdefs.optional.net;
18
19import java.lang.reflect.InvocationTargetException;
20import java.lang.reflect.Method;
21import java.net.Authenticator;
22import java.net.PasswordAuthentication;
23import java.util.Properties;
24import org.apache.tools.ant.BuildException;
25import org.apache.tools.ant.Project;
26import org.apache.tools.ant.Task;
27import org.apache.tools.ant.util.JavaEnvUtils;
28
29/**
30 * Sets Java's web proxy properties, so that tasks and code run in
31 * the same JVM can have through-the-firewall access to remote web sites,
32 * and remote ftp sites.
33 * You can nominate an http and ftp proxy, or a socks server, reset the server
34 * settings, or do nothing at all.
35 * <p>
36 * Examples
37 * <pre>&lt;setproxy/&gt;</pre>
38 * do nothing
39 * <pre>&lt;setproxy proxyhost="firewall"/&gt;</pre>
40 * set the proxy to firewall:80
41 * <pre>&lt;setproxy proxyhost="firewall" proxyport="81"/&gt;</pre>
42 * set the proxy to firewall:81
43 * <pre>&lt;setproxy proxyhost=""/&gt;</pre>
44 * stop using the http proxy; don't change the socks settings
45 * <pre>&lt;setproxy socksproxyhost="socksy"/&gt;</pre>
46 * use socks via socksy:1080
47 * <pre>&lt;setproxy socksproxyhost=""/&gt;</pre>
48 * stop using the socks server.
49 * <p>
50 * You can set a username and password for http with the <tt>proxyHost</tt>
51 * and <tt>proxyPassword</tt> attributes. On Java1.4 and above these can also be
52 * used against SOCKS5 servers.
53 * </p>
54 * @see <a href="http://java.sun.com/j2se/1.4/docs/guide/net/properties.html">
55 * java 1.4 network property list</a>
56 *@since Ant 1.5
57 * @ant.task category="network"
58 */
59public class SetProxy extends Task {
60
61 /**
62 * proxy details
63 */
64 protected String proxyHost = null;
65
66 /**
67 * name of proxy port
68 */
69 protected int proxyPort = 80;
70
71 /**
72 * socks host.
73 */
74 private String socksProxyHost = null;
75
76 /**
77 * Socks proxy port. Default is 1080.
78 */
79 private int socksProxyPort = 1080;
80
81
82 /**
83 * list of non proxy hosts
84 */
85 private String nonProxyHosts = null;
86
87 /**
88 * user for http only
89 */
90 private String proxyUser = null;
91
92 /**
93 * password for http only
94 */
95 private String proxyPassword = null;
96
97 /**
98 * the HTTP/ftp proxy host. Set this to "" for the http proxy
99 * option to be disabled
100 *
101 * @param hostname the new proxy hostname
102 */
103 public void setProxyHost(String hostname) {
104 proxyHost = hostname;
105 }
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 /**
129 * Set the ProxyPort for socks connections. The default value is 1080
130 *
131 * @param port The new SocksProxyPort value
132 */
133 public void setSocksProxyPort(int port) {
134 this.socksProxyPort = port;
135 }
136
137
138 /**
139 * A list of hosts to bypass the proxy on. These should be separated
140 * with the vertical bar character '|'. Only in Java 1.4 does ftp use
141 * this list.
142 * e.g. fozbot.corp.sun.com|*.eng.sun.com
143 * @param nonProxyHosts lists of hosts to talk direct to
144 */
145 public void setNonProxyHosts(String nonProxyHosts) {
146 this.nonProxyHosts = nonProxyHosts;
147 }
148
149 /**
150 * set the proxy user. Probably requires a password to accompany this
151 * setting. Default=""
152 * @param proxyUser username
153 * @since Ant1.6
154 */
155 public void setProxyUser(String proxyUser) {
156 this.proxyUser = proxyUser;
157 }
158
159 /**
160 * Set the password for the proxy. Used only if the proxyUser is set.
161 * @param proxyPassword password to go with the username
162 * @since Ant1.6
163 */
164 public void setProxyPassword(String proxyPassword) {
165 this.proxyPassword = proxyPassword;
166 }
167
168 /**
169 * if the proxy port and host settings are not null, then the settings
170 * get applied these settings last beyond the life of the object and
171 * apply to all network connections
172 * Relevant docs: buglist #4183340
173 */
174
175 public void applyWebProxySettings() {
176 boolean settingsChanged = false;
177 boolean enablingProxy = false;
178 Properties sysprops = System.getProperties();
179 if (proxyHost != null) {
180 settingsChanged = true;
181 if (proxyHost.length() != 0) {
182 traceSettingInfo();
183 enablingProxy = true;
184 sysprops.put("http.proxyHost", proxyHost);
185 String portString = Integer.toString(proxyPort);
186 sysprops.put("http.proxyPort", portString);
187 sysprops.put("https.proxyHost", proxyHost);
188 sysprops.put("https.proxyPort", portString);
189 sysprops.put("ftp.proxyHost", proxyHost);
190 sysprops.put("ftp.proxyPort", portString);
191 if (nonProxyHosts != null) {
192 sysprops.put("http.nonProxyHosts", nonProxyHosts);
193 sysprops.put("https.nonProxyHosts", nonProxyHosts);
194 sysprops.put("ftp.nonProxyHosts", nonProxyHosts);
195 }
196 if (proxyUser != null) {
197 sysprops.put("http.proxyUser", proxyUser);
198 sysprops.put("http.proxyPassword", proxyPassword);
199 }
200 } else {
201 log("resetting http proxy", Project.MSG_VERBOSE);
202 sysprops.remove("http.proxyHost");
203 sysprops.remove("http.proxyPort");
204 sysprops.remove("http.proxyUser");
205 sysprops.remove("http.proxyPassword");
206 sysprops.remove("https.proxyHost");
207 sysprops.remove("https.proxyPort");
208 sysprops.remove("ftp.proxyHost");
209 sysprops.remove("ftp.proxyPort");
210 }
211 }
212
213 //socks
214 if (socksProxyHost != null) {
215 settingsChanged = true;
216 if (socksProxyHost.length() != 0) {
217 enablingProxy = true;
218 sysprops.put("socksProxyHost", socksProxyHost);
219 sysprops.put("socksProxyPort", Integer.toString(socksProxyPort));
220 if (proxyUser != null) {
221 //this may be a java1.4 thingy only
222 sysprops.put("java.net.socks.username", proxyUser);
223 sysprops.put("java.net.socks.password", proxyPassword);
224 }
225
226 } else {
227 log("resetting socks proxy", Project.MSG_VERBOSE);
228 sysprops.remove("socksProxyHost");
229 sysprops.remove("socksProxyPort");
230 sysprops.remove("java.net.socks.username");
231 sysprops.remove("java.net.socks.password");
232 }
233 }
234
235
236 //for Java1.1 we need to tell the system that the settings are new
237 if (settingsChanged
238 && JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_1)) {
239 legacyResetProxySettingsCall(enablingProxy);
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,
259 Project.MSG_VERBOSE);
260 }
261
262
263 /**
264 * make a call to sun.net.www.http.HttpClient.resetProperties();
265 * this is only needed for java 1.1; reflection is used to stop the compiler
266 * whining, and in case cleanroom JVMs dont have the class.
267 * @return true if we did something
268 */
269
270 protected boolean legacyResetProxySettingsCall(boolean setProxy) {
271 System.getProperties().put("http.proxySet", new Boolean(setProxy).toString());
272 try {
273 Class c = Class.forName("sun.net.www.http.HttpClient");
274 Method reset = c.getMethod("resetProperties", null);
275 reset.invoke(null, null);
276 return true;
277 } catch (ClassNotFoundException cnfe) {
278 return false;
279 } catch (NoSuchMethodException e) {
280 return false;
281 } catch (IllegalAccessException e) {
282 return false;
283 } catch (InvocationTargetException e) {
284 return false;
285 }
286 }
287
288
289 /**
290 * Does the work.
291 *
292 * @exception BuildException thrown in unrecoverable error.
293 */
294 public void execute() throws BuildException {
295 applyWebProxySettings();
296 }
297
298 /**
299 * @since 1.6.3
300 */
301 private static final class ProxyAuth extends Authenticator {
302 private PasswordAuthentication auth;
303
304 private ProxyAuth(String user, String pass) {
305 auth = new PasswordAuthentication(user, pass.toCharArray());
306 }
307
308 protected PasswordAuthentication getPasswordAuthentication() {
309 return auth;
310 }
311 }
312}
313
Note: See TracBrowser for help on using the repository browser.