KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > net > SetProxy


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.taskdefs.optional.net;
19
20 import java.net.Authenticator JavaDoc;
21 import java.net.PasswordAuthentication JavaDoc;
22 import java.util.Properties JavaDoc;
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 /**
29  * Sets Java's web proxy properties, so that tasks and code run in
30  * the same JVM can have through-the-firewall access to remote web sites,
31  * and remote ftp sites.
32  * You can nominate an http and ftp proxy, or a socks server, reset the server
33  * settings, or do nothing at all.
34  * <p>
35  * Examples
36  * <pre>&lt;setproxy/&gt;</pre>
37  * do nothing
38  * <pre>&lt;setproxy proxyhost="firewall"/&gt;</pre>
39  * set the proxy to firewall:80
40  * <pre>&lt;setproxy proxyhost="firewall" proxyport="81"/&gt;</pre>
41  * set the proxy to firewall:81
42  * <pre>&lt;setproxy proxyhost=""/&gt;</pre>
43  * stop using the http proxy; don't change the socks settings
44  * <pre>&lt;setproxy socksproxyhost="socksy"/&gt;</pre>
45  * use socks via socksy:1080
46  * <pre>&lt;setproxy socksproxyhost=""/&gt;</pre>
47  * stop using the socks server.
48  * <p>
49  * You can set a username and password for http with the <tt>proxyHost</tt>
50  * and <tt>proxyPassword</tt> attributes. On Java1.4 and above these can also be
51  * used against SOCKS5 servers.
52  * </p>
53  * @see <a HREF="http://java.sun.com/j2se/1.5.0/docs/guide/net/properties.html">
54  * java 1.5 network property list</a>
55   *@since Ant 1.5
56  * @ant.task category="network"
57  */

58 public class SetProxy extends Task {
59
60     // CheckStyle:VisibilityModifier OFF - bc
61
/**
62      * proxy details
63      */

64     protected String JavaDoc proxyHost = null;
65
66     /**
67      * name of proxy port
68      */

69     protected int proxyPort = 80;
70
71     // CheckStyle:VisibilityModifier ON
72

73     /**
74      * socks host.
75      */

76     private String JavaDoc socksProxyHost = null;
77
78     /**
79      * Socks proxy port. Default is 1080.
80      */

81     private int socksProxyPort = 1080;
82
83
84     /**
85      * list of non proxy hosts
86      */

87     private String JavaDoc nonProxyHosts = null;
88
89     /**
90      * user for http only
91      */

92     private String JavaDoc proxyUser = null;
93
94     /**
95      * password for http only
96      */

97     private String JavaDoc proxyPassword = null;
98
99     /**
100      * the HTTP/ftp proxy host. Set this to "" for the http proxy
101      * option to be disabled
102      *
103      * @param hostname the new proxy hostname
104      */

105     public void setProxyHost(String JavaDoc hostname) {
106         proxyHost = hostname;
107     }
108
109
110     /**
111      * the HTTP/ftp proxy port number; default is 80
112      *
113      * @param port port number of the proxy
114      */

115     public void setProxyPort(int port) {
116         proxyPort = port;
117     }
118
119     /**
120      * The name of a Socks server. Set to "" to turn socks
121      * proxying off.
122      *
123      * @param host The new SocksProxyHost value
124      */

125     public void setSocksProxyHost(String JavaDoc host) {
126         this.socksProxyHost = host;
127     }
128
129
130     /**
131      * Set the ProxyPort for socks connections. The default value is 1080
132      *
133      * @param port The new SocksProxyPort value
134      */

135     public void setSocksProxyPort(int port) {
136         this.socksProxyPort = port;
137     }
138
139
140     /**
141      * A list of hosts to bypass the proxy on. These should be separated
142      * with the vertical bar character '|'. Only in Java 1.4 does ftp use
143      * this list.
144      * e.g. fozbot.corp.sun.com|*.eng.sun.com
145      * @param nonProxyHosts lists of hosts to talk direct to
146      */

147     public void setNonProxyHosts(String JavaDoc nonProxyHosts) {
148         this.nonProxyHosts = nonProxyHosts;
149     }
150
151     /**
152      * set the proxy user. Probably requires a password to accompany this
153      * setting. Default=""
154      * @param proxyUser username
155      * @since Ant1.6
156      */

157     public void setProxyUser(String JavaDoc proxyUser) {
158         this.proxyUser = proxyUser;
159     }
160
161     /**
162      * Set the password for the proxy. Used only if the proxyUser is set.
163      * @param proxyPassword password to go with the username
164      * @since Ant1.6
165      */

166     public void setProxyPassword(String JavaDoc proxyPassword) {
167         this.proxyPassword = proxyPassword;
168     }
169
170     /**
171      * if the proxy port and host settings are not null, then the settings
172      * get applied these settings last beyond the life of the object and
173      * apply to all network connections
174      * Relevant docs: buglist #4183340
175      */

176
177     public void applyWebProxySettings() {
178         boolean settingsChanged = false;
179         boolean enablingProxy = false;
180         Properties JavaDoc 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 JavaDoc 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         //socks
216
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                     //this may be a java1.4 thingy only
224
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     /**
248      * list out what is going on
249      */

250     private void traceSettingInfo() {
251         log("Setting proxy to "
252                 + (proxyHost != null ? proxyHost : "''")
253                 + ":" + proxyPort,
254                 Project.MSG_VERBOSE);
255     }
256
257     /**
258      * Does the work.
259      *
260      * @exception BuildException thrown in unrecoverable error.
261      */

262     public void execute() throws BuildException {
263         applyWebProxySettings();
264     }
265
266     /**
267      * @since 1.6.3
268      */

269     private static final class ProxyAuth extends Authenticator JavaDoc {
270         private PasswordAuthentication JavaDoc auth;
271
272         private ProxyAuth(String JavaDoc user, String JavaDoc pass) {
273             auth = new PasswordAuthentication JavaDoc(user, pass.toCharArray());
274         }
275
276         protected PasswordAuthentication JavaDoc getPasswordAuthentication() {
277             return auth;
278         }
279     }
280 }
281
282
Popular Tags