diff --git a/pom.xml b/pom.xml index b088ea6..c6e2def 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ net.gravitydevelopment.updater updater - 2.3 + 2.4 jar Updater http://forums.bukkit.org/threads/updater-2-0-easy-safe-and-policy-compliant-auto-updating-for-your-plugins-new.96681/ diff --git a/src/main/java/net/gravitydevelopment/updater/Updater.java b/src/main/java/net/gravitydevelopment/updater/Updater.java index 901c48e..b86c64b 100644 --- a/src/main/java/net/gravitydevelopment/updater/Updater.java +++ b/src/main/java/net/gravitydevelopment/updater/Updater.java @@ -1,6 +1,7 @@ package net.gravitydevelopment.updater; import java.io.*; +import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; @@ -30,7 +31,7 @@ * If you are unsure about these rules, please read the plugin submission guidelines: http://goo.gl/8iU5l * * @author Gravity - * @version 2.3 + * @version 2.4 */ public class Updater { @@ -390,7 +391,7 @@ private void downloadFile() { BufferedInputStream in = null; FileOutputStream fout = null; try { - URL fileUrl = new URL(this.versionLink); + URL fileUrl = followRedirects(this.versionLink); final int fileLength = fileUrl.openConnection().getContentLength(); in = new BufferedInputStream(fileUrl.openStream()); fout = new FileOutputStream(new File(this.updateFolder, file.getName())); @@ -430,6 +431,33 @@ private void downloadFile() { } } + private URL followRedirects(String location) throws IOException { + URL resourceUrl, base, next; + HttpURLConnection conn; + String redLoc; + while (true) { + resourceUrl = new URL(location); + conn = (HttpURLConnection) resourceUrl.openConnection(); + + conn.setConnectTimeout(15000); + conn.setReadTimeout(15000); + conn.setInstanceFollowRedirects(false); + conn.setRequestProperty("User-Agent", "Mozilla/5.0..."); + + switch (conn.getResponseCode()) { + case HttpURLConnection.HTTP_MOVED_PERM: + case HttpURLConnection.HTTP_MOVED_TEMP: + redLoc = conn.getHeaderField("Location"); + base = new URL(location); + next = new URL(base, redLoc); // Deal with relative URLs + location = next.toExternalForm(); + continue; + } + break; + } + return conn.getURL(); + } + /** * Remove possibly leftover files from the update folder. */