updated getLastModified() to support the file:// protocol

Former-commit-id: 75604221f3e38de0c877fa6e8b5048da593a2a56
This commit is contained in:
Jeremy Long
2013-08-03 12:09:44 -04:00
parent 92a8357690
commit 7365214fb6

View File

@@ -27,6 +27,7 @@ import java.net.HttpURLConnection;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.Proxy; import java.net.Proxy;
import java.net.SocketAddress; import java.net.SocketAddress;
import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@@ -162,7 +163,8 @@ public final class Downloader {
/** /**
* Makes an HTTP Head request to retrieve the last modified date of the * Makes an HTTP Head request to retrieve the last modified date of the
* given URL. * given URL. If the file:// protocol is specified, then the lastTimestamp
* of the file is returned.
* *
* @param url the URL to retrieve the timestamp from * @param url the URL to retrieve the timestamp from
* @return an epoch timestamp * @return an epoch timestamp
@@ -170,21 +172,33 @@ public final class Downloader {
* the HTTP request * the HTTP request
*/ */
public static long getLastModified(URL url) throws DownloadFailedException { public static long getLastModified(URL url) throws DownloadFailedException {
HttpURLConnection conn = null;
long timestamp = 0; long timestamp = 0;
try { //TODO add the FPR protocol?
conn = Downloader.getConnection(url); if ("file:".equalsIgnoreCase(url.getProtocol())) {
conn.setRequestMethod("HEAD"); File f;
conn.connect(); try {
timestamp = conn.getLastModified(); f = new File(url.toURI());
} catch (Exception ex) { } catch (URISyntaxException ex) {
throw new DownloadFailedException("Error making HTTP HEAD request.", ex); final String msg = String.format("Unable to locate '%s'; is the cve.url-2.0.modified property set correctly?", url.toString());
} finally { throw new DownloadFailedException(msg);
if (conn != null) { }
try { timestamp = f.lastModified();
conn.disconnect(); } else {
} finally { HttpURLConnection conn = null;
conn = null; try {
conn = Downloader.getConnection(url);
conn.setRequestMethod("HEAD");
conn.connect();
timestamp = conn.getLastModified();
} catch (Exception ex) {
throw new DownloadFailedException("Error making HTTP HEAD request.", ex);
} finally {
if (conn != null) {
try {
conn.disconnect();
} finally {
conn = null;
}
} }
} }
} }