mobile/android/base/sync/JSONRecordFetcher.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 package org.mozilla.gecko.sync;
     7 import java.util.concurrent.CountDownLatch;
     8 import java.util.concurrent.TimeUnit;
    10 import org.mozilla.gecko.background.common.log.Logger;
    11 import org.mozilla.gecko.sync.delegates.JSONRecordFetchDelegate;
    12 import org.mozilla.gecko.sync.net.AuthHeaderProvider;
    13 import org.mozilla.gecko.sync.net.SyncStorageRecordRequest;
    14 import org.mozilla.gecko.sync.net.SyncStorageRequestDelegate;
    15 import org.mozilla.gecko.sync.net.SyncStorageResponse;
    17 /**
    18  * An object which fetches a chunk of JSON from a URI, using certain credentials,
    19  * and informs its delegate of the result.
    20  */
    21 public class JSONRecordFetcher {
    22   private static final long DEFAULT_AWAIT_TIMEOUT_MSEC = 2 * 60 * 1000;   // Two minutes.
    23   private static final String LOG_TAG = "JSONRecordFetcher";
    25   protected final AuthHeaderProvider authHeaderProvider;
    26   protected final String uri;
    27   protected JSONRecordFetchDelegate delegate;
    29   public JSONRecordFetcher(final String uri, final AuthHeaderProvider authHeaderProvider) {
    30     if (uri == null) {
    31       throw new IllegalArgumentException("uri must not be null");
    32     }
    33     this.uri = uri;
    34     this.authHeaderProvider = authHeaderProvider;
    35   }
    37   protected String getURI() {
    38     return this.uri;
    39   }
    41   private class JSONFetchHandler implements SyncStorageRequestDelegate {
    43     // SyncStorageRequestDelegate methods for fetching.
    44     @Override
    45     public AuthHeaderProvider getAuthHeaderProvider() {
    46       return authHeaderProvider;
    47     }
    49     public String ifUnmodifiedSince() {
    50       return null;
    51     }
    53     public void handleRequestSuccess(SyncStorageResponse response) {
    54       if (response.wasSuccessful()) {
    55         try {
    56           delegate.handleSuccess(response.jsonObjectBody());
    57         } catch (Exception e) {
    58           handleRequestError(e);
    59         }
    60         return;
    61       }
    62       handleRequestFailure(response);
    63     }
    65     @Override
    66     public void handleRequestFailure(SyncStorageResponse response) {
    67       delegate.handleFailure(response);
    68     }
    70     @Override
    71     public void handleRequestError(Exception ex) {
    72       delegate.handleError(ex);
    73     }
    74   }
    76   public void fetch(final JSONRecordFetchDelegate delegate) {
    77     this.delegate = delegate;
    78     try {
    79       final SyncStorageRecordRequest r = new SyncStorageRecordRequest(this.getURI());
    80       r.delegate = new JSONFetchHandler();
    81       r.get();
    82     } catch (Exception e) {
    83       delegate.handleError(e);
    84     }
    85   }
    87   private class LatchedJSONRecordFetchDelegate implements JSONRecordFetchDelegate {
    88     public ExtendedJSONObject body = null;
    89     public Exception exception = null;
    90     private CountDownLatch latch;
    92     public LatchedJSONRecordFetchDelegate(CountDownLatch latch) {
    93       this.latch = latch;
    94     }
    96     @Override
    97     public void handleFailure(SyncStorageResponse response) {
    98       this.exception = new HTTPFailureException(response);
    99       latch.countDown();
   100     }
   102     @Override
   103     public void handleError(Exception e) {
   104       this.exception = e;
   105       latch.countDown();
   106     }
   108     @Override
   109     public void handleSuccess(ExtendedJSONObject body) {
   110       this.body = body;
   111       latch.countDown();
   112     }
   113   }
   115   /**
   116    * Fetch the info record, blocking until it returns.
   117    * @return the info record.
   118    */
   119   public ExtendedJSONObject fetchBlocking() throws HTTPFailureException, Exception {
   120     CountDownLatch latch = new CountDownLatch(1);
   121     LatchedJSONRecordFetchDelegate delegate = new LatchedJSONRecordFetchDelegate(latch);
   122     this.delegate = delegate;
   123     this.fetch(delegate);
   125     // Sanity wait: the resource itself will time out and throw after two
   126     // minutes, so we just want to avoid coding errors causing us to block
   127     // endlessly.
   128     if (!latch.await(DEFAULT_AWAIT_TIMEOUT_MSEC, TimeUnit.MILLISECONDS)) {
   129       Logger.warn(LOG_TAG, "Interrupted fetching info record.");
   130       throw new InterruptedException("info fetch timed out.");
   131     }
   133     if (delegate.body != null) {
   134       return delegate.body;
   135     }
   137     if (delegate.exception != null) {
   138       throw delegate.exception;
   139     }
   141     throw new Exception("Unknown error.");
   142   }
   143 }

mercurial