|
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/. */ |
|
4 |
|
5 package org.mozilla.gecko.sync; |
|
6 |
|
7 import java.util.concurrent.CountDownLatch; |
|
8 import java.util.concurrent.TimeUnit; |
|
9 |
|
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; |
|
16 |
|
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"; |
|
24 |
|
25 protected final AuthHeaderProvider authHeaderProvider; |
|
26 protected final String uri; |
|
27 protected JSONRecordFetchDelegate delegate; |
|
28 |
|
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 } |
|
36 |
|
37 protected String getURI() { |
|
38 return this.uri; |
|
39 } |
|
40 |
|
41 private class JSONFetchHandler implements SyncStorageRequestDelegate { |
|
42 |
|
43 // SyncStorageRequestDelegate methods for fetching. |
|
44 @Override |
|
45 public AuthHeaderProvider getAuthHeaderProvider() { |
|
46 return authHeaderProvider; |
|
47 } |
|
48 |
|
49 public String ifUnmodifiedSince() { |
|
50 return null; |
|
51 } |
|
52 |
|
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 } |
|
64 |
|
65 @Override |
|
66 public void handleRequestFailure(SyncStorageResponse response) { |
|
67 delegate.handleFailure(response); |
|
68 } |
|
69 |
|
70 @Override |
|
71 public void handleRequestError(Exception ex) { |
|
72 delegate.handleError(ex); |
|
73 } |
|
74 } |
|
75 |
|
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 } |
|
86 |
|
87 private class LatchedJSONRecordFetchDelegate implements JSONRecordFetchDelegate { |
|
88 public ExtendedJSONObject body = null; |
|
89 public Exception exception = null; |
|
90 private CountDownLatch latch; |
|
91 |
|
92 public LatchedJSONRecordFetchDelegate(CountDownLatch latch) { |
|
93 this.latch = latch; |
|
94 } |
|
95 |
|
96 @Override |
|
97 public void handleFailure(SyncStorageResponse response) { |
|
98 this.exception = new HTTPFailureException(response); |
|
99 latch.countDown(); |
|
100 } |
|
101 |
|
102 @Override |
|
103 public void handleError(Exception e) { |
|
104 this.exception = e; |
|
105 latch.countDown(); |
|
106 } |
|
107 |
|
108 @Override |
|
109 public void handleSuccess(ExtendedJSONObject body) { |
|
110 this.body = body; |
|
111 latch.countDown(); |
|
112 } |
|
113 } |
|
114 |
|
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); |
|
124 |
|
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 } |
|
132 |
|
133 if (delegate.body != null) { |
|
134 return delegate.body; |
|
135 } |
|
136 |
|
137 if (delegate.exception != null) { |
|
138 throw delegate.exception; |
|
139 } |
|
140 |
|
141 throw new Exception("Unknown error."); |
|
142 } |
|
143 } |