diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/cache/ResourceFactory.java b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/cache/ResourceFactory.java index b5c6d4789..72340e3ba 100644 --- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/cache/ResourceFactory.java +++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/cache/ResourceFactory.java @@ -60,6 +60,23 @@ public interface ResourceFactory { */ Resource generate(String requestId, byte[] content, int off, int len) throws ResourceIOException; + /** + * Creates a {@link Resource} from a given response body. + * @param requestId a unique identifier for this particular response body. + * @param eTag eTag Strong (unique) identifier for the resource entity + * with the given requestId, or {@code null} when not given + * or is weak (non-unique). + * @param content byte array that represents the origin HTTP response body. + * @param off the start offset in the array. + * @param len the number of bytes to read from the array. + * @return a {@code Resource} containing however much of + * the response body was successfully read. + * @throws ResourceIOException + */ + default Resource generate(String requestId, String eTag, byte[] content, int off, int len) throws ResourceIOException { + return generate(requestId, content, off, len); + } + /** * @deprecated Do not use. */ diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/BasicHttpAsyncCache.java b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/BasicHttpAsyncCache.java index 55af33e17..7a5157e9a 100644 --- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/BasicHttpAsyncCache.java +++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/BasicHttpAsyncCache.java @@ -46,6 +46,7 @@ import org.apache.hc.client5.http.cache.ResourceFactory; import org.apache.hc.client5.http.cache.ResourceIOException; import org.apache.hc.client5.http.impl.Operations; import org.apache.hc.client5.http.validator.ETag; +import org.apache.hc.client5.http.validator.ValidatorType; import org.apache.hc.core5.concurrent.CallbackContribution; import org.apache.hc.core5.concurrent.Cancellable; import org.apache.hc.core5.concurrent.ComplexCancellable; @@ -399,7 +400,11 @@ class BasicHttpAsyncCache implements HttpAsyncCache { } final Resource resource; try { - resource = content != null ? resourceFactory.generate(request.getRequestUri(), content.array(), 0, content.length()) : null; + final ETag eTag = ETag.get(originResponse); + resource = content != null ? resourceFactory.generate( + rootKey, + eTag != null && eTag.getType() == ValidatorType.STRONG ? eTag.getValue() : null, + content.array(), 0, content.length()) : null; } catch (final ResourceIOException ex) { if (LOG.isWarnEnabled()) { LOG.warn("I/O error creating cache entry with key {}", rootKey); diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/BasicHttpCache.java b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/BasicHttpCache.java index 274f099c1..0d808d278 100644 --- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/BasicHttpCache.java +++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/BasicHttpCache.java @@ -43,6 +43,7 @@ import org.apache.hc.client5.http.cache.Resource; import org.apache.hc.client5.http.cache.ResourceFactory; import org.apache.hc.client5.http.cache.ResourceIOException; import org.apache.hc.client5.http.validator.ETag; +import org.apache.hc.client5.http.validator.ValidatorType; import org.apache.hc.core5.http.HttpHeaders; import org.apache.hc.core5.http.HttpHost; import org.apache.hc.core5.http.HttpRequest; @@ -232,7 +233,11 @@ class BasicHttpCache implements HttpCache { } final Resource resource; try { - resource = content != null ? resourceFactory.generate(request.getRequestUri(), content.array(), 0, content.length()) : null; + final ETag eTag = ETag.get(originResponse); + resource = content != null ? resourceFactory.generate( + rootKey, + eTag != null && eTag.getType() == ValidatorType.STRONG ? eTag.getValue() : null, + content.array(), 0, content.length()) : null; } catch (final ResourceIOException ex) { if (LOG.isWarnEnabled()) { LOG.warn("I/O error creating cache entry with key {}", rootKey);