Fix ETag caching

This commit is contained in:
Reinhard Pointner 2016-03-08 17:45:00 +00:00
parent 4fae01236a
commit b4498da47e
2 changed files with 9 additions and 5 deletions

View File

@ -184,7 +184,11 @@ public class CachedResource2<K, R> implements Resource<R> {
try {
debug.fine(WebRequest.log(url, lastModified, etagValue));
return WebRequest.fetch(url, lastModified, etagValue, null, responseHeaders);
if (etagValue != null) {
return WebRequest.fetchIfNoneMatch(url, etagValue);
} else {
return WebRequest.fetchIfModified(url, lastModified);
}
} catch (FileNotFoundException e) {
return fileNotFound(url, e);
} finally {

View File

@ -121,7 +121,7 @@ public final class WebRequest {
if (ifModifiedSince > 0) {
connection.setIfModifiedSince(ifModifiedSince);
} else if (etag != null) {
// If-Modified-Since must not be set if If-None-Match is set
// If-Modified-Since must not be set if If-None-Match is set and vice versa
connection.addRequestProperty("If-None-Match", etag.toString());
}
@ -319,12 +319,12 @@ public final class WebRequest {
public static Supplier<String> log(URL url, long lastModified, Object etag) {
return () -> {
List<String> headers = new ArrayList<String>(2);
if (lastModified > 0) {
headers.add("If-Modified-Since: " + DateTimeFormatter.RFC_1123_DATE_TIME.format(ZonedDateTime.ofInstant(Instant.ofEpochMilli(lastModified), ZoneOffset.UTC)));
}
if (etag != null) {
headers.add("If-None-Match: " + etag);
}
if (lastModified > 0) {
headers.add("If-Modified-Since: " + DateTimeFormatter.RFC_1123_DATE_TIME.format(ZonedDateTime.ofInstant(Instant.ofEpochMilli(lastModified), ZoneOffset.UTC)));
}
return "Fetch resource: " + url + (headers.isEmpty() ? "" : " " + headers);
};
}