JAL-3633 Remember system set proxy properties. Add options in Preferences for No...
[jalview.git] / src / jalview / bin / Cache.java
index 40fccb0..621b74b 100755 (executable)
@@ -271,6 +271,13 @@ public class Cache
    */
   public static Logger log;
 
+  // save the proxy properties set at startup
+  public final static String[] startupProxyProperties = {
+      System.getProperty("http.proxyHost"),
+      System.getProperty("http.proxyPort"),
+      System.getProperty("https.proxyHost"),
+      System.getProperty("https.proxyPort") };
+
   /** Jalview Properties */
   public static Properties applicationProperties = new Properties()
   {
@@ -394,16 +401,45 @@ public class Cache
         System.out.println("Error reading properties file: " + ex);
       }
     }
+
+    /* TO BE REPLACED WITH PROXY_TYPE SETTINGS 
     if (getDefault("USE_PROXY", false))
     {
       String proxyServer = getDefault("PROXY_SERVER", ""),
               proxyPort = getDefault("PROXY_PORT", "8080");
+    }
+    */
 
-      System.out.println("Using proxyServer: " + proxyServer
-              + " proxyPort: " + proxyPort);
-
-      System.setProperty("http.proxyHost", proxyServer);
-      System.setProperty("http.proxyPort", proxyPort);
+    // PROXY TYPE settings (now three options "none", "false", "true", but using
+    // backward compatible strings)
+    String proxyType = getDefault("USE_PROXY", "false");
+    // default to upgrading old settings
+    switch (proxyType)
+    {
+    case "none":
+      setProxyProperties(null, null, null, null);
+      break;
+    case "false": // use system settings
+      resetProxyProperties();
+      break;
+    case "true": // use specified proxy settings
+      String httpHost = getDefault("PROXY_SERVER", "");
+      String httpPort = getDefault("PROXY_PORT", "8080");
+      String httpsHost = getDefault("PROXY_SERVER_HTTPS", httpHost);
+      String httpsPort = getDefault("PROXY_PORT_HTTPS", httpPort);
+      setProxyProperties(httpHost, httpPort, httpsHost, httpsPort);
+      break;
+    default:
+      String message = "Incorrect PROXY_TYPE - should be 'none' (clear proxy properties), 'false' (system settings), 'true' (custom settings): "
+              + proxyType;
+      if (Cache.log == null)
+      {
+        System.out.println(message);
+      }
+      else
+      {
+        Cache.log.warn(message);
+      }
     }
 
     // LOAD THE AUTHORS FROM THE authors.props file
@@ -1215,4 +1251,77 @@ public class Cache
     t.printStackTrace(pw);
     return sw.toString();
   }
+
+  // proxy properties methods
+  public static void resetProxyProperties()
+  {
+    setProxyProperties(startupProxyProperties[0], startupProxyProperties[1],
+            startupProxyProperties[2], startupProxyProperties[3]);
+    StringBuilder sb = new StringBuilder();
+    sb.append("Setting proxy properties to: http.proxyHost=")
+            .append(startupProxyProperties[0]).append(", http.proxyPort=")
+            .append(startupProxyProperties[1]).append(", https.proxyHost=")
+            .append(startupProxyProperties[2]).append(", https.proxyPort=")
+            .append(startupProxyProperties[3]);
+    if (Cache.log == null)
+    {
+      System.err.println(sb.toString());
+    }
+    else
+    {
+      Cache.log.debug(sb.toString());
+    }
+  }
+
+  public static void setProxyProperties(String host, String port)
+  {
+    setProxyProperties(host, port, host, port);
+  }
+
+  public static void setProxyProperties(String httpHost, String httpPort,
+          String httpsHost, String httpsPort)
+  {
+    // cannot set property to null -- use clearProperty instead
+
+    // http.proxyHost
+    if (httpHost == null)
+    {
+      System.clearProperty("http.proxyHost");
+    }
+    else
+    {
+      System.setProperty("http.proxyHost", httpHost);
+    }
+
+    // http.proxyPort
+    if (httpPort == null)
+    {
+      System.clearProperty("http.proxyPort");
+    }
+    else
+    {
+      System.setProperty("http.proxyPort", httpPort);
+    }
+
+    // https.proxyHost
+    if (httpsHost == null)
+    {
+      System.clearProperty("https.proxyHost");
+    }
+    else
+    {
+      System.setProperty("https.proxyHost", httpsHost);
+    }
+
+    // https.proxyPort
+    if (httpsPort == null)
+    {
+      System.clearProperty("https.proxyPort");
+    }
+    else
+    {
+      System.setProperty("https.proxyPort", httpsPort);
+    }
+
+  }
 }