import java.net.URL;
import java.net.URLConnection;
+import jalview.bin.Console;
+
public class HttpUtils
{
public final static String JALVIEWSCHEMEPREFIX = "jalview";
}
/**
- * wrapper to follow a URL connection ALLOWING redirects from http to https
+ * wrapper to return a new HttpURLConnection to a new URL when there is a
+ * redirect from http to https, otherwise return the unused original
+ * HttpURLConnection
*
* @param HttpURLConnection
* conn0
throws IOException
{
URL url = conn0.getURL();
- if (url == null)
+ // we are only checking for a redirect from http to https otherwise the java
+ // connection will follow when called (if not unset)
+ if (url == null || !"http".equals(url.getProtocol())
+ || !conn0.getInstanceFollowRedirects())
{
- return null;
+ return conn0;
}
- HttpURLConnection conn = null;
- int response = conn0.getResponseCode();
- boolean followed = false;
- if (response >= 300 && response < 400 && conn0.getFollowRedirects())
+
+ // check the response code
+ HttpURLConnection checkConn = (HttpURLConnection) url.openConnection();
+ httpURLConnectionCopyAttributes(conn0, checkConn);
+
+ boolean redirectToHttps = false;
+ int response = checkConn.getResponseCode();
+ checkConn.disconnect();
+ if (response >= 300 && response < 400)
{
// we are only checking for a redirect from http to https
- if ("http".equals(url.getProtocol()))
+ URL loc = new URL(conn0.getHeaderField("Location"));
+ if (loc != null && "https".equals(loc.getProtocol()))
{
- URL loc = new URL(conn0.getHeaderField("Location"));
- if (loc != null && "https".equals(loc.getProtocol()))
- {
- conn = (HttpURLConnection) loc.openConnection();
- conn.setRequestMethod(conn0.getRequestMethod());
- conn.setDoInput(conn0.getDoInput());
- conn.setUseCaches(conn0.getUseCaches());
- conn.setConnectTimeout(conn0.getConnectTimeout());
- conn.setReadTimeout(conn0.getReadTimeout());
- conn.setInstanceFollowRedirects(
- conn0.getInstanceFollowRedirects());
- followed = true;
- }
+ redirectToHttps = true;
+ url = loc;
}
}
- return followed && conn != null ? conn : conn0;
+
+ if (!redirectToHttps)
+ {
+ return conn0;
+ }
+
+ // We want to return an HttpURLConnection to the new https URL that is
+ // unconnected in case further manipulation of the request is required
+ HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+ httpURLConnectionCopyAttributes(conn0, conn);
+ return conn;
+ }
+
+ private static void httpURLConnectionCopyAttributes(
+ HttpURLConnection conn0, HttpURLConnection conn1)
+ throws ProtocolException
+ {
+ conn1.setRequestMethod(conn0.getRequestMethod());
+ conn1.setDoInput(conn0.getDoInput());
+ conn1.setUseCaches(conn0.getUseCaches());
+ conn1.setConnectTimeout(conn0.getConnectTimeout());
+ conn1.setReadTimeout(conn0.getReadTimeout());
+ conn1.setInstanceFollowRedirects(conn0.getInstanceFollowRedirects());
}
/**
{
if (url == null)
{
+ Console.debug("HttpUtils.openConnection(url) called with null url");
return null;
}
+ Console.debug("HttpUtils.openConnection(url) called with url="
+ + url.toString());
URLConnection conn = null;
String protocol = url.getProtocol();
if ("http".equals(protocol) || "https".equals(protocol))