1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/sync/JSONRecordFetcher.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,143 @@ 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; 1.9 + 1.10 +import java.util.concurrent.CountDownLatch; 1.11 +import java.util.concurrent.TimeUnit; 1.12 + 1.13 +import org.mozilla.gecko.background.common.log.Logger; 1.14 +import org.mozilla.gecko.sync.delegates.JSONRecordFetchDelegate; 1.15 +import org.mozilla.gecko.sync.net.AuthHeaderProvider; 1.16 +import org.mozilla.gecko.sync.net.SyncStorageRecordRequest; 1.17 +import org.mozilla.gecko.sync.net.SyncStorageRequestDelegate; 1.18 +import org.mozilla.gecko.sync.net.SyncStorageResponse; 1.19 + 1.20 +/** 1.21 + * An object which fetches a chunk of JSON from a URI, using certain credentials, 1.22 + * and informs its delegate of the result. 1.23 + */ 1.24 +public class JSONRecordFetcher { 1.25 + private static final long DEFAULT_AWAIT_TIMEOUT_MSEC = 2 * 60 * 1000; // Two minutes. 1.26 + private static final String LOG_TAG = "JSONRecordFetcher"; 1.27 + 1.28 + protected final AuthHeaderProvider authHeaderProvider; 1.29 + protected final String uri; 1.30 + protected JSONRecordFetchDelegate delegate; 1.31 + 1.32 + public JSONRecordFetcher(final String uri, final AuthHeaderProvider authHeaderProvider) { 1.33 + if (uri == null) { 1.34 + throw new IllegalArgumentException("uri must not be null"); 1.35 + } 1.36 + this.uri = uri; 1.37 + this.authHeaderProvider = authHeaderProvider; 1.38 + } 1.39 + 1.40 + protected String getURI() { 1.41 + return this.uri; 1.42 + } 1.43 + 1.44 + private class JSONFetchHandler implements SyncStorageRequestDelegate { 1.45 + 1.46 + // SyncStorageRequestDelegate methods for fetching. 1.47 + @Override 1.48 + public AuthHeaderProvider getAuthHeaderProvider() { 1.49 + return authHeaderProvider; 1.50 + } 1.51 + 1.52 + public String ifUnmodifiedSince() { 1.53 + return null; 1.54 + } 1.55 + 1.56 + public void handleRequestSuccess(SyncStorageResponse response) { 1.57 + if (response.wasSuccessful()) { 1.58 + try { 1.59 + delegate.handleSuccess(response.jsonObjectBody()); 1.60 + } catch (Exception e) { 1.61 + handleRequestError(e); 1.62 + } 1.63 + return; 1.64 + } 1.65 + handleRequestFailure(response); 1.66 + } 1.67 + 1.68 + @Override 1.69 + public void handleRequestFailure(SyncStorageResponse response) { 1.70 + delegate.handleFailure(response); 1.71 + } 1.72 + 1.73 + @Override 1.74 + public void handleRequestError(Exception ex) { 1.75 + delegate.handleError(ex); 1.76 + } 1.77 + } 1.78 + 1.79 + public void fetch(final JSONRecordFetchDelegate delegate) { 1.80 + this.delegate = delegate; 1.81 + try { 1.82 + final SyncStorageRecordRequest r = new SyncStorageRecordRequest(this.getURI()); 1.83 + r.delegate = new JSONFetchHandler(); 1.84 + r.get(); 1.85 + } catch (Exception e) { 1.86 + delegate.handleError(e); 1.87 + } 1.88 + } 1.89 + 1.90 + private class LatchedJSONRecordFetchDelegate implements JSONRecordFetchDelegate { 1.91 + public ExtendedJSONObject body = null; 1.92 + public Exception exception = null; 1.93 + private CountDownLatch latch; 1.94 + 1.95 + public LatchedJSONRecordFetchDelegate(CountDownLatch latch) { 1.96 + this.latch = latch; 1.97 + } 1.98 + 1.99 + @Override 1.100 + public void handleFailure(SyncStorageResponse response) { 1.101 + this.exception = new HTTPFailureException(response); 1.102 + latch.countDown(); 1.103 + } 1.104 + 1.105 + @Override 1.106 + public void handleError(Exception e) { 1.107 + this.exception = e; 1.108 + latch.countDown(); 1.109 + } 1.110 + 1.111 + @Override 1.112 + public void handleSuccess(ExtendedJSONObject body) { 1.113 + this.body = body; 1.114 + latch.countDown(); 1.115 + } 1.116 + } 1.117 + 1.118 + /** 1.119 + * Fetch the info record, blocking until it returns. 1.120 + * @return the info record. 1.121 + */ 1.122 + public ExtendedJSONObject fetchBlocking() throws HTTPFailureException, Exception { 1.123 + CountDownLatch latch = new CountDownLatch(1); 1.124 + LatchedJSONRecordFetchDelegate delegate = new LatchedJSONRecordFetchDelegate(latch); 1.125 + this.delegate = delegate; 1.126 + this.fetch(delegate); 1.127 + 1.128 + // Sanity wait: the resource itself will time out and throw after two 1.129 + // minutes, so we just want to avoid coding errors causing us to block 1.130 + // endlessly. 1.131 + if (!latch.await(DEFAULT_AWAIT_TIMEOUT_MSEC, TimeUnit.MILLISECONDS)) { 1.132 + Logger.warn(LOG_TAG, "Interrupted fetching info record."); 1.133 + throw new InterruptedException("info fetch timed out."); 1.134 + } 1.135 + 1.136 + if (delegate.body != null) { 1.137 + return delegate.body; 1.138 + } 1.139 + 1.140 + if (delegate.exception != null) { 1.141 + throw delegate.exception; 1.142 + } 1.143 + 1.144 + throw new Exception("Unknown error."); 1.145 + } 1.146 +} 1.147 \ No newline at end of file