Don't need to block multiple thread access. Refactor and update a bit for an upcoming patch

git-svn-id: https://zxing.googlecode.com/svn/trunk@1617 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2010-10-06 16:11:05 +00:00
parent 94679de34d
commit 5053864cb3

View file

@ -37,7 +37,6 @@ import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams; import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams; import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.BasicHttpContext; import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.BasicHttpProcessor;
import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpContext;
import java.io.IOException; import java.io.IOException;
@ -55,24 +54,6 @@ import java.io.IOException;
*/ */
public final class AndroidHttpClient implements HttpClient { public final class AndroidHttpClient implements HttpClient {
/**
* Set if HTTP requests are blocked from being executed on this thread
*/
private static final ThreadLocal<Boolean> sThreadBlocked =
new ThreadLocal<Boolean>();
/**
* Interceptor throws an exception if the executing thread is blocked
*/
private static final HttpRequestInterceptor sThreadCheckInterceptor =
new HttpRequestInterceptor() {
public void process(HttpRequest request, HttpContext context) {
if (Boolean.TRUE.equals(sThreadBlocked.get())) {
throw new RuntimeException("This thread forbids HTTP requests");
}
}
};
/** /**
* Create a new HttpClient with reasonable defaults (which you can update). * Create a new HttpClient with reasonable defaults (which you can update).
* *
@ -96,14 +77,13 @@ public final class AndroidHttpClient implements HttpClient {
HttpClientParams.setRedirecting(params, false); HttpClientParams.setRedirecting(params, false);
// Set the specified user agent and register standard protocols. // Set the specified user agent and register standard protocols.
if (userAgent != null) {
HttpProtocolParams.setUserAgent(params, userAgent); HttpProtocolParams.setUserAgent(params, userAgent);
}
SchemeRegistry schemeRegistry = new SchemeRegistry(); SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
schemeRegistry.register(new Scheme("https", ClientConnectionManager manager = new ThreadSafeClientConnManager(params, schemeRegistry);
SSLSocketFactory.getSocketFactory(), 443));
ClientConnectionManager manager =
new ThreadSafeClientConnManager(params, schemeRegistry);
// We use a factory method to modify superclass initialization // We use a factory method to modify superclass initialization
// parameters without the funny call-a-static-method dance. // parameters without the funny call-a-static-method dance.
@ -114,26 +94,7 @@ public final class AndroidHttpClient implements HttpClient {
private AndroidHttpClient(ClientConnectionManager ccm, HttpParams params) { private AndroidHttpClient(ClientConnectionManager ccm, HttpParams params) {
this.delegate = new DefaultHttpClient(ccm, params) { this.delegate = new DelegateHttpClient(ccm, params);
@Override
protected BasicHttpProcessor createHttpProcessor() {
// Add interceptor to prevent making requests from main thread.
BasicHttpProcessor processor = super.createHttpProcessor();
processor.addRequestInterceptor(sThreadCheckInterceptor);
return processor;
}
@Override
protected HttpContext createHttpContext() {
// Same as DefaultHttpClient.createHttpContext() minus the
// cookie store.
HttpContext context = new BasicHttpContext();
context.setAttribute(ClientContext.AUTHSCHEME_REGISTRY, getAuthSchemes());
context.setAttribute(ClientContext.COOKIESPEC_REGISTRY, getCookieSpecs());
context.setAttribute(ClientContext.CREDS_PROVIDER, getCredentialsProvider());
return context;
}
};
} }
/** /**
@ -164,8 +125,7 @@ public final class AndroidHttpClient implements HttpClient {
return delegate.execute(target, request); return delegate.execute(target, request);
} }
public HttpResponse execute(HttpHost target, HttpRequest request, public HttpResponse execute(HttpHost target, HttpRequest request, HttpContext context) throws IOException {
HttpContext context) throws IOException {
return delegate.execute(target, request, context); return delegate.execute(target, request, context);
} }
@ -185,9 +145,26 @@ public final class AndroidHttpClient implements HttpClient {
public <T> T execute(HttpHost target, HttpRequest request, public <T> T execute(HttpHost target, HttpRequest request,
ResponseHandler<? extends T> responseHandler, ResponseHandler<? extends T> responseHandler,
HttpContext context) HttpContext context) throws IOException {
throws IOException {
return delegate.execute(target, request, responseHandler, context); return delegate.execute(target, request, responseHandler, context);
} }
private static class DelegateHttpClient extends DefaultHttpClient {
private DelegateHttpClient(ClientConnectionManager ccm, HttpParams params) {
super(ccm, params);
}
@Override
protected HttpContext createHttpContext() {
// Same as DefaultHttpClient.createHttpContext() minus the
// cookie store.
HttpContext context = new BasicHttpContext();
context.setAttribute(ClientContext.AUTHSCHEME_REGISTRY, getAuthSchemes());
context.setAttribute(ClientContext.COOKIESPEC_REGISTRY, getCookieSpecs());
context.setAttribute(ClientContext.CREDS_PROVIDER, getCredentialsProvider());
return context;
}
}
} }