package com.munzenberger.feed.util;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.zip.GZIPInputStream;

public final class URLProcessor {

	private URLProcessor() {}

	public static InputStream getInputStream(URL url) throws IOException {

		URLConnection conn = url.openConnection();
		return getInputStream(conn);
	}

	private static InputStream getInputStream(URLConnection conn) throws IOException {

		// handle redirects
		if (conn instanceof HttpURLConnection) {
			int response = ((HttpURLConnection)conn).getResponseCode();
			if (response == HttpURLConnection.HTTP_MOVED_PERM ||
					response == HttpURLConnection.HTTP_MOVED_TEMP ||
					response == HttpURLConnection.HTTP_SEE_OTHER ||
					response == 307 ||
					response == 308) {

				String location = conn.getHeaderField("Location");
				if (location != null) {
					return getInputStream(new URL(location));
				}
			}
		}

		String contentEncoding = conn.getHeaderField("Content-Encoding");

		InputStream in = conn.getInputStream();

		// handle gzip encoded responses
		if ("gzip".equalsIgnoreCase(contentEncoding)) {
			in = new GZIPInputStream(in);
		}

		return in;
	}
}

@@ -165,32 +165,80 @@
 .URLConnection;%0A
+import java.util.HashSet;%0Aimport java.util.Set;%0A
 import java.util
@@ -295,16 +295,392 @@
 ssor %7B%0A%0A
+%09private static final Set%3CInteger%3E redirectCodes = new HashSet%3C%3E();%0A%0A%09static %7B%0A%09%09redirectCodes.add(HttpURLConnection.HTTP_MOVED_PERM);%0A%09%09redirectCodes.add(HttpURLConnection.HTTP_MOVED_TEMP);%0A%09%09redirectCodes.add(HttpURLConnection.HTTP_SEE_OTHER);%0A%09%09redirectCodes.add(307);  // Temporary Redirect (since HTTP/1.1)%0A%09%09redirectCodes.add(308);  // Permanent Redirect (RFC 7538)%0A%09%7D%0A%0A
 %09private
@@ -1078,199 +1078,37 @@
  (re
-sponse == HttpURLConnection.HTTP_MOVED_PERM %7C%7C%0A%09%09%09%09%09response == HttpURLConnection.HTTP_MOVED_TEMP %7C%7C%0A%09%09%09%09%09response == HttpURLConnection.HTTP_SEE_OTHER %7C%7C%0A%09%09%09%09%09response == 307 %7C%7C%0A%09%09%09%09%09
+directCodes.contains(
 response
  == 
@@ -1107,20 +1107,13 @@
 onse
- == 308
+)
 ) %7B%0A
-%0A
 %09%09%09%09

declare the possible redirect codes in a Set