/**
* wrapper to follow a URL connection ALLOWING redirects from http to https
*
- * @param URL
- * url
+ * @param HttpURLConnection
+ * conn0
* @return HttpUrlConnection conn
*/
- public static HttpURLConnection openConnection(URL url) throws IOException
+ public static HttpURLConnection followConnection(HttpURLConnection conn0)
+ throws IOException
{
+ URL url = conn0.getURL();
if (url == null)
{
return null;
}
HttpURLConnection conn = null;
- if (url != null && url.getProtocol().startsWith("http"))
+ int response = conn0.getResponseCode();
+ boolean followed = false;
+ if (response >= 300 && response < 400 && conn0.getFollowRedirects())
{
- HttpURLConnection conn0 = (HttpURLConnection) url.openConnection();
- if (conn0 != null)
- {
- conn = HttpUtils.followConnection(conn0);
- }
- else
+ // we are only checking for a redirect from http to https
+ if ("http".equals(url.getProtocol()))
{
- conn = conn0;
+ 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;
+ }
}
}
- return conn;
+ return followed && conn != null ? conn : conn0;
}
/**
* wrapper to follow a URL connection ALLOWING redirects from http to https
*
- * @param HttpURLConnection
- * conn0
+ * @param URL
+ * url
* @return HttpUrlConnection conn
*/
- public static HttpURLConnection followConnection(HttpURLConnection conn0)
- throws IOException
+ public static HttpURLConnection openConnection(URL url) throws IOException
{
- HttpURLConnection newConn = null;
- URL url = conn0.getURL();
if (url == null)
{
return null;
}
- int response = conn0.getResponseCode();
- boolean followed = false;
- if (response >= 300 && response < 400 && conn0.getFollowRedirects())
+ HttpURLConnection conn = null;
+ String protocol = url.getProtocol();
+ if ("http".equals(protocol) || "https".equals(protocol))
{
- // we are only checking for a redirect from http to https
- if ("http".equals(url.getProtocol()))
+ HttpURLConnection conn0 = (HttpURLConnection) url.openConnection();
+ if (conn0 != null)
{
- URL loc = new URL(conn0.getHeaderField("Location"));
- if (loc != null && "https".equals(loc.getProtocol()))
- {
- newConn = (HttpURLConnection) loc.openConnection();
- newConn.setRequestMethod(conn0.getRequestMethod());
- newConn.setDoInput(conn0.getDoInput());
- newConn.setUseCaches(conn0.getUseCaches());
- newConn.setConnectTimeout(conn0.getConnectTimeout());
- newConn.setReadTimeout(conn0.getReadTimeout());
- newConn.setInstanceFollowRedirects(
- conn0.getInstanceFollowRedirects());
- followed = true;
- }
+ conn = HttpUtils.followConnection(conn0);
+ }
+ else
+ {
+ conn = conn0;
}
}
- return followed ? newConn : conn0;
+ return conn;
}
/**
*/
public static InputStream openStream(URL url) throws IOException
{
+ if (url == null)
+ {
+ return null;
+ }
InputStream is = null;
- if (url != null && url.getProtocol().startsWith("http"))
+ String protocol = url.getProtocol();
+ if ("http".equals(protocol) || "https".equals(protocol))
{
HttpURLConnection conn = HttpUtils
.followConnection((HttpURLConnection) url.openConnection());
is = conn.getInputStream();
}
}
+ else
+ {
+ is = url.openStream();
+ }
return is;
}