2 * Copyright (C) 2008 ZXing authors
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.google.zxing.client.android;
19 import org.apache.http.HttpHost;
20 import org.apache.http.HttpRequest;
21 import org.apache.http.HttpRequestInterceptor;
22 import org.apache.http.HttpResponse;
23 import org.apache.http.client.HttpClient;
24 import org.apache.http.client.ResponseHandler;
25 import org.apache.http.client.methods.HttpUriRequest;
26 import org.apache.http.client.params.HttpClientParams;
27 import org.apache.http.client.protocol.ClientContext;
28 import org.apache.http.conn.ClientConnectionManager;
29 import org.apache.http.conn.scheme.PlainSocketFactory;
30 import org.apache.http.conn.scheme.Scheme;
31 import org.apache.http.conn.scheme.SchemeRegistry;
32 import org.apache.http.conn.ssl.SSLSocketFactory;
33 import org.apache.http.impl.client.DefaultHttpClient;
34 import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
35 import org.apache.http.params.BasicHttpParams;
36 import org.apache.http.params.HttpConnectionParams;
37 import org.apache.http.params.HttpParams;
38 import org.apache.http.params.HttpProtocolParams;
39 import org.apache.http.protocol.BasicHttpContext;
40 import org.apache.http.protocol.HttpContext;
42 import java.io.IOException;
45 * <p>Subclass of the Apache {@link DefaultHttpClient} that is configured with
46 * reasonable default settings and registered schemes for Android, and
47 * also lets the user add {@link HttpRequestInterceptor} classes.
48 * Don't create this directly, use the {@link #newInstance} factory method.</p>
50 * <p>This client processes cookies but does not retain them by default.
51 * To retain cookies, simply add a cookie store to the HttpContext:
52 * <pre>context.setAttribute(ClientContext.COOKIE_STORE, cookieStore);</pre>
55 public final class AndroidHttpClient implements HttpClient {
58 * Create a new HttpClient with reasonable defaults (which you can update).
60 * @param userAgent to report in your HTTP requests.
61 * @return AndroidHttpClient for you to use for all your requests.
63 public static AndroidHttpClient newInstance(String userAgent) {
64 HttpParams params = new BasicHttpParams();
66 // Turn off stale checking. Our connections break all the time anyway,
67 // and it's not worth it to pay the penalty of checking every time.
68 HttpConnectionParams.setStaleCheckingEnabled(params, false);
70 // Default connection and socket timeout of 20 seconds. Tweak to taste.
71 HttpConnectionParams.setConnectionTimeout(params, 20 * 1000);
72 HttpConnectionParams.setSoTimeout(params, 20 * 1000);
73 HttpConnectionParams.setSocketBufferSize(params, 8192);
75 // Don't handle redirects -- return them to the caller. Our code
76 // often wants to re-POST after a redirect, which we must do ourselves.
77 HttpClientParams.setRedirecting(params, false);
79 // Set the specified user agent and register standard protocols.
80 if (userAgent != null) {
81 HttpProtocolParams.setUserAgent(params, userAgent);
83 SchemeRegistry schemeRegistry = new SchemeRegistry();
84 schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
85 schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
86 ClientConnectionManager manager = new ThreadSafeClientConnManager(params, schemeRegistry);
88 // We use a factory method to modify superclass initialization
89 // parameters without the funny call-a-static-method dance.
90 return new AndroidHttpClient(manager, params);
93 private final HttpClient delegate;
96 private AndroidHttpClient(ClientConnectionManager ccm, HttpParams params) {
97 this.delegate = new DelegateHttpClient(ccm, params);
101 * Release resources associated with this client. You must call this,
102 * or significant resources (sockets and memory) may be leaked.
104 public void close() {
105 getConnectionManager().shutdown();
108 public HttpParams getParams() {
109 return delegate.getParams();
112 public ClientConnectionManager getConnectionManager() {
113 return delegate.getConnectionManager();
116 public HttpResponse execute(HttpUriRequest request) throws IOException {
117 return delegate.execute(request);
120 public HttpResponse execute(HttpUriRequest request, HttpContext context) throws IOException {
121 return delegate.execute(request, context);
124 public HttpResponse execute(HttpHost target, HttpRequest request) throws IOException {
125 return delegate.execute(target, request);
128 public HttpResponse execute(HttpHost target, HttpRequest request, HttpContext context) throws IOException {
129 return delegate.execute(target, request, context);
132 public <T> T execute(HttpUriRequest request, ResponseHandler<? extends T> responseHandler) throws IOException {
133 return delegate.execute(request, responseHandler);
136 public <T> T execute(HttpUriRequest request, ResponseHandler<? extends T> responseHandler, HttpContext context)
138 return delegate.execute(request, responseHandler, context);
141 public <T> T execute(HttpHost target, HttpRequest request, ResponseHandler<? extends T> responseHandler)
143 return delegate.execute(target, request, responseHandler);
146 public <T> T execute(HttpHost target, HttpRequest request,
147 ResponseHandler<? extends T> responseHandler,
148 HttpContext context) throws IOException {
149 return delegate.execute(target, request, responseHandler, context);
152 private static class DelegateHttpClient extends DefaultHttpClient {
154 private DelegateHttpClient(ClientConnectionManager ccm, HttpParams params) {
159 protected HttpContext createHttpContext() {
160 // Same as DefaultHttpClient.createHttpContext() minus the
162 HttpContext context = new BasicHttpContext();
163 context.setAttribute(ClientContext.AUTHSCHEME_REGISTRY, getAuthSchemes());
164 context.setAttribute(ClientContext.COOKIESPEC_REGISTRY, getCookieSpecs());
165 context.setAttribute(ClientContext.CREDS_PROVIDER, getCredentialsProvider());