Merge branch 'develop' into Release_2_9_0b1_Branch
[jalview.git] / src / jalview / bin / JalviewLite.java
index 30b8d54..4f5c4ee 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
- * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9)
+ * Copyright (C) 2015 The Jalview Authors
  * 
  * This file is part of Jalview.
  * 
@@ -48,6 +48,7 @@ import jalview.javascript.JsCallBack;
 import jalview.javascript.MouseOverStructureListener;
 import jalview.structure.SelectionListener;
 import jalview.structure.StructureSelectionManager;
+import jalview.util.HttpUtils;
 import jalview.util.MessageManager;
 
 import java.applet.Applet;
@@ -62,8 +63,6 @@ import java.awt.event.ActionEvent;
 import java.awt.event.WindowAdapter;
 import java.awt.event.WindowEvent;
 import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.net.URL;
 import java.util.ArrayList;
@@ -1820,7 +1819,7 @@ public class JalviewLite extends Applet implements
        */
       URL documentBase = getDocumentBase();
       String withDocBase = resolveUrlForLocalOrAbsolute(path, documentBase);
-      if (urlExists(withDocBase))
+      if (HttpUtils.isValidUrl(withDocBase))
       {
         if (debug)
         {
@@ -1837,7 +1836,8 @@ public class JalviewLite extends Applet implements
       URL codeBase = getCodeBase();
       String withCodeBase = applet.resolveUrlForLocalOrAbsolute(path,
               codeBase);
-      if (!withCodeBase.equals(withDocBase) && urlExists(withCodeBase))
+      if (!withCodeBase.equals(withDocBase)
+              && HttpUtils.isValidUrl(withCodeBase))
       {
         protocol = AppletFormatAdapter.URL;
         if (debug)
@@ -2409,92 +2409,6 @@ public class JalviewLite extends Applet implements
         return false;
       }
     }
-
-    /**
-     * If the file is not already in URL format, tries to locate it by resolving
-     * as a URL.
-     * 
-     * @param f
-     * @return
-     */
-    String addProtocol(final String f)
-    {
-      if (f.indexOf("://") != -1)
-      {
-        // already has URL format
-        return f;
-      }
-
-      /*
-       * Try relative to document base
-       */
-      URL documentBase = getDocumentBase();
-      System.err.println("Trying documentbase: " + documentBase);
-      String url = applet.resolveUrlForLocalOrAbsolute(f, documentBase);
-      if (urlExists(url))
-      {
-        if (true/* debug */)
-        {
-          System.err.println("Prepended document base '" + documentBase
-                  + "' to make: '" + url + "'");
-        }
-        return url;
-      }
-
-      /*
-       * Try relative to codebase
-       */
-      URL codeBase = getCodeBase();
-      System.err.println("Trying codebase: " + codeBase);
-      url = applet.resolveUrlForLocalOrAbsolute(f, codeBase);
-      if (urlExists(url))
-      {
-        if (true/* debug */)
-        {
-          System.err.println("Prepended codebase '" + codeBase
-                  + "' to make: '" + url + "'");
-        }
-        return url;
-      }
-
-      return f;
-    }
-
-    /**
-     * Returns true if an input stream can be opened on the specified URL, else
-     * false.
-     * 
-     * @param url
-     * @return
-     */
-    private boolean urlExists(String url)
-    {
-      InputStream is = null;
-      try
-      {
-        is = new URL(url).openStream();
-        if (is != null)
-        {
-          return true;
-        }
-      } catch (Exception x)
-      {
-        // ignore
-      } finally
-      {
-        if (is != null)
-        {
-          try
-          {
-            is.close();
-          } catch (IOException e)
-          {
-            // ignore
-          }
-        }
-      }
-      return false;
-    }
   }
 
   /**
@@ -2946,6 +2860,15 @@ public class JalviewLite extends Applet implements
           URL localref)
   {
     String resolvedPath = "";
+    if (targetPath.startsWith("/"))
+    {
+      String codebase = localref.toString();
+      String localfile = localref.getFile();
+      resolvedPath = codebase.substring(0,
+              codebase.length() - localfile.length())
+              + targetPath;
+      return resolvedPath;
+    }
 
     /*
      * get URL path and strip off any trailing file e.g.