Fix ETag caching
This commit is contained in:
parent
4fae01236a
commit
b4498da47e
|
@ -184,7 +184,11 @@ public class CachedResource2<K, R> implements Resource<R> {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
debug.fine(WebRequest.log(url, lastModified, etagValue));
|
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) {
|
} catch (FileNotFoundException e) {
|
||||||
return fileNotFound(url, e);
|
return fileNotFound(url, e);
|
||||||
} finally {
|
} finally {
|
||||||
|
|
|
@ -121,7 +121,7 @@ public final class WebRequest {
|
||||||
if (ifModifiedSince > 0) {
|
if (ifModifiedSince > 0) {
|
||||||
connection.setIfModifiedSince(ifModifiedSince);
|
connection.setIfModifiedSince(ifModifiedSince);
|
||||||
} else if (etag != null) {
|
} 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());
|
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) {
|
public static Supplier<String> log(URL url, long lastModified, Object etag) {
|
||||||
return () -> {
|
return () -> {
|
||||||
List<String> headers = new ArrayList<String>(2);
|
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) {
|
if (etag != null) {
|
||||||
headers.add("If-None-Match: " + etag);
|
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);
|
return "Fetch resource: " + url + (headers.isEmpty() ? "" : " " + headers);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue