Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | package org.mozilla.gecko.sync.repositories; |
michael@0 | 6 | |
michael@0 | 7 | import java.net.URI; |
michael@0 | 8 | import java.net.URISyntaxException; |
michael@0 | 9 | import java.util.ArrayList; |
michael@0 | 10 | |
michael@0 | 11 | import org.mozilla.gecko.sync.InfoCollections; |
michael@0 | 12 | import org.mozilla.gecko.sync.Utils; |
michael@0 | 13 | import org.mozilla.gecko.sync.net.AuthHeaderProvider; |
michael@0 | 14 | import org.mozilla.gecko.sync.repositories.delegates.RepositorySessionCreationDelegate; |
michael@0 | 15 | |
michael@0 | 16 | import android.content.Context; |
michael@0 | 17 | |
michael@0 | 18 | /** |
michael@0 | 19 | * A Server11Repository implements fetching and storing against the Sync 1.1 API. |
michael@0 | 20 | * It doesn't do crypto: that's the job of the middleware. |
michael@0 | 21 | * |
michael@0 | 22 | * @author rnewman |
michael@0 | 23 | */ |
michael@0 | 24 | public class Server11Repository extends Repository { |
michael@0 | 25 | protected String collection; |
michael@0 | 26 | protected URI collectionURI; |
michael@0 | 27 | protected final AuthHeaderProvider authHeaderProvider; |
michael@0 | 28 | protected final InfoCollections infoCollections; |
michael@0 | 29 | |
michael@0 | 30 | /** |
michael@0 | 31 | * Construct a new repository that fetches and stores against the Sync 1.1. API. |
michael@0 | 32 | * |
michael@0 | 33 | * @param collection name. |
michael@0 | 34 | * @param storageURL full URL to storage endpoint. |
michael@0 | 35 | * @param authHeaderProvider to use in requests; may be null. |
michael@0 | 36 | * @param infoCollections instance; must not be null. |
michael@0 | 37 | * @throws URISyntaxException |
michael@0 | 38 | */ |
michael@0 | 39 | public Server11Repository(String collection, String storageURL, AuthHeaderProvider authHeaderProvider, InfoCollections infoCollections) throws URISyntaxException { |
michael@0 | 40 | if (collection == null) { |
michael@0 | 41 | throw new IllegalArgumentException("collection must not be null"); |
michael@0 | 42 | } |
michael@0 | 43 | if (storageURL == null) { |
michael@0 | 44 | throw new IllegalArgumentException("storageURL must not be null"); |
michael@0 | 45 | } |
michael@0 | 46 | if (infoCollections == null) { |
michael@0 | 47 | throw new IllegalArgumentException("infoCollections must not be null"); |
michael@0 | 48 | } |
michael@0 | 49 | this.collection = collection; |
michael@0 | 50 | this.collectionURI = new URI(storageURL + (storageURL.endsWith("/") ? collection : "/" + collection)); |
michael@0 | 51 | this.authHeaderProvider = authHeaderProvider; |
michael@0 | 52 | this.infoCollections = infoCollections; |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | @Override |
michael@0 | 56 | public void createSession(RepositorySessionCreationDelegate delegate, |
michael@0 | 57 | Context context) { |
michael@0 | 58 | delegate.onSessionCreated(new Server11RepositorySession(this)); |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | public URI collectionURI() { |
michael@0 | 62 | return this.collectionURI; |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | public URI collectionURI(boolean full, long newer, long limit, String sort, String ids) throws URISyntaxException { |
michael@0 | 66 | ArrayList<String> params = new ArrayList<String>(); |
michael@0 | 67 | if (full) { |
michael@0 | 68 | params.add("full=1"); |
michael@0 | 69 | } |
michael@0 | 70 | if (newer >= 0) { |
michael@0 | 71 | // Translate local millisecond timestamps into server decimal seconds. |
michael@0 | 72 | String newerString = Utils.millisecondsToDecimalSecondsString(newer); |
michael@0 | 73 | params.add("newer=" + newerString); |
michael@0 | 74 | } |
michael@0 | 75 | if (limit > 0) { |
michael@0 | 76 | params.add("limit=" + limit); |
michael@0 | 77 | } |
michael@0 | 78 | if (sort != null) { |
michael@0 | 79 | params.add("sort=" + sort); // We trust these values. |
michael@0 | 80 | } |
michael@0 | 81 | if (ids != null) { |
michael@0 | 82 | params.add("ids=" + ids); // We trust these values. |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | if (params.size() == 0) { |
michael@0 | 86 | return this.collectionURI; |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | StringBuilder out = new StringBuilder(); |
michael@0 | 90 | char indicator = '?'; |
michael@0 | 91 | for (String param : params) { |
michael@0 | 92 | out.append(indicator); |
michael@0 | 93 | indicator = '&'; |
michael@0 | 94 | out.append(param); |
michael@0 | 95 | } |
michael@0 | 96 | String uri = this.collectionURI + out.toString(); |
michael@0 | 97 | return new URI(uri); |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | public URI wboURI(String id) throws URISyntaxException { |
michael@0 | 101 | return new URI(this.collectionURI + "/" + id); |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | // Override these. |
michael@0 | 105 | @SuppressWarnings("static-method") |
michael@0 | 106 | protected long getDefaultFetchLimit() { |
michael@0 | 107 | return -1; |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | @SuppressWarnings("static-method") |
michael@0 | 111 | protected String getDefaultSort() { |
michael@0 | 112 | return null; |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | public AuthHeaderProvider getAuthHeaderProvider() { |
michael@0 | 116 | return authHeaderProvider; |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | public boolean updateNeeded(long lastSyncTimestamp) { |
michael@0 | 120 | return infoCollections.updateNeeded(collection, lastSyncTimestamp); |
michael@0 | 121 | } |
michael@0 | 122 | } |