diff --git a/src/main/java/org/bioimageanalysis/icy/deeplearning/versionmanager/downloading/DeepLearningVersionDownloader.java b/src/main/java/org/bioimageanalysis/icy/deeplearning/versionmanager/downloading/DeepLearningVersionDownloader.java index e55cfc83420891ed635665c56e7200d96560ebf8..8f4ef749d550c38440eae3f85d13c4004e216e74 100644 --- a/src/main/java/org/bioimageanalysis/icy/deeplearning/versionmanager/downloading/DeepLearningVersionDownloader.java +++ b/src/main/java/org/bioimageanalysis/icy/deeplearning/versionmanager/downloading/DeepLearningVersionDownloader.java @@ -7,6 +7,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.net.HttpURLConnection; import java.net.MalformedURLException; +import java.net.URISyntaxException; import java.net.URL; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; @@ -261,7 +262,8 @@ public class DeepLearningVersionDownloader { HttpURLConnection httpConnection = (HttpURLConnection) (url.openConnection()); while (httpConnection.getResponseCode() == 302) { - url = new URL(httpConnection.getHeaderField("Location")); + // TODO use method from JDLL + url = redirectedURL(url, httpConnection); httpConnection = (HttpURLConnection) (url.openConnection()); } long completeFileSize = httpConnection.getContentLengthLong(); @@ -297,6 +299,36 @@ public class DeepLearningVersionDownloader } return localFilePath; } + + /** + * TODO replae and use method from JDLL + * @param url + * @param conn + * @return + */ + public static URL redirectedURL(URL url, HttpURLConnection conn) { + int statusCode; + try { + statusCode = conn.getResponseCode(); + } catch (IOException ex) { + return url; + } + if (statusCode != HttpURLConnection.HTTP_MOVED_TEMP + && statusCode != HttpURLConnection.HTTP_MOVED_PERM + && statusCode != HttpURLConnection.HTTP_SEE_OTHER) + return url; + String newURL = conn.getHeaderField("Location"); + try { + return new URL(newURL); + } catch (MalformedURLException ex) { + } + try { + String mainDomain = url.toURI().getHost(); + return new URL(mainDomain + newURL); + } catch (URISyntaxException | MalformedURLException e) { + return null; + } + } /** * Retrieves the paths of all resources associated to a given version resolved locally.