michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.sync.repositories; michael@0: michael@0: import java.net.URI; michael@0: import java.net.URISyntaxException; michael@0: import java.util.ArrayList; michael@0: michael@0: import org.mozilla.gecko.sync.InfoCollections; michael@0: import org.mozilla.gecko.sync.Utils; michael@0: import org.mozilla.gecko.sync.net.AuthHeaderProvider; michael@0: import org.mozilla.gecko.sync.repositories.delegates.RepositorySessionCreationDelegate; michael@0: michael@0: import android.content.Context; michael@0: michael@0: /** michael@0: * A Server11Repository implements fetching and storing against the Sync 1.1 API. michael@0: * It doesn't do crypto: that's the job of the middleware. michael@0: * michael@0: * @author rnewman michael@0: */ michael@0: public class Server11Repository extends Repository { michael@0: protected String collection; michael@0: protected URI collectionURI; michael@0: protected final AuthHeaderProvider authHeaderProvider; michael@0: protected final InfoCollections infoCollections; michael@0: michael@0: /** michael@0: * Construct a new repository that fetches and stores against the Sync 1.1. API. michael@0: * michael@0: * @param collection name. michael@0: * @param storageURL full URL to storage endpoint. michael@0: * @param authHeaderProvider to use in requests; may be null. michael@0: * @param infoCollections instance; must not be null. michael@0: * @throws URISyntaxException michael@0: */ michael@0: public Server11Repository(String collection, String storageURL, AuthHeaderProvider authHeaderProvider, InfoCollections infoCollections) throws URISyntaxException { michael@0: if (collection == null) { michael@0: throw new IllegalArgumentException("collection must not be null"); michael@0: } michael@0: if (storageURL == null) { michael@0: throw new IllegalArgumentException("storageURL must not be null"); michael@0: } michael@0: if (infoCollections == null) { michael@0: throw new IllegalArgumentException("infoCollections must not be null"); michael@0: } michael@0: this.collection = collection; michael@0: this.collectionURI = new URI(storageURL + (storageURL.endsWith("/") ? collection : "/" + collection)); michael@0: this.authHeaderProvider = authHeaderProvider; michael@0: this.infoCollections = infoCollections; michael@0: } michael@0: michael@0: @Override michael@0: public void createSession(RepositorySessionCreationDelegate delegate, michael@0: Context context) { michael@0: delegate.onSessionCreated(new Server11RepositorySession(this)); michael@0: } michael@0: michael@0: public URI collectionURI() { michael@0: return this.collectionURI; michael@0: } michael@0: michael@0: public URI collectionURI(boolean full, long newer, long limit, String sort, String ids) throws URISyntaxException { michael@0: ArrayList params = new ArrayList(); michael@0: if (full) { michael@0: params.add("full=1"); michael@0: } michael@0: if (newer >= 0) { michael@0: // Translate local millisecond timestamps into server decimal seconds. michael@0: String newerString = Utils.millisecondsToDecimalSecondsString(newer); michael@0: params.add("newer=" + newerString); michael@0: } michael@0: if (limit > 0) { michael@0: params.add("limit=" + limit); michael@0: } michael@0: if (sort != null) { michael@0: params.add("sort=" + sort); // We trust these values. michael@0: } michael@0: if (ids != null) { michael@0: params.add("ids=" + ids); // We trust these values. michael@0: } michael@0: michael@0: if (params.size() == 0) { michael@0: return this.collectionURI; michael@0: } michael@0: michael@0: StringBuilder out = new StringBuilder(); michael@0: char indicator = '?'; michael@0: for (String param : params) { michael@0: out.append(indicator); michael@0: indicator = '&'; michael@0: out.append(param); michael@0: } michael@0: String uri = this.collectionURI + out.toString(); michael@0: return new URI(uri); michael@0: } michael@0: michael@0: public URI wboURI(String id) throws URISyntaxException { michael@0: return new URI(this.collectionURI + "/" + id); michael@0: } michael@0: michael@0: // Override these. michael@0: @SuppressWarnings("static-method") michael@0: protected long getDefaultFetchLimit() { michael@0: return -1; michael@0: } michael@0: michael@0: @SuppressWarnings("static-method") michael@0: protected String getDefaultSort() { michael@0: return null; michael@0: } michael@0: michael@0: public AuthHeaderProvider getAuthHeaderProvider() { michael@0: return authHeaderProvider; michael@0: } michael@0: michael@0: public boolean updateNeeded(long lastSyncTimestamp) { michael@0: return infoCollections.updateNeeded(collection, lastSyncTimestamp); michael@0: } michael@0: }