JAL-3775 JAL-3633 Make java.net.Authenticator code conditional on not being JalviewJS
authorBen Soares <b.soares@dundee.ac.uk>
Fri, 11 Dec 2020 16:34:53 +0000 (16:34 +0000)
committerBen Soares <b.soares@dundee.ac.uk>
Fri, 11 Dec 2020 16:34:53 +0000 (16:34 +0000)
src/jalview/bin/Cache.java
src/jalview/jbgui/GPreferences.java

index e6f86fe..153eaff 100755 (executable)
@@ -1411,80 +1411,92 @@ public class Cache
         Arrays.fill(displayHttpPw, '*');
         Cache.debug("CACHE Proxy: setting new Authenticator with httpUser='"
                 + httpUser + "' httpPassword='" + displayHttpPw + "'");
-        Authenticator.setDefault(new Authenticator()
+        if (!Platform.isJS())
+        /* *
+         * java.net.Authenticator not implemented in SwingJS yet
+         * 
+         * @j2sIgnore
+         * 
+         */
         {
-          @Override
-          protected PasswordAuthentication getPasswordAuthentication()
+          Authenticator.setDefault(new Authenticator()
           {
-            if (getRequestorType() == RequestorType.PROXY)
+            @Override
+            protected PasswordAuthentication getPasswordAuthentication()
             {
-              String protocol = getRequestingProtocol();
-              boolean needProxyPasswordSet = false;
-              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))))
+              if (getRequestorType() == RequestorType.PROXY)
               {
-                // open Preferences -> Connections
-                String message = MessageManager
-                        .getString("label.proxy_password_required");
-                Preferences.openPreferences(Preferences.CONNECTIONS_TAB,
-                        message);
-                Preferences.getInstance()
-                        .proxyAuthPasswordCheckHighlight(true, true);
-              }
-              else
-              {
-                try
+                String protocol = getRequestingProtocol();
+                boolean needProxyPasswordSet = false;
+                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");
+                  Preferences.openPreferences(Preferences.CONNECTIONS_TAB,
+                          message);
+                  Preferences.getInstance()
+                          .proxyAuthPasswordCheckHighlight(true, true);
+                }
+                else
                 {
-                  if (protocol.equalsIgnoreCase("http")
-                          && getRequestingHost().equalsIgnoreCase(httpHost)
-                          && getRequestingPort() == Integer
-                                  .valueOf(httpPort))
+                  try
                   {
-                    Cache.debug(
-                            "AUTHENTICATOR returning PasswordAuthentication(\""
-                                    + httpUser + "\", '"
-                                    + new String(displayHttpPw) + "')");
-                    return new PasswordAuthentication(httpUser,
-                            httpPassword);
-                  }
-                  if (protocol.equalsIgnoreCase("https")
-                          && getRequestingHost().equalsIgnoreCase(httpsHost)
-                          && getRequestingPort() == Integer
-                                  .valueOf(httpsPort))
+                    if (protocol.equalsIgnoreCase("http")
+                            && getRequestingHost()
+                                    .equalsIgnoreCase(httpHost)
+                            && getRequestingPort() == Integer
+                                    .valueOf(httpPort))
+                    {
+                      Cache.debug(
+                              "AUTHENTICATOR returning PasswordAuthentication(\""
+                                      + httpUser + "\", '"
+                                      + new String(displayHttpPw) + "')");
+                      return new PasswordAuthentication(httpUser,
+                              httpPassword);
+                    }
+                    if (protocol.equalsIgnoreCase("https")
+                            && getRequestingHost()
+                                    .equalsIgnoreCase(httpsHost)
+                            && getRequestingPort() == Integer
+                                    .valueOf(httpsPort))
+                    {
+                      char[] displayHttpsPw = new char[httpPassword.length];
+                      Arrays.fill(displayHttpsPw, '*');
+                      Cache.debug(
+                              "AUTHENTICATOR returning PasswordAuthentication(\""
+                                      + httpsUser + "\", '" + displayHttpsPw
+                                      + "'");
+                      return new PasswordAuthentication(httpsUser,
+                              httpsPassword);
+                    }
+                  } catch (NumberFormatException e)
                   {
-                    char[] displayHttpsPw = new char[httpPassword.length];
-                    Arrays.fill(displayHttpsPw, '*');
-                    Cache.debug(
-                            "AUTHENTICATOR returning PasswordAuthentication(\""
-                                    + httpsUser + "\", '" + displayHttpsPw
-                                    + "'");
-                    return new PasswordAuthentication(httpsUser,
-                            httpsPassword);
+                    Cache.error("Problem with proxy port values [http:"
+                            + httpPort + ", https:" + httpsPort + "]");
                   }
-                } catch (NumberFormatException e)
-                {
-                  Cache.error("Problem with proxy port values [http:"
-                          + httpPort + ", https:" + httpsPort + "]");
+                  Cache.debug(
+                          "AUTHENTICATOR after trying to get PasswordAuthentication");
                 }
-                Cache.debug(
-                        "AUTHENTICATOR after trying to get PasswordAuthentication");
               }
+              // non proxy request
+              Cache.debug("AUTHENTICATOR returning null");
+              return null;
             }
-            // non proxy request
-            Cache.debug("AUTHENTICATOR returning null");
-            return null;
-          }
-        });
+          });
+        } // end of j2sIgnore for java.net.Authenticator
+
         // required to re-enable basic authentication (should be okay for a
         // local proxy)
         Cache.debug(
@@ -1503,8 +1515,17 @@ public class Cache
       /* as noted above, due to bug in java this doesn't work if the sun.net.www.protocol.http.AuthCache
        * has working credentials. No workaround for Java 11.
        */
-      Cache.debug("AUTHENTICATOR setting default Authenticator to null");
-      Authenticator.setDefault(null);
+      if (!Platform.isJS())
+      /* *
+       * java.net.Authenticator not implemented in SwingJS yet
+       * 
+       * @j2sIgnore
+       * 
+       */
+      {
+        Cache.debug("AUTHENTICATOR setting default Authenticator to null");
+        Authenticator.setDefault(null);
+      }
     }
 
     // nonProxyHosts not currently configurable in Preferences
index 38a4732..5af94e4 100755 (executable)
@@ -1205,45 +1205,56 @@ public class GPreferences extends JPanel
     gbc.gridy++;
     proxyPanel.add(hpPanel, gbc);
 
-    // Require authentication checkbox
-    gbc.gridy++;
-    proxyPanel.add(proxyAuth, gbc);
-
-    // username and password
-    JPanel upPanel = new JPanel();
-    upPanel.setLayout(new GridBagLayout());
-    // username row
-    c.gridy = 0;
-    c.gridx = 0;
-    c.gridwidth = 1;
-    c.weightx = 0.4;
-    c.anchor = GridBagConstraints.LINE_END;
-    upPanel.add(proxyAuthUsernameLabel, c);
-
-    c.gridx++;
-    c.weightx = 1.0;
-    c.anchor = GridBagConstraints.LINE_START;
-    upPanel.add(proxyAuthUsernameTB, c);
-
-    // password row
-    c.gridy++;
-    c.gridx = 0;
-    c.weightx = 0.4;
-    c.anchor = GridBagConstraints.LINE_END;
-    upPanel.add(proxyAuthPasswordLabel, c);
-
-    c.gridx++;
-    c.weightx = 1.0;
-    c.anchor = GridBagConstraints.LINE_START;
-    upPanel.add(proxyAuthPasswordPB, c);
-
-    c.gridx++;
-    c.weightx = 0.4;
-    c.anchor = GridBagConstraints.LINE_START;
-    upPanel.add(passwordNotStoredLabel, c);
-
-    gbc.gridy++;
-    proxyPanel.add(upPanel, gbc);
+    if (!Platform.isJS())
+    /**
+     * java.net.Authenticator is not implemented in SwingJS. Not displaying the
+     * Authentication options in Preferences.
+     * 
+     * @j2sIgnore
+     * 
+     */
+    {
+      // Require authentication checkbox
+      gbc.gridy++;
+      proxyPanel.add(proxyAuth, gbc);
+
+      // username and password
+      JPanel upPanel = new JPanel();
+      upPanel.setLayout(new GridBagLayout());
+      // username row
+      c.gridy = 0;
+      c.gridx = 0;
+      c.gridwidth = 1;
+      c.weightx = 0.4;
+      c.anchor = GridBagConstraints.LINE_END;
+      upPanel.add(proxyAuthUsernameLabel, c);
+
+      c.gridx++;
+      c.weightx = 1.0;
+      c.anchor = GridBagConstraints.LINE_START;
+      upPanel.add(proxyAuthUsernameTB, c);
+
+      // password row
+      c.gridy++;
+      c.gridx = 0;
+      c.weightx = 0.4;
+      c.anchor = GridBagConstraints.LINE_END;
+      upPanel.add(proxyAuthPasswordLabel, c);
+
+      c.gridx++;
+      c.weightx = 1.0;
+      c.anchor = GridBagConstraints.LINE_START;
+      upPanel.add(proxyAuthPasswordPB, c);
+
+      c.gridx++;
+      c.weightx = 0.4;
+      c.anchor = GridBagConstraints.LINE_START;
+      upPanel.add(passwordNotStoredLabel, c);
+
+      gbc.gridy++;
+      proxyPanel.add(upPanel, gbc);
+
+    } // end j2sIgnore
 
     applyProxyButton.setText(MessageManager.getString("action.apply"));
     applyProxyButton.addActionListener(new ActionListener()