JAL-1054 Fix wrappers for following http->https redirects
authorBen Soares <b.soares@dundee.ac.uk>
Mon, 13 May 2024 16:12:49 +0000 (17:12 +0100)
committerBen Soares <b.soares@dundee.ac.uk>
Mon, 13 May 2024 16:12:49 +0000 (17:12 +0100)
src/jalview/bin/Cache.java
src/jalview/util/HttpUtils.java

index 136e2f7..48a0a58 100755 (executable)
@@ -498,7 +498,7 @@ public class Cache
       if (authorDetails != null)
       {
         URL localJarFileURL = new URL(authorDetails);
-        InputStream in = HttpUtils.openStream(localJarFileURL);
+        InputStream in = localJarFileURL.openStream();
         applicationProperties.load(in);
         in.close();
       }
index 6ecfab2..9fef54e 100644 (file)
@@ -105,71 +105,72 @@ public class HttpUtils
   /**
    * wrapper to follow a URL connection ALLOWING redirects from http to https
    * 
-   * @param URL
-   *          url
+   * @param HttpURLConnection
+   *          conn0
    * @return HttpUrlConnection conn
    */
-  public static HttpURLConnection openConnection(URL url) throws IOException
+  public static HttpURLConnection followConnection(HttpURLConnection conn0)
+          throws IOException
   {
+    URL url = conn0.getURL();
     if (url == null)
     {
       return null;
     }
     HttpURLConnection conn = null;
-    if (url != null && url.getProtocol().startsWith("http"))
+    int response = conn0.getResponseCode();
+    boolean followed = false;
+    if (response >= 300 && response < 400 && conn0.getFollowRedirects())
     {
-      HttpURLConnection conn0 = (HttpURLConnection) url.openConnection();
-      if (conn0 != null)
-      {
-        conn = HttpUtils.followConnection(conn0);
-      }
-      else
+      // we are only checking for a redirect from http to https
+      if ("http".equals(url.getProtocol()))
       {
-        conn = conn0;
+        URL loc = new URL(conn0.getHeaderField("Location"));
+        if (loc != null && "https".equals(loc.getProtocol()))
+        {
+          conn = (HttpURLConnection) loc.openConnection();
+          conn.setRequestMethod(conn0.getRequestMethod());
+          conn.setDoInput(conn0.getDoInput());
+          conn.setUseCaches(conn0.getUseCaches());
+          conn.setConnectTimeout(conn0.getConnectTimeout());
+          conn.setReadTimeout(conn0.getReadTimeout());
+          conn.setInstanceFollowRedirects(
+                  conn0.getInstanceFollowRedirects());
+          followed = true;
+        }
       }
     }
-    return conn;
+    return followed && conn != null ? conn : conn0;
   }
 
   /**
    * wrapper to follow a URL connection ALLOWING redirects from http to https
    * 
-   * @param HttpURLConnection
-   *          conn0
+   * @param URL
+   *          url
    * @return HttpUrlConnection conn
    */
-  public static HttpURLConnection followConnection(HttpURLConnection conn0)
-          throws IOException
+  public static HttpURLConnection openConnection(URL url) throws IOException
   {
-    HttpURLConnection newConn = null;
-    URL url = conn0.getURL();
     if (url == null)
     {
       return null;
     }
-    int response = conn0.getResponseCode();
-    boolean followed = false;
-    if (response >= 300 && response < 400 && conn0.getFollowRedirects())
+    HttpURLConnection conn = null;
+    String protocol = url.getProtocol();
+    if ("http".equals(protocol) || "https".equals(protocol))
     {
-      // we are only checking for a redirect from http to https
-      if ("http".equals(url.getProtocol()))
+      HttpURLConnection conn0 = (HttpURLConnection) url.openConnection();
+      if (conn0 != null)
       {
-        URL loc = new URL(conn0.getHeaderField("Location"));
-        if (loc != null && "https".equals(loc.getProtocol()))
-        {
-          newConn = (HttpURLConnection) loc.openConnection();
-          newConn.setRequestMethod(conn0.getRequestMethod());
-          newConn.setDoInput(conn0.getDoInput());
-          newConn.setUseCaches(conn0.getUseCaches());
-          newConn.setConnectTimeout(conn0.getConnectTimeout());
-          newConn.setReadTimeout(conn0.getReadTimeout());
-          newConn.setInstanceFollowRedirects(
-                  conn0.getInstanceFollowRedirects());
-          followed = true;
-        }
+        conn = HttpUtils.followConnection(conn0);
+      }
+      else
+      {
+        conn = conn0;
       }
     }
-    return followed ? newConn : conn0;
+    return conn;
   }
 
   /**
@@ -182,8 +183,13 @@ public class HttpUtils
    */
   public static InputStream openStream(URL url) throws IOException
   {
+    if (url == null)
+    {
+      return null;
+    }
     InputStream is = null;
-    if (url != null && url.getProtocol().startsWith("http"))
+    String protocol = url.getProtocol();
+    if ("http".equals(protocol) || "https".equals(protocol))
     {
       HttpURLConnection conn = HttpUtils
               .followConnection((HttpURLConnection) url.openConnection());
@@ -192,6 +198,10 @@ public class HttpUtils
         is = conn.getInputStream();
       }
     }
+    else
+    {
+      is = url.openStream();
+    }
     return is;
   }