summaryrefslogtreecommitdiffstats
path: root/mobile/android/services/src/main/java/org/mozilla/gecko/sync/JSONRecordFetcher.java
blob: 982b5b0266b918e6451a5cbdaccf76edc3ac0232 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.gecko.sync;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.mozilla.gecko.background.common.log.Logger;
import org.mozilla.gecko.sync.delegates.JSONRecordFetchDelegate;
import org.mozilla.gecko.sync.net.AuthHeaderProvider;
import org.mozilla.gecko.sync.net.SyncStorageRecordRequest;
import org.mozilla.gecko.sync.net.SyncStorageRequestDelegate;
import org.mozilla.gecko.sync.net.SyncStorageResponse;

/**
 * An object which fetches a chunk of JSON from a URI, using certain credentials,
 * and informs its delegate of the result.
 */
public class JSONRecordFetcher {
  private static final long DEFAULT_AWAIT_TIMEOUT_MSEC = 2 * 60 * 1000;   // Two minutes.
  private static final String LOG_TAG = "JSONRecordFetcher";

  protected final AuthHeaderProvider authHeaderProvider;
  protected final String uri;
  protected JSONRecordFetchDelegate delegate;

  public JSONRecordFetcher(final String uri, final AuthHeaderProvider authHeaderProvider) {
    if (uri == null) {
      throw new IllegalArgumentException("uri must not be null");
    }
    this.uri = uri;
    this.authHeaderProvider = authHeaderProvider;
  }

  protected String getURI() {
    return this.uri;
  }

  private class JSONFetchHandler implements SyncStorageRequestDelegate {

    // SyncStorageRequestDelegate methods for fetching.
    @Override
    public AuthHeaderProvider getAuthHeaderProvider() {
      return authHeaderProvider;
    }

    @Override
    public String ifUnmodifiedSince() {
      return null;
    }

    @Override
    public void handleRequestSuccess(SyncStorageResponse response) {
      if (response.wasSuccessful()) {
        try {
          delegate.handleSuccess(response.jsonObjectBody());
        } catch (Exception e) {
          handleRequestError(e);
        }
        return;
      }
      handleRequestFailure(response);
    }

    @Override
    public void handleRequestFailure(SyncStorageResponse response) {
      delegate.handleFailure(response);
    }

    @Override
    public void handleRequestError(Exception ex) {
      delegate.handleError(ex);
    }
  }

  public void fetch(final JSONRecordFetchDelegate delegate) {
    this.delegate = delegate;
    try {
      final SyncStorageRecordRequest r = new SyncStorageRecordRequest(this.getURI());
      r.delegate = new JSONFetchHandler();
      r.get();
    } catch (Exception e) {
      delegate.handleError(e);
    }
  }

  private class LatchedJSONRecordFetchDelegate implements JSONRecordFetchDelegate {
    public ExtendedJSONObject body = null;
    public Exception exception = null;
    private final CountDownLatch latch;

    public LatchedJSONRecordFetchDelegate(CountDownLatch latch) {
      this.latch = latch;
    }

    @Override
    public void handleFailure(SyncStorageResponse response) {
      this.exception = new HTTPFailureException(response);
      latch.countDown();
    }

    @Override
    public void handleError(Exception e) {
      this.exception = e;
      latch.countDown();
    }

    @Override
    public void handleSuccess(ExtendedJSONObject body) {
      this.body = body;
      latch.countDown();
    }
  }

  /**
   * Fetch the info record, blocking until it returns.
   * @return the info record.
   */
  public ExtendedJSONObject fetchBlocking() throws HTTPFailureException, Exception {
    CountDownLatch latch = new CountDownLatch(1);
    LatchedJSONRecordFetchDelegate delegate = new LatchedJSONRecordFetchDelegate(latch);
    this.delegate = delegate;
    this.fetch(delegate);

    // Sanity wait: the resource itself will time out and throw after two
    // minutes, so we just want to avoid coding errors causing us to block
    // endlessly.
    if (!latch.await(DEFAULT_AWAIT_TIMEOUT_MSEC, TimeUnit.MILLISECONDS)) {
      Logger.warn(LOG_TAG, "Interrupted fetching info record.");
      throw new InterruptedException("info fetch timed out.");
    }

    if (delegate.body != null) {
      return delegate.body;
    }

    if (delegate.exception != null) {
      throw delegate.exception;
    }

    throw new Exception("Unknown error.");
  }
}