JAL-3633 Set proxy preferences before web calls in jalview.bin.Jalview. Fixed call...
authorBen Soares <b.soares@dundee.ac.uk>
Mon, 27 Jul 2020 19:31:16 +0000 (20:31 +0100)
committerBen Soares <b.soares@dundee.ac.uk>
Fri, 13 Nov 2020 15:04:42 +0000 (15:04 +0000)
src/jalview/bin/Cache.java
src/jalview/bin/Jalview.java
src/jalview/gui/Preferences.java
src/jalview/jbgui/GPreferences.java

index 8051336..8269f32 100755 (executable)
@@ -1306,6 +1306,47 @@ public class Cache
     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))
+    {
+      if (!previousProxyType.equals(proxyType))
+        Cache.log.info("Setting no proxy settings");
+      Cache.setProxyProperties(null, null, null, null, null, null, null,
+              null, null);
+    }
+    else if (proxyType.equals(Cache.PROXYTYPE_CUSTOM))
+    {
+      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 httpHost, String httpPort,
           String httpsHost, String httpsPort, String httpUser,
           char[] httpPassword, String httpsUser, char[] httpsPassword,
@@ -1331,18 +1372,32 @@ public class Cache
           @Override
           protected PasswordAuthentication getPasswordAuthentication()
           {
+            Cache.debug(
+                    "*** START PasswordAuthentication.getPasswordAuthentication()");
+            Cache.debug("*** getRequestorType()=" + getRequestorType());
             if (getRequestorType() == RequestorType.PROXY)
             {
-              String scheme = getRequestingScheme();
+              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
-              (scheme.equalsIgnoreCase("http")
+              (protocol.equalsIgnoreCase("http")
                       && (httpUser != null && httpUser.length() > 0
                               && (httpPassword == null
                                       || httpPassword.length == 0)))
-                      || (scheme.equalsIgnoreCase("https")
+                      || (protocol.equalsIgnoreCase("https")
                               && (httpsUser != null
                                       && httpsUser.length() > 0
                                       && (httpsPassword == null
@@ -1351,25 +1406,33 @@ public class Cache
                 // 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 (scheme.equalsIgnoreCase("http")
+                  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 (scheme.equalsIgnoreCase("https")
+                  if (protocol.equalsIgnoreCase("https")
                           && getRequestingHost().equalsIgnoreCase(httpsHost)
                           && getRequestingPort() == Integer
                                   .valueOf(httpsPort))
                   {
+                    Cache.debug("***+ RETURNING PasswordAuthentication(\""
+                            + httpsUser + "\", \"" + httpsPassword + "\"");
                     return new PasswordAuthentication(httpsUser,
                             httpsPassword);
                   }
@@ -1378,14 +1441,19 @@ public class Cache
                   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)
       {
@@ -1397,10 +1465,13 @@ public class Cache
     {
       // reset the Authenticator to protect http.proxyUser and
       // http.proxyPassword Just In Case
+      Cache.debug("***+ Setting default Authenticator to null");
       Authenticator.setDefault(null);
     }
 
     // nonProxyHosts not currently configurable in Preferences
+    Cache.debug("***+ Setting http.nonProxyHosts property to \""
+            + nonProxyHosts + "\"");
     setOrClearSystemProperty("http.nonProxyHosts", nonProxyHosts);
   }
 
@@ -1410,14 +1481,15 @@ public class Cache
     //
     Cache.info("Opening Preferences for proxy password");
     // Desktop.instance.preferences_actionPerformed(null);
-    Preferences p = new Preferences(GPreferences.CONNECTIONS_TAB);
+    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 : value.toString());
+            (value == null) ? null : new String(value));
   }
 
   public static void setOrClearSystemProperty(String key, String value)
index 6a67750..b1ea60b 100755 (executable)
@@ -415,6 +415,9 @@ public class Jalview
         // t.printStackTrace();
       }
 
+      // set Proxy settings before all the internet calls
+      Cache.setProxyPropertiesFromPreferences();
+
       desktop.setVisible(true);
 
       if (!Platform.isJS())
index 3cada85..da33f39 100755 (executable)
@@ -198,7 +198,12 @@ public class Preferences extends GPreferences
 
   public Preferences(int selectTab)
   {
-    super(selectTab);
+    new Preferences(selectTab, null);
+  }
+
+  public Preferences(int selectTab, String message)
+  {
+    super(selectTab, message);
     frame = new JInternalFrame();
     frame.setContentPane(this);
     if (!Platform.isJS())
@@ -557,9 +562,9 @@ public class Preferences extends GPreferences
       proxyType.setSelected(customProxy.getModel(), true);
       break;
     default:
-      String message = "Incorrect PROXY_TYPE - should be 'none' (clear proxy properties), 'false' (system settings), 'true' (custom settings): "
-              + proxyTypeString;
-      Cache.log.warn(message);
+      Cache.log.warn(
+              "Incorrect PROXY_TYPE - should be 'none' (clear proxy properties), 'false' (system settings), 'true' (custom settings): "
+                      + proxyTypeString);
     }
     proxyServerHttpTB.setText(Cache.getDefault("PROXY_SERVER", ""));
     proxyPortHttpTB.setText(Cache.getDefault("PROXY_PORT", ""));
@@ -570,7 +575,7 @@ public class Preferences extends GPreferences
             .setText(Cache.getDefault("PROXY_AUTH_USERNAME", ""));
     // we are not storing or retrieving proxy password from .jalview_properties
     proxyAuthPasswordTB.setText(Cache.proxyAuthPassword == null ? ""
-            : Cache.proxyAuthPassword.toString());
+            : new String(Cache.proxyAuthPassword));
     setCustomProxyEnabled();
 
     defaultBrowser.setText(Cache.getDefault("DEFAULT_BROWSER", ""));
@@ -942,41 +947,6 @@ public class Preferences extends GPreferences
     }
   }
 
-  public void setProxyFromSettings()
-  {
-    String proxyType = Cache.getDefault("USE_PROXY",
-            Cache.PROXYTYPE_SYSTEM);
-    if (proxyType.equals(Cache.PROXYTYPE_NONE))
-    {
-      if (!previousProxyType.equals(proxyType))
-        Cache.log.info("Setting no proxy settings");
-      Cache.setProxyProperties(null, null, null, null, null, null, null,
-              null, null);
-    }
-    else if (proxyType.equals(Cache.PROXYTYPE_CUSTOM))
-    {
-      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 void saveProxySettings()
   {
     Cache.applicationProperties.setProperty("USE_PROXY",
@@ -991,7 +961,7 @@ public class Preferences extends GPreferences
             Boolean.toString(proxyAuth.isSelected()));
     Cache.setOrRemove("PROXY_AUTH_USERNAME", proxyAuthUsernameTB.getText());
     Cache.proxyAuthPassword = proxyAuthPasswordTB.getPassword();
-    setProxyFromSettings();
+    Cache.setProxyPropertiesFromPreferences(previousProxyType);
   }
 
   /**
index bc9584e..fb0b2a4 100755 (executable)
@@ -383,9 +383,14 @@ public class GPreferences extends JPanel
 
   public GPreferences(int selectTab)
   {
+    new GPreferences(selectTab, null);
+  }
+
+  public GPreferences(int selectTab, String message)
+  {
     try
     {
-      jbInit(selectTab);
+      jbInit(selectTab, message);
     } catch (Exception ex)
     {
       ex.printStackTrace();
@@ -402,12 +407,26 @@ public class GPreferences extends JPanel
     jbInit(0);
   }
 
+  private void jbInit(int selectTab) throws Exception
+  {
+    jbInit(selectTab, null);
+  }
+
   public final static int CONNECTIONS_TAB = 5;
 
-  private void jbInit(int selectTab) throws Exception
+  private void jbInit(int selectTab, String message) throws Exception
   {
     final JTabbedPane tabbedPane = new JTabbedPane();
     this.setLayout(new BorderLayout());
+
+    if (message != null)
+    {
+      JLabel messageLabel = new JLabel(message, JLabel.CENTER);
+      messageLabel.setFont(LABEL_FONT_BOLD);
+      messageLabel.setForeground(Color.RED.darker());
+      this.add(messageLabel, BorderLayout.NORTH);
+    }
+
     JPanel okCancelPanel = initOkCancelPanel();
     this.add(tabbedPane, BorderLayout.CENTER);
     this.add(okCancelPanel, BorderLayout.SOUTH);