BZ-19516 Use BufferedInputStream for reduced memory usage in Zip task, in certain...
[ant.git] / src / main / org / apache / tools / ant / util / ProxySetup.java
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.util;
19
20 import org.apache.tools.ant.Project;
21
22 /**
23 * Code to do proxy setup. This is just factored out of the main system just to
24 * keep everything else less convoluted.
25 * @since Ant1.7
26 */
27
28 public class ProxySetup {
29
30 /**
31 * owner project; used for logging and extracting properties
32 */
33 private Project owner;
34
35 /**
36 * Java1.5 property that enables use of system proxies.
37 */
38 public static final String USE_SYSTEM_PROXIES = "java.net.useSystemProxies";
39 /** the http proxyhost property */
40 public static final String HTTP_PROXY_HOST = "http.proxyHost";
41 /** the http proxyport property */
42 public static final String HTTP_PROXY_PORT = "http.proxyPort";
43 /** the https proxyhost property */
44 public static final String HTTPS_PROXY_HOST = "https.proxyHost";
45 /** the https proxyport property */
46 public static final String HTTPS_PROXY_PORT = "https.proxyPort";
47 /** the ftp proxyhost property */
48 public static final String FTP_PROXY_HOST = "ftp.proxyHost";
49 /** the ftp proxyport property */
50 public static final String FTP_PROXY_PORT = "ftp.proxyPort";
51 /** the ftp proxyport property */
52 public static final String HTTP_NON_PROXY_HOSTS = "http.nonProxyHosts";
53 /** the http hosts not to be proxied property */
54 public static final String HTTPS_NON_PROXY_HOSTS = "https.nonProxyHosts";
55 /** the ftp hosts not to be proxied property */
56 public static final String FTP_NON_PROXY_HOSTS = "ftp.nonProxyHosts";
57 /** the http proxy username property */
58 public static final String HTTP_PROXY_USERNAME = "http.proxyUser";
59 /** the http proxy password property */
60 public static final String HTTP_PROXY_PASSWORD = "http.proxyPassword"; //NOSONAR
61 /** the socks proxy host property */
62 public static final String SOCKS_PROXY_HOST = "socksProxyHost";
63 /** the socks proxy port property */
64 public static final String SOCKS_PROXY_PORT = "socksProxyPort";
65 /** the socks proxy username property */
66 public static final String SOCKS_PROXY_USERNAME = "java.net.socks.username";
67 /** the socks proxy password property */
68 public static final String SOCKS_PROXY_PASSWORD = "java.net.socks.password"; //NOSONAR
69
70 /**
71 * create a proxy setup class bound to this project
72 * @param owner the project that owns this setup.
73 */
74 public ProxySetup(Project owner) {
75 this.owner = owner;
76 }
77
78 /**
79 * Get the current system property settings
80 * @return current value; null for none or no access
81 */
82 public static String getSystemProxySetting() {
83 try {
84 return System.getProperty(USE_SYSTEM_PROXIES);
85 } catch (SecurityException e) {
86 //if you cannot read it, you won't be able to write it either
87 return null;
88 }
89 }
90
91 /**
92 * turn proxies on;
93 * if the proxy key is already set to some value: leave alone.
94 * if an ant property of the value {@link #USE_SYSTEM_PROXIES}
95 * is set, use that instead. Else set to "true".
96 */
97 public void enableProxies() {
98 if (getSystemProxySetting() == null) {
99 String proxies = owner.getProperty(USE_SYSTEM_PROXIES);
100 if (proxies == null || Project.toBoolean(proxies)) {
101 proxies = "true";
102 }
103 String message = "setting " + USE_SYSTEM_PROXIES + " to " + proxies;
104 try {
105 owner.log(message, Project.MSG_DEBUG);
106 System.setProperty(USE_SYSTEM_PROXIES, proxies);
107 } catch (SecurityException e) {
108 //log security exceptions and continue; it aint that
109 //important and may be quite common running Ant embedded.
110 owner.log("Security Exception when " + message);
111 }
112 }
113 }
114
115 }