mirror of
https://github.com/eclipse-sailing-analytics/sailing-analytics.git
synced 2026-09-30 01:16:40 +00:00
this partly reverts a "refactoring" that was none (938e1e8cbc) which replaced
an explicit 60s timeout with a util call that uses 10s
Change-Id: I1dbda0314550dcc77c7d69c3513d1de8430eae07
38 lines
1.5 KiB
Java
38 lines
1.5 KiB
Java
package com.sap.sse.util;
|
|
|
|
import java.io.IOException;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.MalformedURLException;
|
|
import java.net.URL;
|
|
|
|
import com.sap.sse.common.Duration;
|
|
|
|
public class HttpUrlConnectionHelper {
|
|
private static final int HTTP_MAX_REDIRECTS = 5;
|
|
|
|
public static HttpURLConnection redirectConnection(URL url) throws MalformedURLException, IOException {
|
|
HttpURLConnection connection = null;
|
|
URL nextUrl = url;
|
|
for (int counterOfRedirects = 0; counterOfRedirects <= HTTP_MAX_REDIRECTS; counterOfRedirects++) {
|
|
if (connection != null) {
|
|
connection.disconnect();
|
|
}
|
|
connection = (HttpURLConnection) nextUrl.openConnection();
|
|
connection.setInstanceFollowRedirects(false);
|
|
connection.setRequestProperty("User-Agent", "Mozilla/5.0...");
|
|
connection.setDoOutput(true);
|
|
// Initial timeout needs to be big enough to allow the first parts of the response to reach this server
|
|
connection.setReadTimeout((int) Duration.ONE_MINUTE.times(10).asMillis());
|
|
if (connection.getResponseCode() == HttpURLConnection.HTTP_MOVED_PERM
|
|
|| connection.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP) {
|
|
String location = connection.getHeaderField("Location");
|
|
nextUrl = new URL(nextUrl, location);
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
return connection;
|
|
}
|
|
|
|
}
|