public static HttpURLConnection followConnection(HttpURLConnection conn0)
throws IOException
{
+ return followConnection(conn0, false);
+ }
+
+ public static HttpURLConnection followConnection(HttpURLConnection conn0,
+ boolean followAnyway) throws IOException
+ {
URL url = conn0.getURL();
// 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())
+ if (url == null)
+ {
+ return conn0;
+ }
+ if (!conn0.getInstanceFollowRedirects())
+ {
+ return conn0;
+ }
+ if (!"http".equals(url.getProtocol()) && !followAnyway)
{
return conn0;
}
url = loc;
}
}
+ else if (followAnyway)
+ {
+ // checkConn might have followed a https->https redirect
+ url = checkConn.getURL();
+ }
- if (!redirectToHttps)
+ if (!redirectToHttps && !followAnyway)
{
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
+ // We want to return an HttpURLConnection to the new (probably https) URL
+ // that is unconnected in case further manipulation of the request is
+ // required.
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
httpURLConnectionCopyAttributes(conn0, conn);
return conn;
*/
public static URLConnection openConnection(URL url) throws IOException
{
+ return openConnection(url, false);
+ }
+
+ public static URLConnection openConnection(URL url, boolean followAnyway)
+ throws IOException
+ {
if (url == null)
{
Console.debug("HttpUtils.openConnection(url) called with null url");
HttpURLConnection conn0 = (HttpURLConnection) url.openConnection();
if (conn0 != null)
{
- conn = HttpUtils.followConnection(conn0);
+ conn = HttpUtils.followConnection(conn0, followAnyway);
}
else
{
*/
public static InputStream openStream(URL url) throws IOException
{
+ return openStream(url, false);
+ }
+
+ public static InputStream openStream(URL url, boolean followAnyway)
+ throws IOException
+ {
if (url == null)
{
return null;
String protocol = url.getProtocol();
if ("http".equals(protocol) || "https".equals(protocol))
{
- HttpURLConnection conn = HttpUtils
- .followConnection((HttpURLConnection) url.openConnection());
+ HttpURLConnection conn = HttpUtils.followConnection(
+ (HttpURLConnection) url.openConnection(), followAnyway);
if (conn != null)
{
is = conn.getInputStream();
{
@Test(groups = { "Network" }, dataProvider = "urlTargetsAndDestinations")
public void testFollowConnection(String targetUrl, String finalUrl,
- String notUsed0, String notUsed1) throws IOException
+ String notUsed0, String notUsed1, boolean followAnyway)
+ throws IOException
{
URL tUrl = new URL(targetUrl);
URL fUrl = new URL(finalUrl);
- HttpURLConnection conn1 = HttpUtils
- .followConnection((HttpURLConnection) tUrl.openConnection());
+ HttpURLConnection conn1 = HttpUtils.followConnection(
+ (HttpURLConnection) tUrl.openConnection(), followAnyway);
URL url1 = conn1.getURL();
Assert.assertEquals(url1, fUrl, "Final URL is not the same.");
}
@Test(groups = { "Network" }, dataProvider = "urlTargetsAndDestinations")
public void testOpenConnection(String targetUrl, String finalUrl,
- String notUsed0, String notUsed1) throws IOException
+ String notUsed0, String notUsed1, boolean followAnyway)
+ throws IOException
{
URL tUrl = new URL(targetUrl);
URL fUrl = new URL(finalUrl);
HttpURLConnection conn1 = (HttpURLConnection) HttpUtils
- .openConnection(tUrl);
+ .openConnection(tUrl, followAnyway);
URL url1 = conn1.getURL();
Assert.assertEquals(url1, fUrl, "Final URL is not the same.");
}
@Test(groups = { "Network" }, dataProvider = "urlTargetsAndDestinations")
public void testOpenStream(String targetUrl, String finalUrl,
- String inFirstLine, String inDocument) throws IOException
+ String inFirstLine, String inDocument, boolean followAnyway)
+ throws IOException
{
URL tUrl = new URL(targetUrl);
URL fUrl = new URL(finalUrl);
- InputStream is1 = HttpUtils.openStream(tUrl);
+ InputStream is1 = HttpUtils.openStream(tUrl, followAnyway);
BufferedReader br1 = new BufferedReader(new InputStreamReader(is1));
String firstLine = br1.readLine().toLowerCase(Locale.ROOT);
Assert.assertTrue(
String finalUrl, // the URL you end up at
String foundInFirstLine, // some text found in the first line
String foundInDocument, // some text found in the document (and won't be in an error page)
+ boolean followAnyway, // whether to follow redirects even if they're not http->https
*/
return new Object[][] {
//
/*
*/
{ "http://jalview.org/", "https://www.jalview.org/", "<!doctype",
- "Jalview is a" },
+ "Jalview is a", false },
{ "http://www.jalview.org/", "https://www.jalview.org/",
- "<!doctype", "Jalview is a" },
+ "<!doctype", "Jalview is a", false },
+ { "https://jalview.org/", "https://jalview.org/", "<!doctype",
+ "Jalview is a", false },
+ { "http://jalview.org/", "https://www.jalview.org/", "<!doctype",
+ "Jalview is a", true },
+ { "http://www.jalview.org/", "https://www.jalview.org/",
+ "<!doctype", "Jalview is a", true },
{ "https://jalview.org", "https://www.jalview.org/", "<!doctype",
- "Jalview is a" },
+ "Jalview is a", true },
/*
*/
//