Merge branch 'develop' into features/JAL-4219_extended_fasta_rna_ss
[jalview.git] / getdown / src / getdown / core / src / main / java / com / threerings / getdown / data / Application.java
index 9d9fe14..f83add3 100644 (file)
@@ -11,8 +11,6 @@ import java.net.MalformedURLException;
 import java.net.Proxy;
 import java.net.SocketAddress;
 import java.net.InetSocketAddress;
-import java.net.URI;
-import java.net.URISyntaxException;
 import java.net.URL;
 import java.net.URLClassLoader;
 import java.net.URLConnection;
@@ -31,6 +29,7 @@ import java.util.zip.GZIPInputStream;
 
 import jalview.bin.HiDPISetting;
 import jalview.bin.MemorySetting;
+import jalview.util.HttpUtils;
 import jalview.util.LaunchUtils;
 
 import com.threerings.getdown.util.*;
@@ -1117,62 +1116,22 @@ public class Application
             }
         }
 
-        // test for jalview/s URL. Insert startupNotification URI into start of _appargs
+        // test for jalviewX://... URI. Insert startupNotification URI into start of _appargs
         if (! StringUtil.isBlank(_jalviewUri)) {
           _appargs.add(0, _jalviewUri);
         }
+        /* don't convert jalviewX:// URIs to https:// URLs as Jalview can now do that
         if (_appargs.size() > 0) {
-          String uri = _appargs.get(0);
-          try {
-            log.info("TRYING TO PARSE URL '"+uri+"'");
-            URI jalviewUri = new URI(uri);
-            if (jalviewUri != null) {
-              String scheme = jalviewUri.getScheme();
-              if (scheme != null && (scheme.equals("jalview") || scheme.equals("jalviews"))) {
-                boolean https = jalviewUri.getScheme().equals("jalviews");
-                String host = jalviewUri.getHost();
-                int port = jalviewUri.getPort();
-                String file = jalviewUri.getPath();
-                String ref = jalviewUri.getFragment();
-                String query = jalviewUri.getQuery();
-                
-                _appargs.clear();
-                _appargs.add("-open");
-                if (host != null && host.length() > 0) {
-                  URL newUrl = new URL(
-                          (https?"https":"http")
-                          + "://"
-                          + host
-                          + (port > -1? String.valueOf(port) : "")
-                          + jalviewUri.getRawPath()
-                          + (query != null && query.length() > 0 ? "?" + jalviewUri.getRawQuery() : "")
-                          );
-                  _appargs.add(newUrl.toString());
-                } else {
-                  _appargs.add(file);
-                }
-                
-                if (ref != null && ref.length() > 0) {
-                  String[] refArgs = ref.split("&");
-                  for (String refArg : refArgs) {
-                    if (refArg.startsWith("jvmmempc=")) {
-                      jvmmempc = refArg.substring(9);
-                      continue;
-                    }
-                    if (refArg.startsWith("jvmmemmax=")) {
-                      jvmmemmax = refArg.substring(10);
-                      continue;
-                    }
-                    _appargs.add(URLDecoder.decode(refArg, "UTF-8"));
-                  }
-                }
-                
-              }
-            }
-          } catch (URISyntaxException e) {
-            log.error("Malformed jalview URI", uri);
+          String jalviewUri = _appargs.get(0);
+          log.info("Trying to parse uri '"+jalviewUri+"'");
+          if (HttpUtils.isJalviewSchemeUri(jalviewUri)) {
+            String jalviewUrl = HttpUtils.equivalentJalviewUrl(jalviewUri);
+            log.info("Turned url '"+jalviewUri+"' into '"+jalviewUrl+"'");
+            _appargs.clear();
+            _appargs.add(jalviewUrl);
           }
         }
+        */
         
         for (String argString: _appargs) {
           if (argString.startsWith("-jvmmempc=")) {
@@ -1183,6 +1142,14 @@ public class Application
             jvmmemmax = argString.substring(11);
             continue;
           }
+          if (argString.startsWith("--jvmmempc=")) {
+            jvmmempc = argString.substring(11);
+            continue;
+          }
+          if (argString.startsWith("--jvmmemmax=")) {
+            jvmmemmax = argString.substring(12);
+            continue;
+          }
         }
 
         // use saved preferences if no cmdline args
@@ -1238,9 +1205,8 @@ public class Application
         }
 
         // almost finally check the startup file arguments
-        for (File f : _startupFiles) {
-          _appargs.add(f.getAbsolutePath());
-          break; // Only add one file to open
+        for (String f : _startupFiles) {
+          _appargs.add(HttpUtils.isPlausibleUri(f)?f:new File(f).getAbsolutePath());
         }
         
         // check if one arg with recognised extension
@@ -1253,8 +1219,6 @@ public class Application
           }
           if (ext != null && LOCATOR_FILE_EXTENSION.equals(ext.toLowerCase(Locale.ROOT))) {
             // this file extension should have been dealt with in Getdown class
-          } else {
-            _appargs.add(0, "-open");
           }
         }
 
@@ -1979,54 +1943,67 @@ public class Application
     {
         return new File(appdir, path);
     }
-
-    public static void setStartupFilesFromParameterString(String p) {
+    
+    public static void setStartupFilesFromParameters(List<String> parameters) {
       // multiple files *might* be passed in as space separated quoted filenames
       String q = "\"";
-      if (!StringUtil.isBlank(p)) {
-        String[] filenames;
-        // split quoted params or treat as single string array
-        if (p.startsWith(q) && p.endsWith(q)) {
-          // this fails if, e.g.
-          // p=q("stupidfilename\" " "otherfilename")
-          // let's hope no-one ever ends a filename with '" '
-          filenames = p.substring(q.length(),p.length()-q.length()).split(q+" "+q);
-        } else {
-          // single unquoted filename
-          filenames = new String[]{p};
-        }
-
-        // check for locator file.  Only allow one locator file to be double clicked (if multiple files opened, ignore locator files)
-        String locatorFilename = filenames.length >= 1 ? filenames[0] : null;
-        if (
-                !StringUtil.isBlank(locatorFilename)
-                && locatorFilename.toLowerCase(Locale.ROOT).endsWith("."+Application.LOCATOR_FILE_EXTENSION)
-                ) {
-          setLocatorFile(locatorFilename);
-          // remove the locator filename from the filenames array
-          String[] otherFilenames = new String[filenames.length - 1];
-          System.arraycopy(filenames, 1, otherFilenames, 0, otherFilenames.length);
-          filenames = otherFilenames;
-        }
-
-        for (int i = 0; i < filenames.length; i++) {
-          String filename = filenames[i];
-          // skip any other locator files in a multiple file list
-          if (filename.startsWith("jalview://") || filename.startsWith("jalviews://")) {
-            setJalviewUri(filename);
-          } else if (! filename.toLowerCase(Locale.ROOT).endsWith("."+Application.LOCATOR_FILE_EXTENSION)) {
+      for (String p: parameters) {
+        if (!StringUtil.isBlank(p)) {
+          String filename;
+          // split quoted params or treat as single string array
+          if (p.startsWith(q) && p.endsWith(q)) {
+            filename = p.substring(q.length(),p.length()-q.length());
+          } else {
+            // single unquoted filename
+            filename = p;
+          }
+          
+          /* Now letting Jalview convert these URIs.
+          // convert jalviewX://... URLs
+          if (HttpUtils.isJalviewSchemeUri(filename)) {
+            filename = HttpUtils.equivalentJalviewUrl(filename);
+          }
+          */
+
+          // check for locator file.  Only allow one locator file to be double clicked (if multiple files opened, ignore locator files)
+          if (filename.toLowerCase(Locale.ROOT).endsWith("."+Application.LOCATOR_FILE_EXTENSION)) {
+            // convert jalviewX://... JVL files
+            setLocatorFile(HttpUtils.equivalentJalviewUrl(filename));
+          } else {
+            // skip any other locator files in a multiple file list
             addStartupFile(filename);
           }
         }
       }
     }
-    
+        
+    public static String getStartupFilesParameterString(List<String> parameters, boolean changeJalviewSchemeUris) {
+      StringBuilder sb = new StringBuilder();
+      boolean first = true;
+      for(String f : parameters) {
+        if (first) {
+          first = false;
+        } else {
+          sb.append(' ');
+        }
+        String p = changeJalviewSchemeUris ? HttpUtils.equivalentJalviewUrl(f) : f;
+        if (p.contains(" ")) {
+          sb.append('"');
+          sb.append(p);
+          sb.append('"');
+        } else {
+          sb.append(p);
+        }
+      }
+      return sb.toString();
+    }
+
     public static void setLocatorFile(String filename) {
-      _locatorFile = new File(filename);
+      _locatorFile = filename;
     }
     
     public static void addStartupFile(String filename) {
-      _startupFiles.add(new File(filename));
+      _startupFiles.add(filename);
     }
     
     public static void setJalviewUri(String uri) {
@@ -2043,8 +2020,17 @@ public class Application
       try {
         Config tmpConfig = null;
         Map<String, Object> tmpData = new HashMap<>();
-        if (_locatorFile.exists()) {
-          tmpConfig = Config.parseConfig(_locatorFile,  opts);
+        if (HttpUtils.startsWithHttpOrHttps(_locatorFile)) {
+          URL locatorUrl = new URL(_locatorFile);
+          try (InputStream in = ConnectionUtil.open(proxy, locatorUrl, 0, 0).getInputStream();
+                 InputStreamReader reader = new InputStreamReader(in, UTF_8);
+                 BufferedReader bin = new BufferedReader(reader)) {
+                   tmpConfig = Config.parseConfig(bin, opts);
+          }
+        } else if (new File(_locatorFile).exists()) {
+          tmpConfig = Config.parseConfig(new File(_locatorFile),  opts);
+        }
+        if (tmpConfig != null) {
           // appbase is sanitised in HostWhitelist
           Map<String, Object> tmpConfigData = tmpConfig.getData();
           if (tmpConfig != null) {
@@ -2141,8 +2127,8 @@ public class Application
     protected static final String ENV_VAR_PREFIX = "%ENV.";
     protected static final Pattern ENV_VAR_PATTERN = Pattern.compile("%ENV\\.(.*?)%");
  
-    protected static File _locatorFile;
-    protected static List<File> _startupFiles = new ArrayList<>();
+    protected static String _locatorFile;
+    protected static List<String> _startupFiles = new ArrayList<>();
     protected static String _jalviewUri;
     public static final String LOCATOR_FILE_EXTENSION = "jvl";