Reformatting and minor code tweaks

git-svn-id: https://zxing.googlecode.com/svn/trunk@621 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2008-10-17 14:48:22 +00:00
parent 8cb804ec9e
commit 394fb04c81

View file

@ -16,7 +16,6 @@
package com.android.barcodes; package com.android.barcodes;
import android.content.ContentResolver;
import android.util.Log; import android.util.Log;
import org.apache.http.Header; import org.apache.http.Header;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
@ -26,7 +25,7 @@ import org.apache.http.HttpHost;
import org.apache.http.HttpRequest; import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor; import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException; import org.apache.http.HttpMessage;
import org.apache.http.client.HttpClient; import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler; import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.client.methods.HttpUriRequest;
@ -59,31 +58,33 @@ import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream; import java.util.zip.GZIPOutputStream;
/** /**
* Subclass of the Apache {@link DefaultHttpClient} that is configured with * <p>Subclass of the Apache {@link DefaultHttpClient} that is configured with
* reasonable default settings and registered schemes for Android, and * reasonable default settings and registered schemes for Android, and
* also lets the user add {@link HttpRequestInterceptor} classes. * also lets the user add {@link HttpRequestInterceptor} classes.
* Don't create this directly, use the {@link #newInstance} factory method. * Don't create this directly, use the {@link #newInstance} factory method.</p>
* *
* <p>This client processes cookies but does not retain them by default. * <p>This client processes cookies but does not retain them by default.
* To retain cookies, simply add a cookie store to the HttpContext:</p> * To retain cookies, simply add a cookie store to the HttpContext:
*
* <pre>context.setAttribute(ClientContext.COOKIE_STORE, cookieStore);</pre> * <pre>context.setAttribute(ClientContext.COOKIE_STORE, cookieStore);</pre>
* * </p>
* {@hide}
*/ */
public final class AndroidHttpClient implements HttpClient { public final class AndroidHttpClient implements HttpClient {
// Gzip of data shorter than this probably won't be worthwhile // Gzip of data shorter than this probably won't be worthwhile
public static long DEFAULT_SYNC_MIN_GZIP_BYTES = 256; public static final long DEFAULT_SYNC_MIN_GZIP_BYTES = 256;
private static final String TAG = "AndroidHttpClient"; private static final String TAG = "AndroidHttpClient";
/** Set if HTTP requests are blocked from being executed on this thread */ /**
* Set if HTTP requests are blocked from being executed on this thread
*/
private static final ThreadLocal<Boolean> sThreadBlocked = private static final ThreadLocal<Boolean> sThreadBlocked =
new ThreadLocal<Boolean>(); new ThreadLocal<Boolean>();
/** Interceptor throws an exception if the executing thread is blocked */ /**
* Interceptor throws an exception if the executing thread is blocked
*/
private static final HttpRequestInterceptor sThreadCheckInterceptor = private static final HttpRequestInterceptor sThreadCheckInterceptor =
new HttpRequestInterceptor() { new HttpRequestInterceptor() {
public void process(HttpRequest request, HttpContext context) { public void process(HttpRequest request, HttpContext context) {
@ -95,6 +96,7 @@ public final class AndroidHttpClient implements HttpClient {
/** /**
* Create a new HttpClient with reasonable defaults (which you can update). * Create a new HttpClient with reasonable defaults (which you can update).
*
* @param userAgent to report in your HTTP requests. * @param userAgent to report in your HTTP requests.
* @return AndroidHttpClient for you to use for all your requests. * @return AndroidHttpClient for you to use for all your requests.
*/ */
@ -151,15 +153,9 @@ public final class AndroidHttpClient implements HttpClient {
// Same as DefaultHttpClient.createHttpContext() minus the // Same as DefaultHttpClient.createHttpContext() minus the
// cookie store. // cookie store.
HttpContext context = new BasicHttpContext(); HttpContext context = new BasicHttpContext();
context.setAttribute( context.setAttribute(ClientContext.AUTHSCHEME_REGISTRY, getAuthSchemes());
ClientContext.AUTHSCHEME_REGISTRY, context.setAttribute(ClientContext.COOKIESPEC_REGISTRY, getCookieSpecs());
getAuthSchemes()); context.setAttribute(ClientContext.CREDS_PROVIDER, getCredentialsProvider());
context.setAttribute(
ClientContext.COOKIESPEC_REGISTRY,
getCookieSpecs());
context.setAttribute(
ClientContext.CREDS_PROVIDER,
getCredentialsProvider());
return context; return context;
} }
}; };
@ -177,6 +173,7 @@ public final class AndroidHttpClient implements HttpClient {
/** /**
* Block this thread from executing HTTP requests. * Block this thread from executing HTTP requests.
* Used to guard against HTTP requests blocking the main application thread. * Used to guard against HTTP requests blocking the main application thread.
*
* @param blocked if HTTP requests run on this thread should be denied * @param blocked if HTTP requests run on this thread should be denied
*/ */
public static void setThreadBlocked(boolean blocked) { public static void setThreadBlocked(boolean blocked) {
@ -186,10 +183,11 @@ public final class AndroidHttpClient implements HttpClient {
/** /**
* Modifies a request to indicate to the server that we would like a * Modifies a request to indicate to the server that we would like a
* gzipped response. (Uses the "Accept-Encoding" HTTP header.) * gzipped response. (Uses the "Accept-Encoding" HTTP header.)
*
* @param request the request to modify * @param request the request to modify
* @see #getUngzippedContent * @see #getUngzippedContent
*/ */
public static void modifyRequestToAcceptGzipResponse(HttpRequest request) { public static void modifyRequestToAcceptGzipResponse(HttpMessage request) {
request.addHeader("Accept-Encoding", "gzip"); request.addHeader("Accept-Encoding", "gzip");
} }
@ -201,16 +199,20 @@ public final class AndroidHttpClient implements HttpClient {
* @return the input stream to read from * @return the input stream to read from
* @throws IOException * @throws IOException
*/ */
public static InputStream getUngzippedContent(HttpEntity entity) public static InputStream getUngzippedContent(HttpEntity entity) throws IOException {
throws IOException {
InputStream responseStream = entity.getContent(); InputStream responseStream = entity.getContent();
if (responseStream == null) return responseStream; if (responseStream == null) {
return responseStream;
}
Header header = entity.getContentEncoding(); Header header = entity.getContentEncoding();
if (header == null) return responseStream; if (header == null) {
return responseStream;
}
String contentEncoding = header.getValue(); String contentEncoding = header.getValue();
if (contentEncoding == null) return responseStream; if (contentEncoding == null) {
if (contentEncoding.contains("gzip")) responseStream return responseStream;
= new GZIPInputStream(responseStream); }
if (contentEncoding.contains("gzip")) responseStream = new GZIPInputStream(responseStream);
return responseStream; return responseStream;
} }
@ -237,13 +239,11 @@ public final class AndroidHttpClient implements HttpClient {
return delegate.execute(request); return delegate.execute(request);
} }
public HttpResponse execute(HttpUriRequest request, HttpContext context) public HttpResponse execute(HttpUriRequest request, HttpContext context) throws IOException {
throws IOException {
return delegate.execute(request, context); return delegate.execute(request, context);
} }
public HttpResponse execute(HttpHost target, HttpRequest request) public HttpResponse execute(HttpHost target, HttpRequest request) throws IOException {
throws IOException {
return delegate.execute(target, request); return delegate.execute(target, request);
} }
@ -252,27 +252,24 @@ public final class AndroidHttpClient implements HttpClient {
return delegate.execute(target, request, context); return delegate.execute(target, request, context);
} }
public <T> T execute(HttpUriRequest request, public <T> T execute(HttpUriRequest request, ResponseHandler<? extends T> responseHandler) throws IOException {
ResponseHandler<? extends T> responseHandler)
throws IOException, ClientProtocolException {
return delegate.execute(request, responseHandler); return delegate.execute(request, responseHandler);
} }
public <T> T execute(HttpUriRequest request, public <T> T execute(HttpUriRequest request, ResponseHandler<? extends T> responseHandler, HttpContext context)
ResponseHandler<? extends T> responseHandler, HttpContext context) throws IOException {
throws IOException, ClientProtocolException {
return delegate.execute(request, responseHandler, context); return delegate.execute(request, responseHandler, context);
} }
public <T> T execute(HttpHost target, HttpRequest request, public <T> T execute(HttpHost target, HttpRequest request, ResponseHandler<? extends T> responseHandler)
ResponseHandler<? extends T> responseHandler) throws IOException, throws IOException {
ClientProtocolException {
return delegate.execute(target, request, responseHandler); return delegate.execute(target, request, responseHandler);
} }
public <T> T execute(HttpHost target, HttpRequest request, public <T> T execute(HttpHost target, HttpRequest request,
ResponseHandler<? extends T> responseHandler, HttpContext context) ResponseHandler<? extends T> responseHandler,
throws IOException, ClientProtocolException { HttpContext context)
throws IOException {
return delegate.execute(target, request, responseHandler, context); return delegate.execute(target, request, responseHandler, context);
} }
@ -280,13 +277,13 @@ public final class AndroidHttpClient implements HttpClient {
* Compress data to send to server. * Compress data to send to server.
* Creates a Http Entity holding the gzipped data. * Creates a Http Entity holding the gzipped data.
* The data will not be compressed if it is too short. * The data will not be compressed if it is too short.
*
* @param data The bytes to compress * @param data The bytes to compress
* @return Entity holding the data * @return Entity holding the data
*/ */
public static AbstractHttpEntity getCompressedEntity(byte data[], ContentResolver resolver) public static AbstractHttpEntity getCompressedEntity(byte data[]) throws IOException {
throws IOException {
AbstractHttpEntity entity; AbstractHttpEntity entity;
if (data.length < getMinGzipSize(resolver)) { if (data.length < getMinGzipSize()) {
entity = new ByteArrayEntity(data); entity = new ByteArrayEntity(data);
} else { } else {
ByteArrayOutputStream arr = new ByteArrayOutputStream(); ByteArrayOutputStream arr = new ByteArrayOutputStream();
@ -303,7 +300,7 @@ public final class AndroidHttpClient implements HttpClient {
* Retrieves the minimum size for compressing data. * Retrieves the minimum size for compressing data.
* Shorter data will not be compressed. * Shorter data will not be compressed.
*/ */
public static long getMinGzipSize(ContentResolver resolver) { public static long getMinGzipSize() {
return DEFAULT_SYNC_MIN_GZIP_BYTES; return DEFAULT_SYNC_MIN_GZIP_BYTES;
} }
@ -337,7 +334,9 @@ public final class AndroidHttpClient implements HttpClient {
} }
} }
/** cURL logging configuration. */ /**
* cURL logging configuration.
*/
private volatile LoggingConfiguration curlConfiguration; private volatile LoggingConfiguration curlConfiguration;
/** /**
@ -378,12 +377,11 @@ public final class AndroidHttpClient implements HttpClient {
configuration.println(toCurl((HttpUriRequest) request)); configuration.println(toCurl((HttpUriRequest) request));
} }
} }
}
/** /**
* Generates a cURL command equivalent to the given request. * Generates a cURL command equivalent to the given request.
*/ */
private static String toCurl(HttpUriRequest request) throws IOException { private String toCurl(HttpUriRequest request) throws IOException {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
builder.append("curl "); builder.append("curl ");
@ -406,9 +404,9 @@ public final class AndroidHttpClient implements HttpClient {
} }
} }
builder.append("\""); builder.append('"');
builder.append(uri); builder.append(uri);
builder.append("\""); builder.append('"');
if (request instanceof HttpEntityEnclosingRequest) { if (request instanceof HttpEntityEnclosingRequest) {
HttpEntityEnclosingRequest entityRequest = HttpEntityEnclosingRequest entityRequest =
@ -419,11 +417,8 @@ public final class AndroidHttpClient implements HttpClient {
ByteArrayOutputStream stream = new ByteArrayOutputStream(); ByteArrayOutputStream stream = new ByteArrayOutputStream();
entity.writeTo(stream); entity.writeTo(stream);
String entityString = stream.toString(); String entityString = stream.toString();
// TODO: Check the content type, too. // TODO: Check the content type, too.
builder.append(" --data-ascii \"") builder.append(" --data-ascii \"").append(entityString).append('"');
.append(entityString)
.append("\"");
} else { } else {
builder.append(" [TOO MUCH DATA TO INCLUDE]"); builder.append(" [TOO MUCH DATA TO INCLUDE]");
} }
@ -433,3 +428,5 @@ public final class AndroidHttpClient implements HttpClient {
return builder.toString(); return builder.toString();
} }
} }
}