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;
import android.content.ContentResolver;
import android.util.Log;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
@ -26,7 +25,7 @@ import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
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.ResponseHandler;
import org.apache.http.client.methods.HttpUriRequest;
@ -59,31 +58,33 @@ import java.util.zip.GZIPInputStream;
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
* 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.
* 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>
*
* {@hide}
* </p>
*/
public final class AndroidHttpClient implements HttpClient {
// 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";
/** 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 =
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 =
new HttpRequestInterceptor() {
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).
*
* @param userAgent to report in your HTTP 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
// cookie store.
HttpContext context = new BasicHttpContext();
context.setAttribute(
ClientContext.AUTHSCHEME_REGISTRY,
getAuthSchemes());
context.setAttribute(
ClientContext.COOKIESPEC_REGISTRY,
getCookieSpecs());
context.setAttribute(
ClientContext.CREDS_PROVIDER,
getCredentialsProvider());
context.setAttribute(ClientContext.AUTHSCHEME_REGISTRY, getAuthSchemes());
context.setAttribute(ClientContext.COOKIESPEC_REGISTRY, getCookieSpecs());
context.setAttribute(ClientContext.CREDS_PROVIDER, getCredentialsProvider());
return context;
}
};
@ -177,6 +173,7 @@ public final class AndroidHttpClient implements HttpClient {
/**
* Block this thread from executing HTTP requests.
* Used to guard against HTTP requests blocking the main application thread.
*
* @param blocked if HTTP requests run on this thread should be denied
*/
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
* gzipped response. (Uses the "Accept-Encoding" HTTP header.)
*
* @param request the request to modify
* @see #getUngzippedContent
*/
public static void modifyRequestToAcceptGzipResponse(HttpRequest request) {
public static void modifyRequestToAcceptGzipResponse(HttpMessage request) {
request.addHeader("Accept-Encoding", "gzip");
}
@ -201,16 +199,20 @@ public final class AndroidHttpClient implements HttpClient {
* @return the input stream to read from
* @throws IOException
*/
public static InputStream getUngzippedContent(HttpEntity entity)
throws IOException {
public static InputStream getUngzippedContent(HttpEntity entity) throws IOException {
InputStream responseStream = entity.getContent();
if (responseStream == null) return responseStream;
if (responseStream == null) {
return responseStream;
}
Header header = entity.getContentEncoding();
if (header == null) return responseStream;
if (header == null) {
return responseStream;
}
String contentEncoding = header.getValue();
if (contentEncoding == null) return responseStream;
if (contentEncoding.contains("gzip")) responseStream
= new GZIPInputStream(responseStream);
if (contentEncoding == null) {
return responseStream;
}
if (contentEncoding.contains("gzip")) responseStream = new GZIPInputStream(responseStream);
return responseStream;
}
@ -237,13 +239,11 @@ public final class AndroidHttpClient implements HttpClient {
return delegate.execute(request);
}
public HttpResponse execute(HttpUriRequest request, HttpContext context)
throws IOException {
public HttpResponse execute(HttpUriRequest request, HttpContext context) throws IOException {
return delegate.execute(request, context);
}
public HttpResponse execute(HttpHost target, HttpRequest request)
throws IOException {
public HttpResponse execute(HttpHost target, HttpRequest request) throws IOException {
return delegate.execute(target, request);
}
@ -252,27 +252,24 @@ public final class AndroidHttpClient implements HttpClient {
return delegate.execute(target, request, context);
}
public <T> T execute(HttpUriRequest request,
ResponseHandler<? extends T> responseHandler)
throws IOException, ClientProtocolException {
public <T> T execute(HttpUriRequest request, ResponseHandler<? extends T> responseHandler) throws IOException {
return delegate.execute(request, responseHandler);
}
public <T> T execute(HttpUriRequest request,
ResponseHandler<? extends T> responseHandler, HttpContext context)
throws IOException, ClientProtocolException {
public <T> T execute(HttpUriRequest request, ResponseHandler<? extends T> responseHandler, HttpContext context)
throws IOException {
return delegate.execute(request, responseHandler, context);
}
public <T> T execute(HttpHost target, HttpRequest request,
ResponseHandler<? extends T> responseHandler) throws IOException,
ClientProtocolException {
public <T> T execute(HttpHost target, HttpRequest request, ResponseHandler<? extends T> responseHandler)
throws IOException {
return delegate.execute(target, request, responseHandler);
}
public <T> T execute(HttpHost target, HttpRequest request,
ResponseHandler<? extends T> responseHandler, HttpContext context)
throws IOException, ClientProtocolException {
ResponseHandler<? extends T> responseHandler,
HttpContext context)
throws IOException {
return delegate.execute(target, request, responseHandler, context);
}
@ -280,13 +277,13 @@ public final class AndroidHttpClient implements HttpClient {
* Compress data to send to server.
* Creates a Http Entity holding the gzipped data.
* The data will not be compressed if it is too short.
*
* @param data The bytes to compress
* @return Entity holding the data
*/
public static AbstractHttpEntity getCompressedEntity(byte data[], ContentResolver resolver)
throws IOException {
public static AbstractHttpEntity getCompressedEntity(byte data[]) throws IOException {
AbstractHttpEntity entity;
if (data.length < getMinGzipSize(resolver)) {
if (data.length < getMinGzipSize()) {
entity = new ByteArrayEntity(data);
} else {
ByteArrayOutputStream arr = new ByteArrayOutputStream();
@ -303,7 +300,7 @@ public final class AndroidHttpClient implements HttpClient {
* Retrieves the minimum size for compressing data.
* Shorter data will not be compressed.
*/
public static long getMinGzipSize(ContentResolver resolver) {
public static long getMinGzipSize() {
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;
/**
@ -378,17 +377,16 @@ public final class AndroidHttpClient implements HttpClient {
configuration.println(toCurl((HttpUriRequest) 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();
builder.append("curl ");
for (Header header: request.getAllHeaders()) {
for (Header header : request.getAllHeaders()) {
builder.append("--header \"");
builder.append(header.toString().trim());
builder.append("\" ");
@ -406,9 +404,9 @@ public final class AndroidHttpClient implements HttpClient {
}
}
builder.append("\"");
builder.append('"');
builder.append(uri);
builder.append("\"");
builder.append('"');
if (request instanceof HttpEntityEnclosingRequest) {
HttpEntityEnclosingRequest entityRequest =
@ -419,11 +417,8 @@ public final class AndroidHttpClient implements HttpClient {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
entity.writeTo(stream);
String entityString = stream.toString();
// TODO: Check the content type, too.
builder.append(" --data-ascii \"")
.append(entityString)
.append("\"");
builder.append(" --data-ascii \"").append(entityString).append('"');
} else {
builder.append(" [TOO MUCH DATA TO INCLUDE]");
}
@ -432,4 +427,6 @@ public final class AndroidHttpClient implements HttpClient {
return builder.toString();
}
}
}