Skip to content
Snippets Groups Projects
Commit 1c8100d3 authored by carlosuc3m's avatar carlosuc3m
Browse files

add redirection of URLs

parent 69afd529
No related branches found
No related tags found
No related merge requests found
...@@ -7,6 +7,7 @@ import java.io.FileOutputStream; ...@@ -7,6 +7,7 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.nio.channels.Channels; import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel; import java.nio.channels.ReadableByteChannel;
...@@ -261,7 +262,8 @@ public class DeepLearningVersionDownloader ...@@ -261,7 +262,8 @@ public class DeepLearningVersionDownloader
{ {
HttpURLConnection httpConnection = (HttpURLConnection) (url.openConnection()); HttpURLConnection httpConnection = (HttpURLConnection) (url.openConnection());
while (httpConnection.getResponseCode() == 302) { while (httpConnection.getResponseCode() == 302) {
url = new URL(httpConnection.getHeaderField("Location")); // TODO use method from JDLL
url = redirectedURL(url, httpConnection);
httpConnection = (HttpURLConnection) (url.openConnection()); httpConnection = (HttpURLConnection) (url.openConnection());
} }
long completeFileSize = httpConnection.getContentLengthLong(); long completeFileSize = httpConnection.getContentLengthLong();
...@@ -297,6 +299,36 @@ public class DeepLearningVersionDownloader ...@@ -297,6 +299,36 @@ public class DeepLearningVersionDownloader
} }
return localFilePath; 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. * Retrieves the paths of all resources associated to a given version resolved locally.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment