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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/sync/repositories/Server11Repository.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,122 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +package org.mozilla.gecko.sync.repositories;
     1.9 +
    1.10 +import java.net.URI;
    1.11 +import java.net.URISyntaxException;
    1.12 +import java.util.ArrayList;
    1.13 +
    1.14 +import org.mozilla.gecko.sync.InfoCollections;
    1.15 +import org.mozilla.gecko.sync.Utils;
    1.16 +import org.mozilla.gecko.sync.net.AuthHeaderProvider;
    1.17 +import org.mozilla.gecko.sync.repositories.delegates.RepositorySessionCreationDelegate;
    1.18 +
    1.19 +import android.content.Context;
    1.20 +
    1.21 +/**
    1.22 + * A Server11Repository implements fetching and storing against the Sync 1.1 API.
    1.23 + * It doesn't do crypto: that's the job of the middleware.
    1.24 + *
    1.25 + * @author rnewman
    1.26 + */
    1.27 +public class Server11Repository extends Repository {
    1.28 +  protected String collection;
    1.29 +  protected URI collectionURI;
    1.30 +  protected final AuthHeaderProvider authHeaderProvider;
    1.31 +  protected final InfoCollections infoCollections;
    1.32 +
    1.33 +  /**
    1.34 +   * Construct a new repository that fetches and stores against the Sync 1.1. API.
    1.35 +   *
    1.36 +   * @param collection name.
    1.37 +   * @param storageURL full URL to storage endpoint.
    1.38 +   * @param authHeaderProvider to use in requests; may be null.
    1.39 +   * @param infoCollections instance; must not be null.
    1.40 +   * @throws URISyntaxException
    1.41 +   */
    1.42 +  public Server11Repository(String collection, String storageURL, AuthHeaderProvider authHeaderProvider, InfoCollections infoCollections) throws URISyntaxException {
    1.43 +    if (collection == null) {
    1.44 +      throw new IllegalArgumentException("collection must not be null");
    1.45 +    }
    1.46 +    if (storageURL == null) {
    1.47 +      throw new IllegalArgumentException("storageURL must not be null");
    1.48 +    }
    1.49 +    if (infoCollections == null) {
    1.50 +      throw new IllegalArgumentException("infoCollections must not be null");
    1.51 +    }
    1.52 +    this.collection = collection;
    1.53 +    this.collectionURI = new URI(storageURL + (storageURL.endsWith("/") ? collection : "/" + collection));
    1.54 +    this.authHeaderProvider = authHeaderProvider;
    1.55 +    this.infoCollections = infoCollections;
    1.56 +  }
    1.57 +
    1.58 +  @Override
    1.59 +  public void createSession(RepositorySessionCreationDelegate delegate,
    1.60 +                            Context context) {
    1.61 +    delegate.onSessionCreated(new Server11RepositorySession(this));
    1.62 +  }
    1.63 +
    1.64 +  public URI collectionURI() {
    1.65 +    return this.collectionURI;
    1.66 +  }
    1.67 +
    1.68 +  public URI collectionURI(boolean full, long newer, long limit, String sort, String ids) throws URISyntaxException {
    1.69 +    ArrayList<String> params = new ArrayList<String>();
    1.70 +    if (full) {
    1.71 +      params.add("full=1");
    1.72 +    }
    1.73 +    if (newer >= 0) {
    1.74 +      // Translate local millisecond timestamps into server decimal seconds.
    1.75 +      String newerString = Utils.millisecondsToDecimalSecondsString(newer);
    1.76 +      params.add("newer=" + newerString);
    1.77 +    }
    1.78 +    if (limit > 0) {
    1.79 +      params.add("limit=" + limit);
    1.80 +    }
    1.81 +    if (sort != null) {
    1.82 +      params.add("sort=" + sort);       // We trust these values.
    1.83 +    }
    1.84 +    if (ids != null) {
    1.85 +      params.add("ids=" + ids);         // We trust these values.
    1.86 +    }
    1.87 +
    1.88 +    if (params.size() == 0) {
    1.89 +      return this.collectionURI;
    1.90 +    }
    1.91 +
    1.92 +    StringBuilder out = new StringBuilder();
    1.93 +    char indicator = '?';
    1.94 +    for (String param : params) {
    1.95 +      out.append(indicator);
    1.96 +      indicator = '&';
    1.97 +      out.append(param);
    1.98 +    }
    1.99 +    String uri = this.collectionURI + out.toString();
   1.100 +    return new URI(uri);
   1.101 +  }
   1.102 +
   1.103 +  public URI wboURI(String id) throws URISyntaxException {
   1.104 +    return new URI(this.collectionURI + "/" + id);
   1.105 +  }
   1.106 +
   1.107 +  // Override these.
   1.108 +  @SuppressWarnings("static-method")
   1.109 +  protected long getDefaultFetchLimit() {
   1.110 +    return -1;
   1.111 +  }
   1.112 +
   1.113 +  @SuppressWarnings("static-method")
   1.114 +  protected String getDefaultSort() {
   1.115 +    return null;
   1.116 +  }
   1.117 +
   1.118 +  public AuthHeaderProvider getAuthHeaderProvider() {
   1.119 +    return authHeaderProvider;
   1.120 +  }
   1.121 +
   1.122 +  public boolean updateNeeded(long lastSyncTimestamp) {
   1.123 +    return infoCollections.updateNeeded(collection, lastSyncTimestamp);
   1.124 +  }
   1.125 +}

mercurial