1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/sync/net/BaseResource.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,470 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +package org.mozilla.gecko.sync.net; 1.9 + 1.10 +import java.io.BufferedReader; 1.11 +import java.io.IOException; 1.12 +import java.io.UnsupportedEncodingException; 1.13 +import java.lang.ref.WeakReference; 1.14 +import java.net.URI; 1.15 +import java.net.URISyntaxException; 1.16 +import java.security.GeneralSecurityException; 1.17 +import java.security.KeyManagementException; 1.18 +import java.security.NoSuchAlgorithmException; 1.19 +import java.security.SecureRandom; 1.20 + 1.21 +import javax.net.ssl.SSLContext; 1.22 + 1.23 +import org.json.simple.JSONArray; 1.24 +import org.json.simple.JSONObject; 1.25 +import org.mozilla.gecko.background.common.log.Logger; 1.26 +import org.mozilla.gecko.sync.ExtendedJSONObject; 1.27 + 1.28 +import ch.boye.httpclientandroidlib.Header; 1.29 +import ch.boye.httpclientandroidlib.HttpEntity; 1.30 +import ch.boye.httpclientandroidlib.HttpResponse; 1.31 +import ch.boye.httpclientandroidlib.HttpVersion; 1.32 +import ch.boye.httpclientandroidlib.client.AuthCache; 1.33 +import ch.boye.httpclientandroidlib.client.ClientProtocolException; 1.34 +import ch.boye.httpclientandroidlib.client.methods.HttpDelete; 1.35 +import ch.boye.httpclientandroidlib.client.methods.HttpGet; 1.36 +import ch.boye.httpclientandroidlib.client.methods.HttpPost; 1.37 +import ch.boye.httpclientandroidlib.client.methods.HttpPut; 1.38 +import ch.boye.httpclientandroidlib.client.methods.HttpRequestBase; 1.39 +import ch.boye.httpclientandroidlib.client.methods.HttpUriRequest; 1.40 +import ch.boye.httpclientandroidlib.client.protocol.ClientContext; 1.41 +import ch.boye.httpclientandroidlib.conn.ClientConnectionManager; 1.42 +import ch.boye.httpclientandroidlib.conn.scheme.PlainSocketFactory; 1.43 +import ch.boye.httpclientandroidlib.conn.scheme.Scheme; 1.44 +import ch.boye.httpclientandroidlib.conn.scheme.SchemeRegistry; 1.45 +import ch.boye.httpclientandroidlib.conn.ssl.SSLSocketFactory; 1.46 +import ch.boye.httpclientandroidlib.entity.StringEntity; 1.47 +import ch.boye.httpclientandroidlib.impl.client.BasicAuthCache; 1.48 +import ch.boye.httpclientandroidlib.impl.client.DefaultHttpClient; 1.49 +import ch.boye.httpclientandroidlib.impl.conn.tsccm.ThreadSafeClientConnManager; 1.50 +import ch.boye.httpclientandroidlib.params.HttpConnectionParams; 1.51 +import ch.boye.httpclientandroidlib.params.HttpParams; 1.52 +import ch.boye.httpclientandroidlib.params.HttpProtocolParams; 1.53 +import ch.boye.httpclientandroidlib.protocol.BasicHttpContext; 1.54 +import ch.boye.httpclientandroidlib.protocol.HttpContext; 1.55 +import ch.boye.httpclientandroidlib.util.EntityUtils; 1.56 + 1.57 +/** 1.58 + * Provide simple HTTP access to a Sync server or similar. 1.59 + * Implements Basic Auth by asking its delegate for credentials. 1.60 + * Communicates with a ResourceDelegate to asynchronously return responses and errors. 1.61 + * Exposes simple get/post/put/delete methods. 1.62 + */ 1.63 +public class BaseResource implements Resource { 1.64 + private static final String ANDROID_LOOPBACK_IP = "10.0.2.2"; 1.65 + 1.66 + private static final int MAX_TOTAL_CONNECTIONS = 20; 1.67 + private static final int MAX_CONNECTIONS_PER_ROUTE = 10; 1.68 + 1.69 + private boolean retryOnFailedRequest = true; 1.70 + 1.71 + public static boolean rewriteLocalhost = true; 1.72 + 1.73 + private static final String LOG_TAG = "BaseResource"; 1.74 + 1.75 + protected final URI uri; 1.76 + protected BasicHttpContext context; 1.77 + protected DefaultHttpClient client; 1.78 + public ResourceDelegate delegate; 1.79 + protected HttpRequestBase request; 1.80 + public String charset = "utf-8"; 1.81 + 1.82 + protected static WeakReference<HttpResponseObserver> httpResponseObserver = null; 1.83 + 1.84 + public BaseResource(String uri) throws URISyntaxException { 1.85 + this(uri, rewriteLocalhost); 1.86 + } 1.87 + 1.88 + public BaseResource(URI uri) { 1.89 + this(uri, rewriteLocalhost); 1.90 + } 1.91 + 1.92 + public BaseResource(String uri, boolean rewrite) throws URISyntaxException { 1.93 + this(new URI(uri), rewrite); 1.94 + } 1.95 + 1.96 + public BaseResource(URI uri, boolean rewrite) { 1.97 + if (uri == null) { 1.98 + throw new IllegalArgumentException("uri must not be null"); 1.99 + } 1.100 + if (rewrite && "localhost".equals(uri.getHost())) { 1.101 + // Rewrite localhost URIs to refer to the special Android emulator loopback passthrough interface. 1.102 + Logger.debug(LOG_TAG, "Rewriting " + uri + " to point to " + ANDROID_LOOPBACK_IP + "."); 1.103 + try { 1.104 + this.uri = new URI(uri.getScheme(), uri.getUserInfo(), ANDROID_LOOPBACK_IP, uri.getPort(), uri.getPath(), uri.getQuery(), uri.getFragment()); 1.105 + } catch (URISyntaxException e) { 1.106 + Logger.error(LOG_TAG, "Got error rewriting URI for Android emulator.", e); 1.107 + throw new IllegalArgumentException("Invalid URI", e); 1.108 + } 1.109 + } else { 1.110 + this.uri = uri; 1.111 + } 1.112 + } 1.113 + 1.114 + public static synchronized HttpResponseObserver getHttpResponseObserver() { 1.115 + if (httpResponseObserver == null) { 1.116 + return null; 1.117 + } 1.118 + return httpResponseObserver.get(); 1.119 + } 1.120 + 1.121 + public static synchronized void setHttpResponseObserver(HttpResponseObserver newHttpResponseObserver) { 1.122 + if (httpResponseObserver != null) { 1.123 + httpResponseObserver.clear(); 1.124 + } 1.125 + httpResponseObserver = new WeakReference<HttpResponseObserver>(newHttpResponseObserver); 1.126 + } 1.127 + 1.128 + @Override 1.129 + public URI getURI() { 1.130 + return this.uri; 1.131 + } 1.132 + 1.133 + @Override 1.134 + public String getURIString() { 1.135 + return this.uri.toString(); 1.136 + } 1.137 + 1.138 + @Override 1.139 + public String getHostname() { 1.140 + return this.getURI().getHost(); 1.141 + } 1.142 + 1.143 + /** 1.144 + * This shuts up HttpClient, which will otherwise debug log about there 1.145 + * being no auth cache in the context. 1.146 + */ 1.147 + private static void addAuthCacheToContext(HttpUriRequest request, HttpContext context) { 1.148 + AuthCache authCache = new BasicAuthCache(); // Not thread safe. 1.149 + context.setAttribute(ClientContext.AUTH_CACHE, authCache); 1.150 + } 1.151 + 1.152 + /** 1.153 + * Invoke this after delegate and request have been set. 1.154 + * @throws NoSuchAlgorithmException 1.155 + * @throws KeyManagementException 1.156 + */ 1.157 + protected void prepareClient() throws KeyManagementException, NoSuchAlgorithmException, GeneralSecurityException { 1.158 + context = new BasicHttpContext(); 1.159 + 1.160 + // We could reuse these client instances, except that we mess around 1.161 + // with their parameters… so we'd need a pool of some kind. 1.162 + client = new DefaultHttpClient(getConnectionManager()); 1.163 + 1.164 + // TODO: Eventually we should use Apache HttpAsyncClient. It's not out of alpha yet. 1.165 + // Until then, we synchronously make the request, then invoke our delegate's callback. 1.166 + AuthHeaderProvider authHeaderProvider = delegate.getAuthHeaderProvider(); 1.167 + if (authHeaderProvider != null) { 1.168 + Header authHeader = authHeaderProvider.getAuthHeader(request, context, client); 1.169 + if (authHeader != null) { 1.170 + request.addHeader(authHeader); 1.171 + Logger.debug(LOG_TAG, "Added auth header."); 1.172 + } 1.173 + } 1.174 + 1.175 + addAuthCacheToContext(request, context); 1.176 + 1.177 + HttpParams params = client.getParams(); 1.178 + HttpConnectionParams.setConnectionTimeout(params, delegate.connectionTimeout()); 1.179 + HttpConnectionParams.setSoTimeout(params, delegate.socketTimeout()); 1.180 + HttpConnectionParams.setStaleCheckingEnabled(params, false); 1.181 + HttpProtocolParams.setContentCharset(params, charset); 1.182 + HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); 1.183 + final String userAgent = delegate.getUserAgent(); 1.184 + if (userAgent != null) { 1.185 + HttpProtocolParams.setUserAgent(params, userAgent); 1.186 + } 1.187 + delegate.addHeaders(request, client); 1.188 + } 1.189 + 1.190 + private static Object connManagerMonitor = new Object(); 1.191 + private static ClientConnectionManager connManager; 1.192 + 1.193 + // Call within a synchronized block on connManagerMonitor. 1.194 + private static ClientConnectionManager enableTLSConnectionManager() throws KeyManagementException, NoSuchAlgorithmException { 1.195 + SSLContext sslContext = SSLContext.getInstance("TLS"); 1.196 + sslContext.init(null, null, new SecureRandom()); 1.197 + SSLSocketFactory sf = new TLSSocketFactory(sslContext); 1.198 + SchemeRegistry schemeRegistry = new SchemeRegistry(); 1.199 + schemeRegistry.register(new Scheme("https", 443, sf)); 1.200 + schemeRegistry.register(new Scheme("http", 80, new PlainSocketFactory())); 1.201 + ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(schemeRegistry); 1.202 + 1.203 + cm.setMaxTotal(MAX_TOTAL_CONNECTIONS); 1.204 + cm.setDefaultMaxPerRoute(MAX_CONNECTIONS_PER_ROUTE); 1.205 + connManager = cm; 1.206 + return cm; 1.207 + } 1.208 + 1.209 + public static ClientConnectionManager getConnectionManager() throws KeyManagementException, NoSuchAlgorithmException 1.210 + { 1.211 + // TODO: shutdown. 1.212 + synchronized (connManagerMonitor) { 1.213 + if (connManager != null) { 1.214 + return connManager; 1.215 + } 1.216 + return enableTLSConnectionManager(); 1.217 + } 1.218 + } 1.219 + 1.220 + /** 1.221 + * Do some cleanup, so we don't need the stale connection check. 1.222 + */ 1.223 + public static void closeExpiredConnections() { 1.224 + ClientConnectionManager connectionManager; 1.225 + synchronized (connManagerMonitor) { 1.226 + connectionManager = connManager; 1.227 + } 1.228 + if (connectionManager == null) { 1.229 + return; 1.230 + } 1.231 + Logger.trace(LOG_TAG, "Closing expired connections."); 1.232 + connectionManager.closeExpiredConnections(); 1.233 + } 1.234 + 1.235 + public static void shutdownConnectionManager() { 1.236 + ClientConnectionManager connectionManager; 1.237 + synchronized (connManagerMonitor) { 1.238 + connectionManager = connManager; 1.239 + connManager = null; 1.240 + } 1.241 + if (connectionManager == null) { 1.242 + return; 1.243 + } 1.244 + Logger.debug(LOG_TAG, "Shutting down connection manager."); 1.245 + connectionManager.shutdown(); 1.246 + } 1.247 + 1.248 + private void execute() { 1.249 + HttpResponse response; 1.250 + try { 1.251 + response = client.execute(request, context); 1.252 + Logger.debug(LOG_TAG, "Response: " + response.getStatusLine().toString()); 1.253 + } catch (ClientProtocolException e) { 1.254 + delegate.handleHttpProtocolException(e); 1.255 + return; 1.256 + } catch (IOException e) { 1.257 + Logger.debug(LOG_TAG, "I/O exception returned from execute."); 1.258 + if (!retryOnFailedRequest) { 1.259 + delegate.handleHttpIOException(e); 1.260 + } else { 1.261 + retryRequest(); 1.262 + } 1.263 + return; 1.264 + } catch (Exception e) { 1.265 + // Bug 740731: Don't let an exception fall through. Wrapping isn't 1.266 + // optimal, but often the exception is treated as an Exception anyway. 1.267 + if (!retryOnFailedRequest) { 1.268 + // Bug 769671: IOException(Throwable cause) was added only in API level 9. 1.269 + final IOException ex = new IOException(); 1.270 + ex.initCause(e); 1.271 + delegate.handleHttpIOException(ex); 1.272 + } else { 1.273 + retryRequest(); 1.274 + } 1.275 + return; 1.276 + } 1.277 + 1.278 + // Don't retry if the observer or delegate throws! 1.279 + HttpResponseObserver observer = getHttpResponseObserver(); 1.280 + if (observer != null) { 1.281 + observer.observeHttpResponse(response); 1.282 + } 1.283 + delegate.handleHttpResponse(response); 1.284 + } 1.285 + 1.286 + private void retryRequest() { 1.287 + // Only retry once. 1.288 + retryOnFailedRequest = false; 1.289 + Logger.debug(LOG_TAG, "Retrying request..."); 1.290 + this.execute(); 1.291 + } 1.292 + 1.293 + private void go(HttpRequestBase request) { 1.294 + if (delegate == null) { 1.295 + throw new IllegalArgumentException("No delegate provided."); 1.296 + } 1.297 + this.request = request; 1.298 + try { 1.299 + this.prepareClient(); 1.300 + } catch (KeyManagementException e) { 1.301 + Logger.error(LOG_TAG, "Couldn't prepare client.", e); 1.302 + delegate.handleTransportException(e); 1.303 + return; 1.304 + } catch (NoSuchAlgorithmException e) { 1.305 + Logger.error(LOG_TAG, "Couldn't prepare client.", e); 1.306 + delegate.handleTransportException(e); 1.307 + return; 1.308 + } catch (GeneralSecurityException e) { 1.309 + Logger.error(LOG_TAG, "Couldn't prepare client.", e); 1.310 + delegate.handleTransportException(e); 1.311 + return; 1.312 + } catch (Exception e) { 1.313 + // Bug 740731: Don't let an exception fall through. Wrapping isn't 1.314 + // optimal, but often the exception is treated as an Exception anyway. 1.315 + delegate.handleTransportException(new GeneralSecurityException(e)); 1.316 + return; 1.317 + } 1.318 + this.execute(); 1.319 + } 1.320 + 1.321 + @Override 1.322 + public void get() { 1.323 + Logger.debug(LOG_TAG, "HTTP GET " + this.uri.toASCIIString()); 1.324 + this.go(new HttpGet(this.uri)); 1.325 + } 1.326 + 1.327 + /** 1.328 + * Perform an HTTP GET as with {@link BaseResource#get()}, returning only 1.329 + * after callbacks have been invoked. 1.330 + */ 1.331 + public void getBlocking() { 1.332 + // Until we use the asynchronous Apache HttpClient, we can simply call 1.333 + // through. 1.334 + this.get(); 1.335 + } 1.336 + 1.337 + @Override 1.338 + public void delete() { 1.339 + Logger.debug(LOG_TAG, "HTTP DELETE " + this.uri.toASCIIString()); 1.340 + this.go(new HttpDelete(this.uri)); 1.341 + } 1.342 + 1.343 + @Override 1.344 + public void post(HttpEntity body) { 1.345 + Logger.debug(LOG_TAG, "HTTP POST " + this.uri.toASCIIString()); 1.346 + HttpPost request = new HttpPost(this.uri); 1.347 + request.setEntity(body); 1.348 + this.go(request); 1.349 + } 1.350 + 1.351 + @Override 1.352 + public void put(HttpEntity body) { 1.353 + Logger.debug(LOG_TAG, "HTTP PUT " + this.uri.toASCIIString()); 1.354 + HttpPut request = new HttpPut(this.uri); 1.355 + request.setEntity(body); 1.356 + this.go(request); 1.357 + } 1.358 + 1.359 + protected static StringEntity stringEntityWithContentTypeApplicationJSON(String s) throws UnsupportedEncodingException { 1.360 + StringEntity e = new StringEntity(s, "UTF-8"); 1.361 + e.setContentType("application/json"); 1.362 + return e; 1.363 + } 1.364 + 1.365 + /** 1.366 + * Helper for turning a JSON object into a payload. 1.367 + * @throws UnsupportedEncodingException 1.368 + */ 1.369 + protected static StringEntity jsonEntity(JSONObject body) throws UnsupportedEncodingException { 1.370 + return stringEntityWithContentTypeApplicationJSON(body.toJSONString()); 1.371 + } 1.372 + 1.373 + /** 1.374 + * Helper for turning an extended JSON object into a payload. 1.375 + * @throws UnsupportedEncodingException 1.376 + */ 1.377 + protected static StringEntity jsonEntity(ExtendedJSONObject body) throws UnsupportedEncodingException { 1.378 + return stringEntityWithContentTypeApplicationJSON(body.toJSONString()); 1.379 + } 1.380 + 1.381 + /** 1.382 + * Helper for turning a JSON array into a payload. 1.383 + * @throws UnsupportedEncodingException 1.384 + */ 1.385 + protected static HttpEntity jsonEntity(JSONArray toPOST) throws UnsupportedEncodingException { 1.386 + return stringEntityWithContentTypeApplicationJSON(toPOST.toJSONString()); 1.387 + } 1.388 + 1.389 + /** 1.390 + * Best-effort attempt to ensure that the entity has been fully consumed and 1.391 + * that the underlying stream has been closed. 1.392 + * 1.393 + * This releases the connection back to the connection pool. 1.394 + * 1.395 + * @param entity The HttpEntity to be consumed. 1.396 + */ 1.397 + public static void consumeEntity(HttpEntity entity) { 1.398 + try { 1.399 + EntityUtils.consume(entity); 1.400 + } catch (IOException e) { 1.401 + // Doesn't matter. 1.402 + } 1.403 + } 1.404 + 1.405 + /** 1.406 + * Best-effort attempt to ensure that the entity corresponding to the given 1.407 + * HTTP response has been fully consumed and that the underlying stream has 1.408 + * been closed. 1.409 + * 1.410 + * This releases the connection back to the connection pool. 1.411 + * 1.412 + * @param response 1.413 + * The HttpResponse to be consumed. 1.414 + */ 1.415 + public static void consumeEntity(HttpResponse response) { 1.416 + if (response == null) { 1.417 + return; 1.418 + } 1.419 + try { 1.420 + EntityUtils.consume(response.getEntity()); 1.421 + } catch (IOException e) { 1.422 + } 1.423 + } 1.424 + 1.425 + /** 1.426 + * Best-effort attempt to ensure that the entity corresponding to the given 1.427 + * Sync storage response has been fully consumed and that the underlying 1.428 + * stream has been closed. 1.429 + * 1.430 + * This releases the connection back to the connection pool. 1.431 + * 1.432 + * @param response 1.433 + * The SyncStorageResponse to be consumed. 1.434 + */ 1.435 + public static void consumeEntity(SyncStorageResponse response) { 1.436 + if (response.httpResponse() == null) { 1.437 + return; 1.438 + } 1.439 + consumeEntity(response.httpResponse()); 1.440 + } 1.441 + 1.442 + /** 1.443 + * Best-effort attempt to ensure that the reader has been fully consumed, so 1.444 + * that the underlying stream will be closed. 1.445 + * 1.446 + * This should allow the connection to be released back to the connection pool. 1.447 + * 1.448 + * @param reader The BufferedReader to be consumed. 1.449 + */ 1.450 + public static void consumeReader(BufferedReader reader) { 1.451 + try { 1.452 + reader.close(); 1.453 + } catch (IOException e) { 1.454 + // Do nothing. 1.455 + } 1.456 + } 1.457 + 1.458 + public void post(JSONArray jsonArray) throws UnsupportedEncodingException { 1.459 + post(jsonEntity(jsonArray)); 1.460 + } 1.461 + 1.462 + public void put(JSONObject jsonObject) throws UnsupportedEncodingException { 1.463 + put(jsonEntity(jsonObject)); 1.464 + } 1.465 + 1.466 + public void post(ExtendedJSONObject o) throws UnsupportedEncodingException { 1.467 + post(jsonEntity(o)); 1.468 + } 1.469 + 1.470 + public void post(JSONObject jsonObject) throws UnsupportedEncodingException { 1.471 + post(jsonEntity(jsonObject)); 1.472 + } 1.473 +}