JAL-3633 Set proxy preferences before web calls in jalview.bin.Jalview. Fixed call...
[jalview.git] / src / jalview / bin / Cache.java
index 621b74b..8269f32 100755 (executable)
@@ -29,6 +29,8 @@ import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.PrintWriter;
 import java.io.StringWriter;
+import java.net.Authenticator;
+import java.net.PasswordAuthentication;
 import java.net.URL;
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
@@ -50,13 +52,16 @@ import org.apache.log4j.Logger;
 import org.apache.log4j.SimpleLayout;
 
 import jalview.datamodel.PDBEntry;
+import jalview.gui.Preferences;
 import jalview.gui.UserDefinedColours;
+import jalview.jbgui.GPreferences;
 import jalview.schemes.ColourSchemeLoader;
 import jalview.schemes.ColourSchemes;
 import jalview.schemes.UserColourScheme;
 import jalview.structure.StructureImportSettings;
 import jalview.urls.IdOrgSettings;
 import jalview.util.ColorUtils;
+import jalview.util.MessageManager;
 import jalview.util.Platform;
 import jalview.ws.sifts.SiftsSettings;
 
@@ -276,7 +281,22 @@ public class Cache
       System.getProperty("http.proxyHost"),
       System.getProperty("http.proxyPort"),
       System.getProperty("https.proxyHost"),
-      System.getProperty("https.proxyPort") };
+      System.getProperty("https.proxyPort"),
+      System.getProperty("http.proxyUser"),
+      System.getProperty("http.proxyPassword"),
+      System.getProperty("https.proxyUser"),
+      System.getProperty("https.proxyPassword"),
+      System.getProperty("http.nonProxyHosts") };
+
+  public final static String PROXYTYPE_NONE = "none";
+
+  // "false" and "true" for backward compatibility
+  public final static String PROXYTYPE_SYSTEM = "false";
+
+  public final static String PROXYTYPE_CUSTOM = "true";
+
+  // in-memory only storage of proxy password, safer to use char array
+  public static char[] proxyAuthPassword = null;
 
   /** Jalview Properties */
   public static Properties applicationProperties = new Properties()
@@ -412,34 +432,32 @@ public class Cache
 
     // PROXY TYPE settings (now three options "none", "false", "true", but using
     // backward compatible strings)
-    String proxyType = getDefault("USE_PROXY", "false");
+    String proxyType = getDefault("USE_PROXY", PROXYTYPE_SYSTEM);
     // default to upgrading old settings
     switch (proxyType)
     {
-    case "none":
-      setProxyProperties(null, null, null, null);
+    case PROXYTYPE_NONE:
+      clearProxyProperties();
       break;
-    case "false": // use system settings
+    case PROXYTYPE_SYSTEM: // use system settings
       resetProxyProperties();
       break;
-    case "true": // use specified proxy settings
+    case PROXYTYPE_CUSTOM: // 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);
+      String httpUser = getDefault("PROXY_AUTH_USER", null);
+      // https.proxyUser and https.proxyPassword are not able to be
+      // independently set in Preferences yet (or http.nonProxyHosts)
+      String httpsUser = getDefault("PROXY_AUTH_USER_HTTPS", httpUser);
+      setProxyProperties(httpHost, httpPort, httpsHost, httpsPort, httpUser,
+              proxyAuthPassword, httpsUser, proxyAuthPassword, "localhost");
       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);
-      }
+      Cache.warn(message);
     }
 
     // LOAD THE AUTHORS FROM THE authors.props file
@@ -1253,75 +1271,293 @@ public class Cache
   }
 
   // proxy properties methods
+  public static void clearProxyProperties()
+  {
+    setProxyProperties(null, null, null, null, null, null, null, null,
+            null);
+  }
+
   public static void resetProxyProperties()
   {
     setProxyProperties(startupProxyProperties[0], startupProxyProperties[1],
-            startupProxyProperties[2], startupProxyProperties[3]);
+            startupProxyProperties[2], startupProxyProperties[3],
+            startupProxyProperties[4],
+            startupProxyProperties[5] == null ? null
+                    : startupProxyProperties[5].toCharArray(),
+            startupProxyProperties[6],
+            startupProxyProperties[7] == null ? null
+                    : startupProxyProperties[7].toCharArray(),
+            startupProxyProperties[8]);
     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)
+            .append(startupProxyProperties[1])
+            .append(startupProxyProperties[4] != null
+                    && !startupProxyProperties[4].isEmpty()
+                            ? " [" + startupProxyProperties[4] + "]"
+                            : "")
+            .append(", https.proxyHost=").append(startupProxyProperties[2])
+            .append(", https.proxyPort=").append(startupProxyProperties[3])
+            .append(startupProxyProperties[6] != null
+                    && !startupProxyProperties[6].isEmpty()
+                            ? " [" + startupProxyProperties[6] + "]"
+                            : "");
+
+    Cache.debug(sb.toString());
+  }
+
+  public static void setProxyPropertiesFromPreferences()
+  {
+    setProxyPropertiesFromPreferences(Cache.PROXYTYPE_SYSTEM);
+  }
+
+  public static void setProxyPropertiesFromPreferences(
+          String previousProxyType)
+  {
+    String proxyType = Cache.getDefault("USE_PROXY",
+            Cache.PROXYTYPE_SYSTEM);
+    if (proxyType.equals(Cache.PROXYTYPE_NONE))
     {
-      System.err.println(sb.toString());
+      if (!previousProxyType.equals(proxyType))
+        Cache.log.info("Setting no proxy settings");
+      Cache.setProxyProperties(null, null, null, null, null, null, null,
+              null, null);
     }
-    else
+    else if (proxyType.equals(Cache.PROXYTYPE_CUSTOM))
     {
-      Cache.log.debug(sb.toString());
+      if (!previousProxyType.equals(proxyType))
+        Cache.log.info("Setting custom proxy settings");
+      boolean proxyAuthSet = Cache.getDefault("PROXY_AUTH", false);
+      Cache.setProxyProperties(Cache.getDefault("PROXY_SERVER", null),
+              Cache.getDefault("PROXY_PORT", null),
+              Cache.getDefault("PROXY_SERVER_HTTPS", null),
+              Cache.getDefault("PROXY_PORT_HTTPS", null),
+              proxyAuthSet ? Cache.getDefault("PROXY_AUTH_USERNAME", "")
+                      : null,
+              proxyAuthSet ? Cache.proxyAuthPassword : null,
+              proxyAuthSet ? Cache.getDefault("PROXY_AUTH_USERNAME", "")
+                      : null,
+              proxyAuthSet ? Cache.proxyAuthPassword : null, "localhost");
+    }
+    else // systemProxy should be selected and is sensible default anyway
+    {
+      if (!previousProxyType.equals(proxyType))
+        Cache.log.info("Setting system proxy settings");
+      Cache.resetProxyProperties();
     }
-  }
-
-  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)
+          String httpsHost, String httpsPort, String httpUser,
+          char[] httpPassword, String httpsUser, char[] httpsPassword,
+          String nonProxyHosts)
   {
-    // cannot set property to null -- use clearProperty instead
-
-    // http.proxyHost
-    if (httpHost == null)
+    setOrClearSystemProperty("http.proxyHost", httpHost);
+    setOrClearSystemProperty("http.proxyPort", httpPort);
+    setOrClearSystemProperty("https.proxyHost", httpsHost);
+    setOrClearSystemProperty("https.proxyPort", httpsPort);
+    setOrClearSystemProperty("http.proxyUser", httpUser);
+    setOrClearSystemProperty("http.proxyPassword", httpPassword);
+    setOrClearSystemProperty("https.proxyUser", httpsUser);
+    setOrClearSystemProperty("https.proxyPassword", httpsPassword);
+    // are we using a custom proxy (password prompt might be required)?
+    boolean customProxySet = getDefault("USE_PROXY", PROXYTYPE_SYSTEM)
+            .equals(PROXYTYPE_CUSTOM);
+    if (httpUser != null || httpsUser != null)
     {
-      System.clearProperty("http.proxyHost");
+      try
+      {
+        Authenticator.setDefault(new Authenticator()
+        {
+          @Override
+          protected PasswordAuthentication getPasswordAuthentication()
+          {
+            Cache.debug(
+                    "*** START PasswordAuthentication.getPasswordAuthentication()");
+            Cache.debug("*** getRequestorType()=" + getRequestorType());
+            if (getRequestorType() == RequestorType.PROXY)
+            {
+              String protocol = getRequestingProtocol();
+              boolean needProxyPasswordSet = false;
+              Cache.debug("*** customProxySet = " + customProxySet);
+              Cache.debug("*** protocol = " + protocol);
+              Cache.debug("*** httpUser = " + httpUser);
+              Cache.debug(
+                      "*** httpPassword = \"" + (httpPassword == null ? null
+                              : new String(httpPassword)) + "\"");
+              Cache.debug("*** httpsUser = " + httpsUser);
+              Cache.debug("*** httpsPassword = \""
+                      + (httpsPassword == null ? null
+                              : new String(httpsPassword))
+                      + "\"");
+              if (customProxySet &&
+              // we have a username but no password for the scheme being
+              // requested
+              (protocol.equalsIgnoreCase("http")
+                      && (httpUser != null && httpUser.length() > 0
+                              && (httpPassword == null
+                                      || httpPassword.length == 0)))
+                      || (protocol.equalsIgnoreCase("https")
+                              && (httpsUser != null
+                                      && httpsUser.length() > 0
+                                      && (httpsPassword == null
+                                              || httpsPassword.length == 0))))
+              {
+                // open Preferences -> Connections
+                String message = MessageManager
+                        .getString("label.proxy_password_required");
+                Cache.debug("***+ TRYING TO OPEN PREFERENCES");
+                openPreferencesConnectionsForProxyPassword(message);
+                Cache.debug("***+ AFTER TRYING TO OPEN PREFERENCES");
+              }
+              else
+              {
+                Cache.debug("***+ TRYING TO GET PASSWORDAUTHENTICATION");
+                try
+                {
+                  if (protocol.equalsIgnoreCase("http")
+                          && getRequestingHost().equalsIgnoreCase(httpHost)
+                          && getRequestingPort() == Integer
+                                  .valueOf(httpPort))
+                  {
+                    Cache.debug("***+ RETURNING PasswordAuthentication(\""
+                            + httpUser + "\", \"" + new String(httpPassword)
+                            + "\"");
+                    return new PasswordAuthentication(httpUser,
+                            httpPassword);
+                  }
+                  if (protocol.equalsIgnoreCase("https")
+                          && getRequestingHost().equalsIgnoreCase(httpsHost)
+                          && getRequestingPort() == Integer
+                                  .valueOf(httpsPort))
+                  {
+                    Cache.debug("***+ RETURNING PasswordAuthentication(\""
+                            + httpsUser + "\", \"" + httpsPassword + "\"");
+                    return new PasswordAuthentication(httpsUser,
+                            httpsPassword);
+                  }
+                } catch (NumberFormatException e)
+                {
+                  Cache.error("Problem with proxy port values [http:"
+                          + httpPort + ", https:" + httpsPort + "]");
+                }
+                Cache.debug(
+                        "***+ AFTER TRYING TO GET PASSWORDAUTHENTICATION");
+              }
+            }
+            // non proxy request
+            Cache.debug("***+ Returning null");
+            return null;
+          }
+        });
+        // required to re-enable basic authentication (should be okay for a
+        // local proxy)
+        Cache.debug(
+                "***+ Setting jdk.http.auth.tunneling.disabledSchemes to ''");
+        System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
+      } catch (SecurityException e)
+      {
+        Cache.error("Could not set default Authenticator");
+        Cache.debug(getStackTraceString(e));
+      }
     }
     else
     {
-      System.setProperty("http.proxyHost", httpHost);
+      // reset the Authenticator to protect http.proxyUser and
+      // http.proxyPassword Just In Case
+      Cache.debug("***+ Setting default Authenticator to null");
+      Authenticator.setDefault(null);
     }
 
-    // http.proxyPort
-    if (httpPort == null)
+    // nonProxyHosts not currently configurable in Preferences
+    Cache.debug("***+ Setting http.nonProxyHosts property to \""
+            + nonProxyHosts + "\"");
+    setOrClearSystemProperty("http.nonProxyHosts", nonProxyHosts);
+  }
+
+  private static void openPreferencesConnectionsForProxyPassword(
+          String message)
+  {
+    //
+    Cache.info("Opening Preferences for proxy password");
+    // Desktop.instance.preferences_actionPerformed(null);
+    Cache.debug("***+########## TRYING TO OPEN PREFERENCES: " + message);
+    Preferences p = new Preferences(GPreferences.CONNECTIONS_TAB, message);
+    p.grabFocus();
+  }
+
+  public static void setOrClearSystemProperty(String key, char[] value)
+  {
+    setOrClearSystemProperty(key,
+            (value == null) ? null : new String(value));
+  }
+
+  public static void setOrClearSystemProperty(String key, String value)
+  {
+    if (key == null)
+    {
+      return;
+    }
+    if (value == null)
     {
-      System.clearProperty("http.proxyPort");
+      System.clearProperty(key);
     }
     else
     {
-      System.setProperty("http.proxyPort", httpPort);
+      System.setProperty(key, value);
     }
+  }
+
+  public final static int DEBUG = 10;
+
+  public final static int INFO = 20;
+
+  public final static int WARN = 30;
 
-    // https.proxyHost
-    if (httpsHost == null)
+  public final static int ERROR = 40;
+
+  public static boolean println(int level, String message)
+  {
+    if (Cache.log == null)
     {
-      System.clearProperty("https.proxyHost");
+      if (level >= ERROR)
+        System.err.println(message);
+      else
+        System.out.println(message);
+      return false;
     }
-    else
+    if (level >= WARN)
     {
-      System.setProperty("https.proxyHost", httpsHost);
+      Cache.log.warn(message);
     }
-
-    // https.proxyPort
-    if (httpsPort == null)
+    else if (level >= INFO)
     {
-      System.clearProperty("https.proxyPort");
+      Cache.log.info(message);
     }
     else
     {
-      System.setProperty("https.proxyPort", httpsPort);
+      Cache.log.debug(message);
     }
+    return true;
+  }
+
+  public static void debug(String message)
+  {
+    println(DEBUG, message);
+  }
+
+  public static void info(String message)
+  {
+    println(INFO, message);
+  }
 
+  public static void warn(String message)
+  {
+    println(WARN, message);
+  }
+
+  public static void error(String message)
+  {
+    println(ERROR, message);
   }
 }