changeset 17385:07462ba5a12a

mxtools: automatically set system property https proxy Contributed-by: Laurent Daynes <Laurent.Daynes@oracle.com>
author Paul Woegerer <paul.woegerer@oracle.com>
date Thu, 09 Oct 2014 12:10:00 +0200
parents 26c88ce948c5
children 0e120f2819ce
files mxtool/URLConnectionDownload.java
diffstat 1 files changed, 56 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/mxtool/URLConnectionDownload.java	Thu Oct 09 09:48:16 2014 +0200
+++ b/mxtool/URLConnectionDownload.java	Thu Oct 09 12:10:00 2014 +0200
@@ -32,15 +32,51 @@
  * @param urls the URLs to try, stopping after the first successful one
  */
 public class URLConnectionDownload {
+    /**
+     * Iterate over list of environment variable to find one that correctly specify an proxy.
+     *
+     * @param propPrefix indicates which proxy property to set (i.e., http or https)
+     * @param proxyEnvVariableNames list of environment variable
+     * @return a string specifying the proxy url
+     */
+    private static String setProxy(String[] proxyEnvVariableNames, String propPrefix) {
+        String proxy = null;
+        String proxyEnvVar = "";
+        for (String envvar : proxyEnvVariableNames) {
+            proxy = System.getenv(envvar);
+            if (proxy != null) {
+                proxyEnvVar = envvar;
+                break;
+            }
+        }
+        if (proxy != null) {
+            Pattern p = Pattern.compile("(?:http://)?([^:]+)(:\\d+)?");
+            Matcher m = p.matcher(proxy);
+            if (m.matches()) {
+                String host = m.group(1);
+                String port = m.group(2);
+                System.setProperty(propPrefix + ".proxyHost", host);
+                if (port != null) {
+                    port = port.substring(1); // strip ':'
+                    System.setProperty(propPrefix + ".proxyPort", port);
+                }
+                return proxy;
+            } else {
+                System.err.println("Value of " + proxyEnvVar + " is not valid:  " + proxy);
+            }
+        } else {
+            System.err.println("** If behind a firewall without direct internet access, use the " + proxyEnvVariableNames[0] + "  environment variable (e.g. 'env " + proxyEnvVariableNames[0] +
+                            "=proxy.company.com:80 max ...') or download manually with a web browser.");
+        }
+        return "";
+    }
 
-	/**
-	 * Downloads content from a given URL to a given file.
-	 * 
-	 * @param args
-	 *            arg[0] is the path where to write the content. The remainder
-	 *            of args are the URLs to try, stopping after the first
-	 *            successful one
-	 */
+    /**
+     * Downloads content from a given URL to a given file.
+     *
+     * @param args arg[0] is the path where to write the content. The remainder of args are the URLs
+     *            to try, stopping after the first successful one
+     */
     public static void main(String[] args) {
         File path = new File(args[0]);
         boolean verbose = args[1].equals("-v");
@@ -50,33 +86,21 @@
 
         File parent = path.getParentFile();
         makeDirectory(parent);
-        
+
         // Enable use of system proxies
         System.setProperty("java.net.useSystemProxies", "true");
 
-        String proxy = System.getenv("HTTP_PROXY");
-        if (proxy == null) {
-            proxy = System.getenv("http_proxy");
-        }
-
+        // Set standard proxy if any
+        String proxy = setProxy(new String[]{"HTTP_PROXY", "http_proxy"}, "http");
+        // Set proxy for secure http if explicitely set, default to http proxy otherwise
+        String secureProxy = setProxy(new String[]{"HTTPS_PROXY", "https_proxy", "HTTP_PROXY", "http_proxy"}, "https");
         String proxyMsg = "";
-        if (proxy != null) {
-            Pattern p = Pattern.compile("(?:http://)?([^:]+)(:\\d+)?");
-            Matcher m = p.matcher(proxy);
-            if (m.matches()) {
-                String host = m.group(1);
-                String port = m.group(2);
-                System.setProperty("http.proxyHost", host);
-                if (port != null) {
-                    port = port.substring(1); // strip ':'
-                    System.setProperty("http.proxyPort", port);
-                }
-                proxyMsg = " via proxy  " + proxy;
-            } else {
-            	System.err.println("Value of HTTP_PROXY is not valid:  " + proxy);
-            }
-        } else {
-        	System.err.println("** If behind a firewall without direct internet access, use the HTTP_PROXY environment variable (e.g. 'env HTTP_PROXY=proxy.company.com:80 max ...') or download manually with a web browser.");
+        if (secureProxy.length() > 0 && proxy.length() > 0 && !secureProxy.equals(proxy)) {
+            proxyMsg = " via " + proxy + " / " + secureProxy;
+        } else if (proxy.length() > 0) {
+            proxyMsg = " via " + proxy;
+        } else if (secureProxy.length() > 0) {
+            proxyMsg = " via " + secureProxy;
         }
 
         for (String s : urls) {
@@ -91,7 +115,7 @@
                     if (conn instanceof HttpURLConnection) {
                         // HttpURLConnection per default follows redirections,
                         // but not if it changes the protocol (e.g. http ->
-                        // https).  While this is a sane default, in our
+                        // https). While this is a sane default, in our
                         // situation it's okay to follow a protocol transition.
                         HttpURLConnection httpconn = (HttpURLConnection) conn;
                         switch (httpconn.getResponseCode()) {
@@ -137,4 +161,3 @@
         }
     }
 }
-