hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Oleg Kalnichevski <ol...@apache.org>
Subject Re: HttpContext and RequestConfig
Date Wed, 15 Oct 2014 07:27:37 GMT
On Tue, 2014-10-14 at 11:37 -0400, Todd W Lainhart wrote:
> HttpClient 4.3.5 / HttpCore 4.3.2
> 
> I've got an execution context, that while appears to be a single 
> call/response to the caller, can result in multiple network calls - 
> multiple CloseableHttpClient.execute(...) invocations.  Those invocations 
> share HttpContext state, mainly for a shared cookie store.  The 
> CloseableHttpClient has a  custom RequestConfig associated to it.
> 
> One of the invocations creates a new HttpRequest object, with its own 
> RequestConfig that overrides the default.  On subsequent invocations I was 
> surprised to see that the RequestConfig that I set on the request became 
> the shared HttpContext's RequestConfig state, which is used in subsequent 
> invocations.  Reading the source confirms this - the request's config is 
> set on the "local context", which is the shared context I mentioned 
> earlier.
> 
> Is this the intended behavior?  I can see it both ways.
> 

Hi Todd

I find it reasonable that the last request config stays in the context
and gets propagated to subsequent requests within the same context. So,
it was intended. 

> I can work around this by clearing the request config on the context on 
> that one call, but I wanted to raise the question in case I'm missing a 
> concept.  I'm also not finding a "clone" or "copy" of a context, which I 
> thought I had seen at one point.
> 

Deprecated HttpParams supports #copy, but I do not think HttpContext
implementations ever supported #copy or #clone.  

What one might do instead of cloning a context is to wrap it with a
decorator and discard the decorator along with all local modifications.

---
public final class DefaultedHttpContext implements HttpContext {

    private final HttpContext local;
    private final HttpContext defaults;

    public DefaultedHttpContext(final HttpContext local, final
HttpContext defaults) {
        super();
        this.local = Args.notNull(local, "HTTP context");
        this.defaults = defaults;
    }

    public Object getAttribute(final String id) {
        final Object obj = this.local.getAttribute(id);
        if (obj == null) {
            return this.defaults.getAttribute(id);
        } else {
            return obj;
        }
    }

    public Object removeAttribute(final String id) {
        return this.local.removeAttribute(id);
    }

    public void setAttribute(final String id, final Object obj) {
        this.local.setAttribute(id, obj);
    }

}
---

Oleg


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Mime
View raw message