Skip to content

Commit

Permalink
Merge pull request #537 from rctauber/536-gzip-fix
Browse files Browse the repository at this point in the history
Follow redirects for gzip input IRIs
  • Loading branch information
jamesaoverton authored Jul 17, 2019
2 parents 7b5759e + 8b5879f commit 840bfd5
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions robot-core/src/main/java/org/obolibrary/robot/IOHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1118,6 +1119,48 @@ private void addPrefixes(OWLDocumentFormat df, Map<String, String> addPrefixes)
}
}

/**
* Given a URL, check if the URL returns a redirect and return that new URL. Continue following
* redirects until there are no more redirects.
*
* @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
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;
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));
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;
}
}

/**
* 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.
Expand Down Expand Up @@ -1184,6 +1227,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);
Expand Down

0 comments on commit 840bfd5

Please sign in to comment.