Skip to content
Permalink
Browse files
Deprecated message copiers in favor of generic message builders
  • Loading branch information
ok2c committed Mar 11, 2021
1 parent ea9c6ef commit 3de88293fe665e67df1854152faaaa10d6b23ce3
Showing 17 changed files with 100 additions and 65 deletions.
@@ -49,7 +49,6 @@
import org.apache.hc.client5.http.cache.ResourceFactory;
import org.apache.hc.client5.http.cache.ResourceIOException;
import org.apache.hc.client5.http.impl.ExecSupport;
import org.apache.hc.client5.http.impl.RequestCopier;
import org.apache.hc.client5.http.protocol.HttpClientContext;
import org.apache.hc.client5.http.schedule.SchedulingStrategy;
import org.apache.hc.client5.http.utils.DateUtils;
@@ -58,6 +57,7 @@
import org.apache.hc.core5.concurrent.CancellableDependency;
import org.apache.hc.core5.concurrent.ComplexFuture;
import org.apache.hc.core5.concurrent.FutureCallback;
import org.apache.hc.core5.function.Factory;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.EntityDetails;
import org.apache.hc.core5.http.Header;
@@ -72,6 +72,7 @@
import org.apache.hc.core5.http.nio.AsyncEntityProducer;
import org.apache.hc.core5.http.nio.CapacityChannel;
import org.apache.hc.core5.http.protocol.HttpCoreContext;
import org.apache.hc.core5.http.support.BasicRequestBuilder;
import org.apache.hc.core5.net.URIAuthority;
import org.apache.hc.core5.util.Args;
import org.apache.hc.core5.util.ByteArrayBuffer;
@@ -101,7 +102,14 @@ class AsyncCachingExec extends CachingExecBase implements AsyncExecChainHandler
super(config);
this.responseCache = Args.notNull(cache, "Response cache");
this.cacheRevalidator = cacheRevalidator;
this.conditionalRequestBuilder = new ConditionalRequestBuilder<>(RequestCopier.INSTANCE);
this.conditionalRequestBuilder = new ConditionalRequestBuilder<>(new Factory<HttpRequest, HttpRequest>() {

@Override
public HttpRequest create(final HttpRequest request) {
return BasicRequestBuilder.copy(request).build();
}

});
}

AsyncCachingExec(
@@ -695,7 +703,9 @@ void revalidateCacheEntry(
final AsyncExecCallback asyncExecCallback,
final HttpCacheEntry cacheEntry) {
final Date requestDate = getCurrentDate();
final HttpRequest conditionalRequest = conditionalRequestBuilder.buildConditionalRequest(scope.originalRequest, cacheEntry);
final HttpRequest conditionalRequest = conditionalRequestBuilder.buildConditionalRequest(
BasicRequestBuilder.copy(scope.originalRequest).build(),
cacheEntry);
chainProceed(conditionalRequest, entityProducer, scope, chain, new AsyncExecCallback() {

final AtomicReference<AsyncExecCallback> callbackRef = new AtomicReference<>();
@@ -795,7 +805,7 @@ public AsyncDataConsumer handleResponse(
&& (entityProducer == null || entityProducer.isRepeatable())) {

final HttpRequest unconditional = conditionalRequestBuilder.buildUnconditionalRequest(
scope.originalRequest);
BasicRequestBuilder.copy(scope.originalRequest).build());

callback1 = new AsyncExecCallbackWrapper(asyncExecCallback, new Runnable() {

@@ -940,7 +950,9 @@ void negotiateResponseFromVariants(
final AsyncExecCallback asyncExecCallback,
final Map<String, Variant> variants) {
final CancellableDependency operation = scope.cancellableDependency;
final HttpRequest conditionalRequest = conditionalRequestBuilder.buildConditionalRequestFromVariants(request, variants);
final HttpRequest conditionalRequest = conditionalRequestBuilder.buildConditionalRequestFromVariants(
BasicRequestBuilder.copy(request).build(),
variants);

final Date requestDate = getCurrentDate();
chainProceed(conditionalRequest, entityProducer, scope, chain, new AsyncExecCallback() {
@@ -1045,7 +1057,8 @@ public void run() {
});
} else {
if (revalidationResponseIsTooOld(backendResponse, matchingVariant.getEntry())) {
final HttpRequest unconditional = conditionalRequestBuilder.buildUnconditionalRequest(request);
final HttpRequest unconditional = conditionalRequestBuilder.buildUnconditionalRequest(
BasicRequestBuilder.copy(request).build());
scope.clientContext.setAttribute(HttpCoreContext.HTTP_REQUEST, unconditional);
callback = new AsyncExecCallbackWrapper(asyncExecCallback, new Runnable() {

@@ -45,10 +45,10 @@
import org.apache.hc.client5.http.classic.ExecChain;
import org.apache.hc.client5.http.classic.ExecChainHandler;
import org.apache.hc.client5.http.impl.ExecSupport;
import org.apache.hc.client5.http.impl.classic.ClassicRequestCopier;
import org.apache.hc.client5.http.protocol.HttpClientContext;
import org.apache.hc.client5.http.schedule.SchedulingStrategy;
import org.apache.hc.client5.http.utils.DateUtils;
import org.apache.hc.core5.function.Factory;
import org.apache.hc.core5.http.ClassicHttpRequest;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.Header;
@@ -62,6 +62,7 @@
import org.apache.hc.core5.http.io.entity.ByteArrayEntity;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.apache.hc.core5.http.io.support.ClassicRequestBuilder;
import org.apache.hc.core5.http.message.BasicClassicHttpResponse;
import org.apache.hc.core5.http.protocol.HttpCoreContext;
import org.apache.hc.core5.net.URIAuthority;
@@ -110,7 +111,14 @@ class CachingExec extends CachingExecBase implements ExecChainHandler {
super(config);
this.responseCache = Args.notNull(cache, "Response cache");
this.cacheRevalidator = cacheRevalidator;
this.conditionalRequestBuilder = new ConditionalRequestBuilder<>(ClassicRequestCopier.INSTANCE);
this.conditionalRequestBuilder = new ConditionalRequestBuilder<>(new Factory<ClassicHttpRequest, ClassicHttpRequest>() {

@Override
public ClassicHttpRequest create(final ClassicHttpRequest classicHttpRequest) {
return ClassicRequestBuilder.copy(classicHttpRequest).build();
}

});
}

CachingExec(
@@ -31,17 +31,17 @@

import org.apache.hc.client5.http.cache.HeaderConstants;
import org.apache.hc.client5.http.cache.HttpCacheEntry;
import org.apache.hc.client5.http.impl.MessageCopier;
import org.apache.hc.core5.function.Factory;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HeaderElement;
import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.message.MessageSupport;

class ConditionalRequestBuilder<T extends HttpRequest> {

private final MessageCopier<T> messageCopier;
private final Factory<T, T> messageCopier;

ConditionalRequestBuilder(final MessageCopier<T> messageCopier) {
ConditionalRequestBuilder(final Factory<T, T> messageCopier) {
this.messageCopier = messageCopier;
}

@@ -56,8 +56,8 @@
* @return the wrapped request
*/
public T buildConditionalRequest(final T request, final HttpCacheEntry cacheEntry) {
final T newRequest = messageCopier.copy(request);
newRequest.setHeaders(request.getHeaders());
final T newRequest = messageCopier.create(request);

final Header eTag = cacheEntry.getFirstHeader(HeaderConstants.ETAG);
if (eTag != null) {
newRequest.setHeader(HeaderConstants.IF_NONE_MATCH, eTag.getValue());
@@ -95,8 +95,7 @@ public T buildConditionalRequest(final T request, final HttpCacheEntry cacheEntr
* @return the wrapped request
*/
public T buildConditionalRequestFromVariants(final T request, final Map<String, Variant> variants) {
final T newRequest = messageCopier.copy(request);
newRequest.setHeaders(request.getHeaders());
final T newRequest = messageCopier.create(request);

// we do not support partial content so all etags are used
final StringBuilder etags = new StringBuilder();
@@ -124,7 +123,7 @@ public T buildConditionalRequestFromVariants(final T request, final Map<String,
* @return an unconditional validation request
*/
public T buildUnconditionalRequest(final T request) {
final T newRequest = messageCopier.copy(request);
final T newRequest = messageCopier.create(request);
newRequest.addHeader(HeaderConstants.CACHE_CONTROL,HeaderConstants.CACHE_CONTROL_NO_CACHE);
newRequest.addHeader(HeaderConstants.PRAGMA,HeaderConstants.CACHE_CONTROL_NO_CACHE);
newRequest.removeHeaders(HeaderConstants.IF_RANGE);
@@ -33,7 +33,6 @@
import org.apache.hc.client5.http.classic.ExecChain;
import org.apache.hc.client5.http.classic.ExecChainHandler;
import org.apache.hc.client5.http.classic.ExecRuntime;
import org.apache.hc.client5.http.impl.classic.ClassicRequestCopier;
import org.apache.hc.client5.http.protocol.HttpClientContext;
import org.apache.hc.core5.http.ClassicHttpRequest;
import org.apache.hc.core5.http.ClassicHttpResponse;
@@ -42,6 +41,7 @@
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.io.support.ClassicRequestBuilder;
import org.apache.hc.core5.http.message.BasicClassicHttpRequest;
import org.easymock.EasyMock;
import org.easymock.IExpectationSetters;
@@ -107,8 +107,10 @@ public void setUp() {
}

public ClassicHttpResponse execute(final ClassicHttpRequest request) throws IOException, HttpException {
return impl.execute(ClassicRequestCopier.INSTANCE.copy(request), new ExecChain.Scope(
"test", route, request, mockExecRuntime, context), mockExecChain);
return impl.execute(
ClassicRequestBuilder.copy(request).build(),
new ExecChain.Scope("test", route, request, mockExecRuntime, context),
mockExecChain);
}

protected ExecChainHandler createCachingExecChain(final HttpCache cache, final CacheConfig config) {
@@ -27,13 +27,13 @@
package org.apache.hc.client5.http.impl.cache;

import static org.easymock.EasyMock.anyObject;
import static org.easymock.EasyMock.createNiceMock;
import static org.easymock.EasyMock.eq;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.expectLastCall;
import static org.easymock.EasyMock.isA;
import static org.easymock.EasyMock.same;
import static org.easymock.EasyMock.createNiceMock;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.same;
import static org.easymock.EasyMock.verify;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
@@ -59,7 +59,6 @@
import org.apache.hc.client5.http.classic.ExecRuntime;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpOptions;
import org.apache.hc.client5.http.impl.classic.ClassicRequestCopier;
import org.apache.hc.client5.http.protocol.HttpClientContext;
import org.apache.hc.client5.http.utils.DateUtils;
import org.apache.hc.core5.http.ClassicHttpRequest;
@@ -74,6 +73,7 @@
import org.apache.hc.core5.http.io.HttpClientResponseHandler;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.InputStreamEntity;
import org.apache.hc.core5.http.io.support.ClassicRequestBuilder;
import org.apache.hc.core5.http.message.BasicClassicHttpRequest;
import org.apache.hc.core5.http.message.BasicClassicHttpResponse;
import org.apache.hc.core5.http.message.BasicHeader;
@@ -165,8 +165,10 @@ public abstract CachingExec createCachingExecChain(
public abstract CachingExec createCachingExecChain(HttpCache cache, CacheConfig config);

protected ClassicHttpResponse execute(final ClassicHttpRequest request) throws IOException, HttpException {
return impl.execute(ClassicRequestCopier.INSTANCE.copy(request), new ExecChain.Scope(
"test", route, request, mockEndpoint, context), mockExecChain);
return impl.execute(
ClassicRequestBuilder.copy(request).build(),
new ExecChain.Scope("test", route, request, mockEndpoint, context),
mockExecChain);
}

public static ClassicHttpRequest eqRequest(final ClassicHttpRequest in) {
@@ -33,14 +33,15 @@

import org.apache.hc.client5.http.cache.HeaderConstants;
import org.apache.hc.client5.http.cache.HttpCacheEntry;
import org.apache.hc.client5.http.impl.RequestCopier;
import org.apache.hc.client5.http.utils.DateUtils;
import org.apache.hc.core5.function.Factory;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HeaderElement;
import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.message.BasicHeader;
import org.apache.hc.core5.http.message.BasicHttpRequest;
import org.apache.hc.core5.http.message.MessageSupport;
import org.apache.hc.core5.http.support.BasicRequestBuilder;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
@@ -52,7 +53,14 @@ public class TestConditionalRequestBuilder {

@Before
public void setUp() throws Exception {
impl = new ConditionalRequestBuilder<>(RequestCopier.INSTANCE);
impl = new ConditionalRequestBuilder<>(new Factory<HttpRequest, HttpRequest>() {

@Override
public HttpRequest create(final HttpRequest request) {
return BasicRequestBuilder.copy(request).build();
}

});
request = new BasicHttpRequest("GET", "/");
}

@@ -64,16 +72,13 @@ public void testBuildConditionalRequestWithLastModified() {

final HttpRequest basicRequest = new BasicHttpRequest(theMethod, theUri);
basicRequest.addHeader("Accept-Encoding", "gzip");
final HttpRequest requestWrapper = RequestCopier.INSTANCE.copy(basicRequest);

final Header[] headers = new Header[] {
new BasicHeader("Date", DateUtils.formatDate(new Date())),
new BasicHeader("Last-Modified", lastModified) };

final HttpCacheEntry cacheEntry = HttpTestUtils.makeCacheEntry(headers);
final HttpRequest newRequest = impl.buildConditionalRequest(requestWrapper, cacheEntry);

Assert.assertNotSame(basicRequest, newRequest);
final HttpRequest newRequest = impl.buildConditionalRequest(basicRequest, cacheEntry);

Assert.assertEquals(theMethod, newRequest.getMethod());
Assert.assertEquals(theUri, newRequest.getRequestUri());
@@ -100,9 +105,8 @@ public void testConditionalRequestForEntryWithLastModifiedAndEtagIncludesBothAsV
new BasicHeader("ETag", etag)
};
final HttpRequest basicRequest = new BasicHttpRequest("GET", "/");
final HttpRequest requestWrapper = RequestCopier.INSTANCE.copy(basicRequest);
final HttpCacheEntry cacheEntry = HttpTestUtils.makeCacheEntry(headers);
final HttpRequest result = impl.buildConditionalRequest(requestWrapper, cacheEntry);
final HttpRequest result = impl.buildConditionalRequest(basicRequest, cacheEntry);
Assert.assertEquals(lmDate,
result.getFirstHeader("If-Modified-Since").getValue());
Assert.assertEquals(etag,
@@ -117,7 +121,6 @@ public void testBuildConditionalRequestWithETag() {

final HttpRequest basicRequest = new BasicHttpRequest(theMethod, theUri);
basicRequest.addHeader("Accept-Encoding", "gzip");
final HttpRequest requestWrapper = RequestCopier.INSTANCE.copy(basicRequest);

final Header[] headers = new Header[] {
new BasicHeader("Date", DateUtils.formatDate(new Date())),
@@ -126,9 +129,7 @@ public void testBuildConditionalRequestWithETag() {

final HttpCacheEntry cacheEntry = HttpTestUtils.makeCacheEntry(headers);

final HttpRequest newRequest = impl.buildConditionalRequest(requestWrapper, cacheEntry);

Assert.assertNotSame(basicRequest, newRequest);
final HttpRequest newRequest = impl.buildConditionalRequest(basicRequest, cacheEntry);

Assert.assertEquals(theMethod, newRequest.getMethod());
Assert.assertEquals(theUri, newRequest.getRequestUri());
@@ -145,7 +146,6 @@ public void testBuildConditionalRequestWithETag() {
@Test
public void testCacheEntryWithMustRevalidateDoesEndToEndRevalidation() throws Exception {
final HttpRequest basicRequest = new BasicHttpRequest("GET","/");
final HttpRequest requestWrapper = RequestCopier.INSTANCE.copy(basicRequest);
final Date now = new Date();
final Date elevenSecondsAgo = new Date(now.getTime() - 11 * 1000L);
final Date tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
@@ -157,7 +157,7 @@ public void testCacheEntryWithMustRevalidateDoesEndToEndRevalidation() throws Ex
new BasicHeader("Cache-Control","max-age=5, must-revalidate") };
final HttpCacheEntry cacheEntry = HttpTestUtils.makeCacheEntry(elevenSecondsAgo, nineSecondsAgo, cacheEntryHeaders);

final HttpRequest result = impl.buildConditionalRequest(requestWrapper, cacheEntry);
final HttpRequest result = impl.buildConditionalRequest(basicRequest, cacheEntry);

boolean foundMaxAge0 = false;

@@ -174,7 +174,6 @@ public void testCacheEntryWithMustRevalidateDoesEndToEndRevalidation() throws Ex
@Test
public void testCacheEntryWithProxyRevalidateDoesEndToEndRevalidation() throws Exception {
final HttpRequest basicRequest = new BasicHttpRequest("GET", "/");
final HttpRequest requestWrapper = RequestCopier.INSTANCE.copy(basicRequest);
final Date now = new Date();
final Date elevenSecondsAgo = new Date(now.getTime() - 11 * 1000L);
final Date tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
@@ -186,7 +185,7 @@ public void testCacheEntryWithProxyRevalidateDoesEndToEndRevalidation() throws E
new BasicHeader("Cache-Control","max-age=5, proxy-revalidate") };
final HttpCacheEntry cacheEntry = HttpTestUtils.makeCacheEntry(elevenSecondsAgo, nineSecondsAgo, cacheEntryHeaders);

final HttpRequest result = impl.buildConditionalRequest(requestWrapper, cacheEntry);
final HttpRequest result = impl.buildConditionalRequest(basicRequest, cacheEntry);

boolean foundMaxAge0 = false;
final Iterator<HeaderElement> it = MessageSupport.iterate(result, HeaderConstants.CACHE_CONTROL);
@@ -36,7 +36,6 @@
import org.apache.hc.client5.http.classic.ExecChain;
import org.apache.hc.client5.http.classic.ExecChainHandler;
import org.apache.hc.client5.http.classic.ExecRuntime;
import org.apache.hc.client5.http.impl.classic.ClassicRequestCopier;
import org.apache.hc.client5.http.utils.DateUtils;
import org.apache.hc.core5.http.ClassicHttpRequest;
import org.apache.hc.core5.http.ClassicHttpResponse;
@@ -115,8 +114,9 @@ public void setUp() {
}

private ClassicHttpResponse execute(final ClassicHttpRequest request) throws IOException, HttpException {
return impl.execute(ClassicRequestCopier.INSTANCE.copy(request), new ExecChain.Scope(
"test", route, request, mockEndpoint, context), mockExecChain);
return impl.execute(request,
new ExecChain.Scope("test", route, request, mockEndpoint, context),
mockExecChain);
}

protected ExecChainHandler createCachingExecChain(final HttpCache cache, final CacheConfig config) {

0 comments on commit 3de8829

Please sign in to comment.