GoogleHttpClientpublic class GoogleHttpClient extends Object implements HttpClient{@link AndroidHttpClient} wrapper that uses {@link UrlRules} to rewrite URLs
and otherwise tweak HTTP requests. |
Fields Summary |
---|
private static final String | TAG | private final android.net.http.AndroidHttpClient | mClient | private final android.content.ContentResolver | mResolver | private final String | mUserAgent |
Constructors Summary |
---|
public GoogleHttpClient(android.content.ContentResolver resolver, String userAgent)Create an HTTP client. Normally one client is shared throughout an app.
mClient = AndroidHttpClient.newInstance(userAgent);
mResolver = resolver;
mUserAgent = userAgent;
| public GoogleHttpClient(android.content.ContentResolver resolver, String appAndVersion, boolean gzipCapable)GoogleHttpClient(Context, String, boolean) - without SSL session
persistence.
this(resolver, null /* cache */, appAndVersion, gzipCapable);
| public GoogleHttpClient(android.content.Context context, String appAndVersion, boolean gzipCapable)Create an HTTP client. Normaly this client is shared throughout an app.
The HTTP client will construct its User-Agent as follows:
( )
or
( ); gzip
(if gzip capable)
The context has settings for URL rewriting rules and is used to enable
SSL session persistence.
this(context.getContentResolver(), SSLClientSessionCacheFactory.getCache(context),
appAndVersion, gzipCapable);
| private GoogleHttpClient(android.content.ContentResolver resolver, org.apache.harmony.xnet.provider.jsse.SSLClientSessionCache cache, String appAndVersion, boolean gzipCapable)
String userAgent = appAndVersion + " (" + Build.DEVICE + " " + Build.ID + ")";
if (gzipCapable) {
userAgent = userAgent + "; gzip";
}
mClient = AndroidHttpClient.newInstance(userAgent, cache);
mResolver = resolver;
mUserAgent = userAgent;
|
Methods Summary |
---|
public void | close()Release resources associated with this client. You must call this,
or significant resources (sockets and memory) may be leaked.
mClient.close();
| public void | disableCurlLogging()Disables cURL logging for this client.
mClient.disableCurlLogging();
| public void | enableCurlLogging(java.lang.String name, int level)Enables cURL request logging for this client.
mClient.enableCurlLogging(name, level);
| public org.apache.http.HttpResponse | execute(org.apache.http.client.methods.HttpUriRequest request)
return execute(request, (HttpContext) null);
| public org.apache.http.HttpResponse | execute(org.apache.http.HttpHost target, org.apache.http.HttpRequest request)
return mClient.execute(target, request);
| public org.apache.http.HttpResponse | execute(org.apache.http.HttpHost target, org.apache.http.HttpRequest request, org.apache.http.protocol.HttpContext context)
return mClient.execute(target, request, context);
| public T | execute(org.apache.http.client.methods.HttpUriRequest request, org.apache.http.client.ResponseHandler responseHandler)
return mClient.execute(request, responseHandler);
| public T | execute(org.apache.http.client.methods.HttpUriRequest request, org.apache.http.client.ResponseHandler responseHandler, org.apache.http.protocol.HttpContext context)
return mClient.execute(request, responseHandler, context);
| public T | execute(org.apache.http.HttpHost target, org.apache.http.HttpRequest request, org.apache.http.client.ResponseHandler responseHandler)
return mClient.execute(target, request, responseHandler);
| public T | execute(org.apache.http.HttpHost target, org.apache.http.HttpRequest request, org.apache.http.client.ResponseHandler responseHandler, org.apache.http.protocol.HttpContext context)
return mClient.execute(target, request, responseHandler, context);
| public org.apache.http.HttpResponse | execute(org.apache.http.client.methods.HttpUriRequest request, org.apache.http.protocol.HttpContext context)
// Rewrite the supplied URL...
URI uri = request.getURI();
String original = uri.toString();
UrlRules rules = UrlRules.getRules(mResolver);
UrlRules.Rule rule = rules.matchRule(original);
String rewritten = rule.apply(original);
if (rewritten == null) {
Log.w(TAG, "Blocked by " + rule.mName + ": " + original);
throw new BlockedRequestException(rule);
} else if (rewritten == original) {
return executeWithoutRewriting(request, context); // Pass through
}
try {
uri = new URI(rewritten);
} catch (URISyntaxException e) {
throw new RuntimeException("Bad URL from rule: " + rule.mName, e);
}
// Wrap request so we can replace the URI.
RequestWrapper wrapper = wrapRequest(request);
wrapper.setURI(uri);
request = wrapper;
if (Config.LOGV) {
Log.v(TAG, "Rule " + rule.mName + ": " + original + " -> " + rewritten);
}
return executeWithoutRewriting(request, context);
| public org.apache.http.HttpResponse | executeWithoutRewriting(org.apache.http.client.methods.HttpUriRequest request, org.apache.http.protocol.HttpContext context)Execute a request without applying and rewrite rules.
String code = "Error";
long start = SystemClock.elapsedRealtime();
try {
HttpResponse response;
// TODO: if we're logging network stats, and if the apache library is configured
// to follow redirects, count each redirect as an additional round trip.
// see if we're logging network stats.
boolean logNetworkStats = NetworkStatsEntity.shouldLogNetworkStats();
if (logNetworkStats) {
int uid = android.os.Process.myUid();
long startTx = NetStat.getUidTxBytes(uid);
long startRx = NetStat.getUidRxBytes(uid);
response = mClient.execute(request, context);
code = Integer.toString(response.getStatusLine().getStatusCode());
HttpEntity origEntity = response == null ? null : response.getEntity();
if (origEntity != null) {
// yeah, we compute the same thing below. we do need to compute this here
// so we can wrap the HttpEntity in the response.
long now = SystemClock.elapsedRealtime();
long elapsed = now - start;
NetworkStatsEntity entity = new NetworkStatsEntity(origEntity,
mUserAgent, uid, startTx, startRx,
elapsed /* response latency */, now /* processing start time */);
response.setEntity(entity);
}
} else {
response = mClient.execute(request, context);
code = Integer.toString(response.getStatusLine().getStatusCode());
}
return response;
} catch (IOException e) {
code = "IOException";
throw e;
} finally {
// Record some statistics to the checkin service about the outcome.
// Note that this is only describing execute(), not body download.
try {
long elapsed = SystemClock.elapsedRealtime() - start;
ContentValues values = new ContentValues();
values.put(Checkin.Stats.TAG,
Checkin.Stats.Tag.HTTP_STATUS + ":" +
mUserAgent + ":" + code);
values.put(Checkin.Stats.COUNT, 1);
values.put(Checkin.Stats.SUM, elapsed / 1000.0);
mResolver.insert(Checkin.Stats.CONTENT_URI, values);
} catch (Exception e) {
Log.e(TAG, "Error recording stats", e);
}
}
| public org.apache.http.conn.ClientConnectionManager | getConnectionManager()
return mClient.getConnectionManager();
| public static java.lang.String | getGzipCapableUserAgent(java.lang.String originalUserAgent)Mark a user agent as one Google will trust to handle gzipped content.
{@link AndroidHttpClient#modifyRequestToAcceptGzipResponse} is (also)
necessary but not sufficient -- many browsers claim to accept gzip but
have broken handling, so Google checks the user agent as well.
return originalUserAgent + "; gzip";
| public org.apache.http.params.HttpParams | getParams()
return mClient.getParams();
| private static org.apache.http.impl.client.RequestWrapper | wrapRequest(org.apache.http.client.methods.HttpUriRequest request)Wraps the request making it mutable.
try {
// We have to wrap it with the right type. Some code performs
// instanceof checks.
RequestWrapper wrapped;
if (request instanceof HttpEntityEnclosingRequest) {
wrapped = new EntityEnclosingRequestWrapper(
(HttpEntityEnclosingRequest) request);
} else {
wrapped = new RequestWrapper(request);
}
// Copy the headers from the original request into the wrapper.
wrapped.resetHeaders();
return wrapped;
} catch (ProtocolException e) {
throw new ClientProtocolException(e);
}
|
|