JAL-3280 Fixed the version checking URL formulation (getdown build_properties) for...
[jalview.git] / src / jalview / bin / Cache.java
index 8269f32..4a83b35 100755 (executable)
@@ -34,6 +34,7 @@ import java.net.PasswordAuthentication;
 import java.net.URL;
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.Date;
 import java.util.Enumeration;
@@ -54,12 +55,12 @@ 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.ChannelProperties;
 import jalview.util.ColorUtils;
 import jalview.util.MessageManager;
 import jalview.util.Platform;
@@ -309,6 +310,9 @@ public class Cache
     }
   };
 
+  /* build Properties (not all saved to .jalview_properties) */
+  public static Properties buildProperties = new Properties();
+
   /** Default file is ~/.jalview_properties */
   static String propertiesFile;
 
@@ -356,7 +360,8 @@ public class Cache
       // lcastor.addAppender(ap);
       // jalview.bin.Cache.log.addAppender(ap);
       // Tell the user that debug is enabled
-      jalview.bin.Cache.log.debug("Jalview Debugging Output Follows.");
+      jalview.bin.Cache.log.debug(ChannelProperties.getProperty("app_name")
+              + " Debugging Output Follows.");
     } catch (Exception ex)
     {
       System.err.println("Problems initializing the log4j system\n");
@@ -525,9 +530,13 @@ public class Cache
 
       class VersionChecker extends Thread
       {
+
         @Override
         public void run()
         {
+          String remoteBuildPropertiesUrl = Cache
+                  .getAppbaseBuildProperties();
+
           String orgtimeout = System
                   .getProperty("sun.net.client.defaultConnectTimeout");
           if (orgtimeout == null)
@@ -541,28 +550,19 @@ public class Cache
           {
             System.setProperty("sun.net.client.defaultConnectTimeout",
                     "5000");
-            java.net.URL url = new java.net.URL(Cache
-                    .getDefault("www.jalview.org", "http://www.jalview.org")
-                    + "/webstart/jalview.jnlp");
+            java.net.URL url = new java.net.URL(remoteBuildPropertiesUrl);
+
             BufferedReader in = new BufferedReader(
                     new InputStreamReader(url.openStream()));
-            String line = null;
-            while ((line = in.readLine()) != null)
-            {
-              if (line.indexOf("jalview.version") == -1)
-              {
-                continue;
-              }
 
-              line = line.substring(line.indexOf("value=") + 7);
-              line = line.substring(0, line.lastIndexOf("\""));
-              remoteVersion = line;
-              break;
-            }
+            Properties remoteBuildProperties = new Properties();
+            remoteBuildProperties.load(in);
+            remoteVersion = remoteBuildProperties.getProperty("VERSION");
           } catch (Exception ex)
           {
-            System.out.println(
-                    "Non-fatal exception when checking version at www.jalview.org :");
+            System.out
+                    .println("Non-fatal exception when checking version at "
+                            + remoteBuildPropertiesUrl + ":");
             System.out.println(ex);
             remoteVersion = getProperty("VERSION");
           }
@@ -637,8 +637,23 @@ public class Cache
       String buildDetails = resolveResourceURLFor("/.build_properties");
       URL localJarFileURL = new URL(buildDetails);
       InputStream in = localJarFileURL.openStream();
-      applicationProperties.load(in);
+      buildProperties.load(in);
       in.close();
+      if (buildProperties.getProperty("BUILD_DATE", null) != null)
+      {
+        applicationProperties.put("BUILD_DATE",
+                buildProperties.getProperty("BUILD_DATE"));
+      }
+      if (buildProperties.getProperty("INSTALLATION", null) != null)
+      {
+        applicationProperties.put("INSTALLATION",
+                buildProperties.getProperty("INSTALLATION"));
+      }
+      if (buildProperties.getProperty("VERSION", null) != null)
+      {
+        applicationProperties.put("VERSION",
+                buildProperties.getProperty("VERSION"));
+      }
     } catch (Exception ex)
     {
       System.out.println("Error reading build details: " + ex);
@@ -661,8 +676,8 @@ public class Cache
     new BuildDetails(codeVersion, null, codeInstallation);
     if (printVersion && reportVersion)
     {
-      System.out.println(
-              "Jalview Version: " + codeVersion + codeInstallation);
+      System.out.println(ChannelProperties.getProperty("app_name")
+              + " Version: " + codeVersion + codeInstallation);
     }
   }
 
@@ -829,7 +844,8 @@ public class Cache
 
           lvclient.addAppender(log.getAppender("JalviewLogger"));
           // Tell the user that debug is enabled
-          lvclient.debug("Jalview Vamsas Client Debugging Output Follows.");
+          lvclient.debug(ChannelProperties.getProperty("app_name")
+                  + " Vamsas Client Debugging Output Follows.");
         }
       } catch (Exception e)
       {
@@ -868,7 +884,8 @@ public class Cache
 
           lgclient.addAppender(log.getAppender("JalviewLogger"));
           // Tell the user that debug is enabled
-          lgclient.debug("Jalview Groovy Client Debugging Output Follows.");
+          lgclient.debug(ChannelProperties.getProperty("app_name")
+                  + " Groovy Client Debugging Output Follows.");
         }
       } catch (Error e)
       {
@@ -930,7 +947,7 @@ public class Cache
                 .getConstructor(new Class[]
                 { String.class, String.class, String.class })
                 .newInstance(new Object[]
-                { "Jalview Desktop",
+                { ChannelProperties.getProperty("app_name") + " Desktop",
                     (vrs = jalview.bin.Cache.getProperty("VERSION") + "_"
                             + jalview.bin.Cache.getDefault("BUILD_DATE",
                                     "unknown")),
@@ -1206,10 +1223,12 @@ public class Cache
   public static String getVersionDetailsForConsole()
   {
     StringBuilder sb = new StringBuilder();
-    sb.append("Jalview Version: ");
+    sb.append(ChannelProperties.getProperty("app_name"))
+            .append(" Version: ");
     sb.append(jalview.bin.Cache.getDefault("VERSION", "TEST"));
     sb.append("\n");
-    sb.append("Jalview Installation: ");
+    sb.append(ChannelProperties.getProperty("app_name"))
+            .append(" Installation: ");
     sb.append(jalview.bin.Cache.getDefault("INSTALLATION", "unknown"));
     sb.append("\n");
     sb.append("Build Date: ");
@@ -1245,6 +1264,8 @@ public class Cache
     {
       appendIfNotNull(sb, "Getdown appdir: ",
               System.getProperty("getdownappdir"), "\n", null);
+      appendIfNotNull(sb, "Getdown appbase: ",
+              System.getProperty("getdownappbase"), "\n", null);
       appendIfNotNull(sb, "Java home: ", System.getProperty("java.home"),
               "\n", "unknown");
     }
@@ -1316,17 +1337,28 @@ public class Cache
   {
     String proxyType = Cache.getDefault("USE_PROXY",
             Cache.PROXYTYPE_SYSTEM);
-    if (proxyType.equals(Cache.PROXYTYPE_NONE))
+    if (previousProxyType != null
+            && !proxyType.equals(Cache.PROXYTYPE_CUSTOM) // always apply
+                                                         // customProxy
+            && proxyType.equals(previousProxyType))
     {
-      if (!previousProxyType.equals(proxyType))
-        Cache.log.info("Setting no proxy settings");
-      Cache.setProxyProperties(null, null, null, null, null, null, null,
-              null, null);
+      // no change
+      return;
     }
-    else if (proxyType.equals(Cache.PROXYTYPE_CUSTOM))
+    switch (proxyType)
     {
+    case Cache.PROXYTYPE_NONE:
       if (!previousProxyType.equals(proxyType))
-        Cache.log.info("Setting custom proxy settings");
+      {
+        Cache.log.info("Setting no proxy settings");
+        Cache.setProxyProperties(null, null, null, null, null, null, null,
+                null, null);
+      }
+      break;
+    case Cache.PROXYTYPE_CUSTOM:
+      // always re-set a custom proxy -- it might have changed, particularly
+      // password
+      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),
@@ -1338,11 +1370,9 @@ public class Cache
               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");
+      break;
+    default: // system proxy settings by default
+      Cache.log.info("Setting system proxy settings");
       Cache.resetProxyProperties();
     }
   }
@@ -1357,103 +1387,135 @@ public class Cache
     setOrClearSystemProperty("https.proxyHost", httpsHost);
     setOrClearSystemProperty("https.proxyPort", httpsPort);
     setOrClearSystemProperty("http.proxyUser", httpUser);
-    setOrClearSystemProperty("http.proxyPassword", httpPassword);
     setOrClearSystemProperty("https.proxyUser", httpsUser);
-    setOrClearSystemProperty("https.proxyPassword", httpsPassword);
+    // note: passwords for http.proxyPassword and https.proxyPassword are sent
+    // via the Authenticator, properties do not need to be set
+
     // are we using a custom proxy (password prompt might be required)?
     boolean customProxySet = getDefault("USE_PROXY", PROXYTYPE_SYSTEM)
             .equals(PROXYTYPE_CUSTOM);
+
+    /*
+     * A bug in Java means the AuthCache does not get reset, so once it has working credentials,
+     * it never asks for more, so changing the Authenticator has no effect (as getPasswordAuthentication()
+     * is not re-called).
+     * This could lead to password leak to a hostile proxy server, so I'm putting in a hack to clear
+     * the AuthCache.
+     * see https://www.generacodice.com/en/articolo/154918/Reset-the-Authenticator-credentials
+     * ...
+     * Turns out this is only accessible in Java 8, and not in Java 9 onwards, so commenting out
+     */
+    /*
+    try
+    {
+      sun.net.www.protocol.http.AuthCacheValue
+              .setAuthCache(new sun.net.www.protocol.http.AuthCacheImpl());
+    } catch (Throwable t)
+    {
+      Cache.error(t.getMessage());
+      Cache.debug(getStackTraceString(t));
+    }
+    */
+
     if (httpUser != null || httpsUser != null)
     {
       try
       {
-        Authenticator.setDefault(new Authenticator()
+        char[] displayHttpPw = new char[httpPassword == null ? 0
+                : httpPassword.length];
+        Arrays.fill(displayHttpPw, '*');
+        Cache.debug("CACHE Proxy: setting new Authenticator with httpUser='"
+                + httpUser + "' httpPassword='" + displayHttpPw + "'");
+        if (!Platform.isJS())
+        /* *
+         * java.net.Authenticator not implemented in SwingJS yet
+         * 
+         * @j2sIgnore
+         * 
+         */
         {
-          @Override
-          protected PasswordAuthentication getPasswordAuthentication()
+          Authenticator.setDefault(new Authenticator()
           {
-            Cache.debug(
-                    "*** START PasswordAuthentication.getPasswordAuthentication()");
-            Cache.debug("*** getRequestorType()=" + getRequestorType());
-            if (getRequestorType() == RequestorType.PROXY)
+            @Override
+            protected PasswordAuthentication getPasswordAuthentication()
             {
-              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
+              if (getRequestorType() == RequestorType.PROXY)
               {
-                Cache.debug("***+ TRYING TO GET PASSWORDAUTHENTICATION");
-                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("***+ RETURNING PasswordAuthentication(\""
-                            + httpUser + "\", \"" + new String(httpPassword)
-                            + "\"");
-                    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)
                   {
-                    Cache.debug("***+ RETURNING PasswordAuthentication(\""
-                            + httpsUser + "\", \"" + httpsPassword + "\"");
-                    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(
-                        "***+ AFTER TRYING TO GET PASSWORDAUTHENTICATION");
               }
+              // non proxy request
+              Cache.debug("AUTHENTICATOR returning null");
+              return null;
             }
-            // non proxy request
-            Cache.debug("***+ 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(
-                "***+ Setting jdk.http.auth.tunneling.disabledSchemes to ''");
+                "AUTHENTICATOR setting property 'jdk.http.auth.tunneling.disabledSchemes' to \"\"");
         System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
       } catch (SecurityException e)
       {
@@ -1465,27 +1527,28 @@ 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);
+      /* 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.
+       */
+      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
-    Cache.debug("***+ Setting http.nonProxyHosts property to \""
+    Cache.debug("AUTHENTICATOR setting property 'http.nonProxyHosts' 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,
@@ -1508,25 +1571,32 @@ public class Cache
     }
   }
 
-  public final static int DEBUG = 10;
+  public final static int TRACE = 10;
 
-  public final static int INFO = 20;
+  public final static int DEBUG = 20;
 
-  public final static int WARN = 30;
+  public final static int INFO = 30;
 
-  public final static int ERROR = 40;
+  public final static int WARN = 40;
+
+  public final static int ERROR = 50;
 
   public static boolean println(int level, String message)
   {
     if (Cache.log == null)
     {
-      if (level >= ERROR)
+      if (level >= WARN)
         System.err.println(message);
-      else
+      else if (level >= INFO)
         System.out.println(message);
+      // not printing debug or trace messages
       return false;
     }
-    if (level >= WARN)
+    if (level >= ERROR)
+    {
+      Cache.log.error(message);
+    }
+    else if (level >= WARN)
     {
       Cache.log.warn(message);
     }
@@ -1534,13 +1604,22 @@ public class Cache
     {
       Cache.log.info(message);
     }
-    else
+    else if (level >= DEBUG)
     {
       Cache.log.debug(message);
     }
+    else
+    {
+      Cache.log.trace(message);
+    }
     return true;
   }
 
+  public static void trace(String message)
+  {
+    println(TRACE, message);
+  }
+
   public static void debug(String message)
   {
     println(DEBUG, message);
@@ -1560,4 +1639,77 @@ public class Cache
   {
     println(ERROR, message);
   }
+
+  /**
+   * Getdown appbase methods
+   */
+
+  private static final String releaseAppbase;
+
+  private static String getdownAppbase;
+
+  private static String getdownDistDir;
+
+  static
+  {
+    if (!Platform.isJS())
+    {
+      Float specversion = Float
+              .parseFloat(System.getProperty("java.specification.version"));
+      releaseAppbase = (specversion < 9)
+              ? "https://www.jalview.org/getdown/release/1.8"
+              : "https://www.jalview.org/getdown/release/11";
+    }
+    else
+    {
+      // this value currenly made up, can be changed to URL that will be
+      // "https://www.jalview.org/jalview-js/swingjs/j2s/build_properties"
+      releaseAppbase = "https://www.jalview.org/jalview-js";
+      getdownAppbase = releaseAppbase;
+      getdownDistDir = "/swingjs/j2s";
+    }
+  }
+
+  // look for properties (passed in by getdown) otherwise default to release
+  private static void setGetdownAppbase()
+  {
+    if (getdownAppbase != null)
+    {
+      return;
+    }
+    String appbase = System.getProperty("getdownappbase");
+    String distDir = System.getProperty("getdowndistdir");
+    if (appbase == null)
+    {
+      appbase = buildProperties.getProperty("GETDOWNAPPBASE");
+      distDir = buildProperties.getProperty("GETDOWNAPPDISTDIR");
+    }
+    if (appbase == null)
+    {
+      appbase = releaseAppbase;
+      distDir = "release";
+    }
+    if (appbase.endsWith("/"))
+    {
+      appbase = appbase.substring(0, appbase.length() - 1);
+    }
+    if (distDir == null)
+    {
+      distDir = appbase.equals(releaseAppbase) ? "release" : "alt";
+    }
+    getdownAppbase = appbase;
+    getdownDistDir = distDir;
+  }
+
+  public static String getGetdownAppbase()
+  {
+    setGetdownAppbase();
+    return getdownAppbase;
+  }
+
+  public static String getAppbaseBuildProperties()
+  {
+    String appbase = getGetdownAppbase();
+    return appbase + "/" + getdownDistDir + "/build_properties";
+  }
 }