From 5be594c57aa6a0f39c7bc4fc3de67fa53f625cdb Mon Sep 17 00:00:00 2001 From: Ben Soares Date: Tue, 28 May 2024 13:03:31 +0100 Subject: [PATCH] JAL-4409 Don't insist on HttpURLConnection, allows for file:/ protocol connection --- src/jalview/analytics/Plausible.java | 6 ++++-- src/jalview/ext/ensembl/EnsemblRestClient.java | 3 ++- src/jalview/ext/pymol/PymolManager.java | 3 ++- src/jalview/util/HttpUtils.java | 9 +++++++-- src/jalview/ws/dbsources/Uniprot.java | 3 ++- src/jalview/ws/ebi/EBIFetchClient.java | 3 ++- test/jalview/util/HttpUtilsTest.java | 3 ++- 7 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/jalview/analytics/Plausible.java b/src/jalview/analytics/Plausible.java index 2852734..bff0448 100644 --- a/src/jalview/analytics/Plausible.java +++ b/src/jalview/analytics/Plausible.java @@ -213,7 +213,8 @@ public class Plausible try { URL url = new URL(urlSb.toString()); - HttpURLConnection httpURLConnection = HttpUtils.openConnection(url); + HttpURLConnection httpURLConnection = (HttpURLConnection) HttpUtils + .openConnection(url); httpURLConnection.setRequestMethod("POST"); httpURLConnection.setDoOutput(true); @@ -552,7 +553,8 @@ public class Plausible try { URL url = new URL(CONFIG_API_BASE_URL); - HttpURLConnection httpURLConnection = HttpUtils.openConnection(url); + HttpURLConnection httpURLConnection = (HttpURLConnection) HttpUtils + .openConnection(url); httpURLConnection.setRequestMethod("GET"); httpURLConnection.setRequestProperty("User-Agent", USER_AGENT); httpURLConnection.setConnectTimeout(5000); diff --git a/src/jalview/ext/ensembl/EnsemblRestClient.java b/src/jalview/ext/ensembl/EnsemblRestClient.java index f7651be..3cc778a 100644 --- a/src/jalview/ext/ensembl/EnsemblRestClient.java +++ b/src/jalview/ext/ensembl/EnsemblRestClient.java @@ -343,7 +343,8 @@ abstract class EnsemblRestClient extends EnsemblSequenceFetcher { // jalview.bin.Console.outPrintln(System.currentTimeMillis() + " " + url); - HttpURLConnection connection = HttpUtils.openConnection(url); + HttpURLConnection connection = (HttpURLConnection) HttpUtils + .openConnection(url); /* * POST method allows multiple queries in one request; it is supported for diff --git a/src/jalview/ext/pymol/PymolManager.java b/src/jalview/ext/pymol/PymolManager.java index 05c5053..80327b8 100644 --- a/src/jalview/ext/pymol/PymolManager.java +++ b/src/jalview/ext/pymol/PymolManager.java @@ -168,7 +168,8 @@ public class PymolManager try { URL realUrl = new URL(rpcUrl); - HttpURLConnection conn = HttpUtils.openConnection(realUrl); + HttpURLConnection conn = (HttpURLConnection) HttpUtils + .openConnection(realUrl); conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("content-type", "text/xml"); conn.setDoOutput(true); diff --git a/src/jalview/util/HttpUtils.java b/src/jalview/util/HttpUtils.java index 04a820d..d6e4c14 100644 --- a/src/jalview/util/HttpUtils.java +++ b/src/jalview/util/HttpUtils.java @@ -28,6 +28,7 @@ import java.net.ProtocolException; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; +import java.net.URLConnection; public class HttpUtils { @@ -149,13 +150,13 @@ public class HttpUtils * url * @return HttpUrlConnection conn */ - public static HttpURLConnection openConnection(URL url) throws IOException + public static URLConnection openConnection(URL url) throws IOException { if (url == null) { return null; } - HttpURLConnection conn = null; + URLConnection conn = null; String protocol = url.getProtocol(); if ("http".equals(protocol) || "https".equals(protocol)) { @@ -169,6 +170,10 @@ public class HttpUtils conn = conn0; } } + else + { + conn = url.openConnection(); + } return conn; } diff --git a/src/jalview/ws/dbsources/Uniprot.java b/src/jalview/ws/dbsources/Uniprot.java index b493611..89c92d4 100644 --- a/src/jalview/ws/dbsources/Uniprot.java +++ b/src/jalview/ws/dbsources/Uniprot.java @@ -149,7 +149,8 @@ public class Uniprot extends DbSourceProxyImpl + ".xml"; URL url = new URL(downloadstring); - HttpURLConnection urlconn = HttpUtils.openConnection(url); + HttpURLConnection urlconn = (HttpURLConnection) HttpUtils + .openConnection(url); // anything other than 200 means we don't have data // TODO: JAL-3882 reuse the EnsemblRestClient's fair // use/backoff logic to retry when the server tells us to go away diff --git a/src/jalview/ws/ebi/EBIFetchClient.java b/src/jalview/ws/ebi/EBIFetchClient.java index 87526a1..5a5e9ae 100644 --- a/src/jalview/ws/ebi/EBIFetchClient.java +++ b/src/jalview/ws/ebi/EBIFetchClient.java @@ -210,7 +210,8 @@ public class EBIFetchClient try { URL rcall = new URL(url); - HttpURLConnection conn = HttpUtils.openConnection(rcall); + HttpURLConnection conn = (HttpURLConnection) HttpUtils + .openConnection(rcall); int responseCode = conn.getResponseCode(); if (responseCode == 200) { diff --git a/test/jalview/util/HttpUtilsTest.java b/test/jalview/util/HttpUtilsTest.java index fbd8149..d8b757d 100644 --- a/test/jalview/util/HttpUtilsTest.java +++ b/test/jalview/util/HttpUtilsTest.java @@ -32,7 +32,8 @@ public class HttpUtilsTest { URL tUrl = new URL(targetUrl); URL fUrl = new URL(finalUrl); - HttpURLConnection conn1 = HttpUtils.openConnection(tUrl); + HttpURLConnection conn1 = (HttpURLConnection) HttpUtils + .openConnection(tUrl); URL url1 = conn1.getURL(); Assert.assertEquals(url1, fUrl, "Final URL is not the same."); } -- 1.7.10.2