Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions src/main/java/net/imagej/updater/UpToDate.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion src/main/java/net/imagej/updater/util/Downloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
44 changes: 28 additions & 16 deletions src/main/java/net/imagej/updater/util/UpdaterUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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}.
*
Expand All @@ -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
Expand Down