diff --git a/src/main/java/net/imagej/updater/UpToDate.java b/src/main/java/net/imagej/updater/UpToDate.java index 8d403e0..54c66d9 100644 --- a/src/main/java/net/imagej/updater/UpToDate.java +++ b/src/main/java/net/imagej/updater/UpToDate.java @@ -195,9 +195,6 @@ public static boolean haveNetworkConnection() { public static long getLastModified(final String url) { try { final URLConnection connection = new UpdaterUtil(null).openConnection(new URL(url)); - if (connection instanceof HttpURLConnection) ((HttpURLConnection) connection) - .setRequestMethod("HEAD"); - connection.setUseCaches(false); final long lastModified = connection.getLastModified(); connection.getInputStream().close(); return lastModified; diff --git a/src/main/java/net/imagej/updater/util/Downloader.java b/src/main/java/net/imagej/updater/util/Downloader.java index d0c607a..a0807a3 100644 --- a/src/main/java/net/imagej/updater/util/Downloader.java +++ b/src/main/java/net/imagej/updater/util/Downloader.java @@ -104,7 +104,6 @@ protected synchronized void download(final Downloadable current) throws IOException { final URLConnection connection = util.openConnection(new URL(current.getURL())); - connection.setUseCaches(false); lastModified = connection.getLastModified(); int currentTotal = connection.getContentLength(); if (currentTotal < 0) currentTotal = (int) current.getFilesize(); diff --git a/src/main/java/net/imagej/updater/util/UpdaterUtil.java b/src/main/java/net/imagej/updater/util/UpdaterUtil.java index e0691c0..277f43f 100644 --- a/src/main/java/net/imagej/updater/util/UpdaterUtil.java +++ b/src/main/java/net/imagej/updater/util/UpdaterUtil.java @@ -377,10 +377,7 @@ public static long getLastModified(final String url) { public long getLastModified(final URL url) { try { - final URLConnection connection = openConnection(url); - if (connection instanceof HttpURLConnection) ((HttpURLConnection) connection) - .setRequestMethod("HEAD"); - connection.setUseCaches(false); + final URLConnection connection = openConnection(url); final long lastModified = connection.getLastModified(); connection.getInputStream().close(); return lastModified; @@ -389,7 +386,6 @@ public long getLastModified(final URL url) { if (e.getMessage().startsWith("Server returned HTTP response code: 407")) return -111381; if (e.getMessage().startsWith("Server returned HTTP response code: 405")) try { final URLConnection connection = openConnection(url); - connection.setUseCaches(false); final long lastModified = connection.getLastModified(); connection.getInputStream().close(); return lastModified; @@ -413,6 +409,18 @@ public InputStream openStream(final URL url) throws IOException { return openConnection(url).getInputStream(); } + private String getUserAgent() { + final String javaVmVersion = System.getProperty("java.runtime.version"); + final String javaVersion = javaVmVersion != null ? + javaVmVersion : System.getProperty("java.version"); + final String osVersion = System.getProperty("os.version"); + final String os = "" + System.getProperty("os.name") + "-" + + (osVersion != null ? osVersion + "-" : "") + + System.getProperty("os.arch"); + final String agent = "curl/7.22.0 compatible ImageJ updater/2.0.0-SNAPSHOT " + + "(Java " + javaVersion + "/" + os + ")"; + return agent; + } /** * Open a connection to a {@link URL}. * @@ -421,23 +429,27 @@ public InputStream openStream(final URL url) throws IOException { * @param url the URL to open */ public URLConnection openConnection(final URL url) throws IOException { - final URLConnection connection = dropboxURLMapper.get(url).openConnection(); + return openConnection(url, getUserAgent()); + } + + private URLConnection openConnection(final URL url, final String user_agent) throws IOException { + URLConnection connection = dropboxURLMapper.get(url).openConnection(); + connection.setUseCaches(false); if (connection instanceof HttpURLConnection) { HttpURLConnection http = (HttpURLConnection)connection; - final String javaVmVersion = System.getProperty("java.runtime.version"); - final String javaVersion = javaVmVersion != null ? - javaVmVersion : System.getProperty("java.version"); - final String osVersion = System.getProperty("os.version"); - final String os = "" + System.getProperty("os.name") + "-" - + (osVersion != null ? osVersion + "-" : "") - + System.getProperty("os.arch"); - http.setRequestProperty("User-Agent", - "curl/7.22.0 compatible ImageJ updater/2.0.0-SNAPSHOT (Java " - + javaVersion + "/" + os + ")"); + http.setRequestProperty("User-Agent", user_agent); + final int response = http.getResponseCode(); + if (response == HttpURLConnection.HTTP_MOVED_TEMP + || response == HttpURLConnection.HTTP_MOVED_PERM + || response == HttpURLConnection.HTTP_SEE_OTHER) { + final URL new_url = new URL(connection.getHeaderField("Location")); + connection = openConnection(new_url, user_agent); + } } return connection; } + // Get entire byte data public static byte[] readStreamAsBytes(final InputStream input) throws IOException