[prev in list] [next in list] [prev in thread] [next in thread]
List: httpcomponents-commits
Subject: [4/4] httpcomponents-client git commit: Clean up of CacheEntryUpdater (CacheEntryUpdater renamed to
From: olegk () apache ! org
Date: 2017-12-22 14:20:22
Message-ID: c2293bd1c484449888c39aaaf746ed99 () git ! apache ! org
[Download RAW message or body]
Clean up of CacheEntryUpdater (CacheEntryUpdater renamed to CacheUpdateHandler)
Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/repo
Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/commit/d6d3d364
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/tree/d6d3d364
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/diff/d6d3d364
Branch: refs/heads/master
Commit: d6d3d364eb5c4729eb5ae2b2d252a57ffd9fc4ee
Parents: 123a68d
Author: Oleg Kalnichevski <olegk@apache.org>
Authored: Fri Dec 22 15:12:44 2017 +0100
Committer: Oleg Kalnichevski <olegk@apache.org>
Committed: Fri Dec 22 15:12:44 2017 +0100
----------------------------------------------------------------------
.../client5/http/impl/cache/BasicHttpCache.java | 14 +-
.../http/impl/cache/CacheEntryUpdater.java | 216 ----------------
.../http/impl/cache/CacheUpdateHandler.java | 176 +++++++++++++
.../http/impl/cache/TestCacheEntryUpdater.java | 249 -------------------
.../http/impl/cache/TestCacheUpdateHandler.java | 249 +++++++++++++++++++
5 files changed, 432 insertions(+), 472 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/d6d3d364/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/BasicHttpCache.java
----------------------------------------------------------------------
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 61f6311..eb4f3d7 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
@@ -49,7 +49,7 @@ import org.apache.logging.log4j.Logger;
class BasicHttpCache implements HttpCache {
- private final CacheEntryUpdater cacheEntryUpdater;
+ private final CacheUpdateHandler cacheUpdateHandler;
private final CacheKeyGenerator cacheKeyGenerator;
private final HttpCacheInvalidator cacheInvalidator;
private final HttpCacheStorage storage;
@@ -61,7 +61,7 @@ class BasicHttpCache implements HttpCache {
final HttpCacheStorage storage,
final CacheKeyGenerator cacheKeyGenerator,
final HttpCacheInvalidator cacheInvalidator) {
- this.cacheEntryUpdater = new CacheEntryUpdater(resourceFactory);
+ this.cacheUpdateHandler = new CacheUpdateHandler(resourceFactory);
this.cacheKeyGenerator = cacheKeyGenerator;
this.storage = storage;
this.cacheInvalidator = cacheInvalidator;
@@ -130,7 +130,7 @@ class BasicHttpCache implements HttpCache {
@Override
public HttpCacheEntry execute(final HttpCacheEntry existing) throws \
ResourceIOException {
- return \
cacheEntryUpdater.updateParentCacheEntry(req.getRequestUri(), existing, entry, \
variantKey, variantURI); + return \
cacheUpdateHandler.updateParentCacheEntry(req.getRequestUri(), existing, entry, \
variantKey, variantURI); }
});
@@ -152,7 +152,7 @@ class BasicHttpCache implements HttpCache {
@Override
public HttpCacheEntry execute(final HttpCacheEntry existing) throws \
ResourceIOException {
- return \
cacheEntryUpdater.updateParentCacheEntry(req.getRequestUri(), existing, entry, \
variantKey, variantCacheKey); + return \
cacheUpdateHandler.updateParentCacheEntry(req.getRequestUri(), existing, entry, \
variantKey, variantCacheKey); }
});
@@ -169,7 +169,7 @@ class BasicHttpCache implements HttpCache {
final HttpResponse originResponse,
final Date requestSent,
final Date responseReceived) throws ResourceIOException {
- final HttpCacheEntry updatedEntry = cacheEntryUpdater.updateCacheEntry(
+ final HttpCacheEntry updatedEntry = cacheUpdateHandler.updateCacheEntry(
request.getRequestUri(),
stale,
requestSent,
@@ -183,7 +183,7 @@ class BasicHttpCache implements HttpCache {
public HttpCacheEntry updateVariantCacheEntry(final HttpHost target, final \
HttpRequest request,
final HttpCacheEntry stale, final HttpResponse originResponse,
final Date requestSent, final Date responseReceived, final String \
cacheKey) throws ResourceIOException {
- final HttpCacheEntry updatedEntry = cacheEntryUpdater.updateCacheEntry(
+ final HttpCacheEntry updatedEntry = cacheUpdateHandler.updateCacheEntry(
request.getRequestUri(),
stale,
requestSent,
@@ -200,7 +200,7 @@ class BasicHttpCache implements HttpCache {
final ByteArrayBuffer content,
final Date requestSent,
final Date responseReceived) throws ResourceIOException {
- final HttpCacheEntry entry = cacheEntryUpdater.createtCacheEntry(request, \
originResponse, content, requestSent, responseReceived); + final \
HttpCacheEntry entry = cacheUpdateHandler.createtCacheEntry(request, originResponse, \
content, requestSent, responseReceived); storeInCache(host, request, entry);
return entry;
}
http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/d6d3d364/httpclient5 \
-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheEntryUpdater.java
----------------------------------------------------------------------
diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheEntryUpdater.java \
b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheEntryUpdater.java
deleted file mode 100644
index 899fa85..0000000
--- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheEntryUpdater.java
+++ /dev/null
@@ -1,216 +0,0 @@
-/*
- * ====================================================================
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- * ====================================================================
- *
- * This software consists of voluntary contributions made by many
- * individuals on behalf of the Apache Software Foundation. For more
- * information on the Apache Software Foundation, please see
- * <http://www.apache.org/>.
- *
- */
-package org.apache.hc.client5.http.impl.cache;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.List;
-import java.util.ListIterator;
-import java.util.Map;
-
-import org.apache.hc.client5.http.cache.HeaderConstants;
-import org.apache.hc.client5.http.cache.HttpCacheEntry;
-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.utils.DateUtils;
-import org.apache.hc.core5.annotation.Contract;
-import org.apache.hc.core5.annotation.ThreadingBehavior;
-import org.apache.hc.core5.http.Header;
-import org.apache.hc.core5.http.HttpHeaders;
-import org.apache.hc.core5.http.HttpRequest;
-import org.apache.hc.core5.http.HttpResponse;
-import org.apache.hc.core5.http.HttpStatus;
-import org.apache.hc.core5.util.Args;
-import org.apache.hc.core5.util.ByteArrayBuffer;
-
-/**
- * Update a {@link HttpCacheEntry} with new or updated information based on the \
latest
- * 304 status response from the Server. Use the {@link HttpResponse} to perform
- * the processChallenge.
- *
- * @since 4.1
- */
-@Contract(threading = ThreadingBehavior.IMMUTABLE)
-class CacheEntryUpdater {
-
- private final ResourceFactory resourceFactory;
-
- CacheEntryUpdater() {
- this(new HeapResourceFactory());
- }
-
- CacheEntryUpdater(final ResourceFactory resourceFactory) {
- super();
- this.resourceFactory = resourceFactory;
- }
-
- public HttpCacheEntry createtCacheEntry(
- final HttpRequest request,
- final HttpResponse originResponse,
- final ByteArrayBuffer content,
- final Date requestSent,
- final Date responseReceived) throws ResourceIOException {
- final Resource resource;
- if (content != null) {
- resource = resourceFactory.generate(request.getRequestUri(), \
content.array(), 0, content.length());
- } else {
- resource = null;
- }
- return new HttpCacheEntry(
- requestSent,
- responseReceived,
- originResponse.getCode(),
- originResponse.getAllHeaders(),
- resource);
- }
-
- /**
- * Update the entry with the new information from the response. Should only be \
used for
- * 304 responses.
- */
- public HttpCacheEntry updateCacheEntry(
- final String requestId,
- final HttpCacheEntry entry,
- final Date requestDate,
- final Date responseDate,
- final HttpResponse response) throws ResourceIOException {
- Args.check(response.getCode() == HttpStatus.SC_NOT_MODIFIED,
- "Response must have 304 status code");
- final Header[] mergedHeaders = mergeHeaders(entry, response);
- Resource resource = null;
- if (entry.getResource() != null) {
- resource = resourceFactory.copy(requestId, entry.getResource());
- }
- return new HttpCacheEntry(
- requestDate,
- responseDate,
- entry.getStatus(),
- mergedHeaders,
- resource);
- }
-
- protected Header[] mergeHeaders(final HttpCacheEntry entry, final HttpResponse \
response) {
-
- if (entryAndResponseHaveDateHeader(entry, response)
- && entryDateHeaderNewerThenResponse(entry, response)) {
- // Don't merge headers, keep the entry's headers as they are newer.
- return entry.getAllHeaders();
- }
-
- final List<Header> cacheEntryHeaderList = new \
ArrayList<>(Arrays.asList(entry
- .getAllHeaders()));
- removeCacheHeadersThatMatchResponse(cacheEntryHeaderList, response);
- removeCacheEntry1xxWarnings(cacheEntryHeaderList, entry);
- cacheEntryHeaderList.addAll(Arrays.asList(response.getAllHeaders()));
-
- return cacheEntryHeaderList.toArray(new \
Header[cacheEntryHeaderList.size()]);
- }
-
- private void removeCacheHeadersThatMatchResponse(final List<Header> \
cacheEntryHeaderList,
- final HttpResponse response) {
- for (final Header responseHeader : response.getAllHeaders()) {
- final ListIterator<Header> cacheEntryHeaderListIter = \
cacheEntryHeaderList.listIterator();
-
- while (cacheEntryHeaderListIter.hasNext()) {
- final String cacheEntryHeaderName = \
cacheEntryHeaderListIter.next().getName();
-
- if (cacheEntryHeaderName.equals(responseHeader.getName())) {
- cacheEntryHeaderListIter.remove();
- }
- }
- }
- }
-
- private void removeCacheEntry1xxWarnings(final List<Header> \
cacheEntryHeaderList, final HttpCacheEntry entry) {
- final ListIterator<Header> cacheEntryHeaderListIter = \
cacheEntryHeaderList.listIterator();
-
- while (cacheEntryHeaderListIter.hasNext()) {
- final String cacheEntryHeaderName = \
cacheEntryHeaderListIter.next().getName();
-
- if (HeaderConstants.WARNING.equals(cacheEntryHeaderName)) {
- for (final Header cacheEntryWarning : \
entry.getHeaders(HeaderConstants.WARNING)) {
- if (cacheEntryWarning.getValue().startsWith("1")) {
- cacheEntryHeaderListIter.remove();
- }
- }
- }
- }
- }
-
- private boolean entryDateHeaderNewerThenResponse(final HttpCacheEntry entry, \
final HttpResponse response) {
- final Date entryDate = \
DateUtils.parseDate(entry.getFirstHeader(HttpHeaders.DATE)
- .getValue());
- final Date responseDate = \
DateUtils.parseDate(response.getFirstHeader(HttpHeaders.DATE)
- .getValue());
- if (entryDate == null || responseDate == null) {
- return false;
- }
- if (!entryDate.after(responseDate)) {
- return false;
- }
- return true;
- }
-
- private boolean entryAndResponseHaveDateHeader(final HttpCacheEntry entry, final \
HttpResponse response) {
- if (entry.getFirstHeader(HttpHeaders.DATE) != null
- && response.getFirstHeader(HttpHeaders.DATE) != null) {
- return true;
- }
-
- return false;
- }
-
- public HttpCacheEntry updateParentCacheEntry(
- final String requestId,
- final HttpCacheEntry existing,
- final HttpCacheEntry entry,
- final String variantKey,
- final String variantCacheKey) throws ResourceIOException {
- HttpCacheEntry src = existing;
- if (src == null) {
- src = entry;
- }
-
- Resource resource = null;
- if (src.getResource() != null) {
- resource = resourceFactory.copy(requestId, src.getResource());
- }
- final Map<String,String> variantMap = new HashMap<>(src.getVariantMap());
- variantMap.put(variantKey, variantCacheKey);
- return new HttpCacheEntry(
- src.getRequestDate(),
- src.getResponseDate(),
- src.getStatus(),
- src.getAllHeaders(),
- resource,
- variantMap);
- }
-
-}
http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/d6d3d364/httpclient5 \
-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheUpdateHandler.java
----------------------------------------------------------------------
diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheUpdateHandler.java \
b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheUpdateHandler.java
new file mode 100644
index 0000000..5034e04
--- /dev/null
+++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheUpdateHandler.java
@@ -0,0 +1,176 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.hc.client5.http.impl.cache;
+
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.apache.hc.client5.http.cache.HeaderConstants;
+import org.apache.hc.client5.http.cache.HttpCacheEntry;
+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.utils.DateUtils;
+import org.apache.hc.core5.annotation.Contract;
+import org.apache.hc.core5.annotation.ThreadingBehavior;
+import org.apache.hc.core5.http.Header;
+import org.apache.hc.core5.http.HttpHeaders;
+import org.apache.hc.core5.http.HttpRequest;
+import org.apache.hc.core5.http.HttpResponse;
+import org.apache.hc.core5.http.HttpStatus;
+import org.apache.hc.core5.http.MessageHeaders;
+import org.apache.hc.core5.http.message.HeaderGroup;
+import org.apache.hc.core5.util.Args;
+import org.apache.hc.core5.util.ByteArrayBuffer;
+
+/**
+ * Creates new {@link HttpCacheEntry}s and updates existing ones with new or updated \
information + * based on the response from the origin server.
+ *
+ * @since 5.0
+ */
+@Contract(threading = ThreadingBehavior.IMMUTABLE)
+class CacheUpdateHandler {
+
+ private final ResourceFactory resourceFactory;
+
+ CacheUpdateHandler() {
+ this(new HeapResourceFactory());
+ }
+
+ CacheUpdateHandler(final ResourceFactory resourceFactory) {
+ super();
+ this.resourceFactory = resourceFactory;
+ }
+
+ /**
+ * Creates a cache entry for the given request, origin response message and \
response content. + */
+ public HttpCacheEntry createtCacheEntry(
+ final HttpRequest request,
+ final HttpResponse originResponse,
+ final ByteArrayBuffer content,
+ final Date requestSent,
+ final Date responseReceived) throws ResourceIOException {
+ return new HttpCacheEntry(
+ requestSent,
+ responseReceived,
+ originResponse.getCode(),
+ originResponse.getAllHeaders(),
+ content != null ? resourceFactory.generate(request.getRequestUri(), \
content.array(), 0, content.length()) : null); + }
+
+ /**
+ * Update the entry with the new information from the response. Should only be \
used for + * 304 responses.
+ */
+ public HttpCacheEntry updateCacheEntry(
+ final String requestId,
+ final HttpCacheEntry entry,
+ final Date requestDate,
+ final Date responseDate,
+ final HttpResponse response) throws ResourceIOException {
+ Args.check(response.getCode() == HttpStatus.SC_NOT_MODIFIED,
+ "Response must have 304 status code");
+ final Header[] mergedHeaders = mergeHeaders(entry, response);
+ Resource resource = null;
+ if (entry.getResource() != null) {
+ resource = resourceFactory.copy(requestId, entry.getResource());
+ }
+ return new HttpCacheEntry(
+ requestDate,
+ responseDate,
+ entry.getStatus(),
+ mergedHeaders,
+ resource);
+ }
+
+ public HttpCacheEntry updateParentCacheEntry(
+ final String requestId,
+ final HttpCacheEntry existing,
+ final HttpCacheEntry entry,
+ final String variantKey,
+ final String variantCacheKey) throws ResourceIOException {
+ HttpCacheEntry src = existing;
+ if (src == null) {
+ src = entry;
+ }
+
+ Resource resource = null;
+ if (src.getResource() != null) {
+ resource = resourceFactory.copy(requestId, src.getResource());
+ }
+ final Map<String,String> variantMap = new HashMap<>(src.getVariantMap());
+ variantMap.put(variantKey, variantCacheKey);
+ return new HttpCacheEntry(
+ src.getRequestDate(),
+ src.getResponseDate(),
+ src.getStatus(),
+ src.getAllHeaders(),
+ resource,
+ variantMap);
+ }
+
+ private static Date getDate(final MessageHeaders messageHeaders) {
+ final Header dateHeader = messageHeaders.getFirstHeader(HttpHeaders.DATE);
+ return dateHeader != null ? DateUtils.parseDate(dateHeader.getValue()): \
null; + }
+
+ private Header[] mergeHeaders(final HttpCacheEntry entry, final HttpResponse \
response) { + final Date cacheDate = getDate(entry);
+ final Date responseDate = getDate(response);
+ if (cacheDate != null && responseDate != null && \
cacheDate.after(responseDate)) { + // Don't merge headers, keep the \
entry's headers as they are newer. + return entry.getAllHeaders();
+ }
+ final HeaderGroup headerGroup = new HeaderGroup();
+ headerGroup.setHeaders(entry.getAllHeaders());
+ // Remove cache headers that match response
+ for (final Iterator<Header> it = response.headerIterator(); it.hasNext(); ) \
{ + final Header responseHeader = it.next();
+ headerGroup.removeHeaders(responseHeader.getName());
+ }
+ // remove cache entry 1xx warnings
+ for (final Iterator<Header> it = headerGroup.headerIterator(); it.hasNext(); \
) { + final Header cacheHeader = it.next();
+ if (HeaderConstants.WARNING.equalsIgnoreCase(cacheHeader.getName())) {
+ final String warningValue = cacheHeader.getValue();
+ if (warningValue != null && warningValue.startsWith("1")) {
+ it.remove();
+ }
+ }
+ }
+ for (final Iterator<Header> it = response.headerIterator(); it.hasNext(); ) \
{ + final Header responseHeader = it.next();
+ headerGroup.addHeader(responseHeader);
+ }
+ return headerGroup.getAllHeaders();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/d6d3d364/httpclient5 \
-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCacheEntryUpdater.java
----------------------------------------------------------------------
diff --git a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCacheEntryUpdater.java \
b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCacheEntryUpdater.java
deleted file mode 100644
index 1f53353..0000000
--- a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCacheEntryUpdater.java
+++ /dev/null
@@ -1,249 +0,0 @@
-/*
- * ====================================================================
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- * ====================================================================
- *
- * This software consists of voluntary contributions made by many
- * individuals on behalf of the Apache Software Foundation. For more
- * information on the Apache Software Foundation, please see
- * <http://www.apache.org/>.
- *
- */
-package org.apache.hc.client5.http.impl.cache;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotSame;
-import static org.junit.Assert.assertThat;
-import static org.junit.Assert.fail;
-
-import java.io.IOException;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.Map;
-
-import org.apache.hc.client5.http.cache.HttpCacheEntry;
-import org.apache.hc.client5.http.utils.DateUtils;
-import org.apache.hc.core5.http.Header;
-import org.apache.hc.core5.http.HttpResponse;
-import org.apache.hc.core5.http.HttpStatus;
-import org.apache.hc.core5.http.message.BasicHeader;
-import org.apache.hc.core5.http.message.BasicHttpResponse;
-import org.junit.Before;
-import org.junit.Test;
-
-public class TestCacheEntryUpdater {
-
- private Date requestDate;
- private Date responseDate;
-
- private CacheEntryUpdater impl;
- private HttpCacheEntry entry;
- private Date now;
- private Date oneSecondAgo;
- private Date twoSecondsAgo;
- private Date eightSecondsAgo;
- private Date tenSecondsAgo;
- private HttpResponse response;
-
- @Before
- public void setUp() throws Exception {
- requestDate = new Date(System.currentTimeMillis() - 1000);
- responseDate = new Date();
-
- now = new Date();
- oneSecondAgo = new Date(now.getTime() - 1000L);
- twoSecondsAgo = new Date(now.getTime() - 2000L);
- eightSecondsAgo = new Date(now.getTime() - 8000L);
- tenSecondsAgo = new Date(now.getTime() - 10000L);
-
- response = new BasicHttpResponse(HttpStatus.SC_NOT_MODIFIED, "Not \
Modified");
-
- impl = new CacheEntryUpdater();
- }
-
- @Test
- public void testUpdateCacheEntryReturnsDifferentEntryInstance()
- throws IOException {
- entry = HttpTestUtils.makeCacheEntry();
- final HttpCacheEntry newEntry = impl.updateCacheEntry(null, entry,
- requestDate, responseDate, response);
- assertNotSame(newEntry, entry);
- }
-
- @Test
- public void testHeadersAreMergedCorrectly() throws IOException {
- final Header[] headers = {
- new BasicHeader("Date", DateUtils.formatDate(responseDate)),
- new BasicHeader("ETag", "\"etag\"")};
- entry = HttpTestUtils.makeCacheEntry(headers);
- response.setHeaders();
-
- final HttpCacheEntry updatedEntry = impl.updateCacheEntry(null, entry,
- new Date(), new Date(), response);
-
- assertThat(updatedEntry, ContainsHeaderMatcher.contains("Date", \
DateUtils.formatDate(responseDate)));
- assertThat(updatedEntry, ContainsHeaderMatcher.contains("ETag", \
"\"etag\""));
- }
-
- @Test
- public void testNewerHeadersReplaceExistingHeaders() throws IOException {
- final Header[] headers = {
- new BasicHeader("Date", DateUtils.formatDate(requestDate)),
- new BasicHeader("Cache-Control", "private"),
- new BasicHeader("ETag", "\"etag\""),
- new BasicHeader("Last-Modified", DateUtils.formatDate(requestDate)),
- new BasicHeader("Cache-Control", "max-age=0"),};
- entry = HttpTestUtils.makeCacheEntry(headers);
-
- response.setHeaders(new BasicHeader("Last-Modified", \
DateUtils.formatDate(responseDate)),
- new BasicHeader("Cache-Control", "public"));
-
- final HttpCacheEntry updatedEntry = impl.updateCacheEntry(null, entry,
- new Date(), new Date(), response);
-
- assertThat(updatedEntry, ContainsHeaderMatcher.contains("Date", \
DateUtils.formatDate(requestDate)));
- assertThat(updatedEntry, ContainsHeaderMatcher.contains("ETag", \
"\"etag\""));
- assertThat(updatedEntry, ContainsHeaderMatcher.contains("Last-Modified", \
DateUtils.formatDate(responseDate)));
- assertThat(updatedEntry, ContainsHeaderMatcher.contains("Cache-Control", \
"public"));
- }
-
- @Test
- public void testNewHeadersAreAddedByMerge() throws IOException {
-
- final Header[] headers = {
- new BasicHeader("Date", DateUtils.formatDate(requestDate)),
- new BasicHeader("ETag", "\"etag\"")};
-
- entry = HttpTestUtils.makeCacheEntry(headers);
- response.setHeaders(new BasicHeader("Last-Modified", \
DateUtils.formatDate(responseDate)),
- new BasicHeader("Cache-Control", "public"));
-
- final HttpCacheEntry updatedEntry = impl.updateCacheEntry(null, entry,
- new Date(), new Date(), response);
-
- assertThat(updatedEntry, ContainsHeaderMatcher.contains("Date", \
DateUtils.formatDate(requestDate)));
- assertThat(updatedEntry, ContainsHeaderMatcher.contains("ETag", \
"\"etag\""));
- assertThat(updatedEntry, ContainsHeaderMatcher.contains("Last-Modified", \
DateUtils.formatDate(responseDate)));
- assertThat(updatedEntry, ContainsHeaderMatcher.contains("Cache-Control", \
"public"));
- }
-
- @Test
- public void oldHeadersRetainedIfResponseOlderThanEntry()
- throws Exception {
- final Header[] headers = {
- new BasicHeader("Date", DateUtils.formatDate(oneSecondAgo)),
- new BasicHeader("ETag", "\"new-etag\"")
- };
- entry = HttpTestUtils.makeCacheEntry(twoSecondsAgo, now, headers);
- response.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
- response.setHeader("ETag", "\"old-etag\"");
- final HttpCacheEntry result = impl.updateCacheEntry("A", entry, new Date(),
- new Date(), response);
- assertThat(result, ContainsHeaderMatcher.contains("Date", \
DateUtils.formatDate(oneSecondAgo)));
- assertThat(result, ContainsHeaderMatcher.contains("ETag", "\"new-etag\""));
- }
-
- @Test
- public void testUpdatedEntryHasLatestRequestAndResponseDates()
- throws IOException {
- entry = HttpTestUtils.makeCacheEntry(tenSecondsAgo, eightSecondsAgo);
- final HttpCacheEntry updated = impl.updateCacheEntry(null, entry,
- twoSecondsAgo, oneSecondAgo, response);
-
- assertEquals(twoSecondsAgo, updated.getRequestDate());
- assertEquals(oneSecondAgo, updated.getResponseDate());
- }
-
- @Test
- public void entry1xxWarningsAreRemovedOnUpdate() throws Exception {
- final Header[] headers = {
- new BasicHeader("Warning", "110 fred \"Response is stale\""),
- new BasicHeader("ETag", "\"old\""),
- new BasicHeader("Date", DateUtils.formatDate(eightSecondsAgo))
- };
- entry = HttpTestUtils.makeCacheEntry(tenSecondsAgo, eightSecondsAgo, \
headers);
- response.setHeader("ETag", "\"new\"");
- response.setHeader("Date", DateUtils.formatDate(twoSecondsAgo));
- final HttpCacheEntry updated = impl.updateCacheEntry(null, entry,
- twoSecondsAgo, oneSecondAgo, response);
-
- assertEquals(0, updated.getHeaders("Warning").length);
- }
-
- @Test
- public void entryWithMalformedDateIsStillUpdated() throws Exception {
- final Header[] headers = {
- new BasicHeader("ETag", "\"old\""),
- new BasicHeader("Date", "bad-date")
- };
- entry = HttpTestUtils.makeCacheEntry(tenSecondsAgo, eightSecondsAgo, \
headers);
- response.setHeader("ETag", "\"new\"");
- response.setHeader("Date", DateUtils.formatDate(twoSecondsAgo));
- final HttpCacheEntry updated = impl.updateCacheEntry(null, entry,
- twoSecondsAgo, oneSecondAgo, response);
-
- assertEquals("\"new\"", updated.getFirstHeader("ETag").getValue());
- }
-
- @Test
- public void entryIsStillUpdatedByResponseWithMalformedDate() throws Exception {
- final Header[] headers = {
- new BasicHeader("ETag", "\"old\""),
- new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo))
- };
- entry = HttpTestUtils.makeCacheEntry(tenSecondsAgo, eightSecondsAgo, \
headers);
- response.setHeader("ETag", "\"new\"");
- response.setHeader("Date", "bad-date");
- final HttpCacheEntry updated = impl.updateCacheEntry(null, entry,
- twoSecondsAgo, oneSecondAgo, response);
-
- assertEquals("\"new\"", updated.getFirstHeader("ETag").getValue());
- }
-
- @Test
- public void cannotUpdateFromANon304OriginResponse() throws Exception {
- entry = HttpTestUtils.makeCacheEntry();
- response = new BasicHttpResponse(HttpStatus.SC_OK, "OK");
- try {
- impl.updateCacheEntry("A", entry, new Date(), new Date(),
- response);
- fail("should have thrown exception");
- } catch (final IllegalArgumentException expected) {
- }
- }
-
- @Test
- public void testCacheUpdateAddsVariantURIToParentEntry() throws Exception {
- final String parentCacheKey = "parentCacheKey";
- final String variantCacheKey = "variantCacheKey";
- final String existingVariantKey = "existingVariantKey";
- final String newVariantCacheKey = "newVariantCacheKey";
- final String newVariantKey = "newVariantKey";
- final Map<String,String> existingVariants = new HashMap<>();
- existingVariants.put(existingVariantKey, variantCacheKey);
- final HttpCacheEntry parent = \
HttpTestUtils.makeCacheEntry(existingVariants);
- final HttpCacheEntry variant = HttpTestUtils.makeCacheEntry();
-
- final HttpCacheEntry result = impl.updateParentCacheEntry(parentCacheKey, \
parent, variant, newVariantKey, newVariantCacheKey);
- final Map<String,String> resultMap = result.getVariantMap();
- assertEquals(2, resultMap.size());
- assertEquals(variantCacheKey, resultMap.get(existingVariantKey));
- assertEquals(newVariantCacheKey, resultMap.get(newVariantKey));
- }
-
-}
http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/d6d3d364/httpclient5 \
-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCacheUpdateHandler.java
----------------------------------------------------------------------
diff --git a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCacheUpdateHandler.java \
b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCacheUpdateHandler.java
new file mode 100644
index 0000000..8170105
--- /dev/null
+++ b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCacheUpdateHandler.java
@@ -0,0 +1,249 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.hc.client5.http.impl.cache;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotSame;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.fail;
+
+import java.io.IOException;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.hc.client5.http.cache.HttpCacheEntry;
+import org.apache.hc.client5.http.utils.DateUtils;
+import org.apache.hc.core5.http.Header;
+import org.apache.hc.core5.http.HttpResponse;
+import org.apache.hc.core5.http.HttpStatus;
+import org.apache.hc.core5.http.message.BasicHeader;
+import org.apache.hc.core5.http.message.BasicHttpResponse;
+import org.junit.Before;
+import org.junit.Test;
+
+public class TestCacheUpdateHandler {
+
+ private Date requestDate;
+ private Date responseDate;
+
+ private CacheUpdateHandler impl;
+ private HttpCacheEntry entry;
+ private Date now;
+ private Date oneSecondAgo;
+ private Date twoSecondsAgo;
+ private Date eightSecondsAgo;
+ private Date tenSecondsAgo;
+ private HttpResponse response;
+
+ @Before
+ public void setUp() throws Exception {
+ requestDate = new Date(System.currentTimeMillis() - 1000);
+ responseDate = new Date();
+
+ now = new Date();
+ oneSecondAgo = new Date(now.getTime() - 1000L);
+ twoSecondsAgo = new Date(now.getTime() - 2000L);
+ eightSecondsAgo = new Date(now.getTime() - 8000L);
+ tenSecondsAgo = new Date(now.getTime() - 10000L);
+
+ response = new BasicHttpResponse(HttpStatus.SC_NOT_MODIFIED, "Not \
Modified"); +
+ impl = new CacheUpdateHandler();
+ }
+
+ @Test
+ public void testUpdateCacheEntryReturnsDifferentEntryInstance()
+ throws IOException {
+ entry = HttpTestUtils.makeCacheEntry();
+ final HttpCacheEntry newEntry = impl.updateCacheEntry(null, entry,
+ requestDate, responseDate, response);
+ assertNotSame(newEntry, entry);
+ }
+
+ @Test
+ public void testHeadersAreMergedCorrectly() throws IOException {
+ final Header[] headers = {
+ new BasicHeader("Date", DateUtils.formatDate(responseDate)),
+ new BasicHeader("ETag", "\"etag\"")};
+ entry = HttpTestUtils.makeCacheEntry(headers);
+ response.setHeaders();
+
+ final HttpCacheEntry updatedEntry = impl.updateCacheEntry(null, entry,
+ new Date(), new Date(), response);
+
+ assertThat(updatedEntry, ContainsHeaderMatcher.contains("Date", \
DateUtils.formatDate(responseDate))); + assertThat(updatedEntry, \
ContainsHeaderMatcher.contains("ETag", "\"etag\"")); + }
+
+ @Test
+ public void testNewerHeadersReplaceExistingHeaders() throws IOException {
+ final Header[] headers = {
+ new BasicHeader("Date", DateUtils.formatDate(requestDate)),
+ new BasicHeader("Cache-Control", "private"),
+ new BasicHeader("ETag", "\"etag\""),
+ new BasicHeader("Last-Modified", DateUtils.formatDate(requestDate)),
+ new BasicHeader("Cache-Control", "max-age=0"),};
+ entry = HttpTestUtils.makeCacheEntry(headers);
+
+ response.setHeaders(new BasicHeader("Last-Modified", \
DateUtils.formatDate(responseDate)), + new \
BasicHeader("Cache-Control", "public")); +
+ final HttpCacheEntry updatedEntry = impl.updateCacheEntry(null, entry,
+ new Date(), new Date(), response);
+
+ assertThat(updatedEntry, ContainsHeaderMatcher.contains("Date", \
DateUtils.formatDate(requestDate))); + assertThat(updatedEntry, \
ContainsHeaderMatcher.contains("ETag", "\"etag\"")); + \
assertThat(updatedEntry, ContainsHeaderMatcher.contains("Last-Modified", \
DateUtils.formatDate(responseDate))); + assertThat(updatedEntry, \
ContainsHeaderMatcher.contains("Cache-Control", "public")); + }
+
+ @Test
+ public void testNewHeadersAreAddedByMerge() throws IOException {
+
+ final Header[] headers = {
+ new BasicHeader("Date", DateUtils.formatDate(requestDate)),
+ new BasicHeader("ETag", "\"etag\"")};
+
+ entry = HttpTestUtils.makeCacheEntry(headers);
+ response.setHeaders(new BasicHeader("Last-Modified", \
DateUtils.formatDate(responseDate)), + new \
BasicHeader("Cache-Control", "public")); +
+ final HttpCacheEntry updatedEntry = impl.updateCacheEntry(null, entry,
+ new Date(), new Date(), response);
+
+ assertThat(updatedEntry, ContainsHeaderMatcher.contains("Date", \
DateUtils.formatDate(requestDate))); + assertThat(updatedEntry, \
ContainsHeaderMatcher.contains("ETag", "\"etag\"")); + \
assertThat(updatedEntry, ContainsHeaderMatcher.contains("Last-Modified", \
DateUtils.formatDate(responseDate))); + assertThat(updatedEntry, \
ContainsHeaderMatcher.contains("Cache-Control", "public")); + }
+
+ @Test
+ public void oldHeadersRetainedIfResponseOlderThanEntry()
+ throws Exception {
+ final Header[] headers = {
+ new BasicHeader("Date", DateUtils.formatDate(oneSecondAgo)),
+ new BasicHeader("ETag", "\"new-etag\"")
+ };
+ entry = HttpTestUtils.makeCacheEntry(twoSecondsAgo, now, headers);
+ response.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
+ response.setHeader("ETag", "\"old-etag\"");
+ final HttpCacheEntry result = impl.updateCacheEntry("A", entry, new Date(),
+ new Date(), response);
+ assertThat(result, ContainsHeaderMatcher.contains("Date", \
DateUtils.formatDate(oneSecondAgo))); + assertThat(result, \
ContainsHeaderMatcher.contains("ETag", "\"new-etag\"")); + }
+
+ @Test
+ public void testUpdatedEntryHasLatestRequestAndResponseDates()
+ throws IOException {
+ entry = HttpTestUtils.makeCacheEntry(tenSecondsAgo, eightSecondsAgo);
+ final HttpCacheEntry updated = impl.updateCacheEntry(null, entry,
+ twoSecondsAgo, oneSecondAgo, response);
+
+ assertEquals(twoSecondsAgo, updated.getRequestDate());
+ assertEquals(oneSecondAgo, updated.getResponseDate());
+ }
+
+ @Test
+ public void entry1xxWarningsAreRemovedOnUpdate() throws Exception {
+ final Header[] headers = {
+ new BasicHeader("Warning", "110 fred \"Response is stale\""),
+ new BasicHeader("ETag", "\"old\""),
+ new BasicHeader("Date", DateUtils.formatDate(eightSecondsAgo))
+ };
+ entry = HttpTestUtils.makeCacheEntry(tenSecondsAgo, eightSecondsAgo, \
headers); + response.setHeader("ETag", "\"new\"");
+ response.setHeader("Date", DateUtils.formatDate(twoSecondsAgo));
+ final HttpCacheEntry updated = impl.updateCacheEntry(null, entry,
+ twoSecondsAgo, oneSecondAgo, response);
+
+ assertEquals(0, updated.getHeaders("Warning").length);
+ }
+
+ @Test
+ public void entryWithMalformedDateIsStillUpdated() throws Exception {
+ final Header[] headers = {
+ new BasicHeader("ETag", "\"old\""),
+ new BasicHeader("Date", "bad-date")
+ };
+ entry = HttpTestUtils.makeCacheEntry(tenSecondsAgo, eightSecondsAgo, \
headers); + response.setHeader("ETag", "\"new\"");
+ response.setHeader("Date", DateUtils.formatDate(twoSecondsAgo));
+ final HttpCacheEntry updated = impl.updateCacheEntry(null, entry,
+ twoSecondsAgo, oneSecondAgo, response);
+
+ assertEquals("\"new\"", updated.getFirstHeader("ETag").getValue());
+ }
+
+ @Test
+ public void entryIsStillUpdatedByResponseWithMalformedDate() throws Exception {
+ final Header[] headers = {
+ new BasicHeader("ETag", "\"old\""),
+ new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo))
+ };
+ entry = HttpTestUtils.makeCacheEntry(tenSecondsAgo, eightSecondsAgo, \
headers); + response.setHeader("ETag", "\"new\"");
+ response.setHeader("Date", "bad-date");
+ final HttpCacheEntry updated = impl.updateCacheEntry(null, entry,
+ twoSecondsAgo, oneSecondAgo, response);
+
+ assertEquals("\"new\"", updated.getFirstHeader("ETag").getValue());
+ }
+
+ @Test
+ public void cannotUpdateFromANon304OriginResponse() throws Exception {
+ entry = HttpTestUtils.makeCacheEntry();
+ response = new BasicHttpResponse(HttpStatus.SC_OK, "OK");
+ try {
+ impl.updateCacheEntry("A", entry, new Date(), new Date(),
+ response);
+ fail("should have thrown exception");
+ } catch (final IllegalArgumentException expected) {
+ }
+ }
+
+ @Test
+ public void testCacheUpdateAddsVariantURIToParentEntry() throws Exception {
+ final String parentCacheKey = "parentCacheKey";
+ final String variantCacheKey = "variantCacheKey";
+ final String existingVariantKey = "existingVariantKey";
+ final String newVariantCacheKey = "newVariantCacheKey";
+ final String newVariantKey = "newVariantKey";
+ final Map<String,String> existingVariants = new HashMap<>();
+ existingVariants.put(existingVariantKey, variantCacheKey);
+ final HttpCacheEntry parent = \
HttpTestUtils.makeCacheEntry(existingVariants); + final HttpCacheEntry variant \
= HttpTestUtils.makeCacheEntry(); +
+ final HttpCacheEntry result = impl.updateParentCacheEntry(parentCacheKey, \
parent, variant, newVariantKey, newVariantCacheKey); + final \
Map<String,String> resultMap = result.getVariantMap(); + assertEquals(2, \
resultMap.size()); + assertEquals(variantCacheKey, \
resultMap.get(existingVariantKey)); + assertEquals(newVariantCacheKey, \
resultMap.get(newVariantKey)); + }
+
+}
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic