JAL-3633 Set proxy preferences before web calls in jalview.bin.Jalview. Fixed call...
[jalview.git] / src / jalview / bin / Cache.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)