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
|
/* 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.repositories.downloaders;
import org.mozilla.gecko.background.common.log.Logger;
import org.mozilla.gecko.sync.CryptoRecord;
import org.mozilla.gecko.sync.HTTPFailureException;
import org.mozilla.gecko.sync.crypto.KeyBundle;
import org.mozilla.gecko.sync.net.AuthHeaderProvider;
import org.mozilla.gecko.sync.net.SyncStorageCollectionRequest;
import org.mozilla.gecko.sync.net.SyncStorageResponse;
import org.mozilla.gecko.sync.net.WBOCollectionRequestDelegate;
import org.mozilla.gecko.sync.repositories.delegates.RepositorySessionFetchRecordsDelegate;
/**
* Delegate that gets passed into fetch methods to handle server response from fetch.
*/
public class BatchingDownloaderDelegate extends WBOCollectionRequestDelegate {
public static final String LOG_TAG = "BatchingDownloaderDelegate";
private BatchingDownloader downloader;
private RepositorySessionFetchRecordsDelegate fetchRecordsDelegate;
public SyncStorageCollectionRequest request;
// Used to pass back to BatchDownloader to start another fetch with these parameters if needed.
private long newer;
private long batchLimit;
private boolean full;
private String sort;
private String ids;
public BatchingDownloaderDelegate(final BatchingDownloader downloader,
final RepositorySessionFetchRecordsDelegate fetchRecordsDelegate,
final SyncStorageCollectionRequest request, long newer,
long batchLimit, boolean full, String sort, String ids) {
this.downloader = downloader;
this.fetchRecordsDelegate = fetchRecordsDelegate;
this.request = request;
this.newer = newer;
this.batchLimit = batchLimit;
this.full = full;
this.sort = sort;
this.ids = ids;
}
@Override
public AuthHeaderProvider getAuthHeaderProvider() {
return this.downloader.getServerRepository().getAuthHeaderProvider();
}
@Override
public String ifUnmodifiedSince() {
return this.downloader.getLastModified();
}
@Override
public void handleRequestSuccess(SyncStorageResponse response) {
Logger.debug(LOG_TAG, "Fetch done.");
if (response.lastModified() != null) {
this.downloader.onFetchCompleted(response, this.fetchRecordsDelegate, this.request,
this.newer, this.batchLimit, this.full, this.sort, this.ids);
return;
}
this.downloader.onFetchFailed(
new IllegalStateException("Missing last modified header from response"),
this.fetchRecordsDelegate,
this.request);
}
@Override
public void handleRequestFailure(SyncStorageResponse response) {
this.handleRequestError(new HTTPFailureException(response));
}
@Override
public void handleRequestError(final Exception ex) {
Logger.warn(LOG_TAG, "Got request error.", ex);
this.downloader.onFetchFailed(ex, this.fetchRecordsDelegate, this.request);
}
@Override
public void handleWBO(CryptoRecord record) {
this.downloader.onFetchedRecord(record, this.fetchRecordsDelegate);
}
@Override
public KeyBundle keyBundle() {
return null;
}
}
|