From 0ced7b4563168073ce2e385c96309b147b3c7dbd Mon Sep 17 00:00:00 2001 From: rctauber Date: Tue, 16 Jul 2019 10:39:00 -0700 Subject: [PATCH 1/2] Follow redirects for gzip input IRI --- .../java/org/obolibrary/robot/IOHelper.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/robot-core/src/main/java/org/obolibrary/robot/IOHelper.java b/robot-core/src/main/java/org/obolibrary/robot/IOHelper.java index 05a5e457f..ec0fe1f1a 100644 --- a/robot-core/src/main/java/org/obolibrary/robot/IOHelper.java +++ b/robot-core/src/main/java/org/obolibrary/robot/IOHelper.java @@ -8,6 +8,7 @@ import com.github.jsonldjava.utils.JsonUtils; import com.google.common.collect.Sets; import java.io.*; +import java.net.HttpURLConnection; import java.net.URL; import java.util.HashMap; import java.util.HashSet; @@ -1118,6 +1119,38 @@ private void addPrefixes(OWLDocumentFormat df, Map addPrefixes) } } + /** + * Given a URL, check if the URL returns a redirect and return that new URL. Continue following + * redirects until status is HTTP_OK. + * + * @param url URL to follow redirects + * @return URL after all redirects + * @throws IOException on issue making URL connection + */ + private URL followRedirects(URL url) throws IOException { + // Check if the URL redirects + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + int status = conn.getResponseCode(); + boolean redirect = false; + if (status != HttpURLConnection.HTTP_OK) { + if (status == HttpURLConnection.HTTP_MOVED_TEMP + || status == HttpURLConnection.HTTP_MOVED_PERM + || status == HttpURLConnection.HTTP_SEE_OTHER) { + redirect = true; + } + } + + if (redirect) { + // Get the new URL and then check that for redirect + String newURL = conn.getHeaderField("Location"); + logger.info(String.format("<%s> redirecting to <%s>...", url.toString(), newURL)); + return followRedirects(new URL(newURL)); + } else { + // Otherwise just return the URL + return url; + } + } + /** * Given an ontology, a document format, and a boolean indicating to check OBO formatting, return * the ontology file in the OWLDocumentFormat as a byte array. @@ -1184,6 +1217,10 @@ private OWLOntology loadCompressedOntology(File gzipFile, String catalogPath) th * @throws IOException on any problem */ private OWLOntology loadCompressedOntology(URL url, String catalogPath) throws IOException { + // Check for redirects + url = followRedirects(url); + + // Open an input stream InputStream is; try { is = new BufferedInputStream(url.openStream(), 1024); From 8b5879f4cbfa6eb0c04e162da7ea30de554a698d Mon Sep 17 00:00:00 2001 From: rctauber Date: Tue, 16 Jul 2019 10:47:52 -0700 Subject: [PATCH 2/2] Handle FTP --- .../main/java/org/obolibrary/robot/IOHelper.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/robot-core/src/main/java/org/obolibrary/robot/IOHelper.java b/robot-core/src/main/java/org/obolibrary/robot/IOHelper.java index ec0fe1f1a..b5fb450be 100644 --- a/robot-core/src/main/java/org/obolibrary/robot/IOHelper.java +++ b/robot-core/src/main/java/org/obolibrary/robot/IOHelper.java @@ -1121,7 +1121,7 @@ private void addPrefixes(OWLDocumentFormat df, Map addPrefixes) /** * Given a URL, check if the URL returns a redirect and return that new URL. Continue following - * redirects until status is HTTP_OK. + * redirects until there are no more redirects. * * @param url URL to follow redirects * @return URL after all redirects @@ -1129,6 +1129,10 @@ private void addPrefixes(OWLDocumentFormat df, Map addPrefixes) */ private URL followRedirects(URL url) throws IOException { // Check if the URL redirects + if (url.toString().startsWith("ftp")) { + // Trying to open HttpURLConnection on FTP will throw exception + return url; + } HttpURLConnection conn = (HttpURLConnection) url.openConnection(); int status = conn.getResponseCode(); boolean redirect = false; @@ -1144,7 +1148,13 @@ private URL followRedirects(URL url) throws IOException { // Get the new URL and then check that for redirect String newURL = conn.getHeaderField("Location"); logger.info(String.format("<%s> redirecting to <%s>...", url.toString(), newURL)); - return followRedirects(new URL(newURL)); + if (newURL.startsWith("ftp")) { + // No more redirects + return new URL(newURL); + } else { + // Check again if there is another redirect + return followRedirects(new URL(newURL)); + } } else { // Otherwise just return the URL return url;