HTTPCLIENT-1437: Made Executor#execute thread safe

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1545356 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2013-11-25 18:05:24 +00:00
parent 92b7e8cd97
commit 037b915bd0
2 changed files with 13 additions and 18 deletions

View File

@ -1,6 +1,9 @@
Changes since 4.3.1 Changes since 4.3.1
------------------- -------------------
* [HTTPCLIENT-1437] Made Executor#execute thread safe.
Contributed by Oleg Kalnichevski <olegk at apache.org>
* [HTTPCLIENT-1119] SNI support (Oracle Java 1.7+ only). * [HTTPCLIENT-1119] SNI support (Oracle Java 1.7+ only).
Contributed by Bruno Harbulot <bruno at distributedmatter.net> Contributed by Bruno Harbulot <bruno at distributedmatter.net>

View File

@ -59,7 +59,6 @@ import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicHeader; import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.BasicHttpContext;
/** /**
* An Executor for fluent requests * An Executor for fluent requests
@ -108,23 +107,19 @@ public class Executor {
} }
private final HttpClient httpclient; private final HttpClient httpclient;
private final BasicHttpContext localContext;
private final AuthCache authCache; private final AuthCache authCache;
private final CredentialsProvider credentialsProvider;
private CredentialsProvider credentialsProvider; private volatile CookieStore cookieStore;
private CookieStore cookieStore;
Executor(final HttpClient httpclient) { Executor(final HttpClient httpclient) {
super(); super();
this.httpclient = httpclient; this.httpclient = httpclient;
this.localContext = new BasicHttpContext(); this.credentialsProvider = new BasicCredentialsProvider();
this.authCache = new BasicAuthCache(); this.authCache = new BasicAuthCache();
} }
public Executor auth(final AuthScope authScope, final Credentials creds) { public Executor auth(final AuthScope authScope, final Credentials creds) {
if (this.credentialsProvider == null) {
this.credentialsProvider = new BasicCredentialsProvider();
}
this.credentialsProvider.setCredentials(authScope, creds); this.credentialsProvider.setCredentials(authScope, creds);
return this; return this;
} }
@ -179,9 +174,7 @@ public class Executor {
} }
public Executor clearAuth() { public Executor clearAuth() {
if (this.credentialsProvider != null) { this.credentialsProvider.clear();
this.credentialsProvider.clear();
}
return this; return this;
} }
@ -191,9 +184,7 @@ public class Executor {
} }
public Executor clearCookies() { public Executor clearCookies() {
if (this.cookieStore != null) { this.cookieStore.clear();
this.cookieStore.clear();
}
return this; return this;
} }
@ -207,12 +198,13 @@ public class Executor {
*/ */
public Response execute( public Response execute(
final Request request) throws ClientProtocolException, IOException { final Request request) throws ClientProtocolException, IOException {
this.localContext.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credentialsProvider); final HttpClientContext localContext = HttpClientContext.create();
this.localContext.setAttribute(HttpClientContext.AUTH_CACHE, this.authCache); localContext.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credentialsProvider);
this.localContext.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore); localContext.setAttribute(HttpClientContext.AUTH_CACHE, this.authCache);
localContext.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore);
final HttpRequestBase httprequest = request.prepareRequest(); final HttpRequestBase httprequest = request.prepareRequest();
httprequest.reset(); httprequest.reset();
return new Response(this.httpclient.execute(httprequest, this.localContext)); return new Response(this.httpclient.execute(httprequest, localContext));
} }
/** /**