diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/cache/HttpCacheContext.java b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/cache/HttpCacheContext.java index 6f3c093a0c..65936b619a 100644 --- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/cache/HttpCacheContext.java +++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/cache/HttpCacheContext.java @@ -68,7 +68,7 @@ public static HttpCacheContext adapt(final HttpContext context) { } public static HttpCacheContext create() { - return new HttpCacheContext(new HttpClientContext()); + return new HttpCacheContext(); } public HttpCacheContext(final HttpContext context) { diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/AsyncCachingExec.java b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/AsyncCachingExec.java index 5c08c53368..752fc4e0e3 100644 --- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/AsyncCachingExec.java +++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/AsyncCachingExec.java @@ -804,7 +804,7 @@ private void handleCacheHit( scope.route, scope.originalRequest, new ComplexFuture<>(null), - HttpClientContext.create(), + HttpCacheContext.create(), scope.execRuntime.fork(), scope.scheduler, scope.execCount); diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachingExec.java b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachingExec.java index 1e58d94320..e76e891a5e 100644 --- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachingExec.java +++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachingExec.java @@ -318,7 +318,7 @@ private ClassicHttpResponse handleCacheHit( scope.route, scope.originalRequest, scope.execRuntime.fork(null), - HttpClientContext.create()); + HttpCacheContext.create()); if (LOG.isDebugEnabled()) { LOG.debug("{} starting asynchronous revalidation exchange {}", exchangeId, revalidationExchangeId); } diff --git a/httpclient5-fluent/src/main/java/org/apache/hc/client5/http/fluent/Executor.java b/httpclient5-fluent/src/main/java/org/apache/hc/client5/http/fluent/Executor.java index bdc49b0a75..a676c3d309 100644 --- a/httpclient5-fluent/src/main/java/org/apache/hc/client5/http/fluent/Executor.java +++ b/httpclient5-fluent/src/main/java/org/apache/hc/client5/http/fluent/Executor.java @@ -232,14 +232,14 @@ public Response execute( final HttpClientContext localContext = HttpClientContext.create(); final CredentialsStore credentialsStoreSnapshot = credentialsStore; if (credentialsStoreSnapshot != null) { - localContext.setAttribute(HttpClientContext.CREDS_PROVIDER, credentialsStoreSnapshot); + localContext.setCredentialsProvider(credentialsStoreSnapshot); } if (this.authCache != null) { - localContext.setAttribute(HttpClientContext.AUTH_CACHE, this.authCache); + localContext.setAuthCache(this.authCache); } final CookieStore cookieStoreSnapshot = cookieStore; if (cookieStoreSnapshot != null) { - localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStoreSnapshot); + localContext.setCookieStore(cookieStoreSnapshot); } return new Response(request.internalExecute(this.httpclient, localContext)); } diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/ContextBuilder.java b/httpclient5/src/main/java/org/apache/hc/client5/http/ContextBuilder.java index 178eba6bf0..8878f15504 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/ContextBuilder.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/ContextBuilder.java @@ -102,7 +102,7 @@ public ContextBuilder preemptiveBasicAuth(final HttpHost host, final UsernamePas @Override protected HttpClientContext createContext() { - return new HttpClientContext(); + return HttpClientContext.create(); } } diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/async/AsyncExecChain.java b/httpclient5/src/main/java/org/apache/hc/client5/http/async/AsyncExecChain.java index 2ba4c192de..32511417eb 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/async/AsyncExecChain.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/async/AsyncExecChain.java @@ -81,7 +81,7 @@ public Scope( this.route = Args.notNull(route, "Route"); this.originalRequest = Args.notNull(originalRequest, "Original request"); this.cancellableDependency = Args.notNull(cancellableDependency, "Dependency"); - this.clientContext = clientContext != null ? clientContext : HttpClientContext.create(); + this.clientContext = Args.notNull(clientContext, "HTTP context"); this.execRuntime = Args.notNull(execRuntime, "Exec runtime"); this.scheduler = scheduler; this.execCount = execCount != null ? execCount : new AtomicInteger(1); diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/classic/ExecChain.java b/httpclient5/src/main/java/org/apache/hc/client5/http/classic/ExecChain.java index d79c14b947..63d785a354 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/classic/ExecChain.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/classic/ExecChain.java @@ -59,7 +59,7 @@ public Scope(final String exchangeId, final HttpRoute route, final ClassicHttpRe this.route = Args.notNull(route, "Route"); this.originalRequest = Args.notNull(originalRequest, "Original request"); this.execRuntime = Args.notNull(execRuntime, "Exec runtime"); - this.clientContext = clientContext != null ? clientContext : HttpClientContext.create(); + this.clientContext = Args.notNull(clientContext, "HTTP context"); } } diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/DefaultAuthenticationStrategy.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/DefaultAuthenticationStrategy.java index 726c346634..1d93498c7d 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/DefaultAuthenticationStrategy.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/DefaultAuthenticationStrategy.java @@ -77,7 +77,7 @@ public List select( Args.notNull(challengeType, "ChallengeType"); Args.notNull(challenges, "Map of auth challenges"); Args.notNull(context, "HTTP context"); - final HttpClientContext clientContext = HttpClientContext.adapt(context); + final HttpClientContext clientContext = HttpClientContext.cast(context); final String exchangeId = clientContext.getExchangeId(); final List options = new ArrayList<>(); @@ -88,7 +88,7 @@ public List select( } return options; } - final RequestConfig config = clientContext.getRequestConfig(); + final RequestConfig config = clientContext.getRequestConfigOrDefault(); Collection authPrefs = challengeType == ChallengeType.TARGET ? config.getTargetPreferredAuthSchemes() : config.getProxyPreferredAuthSchemes(); if (authPrefs == null) { diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/DefaultConnectionKeepAliveStrategy.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/DefaultConnectionKeepAliveStrategy.java index 55bd3a3b4e..5d0cf54f3f 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/DefaultConnectionKeepAliveStrategy.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/DefaultConnectionKeepAliveStrategy.java @@ -71,8 +71,8 @@ public TimeValue getKeepAliveDuration(final HttpResponse response, final HttpCon } } } - final HttpClientContext clientContext = HttpClientContext.adapt(context); - final RequestConfig requestConfig = clientContext.getRequestConfig(); + final HttpClientContext clientContext = HttpClientContext.cast(context); + final RequestConfig requestConfig = clientContext.getRequestConfigOrDefault(); return requestConfig.getConnectionKeepAlive(); } diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/DefaultUserTokenHandler.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/DefaultUserTokenHandler.java index a41872fd82..cf21866807 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/DefaultUserTokenHandler.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/DefaultUserTokenHandler.java @@ -69,7 +69,7 @@ public Object getUserToken(final HttpRoute route, final HttpContext context) { @Override public Object getUserToken(final HttpRoute route, final HttpRequest request, final HttpContext context) { - final HttpClientContext clientContext = HttpClientContext.adapt(context); + final HttpClientContext clientContext = HttpClientContext.cast(context); final HttpHost target = request != null ? new HttpHost(request.getScheme(), request.getAuthority()) : route.getTargetHost(); diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/AbstractMinimalHttpAsyncClientBase.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/AbstractMinimalHttpAsyncClientBase.java index 9058a967a0..39f91eea44 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/AbstractMinimalHttpAsyncClientBase.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/AbstractMinimalHttpAsyncClientBase.java @@ -29,7 +29,6 @@ import java.util.concurrent.Future; import java.util.concurrent.ThreadFactory; -import org.apache.hc.client5.http.protocol.HttpClientContext; import org.apache.hc.core5.concurrent.Cancellable; import org.apache.hc.core5.concurrent.ComplexFuture; import org.apache.hc.core5.concurrent.FutureCallback; @@ -86,7 +85,7 @@ public void cancelled() { } public final Cancellable execute(final AsyncClientExchangeHandler exchangeHandler) { - return execute(exchangeHandler, null, HttpClientContext.create()); + return execute(exchangeHandler, null, null); } public abstract Cancellable execute( diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/AsyncConnectExec.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/AsyncConnectExec.java index cb67c84908..40c87b8eec 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/AsyncConnectExec.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/AsyncConnectExec.java @@ -509,7 +509,7 @@ private boolean needAuthentication( final HttpHost proxy, final HttpResponse response, final HttpClientContext context) { - final RequestConfig config = context.getRequestConfig(); + final RequestConfig config = context.getRequestConfigOrDefault(); if (config.isAuthenticationEnabled()) { final boolean proxyAuthRequested = authenticator.isChallenged(proxy, ChallengeType.PROXY, response, proxyAuthExchange, context); diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/AsyncProtocolExec.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/AsyncProtocolExec.java index 2f696bbd5c..907b23e46b 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/AsyncProtocolExec.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/AsyncProtocolExec.java @@ -306,7 +306,7 @@ private boolean needAuthentication( final String pathPrefix, final HttpResponse response, final HttpClientContext context) { - final RequestConfig config = context.getRequestConfig(); + final RequestConfig config = context.getRequestConfigOrDefault(); if (config.isAuthenticationEnabled()) { final boolean targetAuthRequested = authenticator.isChallenged( target, ChallengeType.TARGET, response, targetAuthExchange, context); diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/AsyncRedirectExec.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/AsyncRedirectExec.java index 225ff1efbc..b213abe51d 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/AsyncRedirectExec.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/AsyncRedirectExec.java @@ -118,7 +118,7 @@ public AsyncDataConsumer handleResponse( final EntityDetails entityDetails) throws HttpException, IOException { state.redirectURI = null; - final RequestConfig config = clientContext.getRequestConfig(); + final RequestConfig config = clientContext.getRequestConfigOrDefault(); if (config.isRedirectsEnabled() && redirectStrategy.isRedirected(request, response, clientContext)) { if (state.redirectCount >= state.maxRedirects) { throw new RedirectException("Maximum redirects (" + state.maxRedirects + ") exceeded"); @@ -263,11 +263,11 @@ public void execute( RedirectLocations redirectLocations = clientContext.getRedirectLocations(); if (redirectLocations == null) { redirectLocations = new RedirectLocations(); - clientContext.setAttribute(HttpClientContext.REDIRECT_LOCATIONS, redirectLocations); + clientContext.setRedirectLocations(redirectLocations); } redirectLocations.clear(); - final RequestConfig config = clientContext.getRequestConfig(); + final RequestConfig config = clientContext.getRequestConfigOrDefault(); final State state = new State(); state.maxRedirects = config.getMaxRedirects() > 0 ? config.getMaxRedirects() : 50; diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/CloseableHttpAsyncClient.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/CloseableHttpAsyncClient.java index 8abe0fc7fc..dde15a9c4e 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/CloseableHttpAsyncClient.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/CloseableHttpAsyncClient.java @@ -33,7 +33,6 @@ import org.apache.hc.client5.http.async.methods.SimpleHttpResponse; import org.apache.hc.client5.http.async.methods.SimpleRequestProducer; import org.apache.hc.client5.http.async.methods.SimpleResponseConsumer; -import org.apache.hc.client5.http.protocol.HttpClientContext; import org.apache.hc.core5.annotation.Contract; import org.apache.hc.core5.annotation.ThreadingBehavior; import org.apache.hc.core5.concurrent.FutureCallback; @@ -113,7 +112,7 @@ public final Future execute( final FutureCallback callback) { Args.notNull(requestProducer, "Request producer"); Args.notNull(responseConsumer, "Response consumer"); - return execute(requestProducer, responseConsumer, HttpClientContext.create(), callback); + return execute(requestProducer, responseConsumer, null, callback); } public final Future execute( @@ -127,7 +126,7 @@ public final Future execute( public final Future execute( final SimpleHttpRequest request, final FutureCallback callback) { - return execute(request, HttpClientContext.create(), callback); + return execute(request, null, callback); } public abstract void register(String hostname, String uriPattern, Supplier supplier); diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/H2AsyncMainClientExec.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/H2AsyncMainClientExec.java index f961ff52e9..563d542e31 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/H2AsyncMainClientExec.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/H2AsyncMainClientExec.java @@ -125,9 +125,8 @@ public void cancel() { @Override public void produceRequest(final RequestChannel channel, final HttpContext context) throws HttpException, IOException { - - clientContext.setAttribute(HttpClientContext.HTTP_ROUTE, route); clientContext.setRequest(request); + clientContext.setRoute(route); httpProcessor.process(request, entityProducer, clientContext); channel.sendRequest(request, entityProducer, context); diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncMainClientExec.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncMainClientExec.java index 4221eadc15..5abddaf1f2 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncMainClientExec.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncMainClientExec.java @@ -148,7 +148,7 @@ public void produceRequest( final RequestChannel channel, final HttpContext context) throws HttpException, IOException { - clientContext.setAttribute(HttpClientContext.HTTP_ROUTE, route); + clientContext.setRoute(route); clientContext.setRequest(request); httpProcessor.process(request, entityProducer, clientContext); @@ -248,7 +248,7 @@ public void consumeResponse( Object userToken = clientContext.getUserToken(); if (userToken == null) { userToken = userTokenHandler.getUserToken(route, request, clientContext); - clientContext.setAttribute(HttpClientContext.USER_TOKEN, userToken); + clientContext.setUserToken(userToken); } execRuntime.markConnectionReusable(userToken, keepAliveDuration); if (entityDetails == null) { diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/InternalAbstractHttpAsyncClient.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/InternalAbstractHttpAsyncClient.java index 1795cd3831..0412243647 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/InternalAbstractHttpAsyncClient.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/InternalAbstractHttpAsyncClient.java @@ -84,6 +84,7 @@ abstract class InternalAbstractHttpAsyncClient extends AbstractHttpAsyncClientBa private final static ThreadFactory SCHEDULER_THREAD_FACTORY = new DefaultThreadFactory("Scheduled-executor", true); private static final Logger LOG = LoggerFactory.getLogger(InternalAbstractHttpAsyncClient.class); + private final AsyncExecChainElement execChain; private final Lookup cookieSpecRegistry; private final Lookup authSchemeRegistry; @@ -165,20 +166,20 @@ void internalClose(final CloseMode closeMode) { } private void setupContext(final HttpClientContext context) { - if (context.getAttribute(HttpClientContext.AUTHSCHEME_REGISTRY) == null) { - context.setAttribute(HttpClientContext.AUTHSCHEME_REGISTRY, authSchemeRegistry); + if (context.getAuthSchemeRegistry() == null) { + context.setAuthSchemeRegistry(authSchemeRegistry); } - if (context.getAttribute(HttpClientContext.COOKIESPEC_REGISTRY) == null) { - context.setAttribute(HttpClientContext.COOKIESPEC_REGISTRY, cookieSpecRegistry); + if (context.getCookieSpecRegistry() == null) { + context.setCookieSpecRegistry(cookieSpecRegistry); } - if (context.getAttribute(HttpClientContext.COOKIE_STORE) == null) { - context.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore); + if (context.getCookieStore() == null) { + context.setCookieStore(cookieStore); } - if (context.getAttribute(HttpClientContext.CREDS_PROVIDER) == null) { - context.setAttribute(HttpClientContext.CREDS_PROVIDER, credentialsProvider); + if (context.getCredentialsProvider() == null) { + context.setCredentialsProvider(credentialsProvider); } - if (context.getAttribute(HttpClientContext.REQUEST_CONFIG) == null) { - context.setAttribute(HttpClientContext.REQUEST_CONFIG, defaultConfig); + if (context.getRequestConfig() == null) { + context.setRequestConfig(defaultConfig); } } @@ -199,7 +200,7 @@ protected Future doExecute( if (!isRunning()) { throw new CancellationException("Request execution cancelled"); } - final HttpClientContext clientContext = context != null ? HttpClientContext.adapt(context) : HttpClientContext.create(); + final HttpClientContext clientContext = HttpClientContext.adapt(context); requestProducer.sendRequest((request, entityDetails, c) -> { RequestConfig requestConfig = null; diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/InternalH2AsyncExecRuntime.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/InternalH2AsyncExecRuntime.java index 8ebe34f539..a09e09a417 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/InternalH2AsyncExecRuntime.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/InternalH2AsyncExecRuntime.java @@ -87,7 +87,7 @@ public Cancellable acquireEndpoint( final HttpClientContext context, final FutureCallback callback) { if (sessionRef.get() == null) { - final RequestConfig requestConfig = context.getRequestConfig(); + final RequestConfig requestConfig = context.getRequestConfigOrDefault(); @SuppressWarnings("deprecation") final Timeout connectTimeout = requestConfig.getConnectTimeout(); if (log.isDebugEnabled()) { @@ -183,7 +183,7 @@ public Cancellable connectEndpoint( return Operations.nonCancellable(); } final HttpRoute route = endpoint.route; - final RequestConfig requestConfig = context.getRequestConfig(); + final RequestConfig requestConfig = context.getRequestConfigOrDefault(); @SuppressWarnings("deprecation") final Timeout connectTimeout = requestConfig.getConnectTimeout(); if (log.isDebugEnabled()) { @@ -262,7 +262,7 @@ public Cancellable execute( Command.Priority.NORMAL); } else { final HttpRoute route = endpoint.route; - final RequestConfig requestConfig = context.getRequestConfig(); + final RequestConfig requestConfig = context.getRequestConfigOrDefault(); @SuppressWarnings("deprecation") final Timeout connectTimeout = requestConfig.getConnectTimeout(); connPool.getSession(route, connectTimeout, new FutureCallback() { diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/InternalHttpAsyncExecRuntime.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/InternalHttpAsyncExecRuntime.java index 197ac37308..a1e33e1d66 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/InternalHttpAsyncExecRuntime.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/InternalHttpAsyncExecRuntime.java @@ -98,7 +98,7 @@ public Cancellable acquireEndpoint( final FutureCallback callback) { if (endpointRef.get() == null) { state = object; - final RequestConfig requestConfig = context.getRequestConfig(); + final RequestConfig requestConfig = context.getRequestConfigOrDefault(); final Timeout connectionRequestTimeout = requestConfig.getConnectionRequestTimeout(); if (log.isDebugEnabled()) { log.debug("{} acquiring endpoint ({})", id, connectionRequestTimeout); @@ -208,7 +208,7 @@ public Cancellable connectEndpoint( callback.completed(this); return Operations.nonCancellable(); } - final RequestConfig requestConfig = context.getRequestConfig(); + final RequestConfig requestConfig = context.getRequestConfigOrDefault(); @SuppressWarnings("deprecation") final Timeout connectTimeout = requestConfig.getConnectTimeout(); if (log.isDebugEnabled()) { @@ -280,13 +280,13 @@ public Cancellable execute( if (log.isDebugEnabled()) { log.debug("{} start execution {}", ConnPoolSupport.getId(endpoint), id); } - final RequestConfig requestConfig = context.getRequestConfig(); + final RequestConfig requestConfig = context.getRequestConfigOrDefault(); final Timeout responseTimeout = requestConfig.getResponseTimeout(); if (responseTimeout != null) { endpoint.setSocketTimeout(responseTimeout); } endpoint.execute(id, exchangeHandler, context); - if (context.getRequestConfig().isHardCancellationEnabled()) { + if (context.getRequestConfigOrDefault().isHardCancellationEnabled()) { return () -> { exchangeHandler.cancel(); return true; diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/MinimalH2AsyncClient.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/MinimalH2AsyncClient.java index 261541ef6a..cd430d1030 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/MinimalH2AsyncClient.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/MinimalH2AsyncClient.java @@ -71,6 +71,7 @@ import org.apache.hc.core5.reactor.IOEventHandlerFactory; import org.apache.hc.core5.reactor.IOReactorConfig; import org.apache.hc.core5.reactor.IOSession; +import org.apache.hc.core5.util.Args; import org.apache.hc.core5.util.Timeout; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -122,12 +123,13 @@ public Cancellable execute( final AsyncClientExchangeHandler exchangeHandler, final HandlerFactory pushHandlerFactory, final HttpContext context) { + Args.notNull(exchangeHandler, "Message exchange handler"); final ComplexCancellable cancellable = new ComplexCancellable(); try { if (!isRunning()) { throw new CancellationException("Request execution cancelled"); } - final HttpClientContext clientContext = context != null ? HttpClientContext.adapt(context) : HttpClientContext.create(); + final HttpClientContext clientContext = HttpClientContext.adapt(context); exchangeHandler.produceRequest((request, entityDetails, context1) -> { RequestConfig requestConfig = null; if (request instanceof Configurable) { @@ -136,7 +138,7 @@ public Cancellable execute( if (requestConfig != null) { clientContext.setRequestConfig(requestConfig); } else { - requestConfig = clientContext.getRequestConfig(); + requestConfig = clientContext.getRequestConfigOrDefault(); } @SuppressWarnings("deprecation") final Timeout connectTimeout = requestConfig.getConnectTimeout(); diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/MinimalHttpAsyncClient.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/MinimalHttpAsyncClient.java index 1a4b5e6012..0c71ca1c08 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/MinimalHttpAsyncClient.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/MinimalHttpAsyncClient.java @@ -205,7 +205,7 @@ public void cancelled() { public Future lease( final HttpHost host, final FutureCallback callback) { - return lease(host, HttpClientContext.create(), callback); + return lease(host, null, callback); } public Future lease( @@ -213,14 +213,13 @@ public Future lease( final HttpContext context, final FutureCallback callback) { Args.notNull(host, "Host"); - Args.notNull(context, "HTTP context"); final BasicFuture future = new BasicFuture<>(callback); if (!isRunning()) { future.failed(new CancellationException("Connection lease cancelled")); return future; } final HttpClientContext clientContext = HttpClientContext.adapt(context); - final RequestConfig requestConfig = clientContext.getRequestConfig(); + final RequestConfig requestConfig = clientContext.getRequestConfigOrDefault(); final Timeout connectionRequestTimeout = requestConfig.getConnectionRequestTimeout(); @SuppressWarnings("deprecation") final Timeout connectTimeout = requestConfig.getConnectTimeout(); @@ -260,7 +259,7 @@ public Cancellable execute( if (!isRunning()) { throw new CancellationException("Request execution cancelled"); } - final HttpClientContext clientContext = context != null ? HttpClientContext.adapt(context) : HttpClientContext.create(); + final HttpClientContext clientContext = HttpClientContext.adapt(context); exchangeHandler.produceRequest((request, entityDetails, context1) -> { RequestConfig requestConfig = null; if (request instanceof Configurable) { @@ -269,7 +268,7 @@ public Cancellable execute( if (requestConfig != null) { clientContext.setRequestConfig(requestConfig); } else { - requestConfig = clientContext.getRequestConfig(); + requestConfig = clientContext.getRequestConfigOrDefault(); } final Timeout connectionRequestTimeout = requestConfig.getConnectionRequestTimeout(); @SuppressWarnings("deprecation") @@ -456,7 +455,7 @@ public void execute( final HttpContext context) { Asserts.check(!released.get(), "Endpoint has already been released"); - final HttpClientContext clientContext = context != null ? HttpClientContext.adapt(context) : HttpClientContext.create(); + final HttpClientContext clientContext = HttpClientContext.adapt(context); final String exchangeId = ExecSupport.getNextExchangeId(); clientContext.setExchangeId(exchangeId); if (LOG.isDebugEnabled()) { diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/AuthCacheKeeper.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/AuthCacheKeeper.java index 10ce0b6baa..4222c96648 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/AuthCacheKeeper.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/AuthCacheKeeper.java @@ -62,7 +62,7 @@ public void updateOnChallenge(final HttpHost host, final String pathPrefix, final AuthExchange authExchange, final HttpContext context) { - clearCache(host, pathPrefix, HttpClientContext.adapt(context)); + clearCache(host, pathPrefix, HttpClientContext.cast(context)); } public void updateOnNoChallenge(final HttpHost host, @@ -70,7 +70,7 @@ public void updateOnNoChallenge(final HttpHost host, final AuthExchange authExchange, final HttpContext context) { if (authExchange.getState() == AuthExchange.State.SUCCESS) { - updateCache(host, pathPrefix, authExchange.getAuthScheme(), HttpClientContext.adapt(context)); + updateCache(host, pathPrefix, authExchange.getAuthScheme(), HttpClientContext.cast(context)); } } @@ -79,7 +79,7 @@ public void updateOnResponse(final HttpHost host, final AuthExchange authExchange, final HttpContext context) { if (authExchange.getState() == AuthExchange.State.FAILURE) { - clearCache(host, pathPrefix, HttpClientContext.adapt(context)); + clearCache(host, pathPrefix, HttpClientContext.cast(context)); } } @@ -88,9 +88,9 @@ public void loadPreemptively(final HttpHost host, final AuthExchange authExchange, final HttpContext context) { if (authExchange.getState() == AuthExchange.State.UNCHALLENGED) { - AuthScheme authScheme = loadFromCache(host, pathPrefix, HttpClientContext.adapt(context)); + AuthScheme authScheme = loadFromCache(host, pathPrefix, HttpClientContext.cast(context)); if (authScheme == null && pathPrefix != null) { - authScheme = loadFromCache(host, null, HttpClientContext.adapt(context)); + authScheme = loadFromCache(host, null, HttpClientContext.cast(context)); } if (authScheme != null) { authExchange.select(authScheme); diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/BasicScheme.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/BasicScheme.java index 1d2c5b59c2..34ceaf4745 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/BasicScheme.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/BasicScheme.java @@ -162,7 +162,7 @@ public boolean isResponseReady( } if (LOG.isDebugEnabled()) { - final HttpClientContext clientContext = HttpClientContext.adapt(context); + final HttpClientContext clientContext = HttpClientContext.cast(context); final String exchangeId = clientContext.getExchangeId(); LOG.debug("{} No credentials found for auth scope [{}]", exchangeId, authScope); } diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/BearerScheme.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/BearerScheme.java index 113f1d3809..dd1d17d271 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/BearerScheme.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/BearerScheme.java @@ -141,7 +141,7 @@ public boolean isResponseReady( } if (LOG.isDebugEnabled()) { - final HttpClientContext clientContext = HttpClientContext.adapt(context); + final HttpClientContext clientContext = HttpClientContext.cast(context); final String exchangeId = clientContext.getExchangeId(); LOG.debug("{} No credentials found for auth scope [{}]", exchangeId, authScope); } diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/DigestScheme.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/DigestScheme.java index e2450c5e60..84ae79585f 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/DigestScheme.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/DigestScheme.java @@ -227,7 +227,7 @@ public boolean isResponseReady( } if (LOG.isDebugEnabled()) { - final HttpClientContext clientContext = HttpClientContext.adapt(context); + final HttpClientContext clientContext = HttpClientContext.cast(context); final String exchangeId = clientContext.getExchangeId(); LOG.debug("{} No credentials found for auth scope [{}]", exchangeId, authScope); } diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/GGSSchemeBase.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/GGSSchemeBase.java index dddf512dfb..e8fdc3b6c3 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/GGSSchemeBase.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/GGSSchemeBase.java @@ -118,7 +118,7 @@ public void processChallenge( state = State.CHALLENGE_RECEIVED; } else { if (LOG.isDebugEnabled()) { - final HttpClientContext clientContext = HttpClientContext.adapt(context); + final HttpClientContext clientContext = HttpClientContext.cast(context); final String exchangeId = clientContext.getExchangeId(); LOG.debug("{} Authentication already attempted", exchangeId); } @@ -225,7 +225,7 @@ public String generateAuthResponse( } if (LOG.isDebugEnabled()) { - final HttpClientContext clientContext = HttpClientContext.adapt(context); + final HttpClientContext clientContext = HttpClientContext.cast(context); final String exchangeId = clientContext.getExchangeId(); LOG.debug("{} init {}", exchangeId, authServer); } @@ -252,7 +252,7 @@ public String generateAuthResponse( final Base64 codec = new Base64(0); final String tokenstr = new String(codec.encode(token)); if (LOG.isDebugEnabled()) { - final HttpClientContext clientContext = HttpClientContext.adapt(context); + final HttpClientContext clientContext = HttpClientContext.cast(context); final String exchangeId = clientContext.getExchangeId(); LOG.debug("{} Sending response '{}' back to the auth server", exchangeId, tokenstr); } diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/HttpAuthenticator.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/HttpAuthenticator.java index a5507fdd6c..cd9f7ce723 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/HttpAuthenticator.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/HttpAuthenticator.java @@ -109,7 +109,7 @@ public boolean isChallenged( throw new IllegalStateException("Unexpected challenge type: " + challengeType); } - final HttpClientContext clientContext = HttpClientContext.adapt(context); + final HttpClientContext clientContext = HttpClientContext.cast(context); final String exchangeId = clientContext.getExchangeId(); if (response.getCode() == challengeCode) { @@ -155,7 +155,7 @@ public boolean updateAuthState( final AuthExchange authExchange, final HttpContext context) { - final HttpClientContext clientContext = HttpClientContext.adapt(context); + final HttpClientContext clientContext = HttpClientContext.cast(context); final String exchangeId = clientContext.getExchangeId(); if (LOG.isDebugEnabled()) { @@ -304,7 +304,7 @@ public void addAuthResponse( final HttpRequest request, final AuthExchange authExchange, final HttpContext context) { - final HttpClientContext clientContext = HttpClientContext.adapt(context); + final HttpClientContext clientContext = HttpClientContext.cast(context); final String exchangeId = clientContext.getExchangeId(); AuthScheme authScheme = authExchange.getAuthScheme(); switch (authExchange.getState()) { diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/NTLMScheme.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/NTLMScheme.java index c8f52c886a..080b1f8450 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/NTLMScheme.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/NTLMScheme.java @@ -146,7 +146,7 @@ public boolean isResponseReady( } if (LOG.isDebugEnabled()) { - final HttpClientContext clientContext = HttpClientContext.adapt(context); + final HttpClientContext clientContext = HttpClientContext.cast(context); final String exchangeId = clientContext.getExchangeId(); LOG.debug("{} No credentials found for auth scope [{}]", exchangeId, authScope); } diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/SystemDefaultCredentialsProvider.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/SystemDefaultCredentialsProvider.java index f8d453b955..5086e01a0e 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/SystemDefaultCredentialsProvider.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/SystemDefaultCredentialsProvider.java @@ -105,7 +105,7 @@ public Credentials getCredentials(final AuthScope authScope, final HttpContext c } final String host = authScope.getHost(); if (host != null) { - final HttpClientContext clientContext = context != null ? HttpClientContext.adapt(context) : null; + final HttpClientContext clientContext = context != null ? HttpClientContext.cast(context) : null; final String protocol = authScope.getProtocol() != null ? authScope.getProtocol() : (authScope.getPort() == 443 ? URIScheme.HTTPS.id : URIScheme.HTTP.id); PasswordAuthentication systemcreds = getSystemCreds( protocol, authScope, Authenticator.RequestorType.SERVER, clientContext); diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/ConnectExec.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/ConnectExec.java index e303647de3..27fa438e71 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/ConnectExec.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/ConnectExec.java @@ -214,7 +214,7 @@ private boolean createTunnelToTarget( final ExecRuntime execRuntime, final HttpClientContext context) throws HttpException, IOException { - final RequestConfig config = context.getRequestConfig(); + final RequestConfig config = context.getRequestConfigOrDefault(); final HttpHost target = route.getTargetHost(); final HttpHost proxy = route.getProxyHost(); diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/ContentCompressionExec.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/ContentCompressionExec.java index 0ffe820ce1..fd910d1f9c 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/ContentCompressionExec.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/ContentCompressionExec.java @@ -137,7 +137,7 @@ public ClassicHttpResponse execute( Args.notNull(scope, "Scope"); final HttpClientContext clientContext = scope.clientContext; - final RequestConfig requestConfig = clientContext.getRequestConfig(); + final RequestConfig requestConfig = clientContext.getRequestConfigOrDefault(); /* Signal support for Accept-Encoding transfer encodings. */ if (!request.containsHeader(HttpHeaders.ACCEPT_ENCODING) && requestConfig.isContentCompressionEnabled()) { diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/HttpRequestRetryExec.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/HttpRequestRetryExec.java index fbd464c94d..23c49d557e 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/HttpRequestRetryExec.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/HttpRequestRetryExec.java @@ -167,7 +167,7 @@ public ClassicHttpResponse execute( final TimeValue nextInterval = retryStrategy.getRetryInterval(response, execCount, context); // Make sure the retry interval does not exceed the response timeout if (TimeValue.isPositive(nextInterval)) { - final RequestConfig requestConfig = context.getRequestConfig(); + final RequestConfig requestConfig = context.getRequestConfigOrDefault(); final Timeout responseTimeout = requestConfig.getResponseTimeout(); if (responseTimeout != null && nextInterval.compareTo(responseTimeout) > 0) { return response; diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/InternalExecRuntime.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/InternalExecRuntime.java index 9206b9432e..49568b7ec2 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/InternalExecRuntime.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/InternalExecRuntime.java @@ -97,7 +97,7 @@ public void acquireEndpoint( final String id, final HttpRoute route, final Object object, final HttpClientContext context) throws IOException { Args.notNull(route, "Route"); if (endpointRef.get() == null) { - final RequestConfig requestConfig = context.getRequestConfig(); + final RequestConfig requestConfig = context.getRequestConfigOrDefault(); final Timeout connectionRequestTimeout = requestConfig.getConnectionRequestTimeout(); if (log.isDebugEnabled()) { log.debug("{} acquiring endpoint ({})", id, connectionRequestTimeout); @@ -155,7 +155,7 @@ private void connectEndpoint(final ConnectionEndpoint endpoint, final HttpClient if (isExecutionAborted()) { throw new RequestFailedException("Request aborted"); } - final RequestConfig requestConfig = context.getRequestConfig(); + final RequestConfig requestConfig = context.getRequestConfigOrDefault(); @SuppressWarnings("deprecation") final Timeout connectTimeout = requestConfig.getConnectTimeout(); if (log.isDebugEnabled()) { @@ -222,7 +222,7 @@ public ClassicHttpResponse execute( if (isExecutionAborted()) { throw new RequestFailedException("Request aborted"); } - final RequestConfig requestConfig = context.getRequestConfig(); + final RequestConfig requestConfig = context.getRequestConfigOrDefault(); final Timeout responseTimeout = requestConfig.getResponseTimeout(); if (responseTimeout != null) { endpoint.setSocketTimeout(responseTimeout); diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/InternalHttpClient.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/InternalHttpClient.java index 1c299972e7..15bd5d089b 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/InternalHttpClient.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/InternalHttpClient.java @@ -121,20 +121,20 @@ private HttpRoute determineRoute(final HttpHost target, final HttpRequest reques } private void setupContext(final HttpClientContext context) { - if (context.getAttribute(HttpClientContext.AUTHSCHEME_REGISTRY) == null) { - context.setAttribute(HttpClientContext.AUTHSCHEME_REGISTRY, this.authSchemeRegistry); + if (context.getAuthSchemeRegistry() == null) { + context.setAuthSchemeRegistry(this.authSchemeRegistry); } - if (context.getAttribute(HttpClientContext.COOKIESPEC_REGISTRY) == null) { - context.setAttribute(HttpClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry); + if (context.getCookieSpecRegistry() == null) { + context.setCookieSpecRegistry(this.cookieSpecRegistry); } - if (context.getAttribute(HttpClientContext.COOKIE_STORE) == null) { - context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore); + if (context.getCookieStore() == null) { + context.setCookieStore(this.cookieStore); } - if (context.getAttribute(HttpClientContext.CREDS_PROVIDER) == null) { - context.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credentialsProvider); + if (context.getCredentialsProvider() == null) { + context.setCredentialsProvider(this.credentialsProvider); } - if (context.getAttribute(HttpClientContext.REQUEST_CONFIG) == null) { - context.setAttribute(HttpClientContext.REQUEST_CONFIG, this.defaultConfig); + if (context.getRequestConfig() == null) { + context.setRequestConfig(this.defaultConfig); } } @@ -145,8 +145,7 @@ protected CloseableHttpResponse doExecute( final HttpContext context) throws IOException { Args.notNull(request, "HTTP request"); try { - final HttpClientContext localcontext = HttpClientContext.adapt( - context != null ? context : new HttpClientContext()); + final HttpClientContext localcontext = HttpClientContext.adapt(context); RequestConfig config = null; if (request instanceof Configurable) { config = ((Configurable) request).getConfig(); diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/MainClientExec.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/MainClientExec.java index fcb5b912d3..5a3789cd25 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/MainClientExec.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/MainClientExec.java @@ -113,7 +113,7 @@ public ClassicHttpResponse execute( } try { // Run request protocol interceptors - context.setAttribute(HttpClientContext.HTTP_ROUTE, route); + context.setRoute(route); context.setRequest(request); httpProcessor.process(request, request.getEntity(), context); @@ -146,7 +146,7 @@ public ClassicHttpResponse execute( Object userToken = context.getUserToken(); if (userToken == null) { userToken = userTokenHandler.getUserToken(route, request, context); - context.setAttribute(HttpClientContext.USER_TOKEN, userToken); + context.setUserToken(userToken); } // The connection is in or can be brought to a re-usable state. diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/MinimalHttpClient.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/MinimalHttpClient.java index 77f7ea0ffe..9dd1dab550 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/MinimalHttpClient.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/MinimalHttpClient.java @@ -118,8 +118,7 @@ protected CloseableHttpResponse doExecute( if (request.getAuthority() == null) { request.setAuthority(new URIAuthority(target)); } - final HttpClientContext clientContext = HttpClientContext.adapt( - context != null ? context : new HttpClientContext()); + final HttpClientContext clientContext = HttpClientContext.adapt(context); RequestConfig config = null; if (request instanceof Configurable) { config = ((Configurable) request).getConfig(); @@ -142,7 +141,7 @@ protected CloseableHttpResponse doExecute( } clientContext.setRequest(request); - clientContext.setAttribute(HttpClientContext.HTTP_ROUTE, route); + clientContext.setRoute(route); httpProcessor.process(request, request.getEntity(), clientContext); final ClassicHttpResponse response = execRuntime.execute(exchangeId, request, clientContext); diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/ProtocolExec.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/ProtocolExec.java index 8ef37e4d07..bfebce0eaf 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/ProtocolExec.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/ProtocolExec.java @@ -266,7 +266,7 @@ private boolean needAuthentication( final String pathPrefix, final HttpResponse response, final HttpClientContext context) { - final RequestConfig config = context.getRequestConfig(); + final RequestConfig config = context.getRequestConfigOrDefault(); if (config.isAuthenticationEnabled()) { final boolean targetAuthRequested = authenticator.isChallenged( target, ChallengeType.TARGET, response, targetAuthExchange, context); diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/ProxyClient.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/ProxyClient.java index d31cc696cb..29394f2dd0 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/ProxyClient.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/ProxyClient.java @@ -145,7 +145,7 @@ public Socket tunnel( proxy, false, TunnelType.TUNNELLED, LayerType.PLAIN); final ManagedHttpClientConnection conn = this.connFactory.createConnection(null); - final HttpClientContext context = new HttpClientContext(); + final HttpClientContext context = HttpClientContext.create(); ClassicHttpResponse response; final ClassicHttpRequest connect = new BasicClassicHttpRequest(Method.CONNECT, proxy, target.toHostString()); @@ -155,10 +155,10 @@ public Socket tunnel( // Populate the execution context context.setRequest(connect); - context.setAttribute(HttpClientContext.HTTP_ROUTE, route); - context.setAttribute(HttpClientContext.CREDS_PROVIDER, credsProvider); - context.setAttribute(HttpClientContext.AUTHSCHEME_REGISTRY, this.authSchemeRegistry); - context.setAttribute(HttpClientContext.REQUEST_CONFIG, this.requestConfig); + context.setRoute(route); + context.setCredentialsProvider(credsProvider); + context.setAuthSchemeRegistry(this.authSchemeRegistry); + context.setRequestConfig(this.requestConfig); this.requestExec.preProcess(connect, this.httpProcessor, context); diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/RedirectExec.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/RedirectExec.java index 827c0c7e13..47715621a0 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/RedirectExec.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/RedirectExec.java @@ -99,14 +99,9 @@ public ClassicHttpResponse execute( Args.notNull(scope, "Scope"); final HttpClientContext context = scope.clientContext; - RedirectLocations redirectLocations = context.getRedirectLocations(); - if (redirectLocations == null) { - redirectLocations = new RedirectLocations(); - context.setAttribute(HttpClientContext.REDIRECT_LOCATIONS, redirectLocations); - } - redirectLocations.clear(); + context.setRedirectLocations(null); - final RequestConfig config = context.getRequestConfig(); + final RequestConfig config = context.getRequestConfigOrDefault(); final int maxRedirects = config.getMaxRedirects() > 0 ? config.getMaxRedirects() : 50; ClassicHttpRequest originalRequest = scope.originalRequest; ClassicHttpRequest currentRequest = request; @@ -139,6 +134,7 @@ public ClassicHttpResponse execute( redirectUri); } + final RedirectLocations redirectLocations = context.getRedirectLocations(); if (!config.isCircularRedirectsAllowed()) { if (redirectLocations.contains(redirectUri)) { throw new CircularRedirectException("Circular redirect to '" + redirectUri + "'"); diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/routing/DefaultRoutePlanner.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/routing/DefaultRoutePlanner.java index 63b050d1ed..076a750a7d 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/routing/DefaultRoutePlanner.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/routing/DefaultRoutePlanner.java @@ -76,8 +76,8 @@ public final HttpRoute determineRoute(final HttpHost host, final HttpRequest req if (host == null) { throw new ProtocolException("Target host is not specified"); } - final HttpClientContext clientContext = HttpClientContext.adapt(context); - final RequestConfig config = clientContext.getRequestConfig(); + final HttpClientContext clientContext = HttpClientContext.cast(context); + final RequestConfig config = clientContext.getRequestConfigOrDefault(); @SuppressWarnings("deprecation") HttpHost proxy = config.getProxy(); if (proxy == null) { diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/protocol/HttpClientContext.java b/httpclient5/src/main/java/org/apache/hc/client5/http/protocol/HttpClientContext.java index 6cffddf634..a8c1e75475 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/protocol/HttpClientContext.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/protocol/HttpClientContext.java @@ -30,6 +30,8 @@ import java.util.HashMap; import java.util.Map; +import javax.net.ssl.SSLSession; + import org.apache.hc.client5.http.HttpRoute; import org.apache.hc.client5.http.RouteInfo; import org.apache.hc.client5.http.auth.AuthCache; @@ -42,11 +44,15 @@ import org.apache.hc.client5.http.cookie.CookieSpec; import org.apache.hc.client5.http.cookie.CookieSpecFactory; import org.apache.hc.client5.http.cookie.CookieStore; +import org.apache.hc.core5.annotation.Internal; +import org.apache.hc.core5.http.EndpointDetails; 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.ProtocolVersion; import org.apache.hc.core5.http.config.Lookup; import org.apache.hc.core5.http.protocol.HttpContext; import org.apache.hc.core5.http.protocol.HttpCoreContext; -import org.apache.hc.core5.util.Args; /** * Adaptor class that provides convenience type safe setters and getters @@ -58,96 +64,129 @@ public class HttpClientContext extends HttpCoreContext { /** - * Attribute name of a {@link RouteInfo} - * object that represents the actual connection route. + * @deprecated Use getter methods */ + @Deprecated public static final String HTTP_ROUTE = "http.route"; /** - * Attribute name of a {@link RedirectLocations} object that represents a collection of all - * redirect locations received in the process of request execution. + * @deprecated Use getter methods */ + @Deprecated public static final String REDIRECT_LOCATIONS = "http.protocol.redirect-locations"; /** - * Attribute name of a {@link org.apache.hc.core5.http.config.Lookup} object that represents - * the actual {@link CookieSpecFactory} registry. + * @deprecated Use getter methods */ + @Deprecated public static final String COOKIESPEC_REGISTRY = "http.cookiespec-registry"; /** - * Attribute name of a {@link org.apache.hc.client5.http.cookie.CookieSpec} - * object that represents the actual cookie specification. + * @deprecated Use getter methods */ + @Deprecated public static final String COOKIE_SPEC = "http.cookie-spec"; /** - * Attribute name of a {@link org.apache.hc.client5.http.cookie.CookieOrigin} - * object that represents the actual details of the origin server. + * @deprecated Use getter methods */ + @Deprecated public static final String COOKIE_ORIGIN = "http.cookie-origin"; /** - * Attribute name of a {@link CookieStore} - * object that represents the actual cookie store. + * @deprecated Use getter methods */ + @Deprecated public static final String COOKIE_STORE = "http.cookie-store"; /** - * Attribute name of a {@link CredentialsProvider} - * object that represents the actual credentials provider. + * @deprecated Use getter methods */ + @Deprecated public static final String CREDS_PROVIDER = "http.auth.credentials-provider"; /** - * Attribute name of a {@link AuthCache} object - * that represents the auth scheme cache. + * @deprecated Use getter methods */ + @Deprecated public static final String AUTH_CACHE = "http.auth.auth-cache"; /** - * Attribute name of a map containing actual {@link AuthExchange}s keyed by their respective - * {@link org.apache.hc.core5.http.HttpHost}. + * @deprecated Use getter methods */ + @Deprecated public static final String AUTH_EXCHANGE_MAP = "http.auth.exchanges"; /** - * Attribute name of a {@link java.lang.Object} object that represents - * the actual user identity such as user {@link java.security.Principal}. + * @deprecated Use getter methods */ + @Deprecated public static final String USER_TOKEN = "http.user-token"; /** - * Attribute name of a {@link org.apache.hc.core5.http.config.Lookup} object that represents - * the actual {@link AuthSchemeFactory} registry. + * @deprecated Use getter methods */ + @Deprecated public static final String AUTHSCHEME_REGISTRY = "http.authscheme-registry"; /** - * Attribute name of a {@link org.apache.hc.client5.http.config.RequestConfig} object that - * represents the actual request configuration. + * @deprecated Use getter methods */ + @Deprecated public static final String REQUEST_CONFIG = "http.request-config"; /** - * Attribute name of a {@link java.lang.String} object that represents the ID of the - * current message exchange. + * @deprecated Use getter methods */ + @Deprecated public static final String EXCHANGE_ID = "http.exchange-id"; @SuppressWarnings("deprecation") public static HttpClientContext adapt(final HttpContext context) { - Args.notNull(context, "HTTP context"); if (context instanceof HttpClientContext) { return (HttpClientContext) context; } - return new HttpClientContext(context); + return new HttpClientContext.Delegate(HttpCoreContext.adapt(context)); + } + + /** + * Casts the given generic {@link HttpContext} as {@link HttpClientContext} + * or creates a new {@link HttpClientContext} with the given {@link HttpContext} + * as a parent. + * + * @since 5.4 + */ + public static HttpClientContext cast(final HttpContext context) { + if (context == null) { + return null; + } + if (context instanceof HttpClientContext) { + return (HttpClientContext) context; + } else { + throw new IllegalStateException("Unexpected context type: " + context.getClass().getSimpleName() + + "; required context type: " + HttpClientContext.class.getSimpleName()); + } } public static HttpClientContext create() { return new HttpClientContext(); } + private HttpRoute route; + private RedirectLocations redirectLocations; + private CookieSpec cookieSpec; + private CookieOrigin cookieOrigin; + private Map authExchangeMap; + private String exchangeId; + + private Lookup cookieSpecFactoryLookup; + private Lookup authSchemeFactoryLookup; + private CookieStore cookieStore; + private CredentialsProvider credentialsProvider; + private AuthCache authCache; + private Object userToken; + private RequestConfig requestConfig; + public HttpClientContext(final HttpContext context) { super(context); } @@ -156,99 +195,131 @@ public HttpClientContext() { super(); } + /** + * Represents current route used to execute message exchanges. + *

+ * This context attribute is expected to be populated by the protocol handler + * in the course of request execution. + */ public RouteInfo getHttpRoute() { - return getAttribute(HTTP_ROUTE, HttpRoute.class); + return route; } + /** + * @since 5.4 + */ + @Internal + public void setRoute(final HttpRoute route) { + this.route = route; + } + + /** + * Represents a collection of all redirects executed in the course of request execution. + *

+ * This context attribute is expected to be populated by the protocol handler + * in the course of request execution. + */ public RedirectLocations getRedirectLocations() { - return getAttribute(REDIRECT_LOCATIONS, RedirectLocations.class); + if (this.redirectLocations == null) { + this.redirectLocations = new RedirectLocations(); + } + return this.redirectLocations; + } + + /** + * @since 5.4 + */ + @Internal + public void setRedirectLocations(final RedirectLocations redirectLocations) { + this.redirectLocations = redirectLocations; } public CookieStore getCookieStore() { - return getAttribute(COOKIE_STORE, CookieStore.class); + return cookieStore; } public void setCookieStore(final CookieStore cookieStore) { - setAttribute(COOKIE_STORE, cookieStore); + this.cookieStore = cookieStore; } public CookieSpec getCookieSpec() { - return getAttribute(COOKIE_SPEC, CookieSpec.class); + return cookieSpec; + } + + /** + * @since 5.4 + */ + @Internal + public void setCookieSpec(final CookieSpec cookieSpec) { + this.cookieSpec = cookieSpec; } public CookieOrigin getCookieOrigin() { - return getAttribute(COOKIE_ORIGIN, CookieOrigin.class); + return cookieOrigin; } - @SuppressWarnings("unchecked") - private Lookup getLookup(final String name) { - return (Lookup) getAttribute(name, Lookup.class); + /** + * @since 5.4 + */ + @Internal + public void setCookieOrigin(final CookieOrigin cookieOrigin) { + this.cookieOrigin = cookieOrigin; } public Lookup getCookieSpecRegistry() { - return getLookup(COOKIESPEC_REGISTRY); + return cookieSpecFactoryLookup; } public void setCookieSpecRegistry(final Lookup lookup) { - setAttribute(COOKIESPEC_REGISTRY, lookup); + this.cookieSpecFactoryLookup = lookup; } public Lookup getAuthSchemeRegistry() { - return getLookup(AUTHSCHEME_REGISTRY); + return authSchemeFactoryLookup; } public void setAuthSchemeRegistry(final Lookup lookup) { - setAttribute(AUTHSCHEME_REGISTRY, lookup); + this.authSchemeFactoryLookup = lookup; } public CredentialsProvider getCredentialsProvider() { - return getAttribute(CREDS_PROVIDER, CredentialsProvider.class); + return credentialsProvider; } public void setCredentialsProvider(final CredentialsProvider credentialsProvider) { - setAttribute(CREDS_PROVIDER, credentialsProvider); + this.credentialsProvider = credentialsProvider; } public AuthCache getAuthCache() { - return getAttribute(AUTH_CACHE, AuthCache.class); + return authCache; } public void setAuthCache(final AuthCache authCache) { - setAttribute(AUTH_CACHE, authCache); + this.authCache = authCache; } /** * @since 5.0 */ - @SuppressWarnings("unchecked") public Map getAuthExchanges() { - Map map = (Map) getAttribute(AUTH_EXCHANGE_MAP); - if (map == null) { - map = new HashMap<>(); - setAttribute(AUTH_EXCHANGE_MAP, map); + if (authExchangeMap == null) { + authExchangeMap = new HashMap<>(); } - return map; + return authExchangeMap; } /** * @since 5.0 */ public AuthExchange getAuthExchange(final HttpHost host) { - final Map authExchangeMap = getAuthExchanges(); - AuthExchange authExchange = authExchangeMap.get(host); - if (authExchange == null) { - authExchange = new AuthExchange(); - authExchangeMap.put(host, authExchange); - } - return authExchange; + return getAuthExchanges().computeIfAbsent(host, k -> new AuthExchange()); } /** * @since 5.0 */ public void setAuthExchange(final HttpHost host, final AuthExchange authExchange) { - final Map authExchangeMap = getAuthExchanges(); - authExchangeMap.put(host, authExchange); + getAuthExchanges().put(host, authExchange); } /** @@ -257,43 +328,143 @@ public void setAuthExchange(final HttpHost host, final AuthExchange authExchange public void resetAuthExchange(final HttpHost host, final AuthScheme authScheme) { final AuthExchange authExchange = new AuthExchange(); authExchange.select(authScheme); - final Map authExchangeMap = getAuthExchanges(); - authExchangeMap.put(host, authExchange); + getAuthExchanges().put(host, authExchange); } + /** + * @deprecated Use {@link #getUserToken()} + */ + @Deprecated + @SuppressWarnings("unchecked") public T getUserToken(final Class clazz) { - return getAttribute(USER_TOKEN, clazz); + return (T) getUserToken(); } public Object getUserToken() { - return getAttribute(USER_TOKEN); + return userToken; } - public void setUserToken(final Object obj) { - setAttribute(USER_TOKEN, obj); + public void setUserToken(final Object userToken) { + this.userToken = userToken; } public RequestConfig getRequestConfig() { - final RequestConfig config = getAttribute(REQUEST_CONFIG, RequestConfig.class); - return config != null ? config : RequestConfig.DEFAULT; + return requestConfig; } - public void setRequestConfig(final RequestConfig config) { - setAttribute(REQUEST_CONFIG, config); + /** + * Returns {@link RequestConfig} set in the context or {@link RequestConfig#DEFAULT} + * if not present in the context. + * + * @since 5.4 + */ + public final RequestConfig getRequestConfigOrDefault() { + final RequestConfig requestConfig = getRequestConfig(); + return requestConfig != null ? requestConfig : RequestConfig.DEFAULT; + } + + public void setRequestConfig(final RequestConfig requestConfig) { + this.requestConfig = requestConfig; } /** * @since 5.1 */ public String getExchangeId() { - return getAttribute(EXCHANGE_ID, String.class); + return exchangeId; } /** * @since 5.1 */ - public void setExchangeId(final String id) { - setAttribute(EXCHANGE_ID, id); + public void setExchangeId(final String exchangeId) { + this.exchangeId = exchangeId; + } + + static class Delegate extends HttpClientContext { + + final private HttpCoreContext parent; + + Delegate(final HttpCoreContext parent) { + super(parent); + this.parent = parent; + } + + @Override + public ProtocolVersion getProtocolVersion() { + return parent.getProtocolVersion(); + } + + @Override + public void setProtocolVersion(final ProtocolVersion version) { + parent.setProtocolVersion(version); + } + + @Override + public Object getAttribute(final String id) { + return parent.getAttribute(id); + } + + @Override + public Object setAttribute(final String id, final Object obj) { + return parent.setAttribute(id, obj); + } + + @Override + public Object removeAttribute(final String id) { + return parent.removeAttribute(id); + } + + @Override + public T getAttribute(final String id, final Class clazz) { + return parent.getAttribute(id, clazz); + } + + @Override + public HttpRequest getRequest() { + return parent.getRequest(); + } + + @Override + public void setRequest(final HttpRequest request) { + parent.setRequest(request); + } + + @Override + public HttpResponse getResponse() { + return parent.getResponse(); + } + + @Override + public void setResponse(final HttpResponse response) { + parent.setResponse(response); + } + + @Override + public EndpointDetails getEndpointDetails() { + return parent.getEndpointDetails(); + } + + @Override + public void setEndpointDetails(final EndpointDetails endpointDetails) { + parent.setEndpointDetails(endpointDetails); + } + + @Override + public SSLSession getSSLSession() { + return parent.getSSLSession(); + } + + @Override + public void setSSLSession(final SSLSession sslSession) { + parent.setSSLSession(sslSession); + } + + @Override + public String toString() { + return parent.toString(); + } + } } diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/protocol/RequestAddCookies.java b/httpclient5/src/main/java/org/apache/hc/client5/http/protocol/RequestAddCookies.java index 39d67977c0..8b2b1b9c86 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/protocol/RequestAddCookies.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/protocol/RequestAddCookies.java @@ -33,13 +33,13 @@ import java.util.List; import org.apache.hc.client5.http.RouteInfo; -import org.apache.hc.client5.http.cookie.StandardCookieSpec; import org.apache.hc.client5.http.config.RequestConfig; import org.apache.hc.client5.http.cookie.Cookie; import org.apache.hc.client5.http.cookie.CookieOrigin; import org.apache.hc.client5.http.cookie.CookieSpec; import org.apache.hc.client5.http.cookie.CookieSpecFactory; import org.apache.hc.client5.http.cookie.CookieStore; +import org.apache.hc.client5.http.cookie.StandardCookieSpec; import org.apache.hc.core5.annotation.Contract; import org.apache.hc.core5.annotation.ThreadingBehavior; import org.apache.hc.core5.http.EntityDetails; @@ -90,7 +90,7 @@ public void process(final HttpRequest request, final EntityDetails entity, final return; } - final HttpClientContext clientContext = HttpClientContext.adapt(context); + final HttpClientContext clientContext = HttpClientContext.cast(context); final String exchangeId = clientContext.getExchangeId(); // Obtain cookie store @@ -120,7 +120,7 @@ public void process(final HttpRequest request, final EntityDetails entity, final return; } - final RequestConfig config = clientContext.getRequestConfig(); + final RequestConfig config = clientContext.getRequestConfigOrDefault(); String cookieSpecName = config.getCookieSpec(); if (cookieSpecName == null) { cookieSpecName = StandardCookieSpec.STRICT; @@ -190,8 +190,8 @@ public void process(final HttpRequest request, final EntityDetails entity, final // Stick the CookieSpec and CookieOrigin instances to the HTTP context // so they could be obtained by the response interceptor - context.setAttribute(HttpClientContext.COOKIE_SPEC, cookieSpec); - context.setAttribute(HttpClientContext.COOKIE_ORIGIN, cookieOrigin); + clientContext.setCookieSpec(cookieSpec); + clientContext.setCookieOrigin(cookieOrigin); } } diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/protocol/RequestAuthCache.java b/httpclient5/src/main/java/org/apache/hc/client5/http/protocol/RequestAuthCache.java index 608357daa1..02635a8289 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/protocol/RequestAuthCache.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/protocol/RequestAuthCache.java @@ -72,7 +72,7 @@ public void process(final HttpRequest request, final EntityDetails entity, final Args.notNull(request, "HTTP request"); Args.notNull(context, "HTTP context"); - final HttpClientContext clientContext = HttpClientContext.adapt(context); + final HttpClientContext clientContext = HttpClientContext.cast(context); final String exchangeId = clientContext.getExchangeId(); final AuthCache authCache = clientContext.getAuthCache(); diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/protocol/RequestClientConnControl.java b/httpclient5/src/main/java/org/apache/hc/client5/http/protocol/RequestClientConnControl.java index 2798c5801c..92ac5121bd 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/protocol/RequestClientConnControl.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/protocol/RequestClientConnControl.java @@ -70,7 +70,7 @@ public void process(final HttpRequest request, final EntityDetails entity, final return; } - final HttpClientContext clientContext = HttpClientContext.adapt(context); + final HttpClientContext clientContext = HttpClientContext.cast(context); final String exchangeId = clientContext.getExchangeId(); // Obtain the client connection (required) diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/protocol/RequestExpectContinue.java b/httpclient5/src/main/java/org/apache/hc/client5/http/protocol/RequestExpectContinue.java index d78a28c10e..6e2879a552 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/protocol/RequestExpectContinue.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/protocol/RequestExpectContinue.java @@ -66,12 +66,12 @@ public void process(final HttpRequest request, final EntityDetails entity, final Args.notNull(request, "HTTP request"); if (!request.containsHeader(HttpHeaders.EXPECT)) { - final HttpClientContext clientContext = HttpClientContext.adapt(context); + final HttpClientContext clientContext = HttpClientContext.cast(context); final ProtocolVersion version = request.getVersion() != null ? request.getVersion() : clientContext.getProtocolVersion(); // Do not send the expect header if request body is known to be empty if (entity != null && entity.getContentLength() != 0 && !version.lessEquals(HttpVersion.HTTP_1_0)) { - final RequestConfig config = clientContext.getRequestConfig(); + final RequestConfig config = clientContext.getRequestConfigOrDefault(); if (config.isExpectContinueEnabled()) { request.addHeader(HttpHeaders.EXPECT, HeaderElements.CONTINUE); } diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/protocol/RequestUpgrade.java b/httpclient5/src/main/java/org/apache/hc/client5/http/protocol/RequestUpgrade.java index d5ca81d222..20e316e575 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/protocol/RequestUpgrade.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/protocol/RequestUpgrade.java @@ -63,8 +63,8 @@ public void process( Args.notNull(request, "HTTP request"); Args.notNull(context, "HTTP context"); - final HttpClientContext clientContext = HttpClientContext.adapt(context); - final RequestConfig requestConfig = clientContext.getRequestConfig(); + final HttpClientContext clientContext = HttpClientContext.cast(context); + final RequestConfig requestConfig = clientContext.getRequestConfigOrDefault(); if (requestConfig.isProtocolUpgradeEnabled()) { final ProtocolVersion version = request.getVersion() != null ? request.getVersion() : clientContext.getProtocolVersion(); if (!request.containsHeader(HttpHeaders.UPGRADE) && version.getMajor() == 1 && version.getMinor() >= 1) { diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/protocol/ResponseProcessCookies.java b/httpclient5/src/main/java/org/apache/hc/client5/http/protocol/ResponseProcessCookies.java index fbfd9c470b..7907b880f4 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/protocol/ResponseProcessCookies.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/protocol/ResponseProcessCookies.java @@ -77,7 +77,7 @@ public void process(final HttpResponse response, final EntityDetails entity, fin Args.notNull(response, "HTTP request"); Args.notNull(context, "HTTP context"); - final HttpClientContext clientContext = HttpClientContext.adapt(context); + final HttpClientContext clientContext = HttpClientContext.cast(context); final String exchangeId = clientContext.getExchangeId(); // Obtain actual CookieSpec instance diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestHttpAuthenticator.java b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestHttpAuthenticator.java index 8347c5abaf..7773d9887b 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestHttpAuthenticator.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestHttpAuthenticator.java @@ -74,7 +74,7 @@ public String getName() { private AuthExchange authExchange; private CacheableAuthState authScheme; - private HttpContext context; + private HttpClientContext context; private HttpHost defaultHost; private CredentialsProvider credentialsProvider; private Lookup authSchemeRegistry; @@ -89,12 +89,12 @@ public void setUp() throws Exception { this.context = new HttpClientContext(); this.defaultHost = new HttpHost("localhost", 80); this.credentialsProvider = Mockito.mock(CredentialsProvider.class); - this.context.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credentialsProvider); + this.context.setCredentialsProvider(this.credentialsProvider); this.authSchemeRegistry = RegistryBuilder.create() .register(StandardAuthScheme.BASIC, BasicSchemeFactory.INSTANCE) .register(StandardAuthScheme.DIGEST, DigestSchemeFactory.INSTANCE) .build(); - this.context.setAttribute(HttpClientContext.AUTHSCHEME_REGISTRY, this.authSchemeRegistry); + this.context.setAuthSchemeRegistry(this.authSchemeRegistry); this.httpAuthenticator = new HttpAuthenticator(); } diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestContentCompressionExec.java b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestContentCompressionExec.java index 51c8b539e3..904bb7efe1 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestContentCompressionExec.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestContentCompressionExec.java @@ -225,7 +225,7 @@ public void testContentEncodingRequestParameter() throws Exception { .setContentCompressionEnabled(false) .build(); - context.setAttribute(HttpClientContext.REQUEST_CONFIG, config); + context.setRequestConfig(config); Mockito.when(execChain.proceed(request, scope)).thenReturn(response); diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestInternalHttpClient.java b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestInternalHttpClient.java index 5208bc53d8..73b0f374e5 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestInternalHttpClient.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestInternalHttpClient.java @@ -155,7 +155,7 @@ public void testExecuteDefaultContext() throws Exception { Assertions.assertSame(authSchemeRegistry, context.getAuthSchemeRegistry()); Assertions.assertSame(cookieStore, context.getCookieStore()); Assertions.assertSame(credentialsProvider, context.getCredentialsProvider()); - Assertions.assertSame(defaultConfig, context.getRequestConfig()); + Assertions.assertSame(defaultConfig, context.getRequestConfigOrDefault()); } @Test @@ -176,7 +176,7 @@ public void testExecuteRequestConfig() throws Exception { final HttpClientContext context = HttpClientContext.create(); client.execute(httpget, context, response -> null); - Assertions.assertSame(config, context.getRequestConfig()); + Assertions.assertSame(config, context.getRequestConfigOrDefault()); } @Test @@ -212,7 +212,7 @@ public void testExecuteLocalContext() throws Exception { Assertions.assertSame(localAuthSchemeRegistry, context.getAuthSchemeRegistry()); Assertions.assertSame(localCookieStore, context.getCookieStore()); Assertions.assertSame(localCredentialsProvider, context.getCredentialsProvider()); - Assertions.assertSame(localConfig, context.getRequestConfig()); + Assertions.assertSame(localConfig, context.getRequestConfigOrDefault()); } @Test diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/protocol/TestRequestAddCookies.java b/httpclient5/src/test/java/org/apache/hc/client5/http/protocol/TestRequestAddCookies.java index f14d86ce9a..a100ff67b9 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/protocol/TestRequestAddCookies.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/protocol/TestRequestAddCookies.java @@ -106,9 +106,9 @@ public void testAddCookies() throws Exception { final HttpRoute route = new HttpRoute(this.target, null, false); final HttpClientContext context = HttpClientContext.create(); - context.setAttribute(HttpClientContext.HTTP_ROUTE, route); - context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore); - context.setAttribute(HttpClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry); + context.setRoute(route); + context.setCookieStore(this.cookieStore); + context.setCookieSpecRegistry(this.cookieSpecRegistry); final HttpRequestInterceptor interceptor = RequestAddCookies.INSTANCE; interceptor.process(request, null, context); @@ -133,9 +133,9 @@ public void testCookiesForConnectRequest() throws Exception { final HttpRoute route = new HttpRoute(this.target, null, false); final HttpClientContext context = HttpClientContext.create(); - context.setAttribute(HttpClientContext.HTTP_ROUTE, route); - context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore); - context.setAttribute(HttpClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry); + context.setRoute(route); + context.setCookieStore(this.cookieStore); + context.setCookieSpecRegistry(this.cookieSpecRegistry); final HttpRequestInterceptor interceptor = RequestAddCookies.INSTANCE; interceptor.process(request, null, context); @@ -152,9 +152,9 @@ public void testNoCookieStore() throws Exception { final HttpRoute route = new HttpRoute(this.target, null, false); final HttpClientContext context = HttpClientContext.create(); - context.setAttribute(HttpClientContext.HTTP_ROUTE, route); - context.setAttribute(HttpClientContext.COOKIE_STORE, null); - context.setAttribute(HttpClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry); + context.setRoute(route); + context.setCookieStore(null); + context.setCookieSpecRegistry(this.cookieSpecRegistry); final HttpRequestInterceptor interceptor = RequestAddCookies.INSTANCE; interceptor.process(request, null, context); @@ -171,9 +171,9 @@ public void testNoCookieSpecRegistry() throws Exception { final HttpRoute route = new HttpRoute(this.target, null, false); final HttpClientContext context = HttpClientContext.create(); - context.setAttribute(HttpClientContext.HTTP_ROUTE, route); - context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore); - context.setAttribute(HttpClientContext.COOKIESPEC_REGISTRY, null); + context.setRoute(route); + context.setCookieStore(this.cookieStore); + context.setCookieSpecRegistry(null); final HttpRequestInterceptor interceptor = RequestAddCookies.INSTANCE; interceptor.process(request, null, context); @@ -189,8 +189,8 @@ public void testNoHttpConnection() throws Exception { final HttpClientContext context = HttpClientContext.create(); context.setEndpointDetails(null); - context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore); - context.setAttribute(HttpClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry); + context.setCookieStore(this.cookieStore); + context.setCookieSpecRegistry(this.cookieSpecRegistry); final HttpRequestInterceptor interceptor = RequestAddCookies.INSTANCE; interceptor.process(request, null, context); @@ -209,10 +209,10 @@ public void testAddCookiesUsingExplicitCookieSpec() throws Exception { final HttpRoute route = new HttpRoute(this.target, null, false); final HttpClientContext context = HttpClientContext.create(); - context.setAttribute(HttpClientContext.HTTP_ROUTE, route); - context.setAttribute(HttpClientContext.REQUEST_CONFIG, config); - context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore); - context.setAttribute(HttpClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry); + context.setRoute(route); + context.setRequestConfig(config); + context.setCookieStore(this.cookieStore); + context.setCookieSpecRegistry(this.cookieSpecRegistry); final HttpRequestInterceptor interceptor = RequestAddCookies.INSTANCE; interceptor.process(request, null, context); @@ -233,9 +233,9 @@ public void testAuthScopeInvalidRequestURI() throws Exception { final HttpRoute route = new HttpRoute(this.target, null, false); final HttpClientContext context = HttpClientContext.create(); - context.setAttribute(HttpClientContext.HTTP_ROUTE, route); - context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore); - context.setAttribute(HttpClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry); + context.setRoute(route); + context.setCookieStore(this.cookieStore); + context.setCookieSpecRegistry(this.cookieSpecRegistry); final HttpRequestInterceptor interceptor = RequestAddCookies.INSTANCE; interceptor.process(request, null, context); @@ -249,9 +249,9 @@ public void testAuthScopeRemotePortWhenDirect() throws Exception { final HttpRoute route = new HttpRoute(new HttpHost("localhost.local", 1234), null, false); final HttpClientContext context = HttpClientContext.create(); - context.setAttribute(HttpClientContext.HTTP_ROUTE, route); - context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore); - context.setAttribute(HttpClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry); + context.setRoute(route); + context.setCookieStore(this.cookieStore); + context.setCookieSpecRegistry(this.cookieSpecRegistry); final HttpRequestInterceptor interceptor = RequestAddCookies.INSTANCE; interceptor.process(request, null, context); @@ -273,9 +273,9 @@ public void testAuthDefaultHttpPortWhenProxy() throws Exception { new HttpHost("localhost.local", 80), null, new HttpHost("localhost", 8888), false); final HttpClientContext context = HttpClientContext.create(); - context.setAttribute(HttpClientContext.HTTP_ROUTE, route); - context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore); - context.setAttribute(HttpClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry); + context.setRoute(route); + context.setCookieStore(this.cookieStore); + context.setCookieSpecRegistry(this.cookieSpecRegistry); final HttpRequestInterceptor interceptor = RequestAddCookies.INSTANCE; interceptor.process(request, null, context); @@ -298,9 +298,9 @@ public void testAuthDefaultHttpsPortWhenProxy() throws Exception { new HttpHost("http", "localhost", 8888), true, TunnelType.TUNNELLED, LayerType.LAYERED); final HttpClientContext context = HttpClientContext.create(); - context.setAttribute(HttpClientContext.HTTP_ROUTE, route); - context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore); - context.setAttribute(HttpClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry); + context.setRoute(route); + context.setCookieStore(this.cookieStore); + context.setCookieSpecRegistry(this.cookieSpecRegistry); final HttpRequestInterceptor interceptor = RequestAddCookies.INSTANCE; interceptor.process(request, null, context); @@ -330,9 +330,9 @@ public void testExcludeExpiredCookies() throws Exception { final HttpRoute route = new HttpRoute(this.target, null, false); final HttpClientContext context = HttpClientContext.create(); - context.setAttribute(HttpClientContext.HTTP_ROUTE, route); - context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore); - context.setAttribute(HttpClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry); + context.setRoute(route); + context.setCookieStore(this.cookieStore); + context.setCookieSpecRegistry(this.cookieSpecRegistry); // Make sure the third cookie expires Thread.sleep(200); @@ -361,9 +361,9 @@ public void testNoMatchingCookies() throws Exception { final HttpRoute route = new HttpRoute(this.target, null, false); final HttpClientContext context = HttpClientContext.create(); - context.setAttribute(HttpClientContext.HTTP_ROUTE, route); - context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore); - context.setAttribute(HttpClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry); + context.setRoute(route); + context.setCookieStore(this.cookieStore); + context.setCookieSpecRegistry(this.cookieSpecRegistry); final HttpRequestInterceptor interceptor = RequestAddCookies.INSTANCE; interceptor.process(request, null, context); @@ -396,9 +396,9 @@ public void testCookieOrder() throws Exception { final HttpRoute route = new HttpRoute(this.target, null, false); final HttpClientContext context = HttpClientContext.create(); - context.setAttribute(HttpClientContext.HTTP_ROUTE, route); - context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore); - context.setAttribute(HttpClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry); + context.setRoute(route); + context.setCookieStore(this.cookieStore); + context.setCookieSpecRegistry(this.cookieSpecRegistry); final HttpRequestInterceptor interceptor = RequestAddCookies.INSTANCE; interceptor.process(request, null, context); diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/protocol/TestRequestClientConnControl.java b/httpclient5/src/test/java/org/apache/hc/client5/http/protocol/TestRequestClientConnControl.java index 2baa3deb90..a44a6760d0 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/protocol/TestRequestClientConnControl.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/protocol/TestRequestClientConnControl.java @@ -68,7 +68,7 @@ public void testConnectionKeepAliveForDirectRequests() throws Exception { final HttpHost target = new HttpHost("http", "localhost", 80); final HttpRoute route = new HttpRoute(target, null, false); - context.setAttribute(HttpClientContext.HTTP_ROUTE, route); + context.setRoute(route); final HttpRequestInterceptor interceptor = new RequestClientConnControl(); interceptor.process(request, null, context); @@ -88,7 +88,7 @@ public void testConnectionKeepAliveForTunneledRequests() throws Exception { final HttpRoute route = new HttpRoute(target, null, proxy, true, TunnelType.TUNNELLED, LayerType.LAYERED); - context.setAttribute(HttpClientContext.HTTP_ROUTE, route); + context.setRoute(route); final HttpRequestInterceptor interceptor = new RequestClientConnControl(); interceptor.process(request, null, context); @@ -108,7 +108,7 @@ public void testProxyConnectionKeepAliveForRequestsOverProxy() throws Exception final HttpRoute route = new HttpRoute(target, null, proxy, false, TunnelType.PLAIN, LayerType.PLAIN); - context.setAttribute(HttpClientContext.HTTP_ROUTE, route); + context.setRoute(route); final HttpRequestInterceptor interceptor = new RequestClientConnControl(); interceptor.process(request, null, context); @@ -128,7 +128,7 @@ public void testPreserveCustomConnectionHeader() throws Exception { final HttpRoute route = new HttpRoute(target, null, proxy, true, TunnelType.TUNNELLED, LayerType.LAYERED); - context.setAttribute(HttpClientContext.HTTP_ROUTE, route); + context.setRoute(route); final HttpRequestInterceptor interceptor = new RequestClientConnControl(); interceptor.process(request, null, context); diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/protocol/TestRequestExpectContinue.java b/httpclient5/src/test/java/org/apache/hc/client5/http/protocol/TestRequestExpectContinue.java index 5484736f04..7b91db8fa6 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/protocol/TestRequestExpectContinue.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/protocol/TestRequestExpectContinue.java @@ -46,7 +46,7 @@ public class TestRequestExpectContinue { public void testRequestExpectContinueGenerated() throws Exception { final HttpClientContext context = HttpClientContext.create(); final RequestConfig config = RequestConfig.custom().setExpectContinueEnabled(true).build(); - context.setAttribute(HttpClientContext.REQUEST_CONFIG, config); + context.setRequestConfig(config); final ClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/"); final String s = "whatever"; final StringEntity entity = new StringEntity(s, StandardCharsets.US_ASCII); @@ -62,7 +62,7 @@ public void testRequestExpectContinueGenerated() throws Exception { public void testRequestExpectContinueNotGenerated() throws Exception { final HttpClientContext context = new HttpClientContext(); final RequestConfig config = RequestConfig.custom().setExpectContinueEnabled(false).build(); - context.setAttribute(HttpClientContext.REQUEST_CONFIG, config); + context.setRequestConfig(config); final ClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/"); final String s = "whatever"; final StringEntity entity = new StringEntity(s, StandardCharsets.US_ASCII); @@ -77,7 +77,7 @@ public void testRequestExpectContinueNotGenerated() throws Exception { public void testRequestExpectContinueHTTP10() throws Exception { final HttpClientContext context = new HttpClientContext(); final RequestConfig config = RequestConfig.custom().setExpectContinueEnabled(true).build(); - context.setAttribute(HttpClientContext.REQUEST_CONFIG, config); + context.setRequestConfig(config); final ClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/"); request.setVersion(HttpVersion.HTTP_1_0); final String s = "whatever"; @@ -93,7 +93,7 @@ public void testRequestExpectContinueHTTP10() throws Exception { public void testRequestExpectContinueZeroContent() throws Exception { final HttpClientContext context = new HttpClientContext(); final RequestConfig config = RequestConfig.custom().setExpectContinueEnabled(true).build(); - context.setAttribute(HttpClientContext.REQUEST_CONFIG, config); + context.setRequestConfig(config); final ClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/"); final String s = ""; final StringEntity entity = new StringEntity(s, StandardCharsets.US_ASCII); diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/protocol/TestResponseProcessCookies.java b/httpclient5/src/test/java/org/apache/hc/client5/http/protocol/TestResponseProcessCookies.java index e8eb8306e7..2b7da2d09f 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/protocol/TestResponseProcessCookies.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/protocol/TestResponseProcessCookies.java @@ -76,9 +76,9 @@ public void testParseCookies() throws Exception { response.addHeader("Set-Cookie", "name1=value1"); final HttpClientContext context = HttpClientContext.create(); - context.setAttribute(HttpClientContext.COOKIE_ORIGIN, this.cookieOrigin); - context.setAttribute(HttpClientContext.COOKIE_SPEC, this.cookieSpec); - context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore); + context.setCookieOrigin(this.cookieOrigin); + context.setCookieSpec(this.cookieSpec); + context.setCookieStore(this.cookieStore); final HttpResponseInterceptor interceptor = ResponseProcessCookies.INSTANCE; interceptor.process(response, null, context); @@ -99,9 +99,9 @@ public void testNoCookieOrigin() throws Exception { response.addHeader("Set-Cookie", "name1=value1"); final HttpClientContext context = HttpClientContext.create(); - context.setAttribute(HttpClientContext.COOKIE_ORIGIN, null); - context.setAttribute(HttpClientContext.COOKIE_SPEC, this.cookieSpec); - context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore); + context.setCookieOrigin(null); + context.setCookieSpec(this.cookieSpec); + context.setCookieStore(this.cookieStore); final HttpResponseInterceptor interceptor = ResponseProcessCookies.INSTANCE; interceptor.process(response, null, context); @@ -117,9 +117,9 @@ public void testNoCookieSpec() throws Exception { response.addHeader("Set-Cookie", "name1=value1"); final HttpClientContext context = HttpClientContext.create(); - context.setAttribute(HttpClientContext.COOKIE_ORIGIN, this.cookieOrigin); - context.setAttribute(HttpClientContext.COOKIE_SPEC, null); - context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore); + context.setCookieOrigin(this.cookieOrigin); + context.setCookieSpec(null); + context.setCookieStore(this.cookieStore); final HttpResponseInterceptor interceptor = ResponseProcessCookies.INSTANCE; interceptor.process(response, null, context); @@ -135,9 +135,9 @@ public void testNoCookieStore() throws Exception { response.addHeader("Set-Cookie", "name1=value1"); final HttpClientContext context = HttpClientContext.create(); - context.setAttribute(HttpClientContext.COOKIE_ORIGIN, this.cookieOrigin); - context.setAttribute(HttpClientContext.COOKIE_SPEC, this.cookieSpec); - context.setAttribute(HttpClientContext.COOKIE_STORE, null); + context.setCookieOrigin(this.cookieOrigin); + context.setCookieSpec(this.cookieSpec); + context.setCookieStore(null); final HttpResponseInterceptor interceptor = ResponseProcessCookies.INSTANCE; interceptor.process(response, null, context);