mobile/android/base/sync/repositories/Server11Repository.java

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

mercurial