mobile/android/base/sync/net/SyncStorageRecordRequest.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/sync/net/SyncStorageRecordRequest.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,114 @@
     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.net;
     1.9 +
    1.10 +import java.io.UnsupportedEncodingException;
    1.11 +import java.net.URI;
    1.12 +import java.net.URISyntaxException;
    1.13 +
    1.14 +import org.json.simple.JSONArray;
    1.15 +import org.json.simple.JSONObject;
    1.16 +import org.mozilla.gecko.sync.CryptoRecord;
    1.17 +import org.mozilla.gecko.sync.ThreadPool;
    1.18 +
    1.19 +/**
    1.20 + * Resource class that implements expected headers and processing for Sync.
    1.21 + * Accepts a simplified delegate.
    1.22 + *
    1.23 + * Includes:
    1.24 + * * Basic Auth headers (via Resource)
    1.25 + * * Error responses:
    1.26 + *   * 401
    1.27 + *   * 503
    1.28 + * * Headers:
    1.29 + *   * Retry-After
    1.30 + *   * X-Weave-Backoff
    1.31 + *   * X-Backoff
    1.32 + *   * X-Weave-Records?
    1.33 + *   * ...
    1.34 + * * Timeouts
    1.35 + * * Network errors
    1.36 + * * application/newlines
    1.37 + * * JSON parsing
    1.38 + * * Content-Type and Content-Length validation.
    1.39 + */
    1.40 +public class SyncStorageRecordRequest extends SyncStorageRequest {
    1.41 +
    1.42 +  public class SyncStorageRecordResourceDelegate extends SyncStorageResourceDelegate {
    1.43 +    SyncStorageRecordResourceDelegate(SyncStorageRequest request) {
    1.44 +      super(request);
    1.45 +    }
    1.46 +  }
    1.47 +
    1.48 +  public SyncStorageRecordRequest(URI uri) {
    1.49 +    super(uri);
    1.50 +  }
    1.51 +
    1.52 +  public SyncStorageRecordRequest(String url) throws URISyntaxException {
    1.53 +    this(new URI(url));
    1.54 +  }
    1.55 +
    1.56 +  @Override
    1.57 +  protected BaseResourceDelegate makeResourceDelegate(SyncStorageRequest request) {
    1.58 +    return new SyncStorageRecordResourceDelegate(request);
    1.59 +  }
    1.60 +
    1.61 +  @SuppressWarnings("unchecked")
    1.62 +  public void post(JSONObject body) {
    1.63 +    // Let's do this the trivial way for now.
    1.64 +    // Note that POSTs should be an array, so we wrap here.
    1.65 +    final JSONArray toPOST = new JSONArray();
    1.66 +    toPOST.add(body);
    1.67 +    try {
    1.68 +      this.resource.post(toPOST);
    1.69 +    } catch (UnsupportedEncodingException e) {
    1.70 +      this.delegate.handleRequestError(e);
    1.71 +    }
    1.72 +  }
    1.73 +
    1.74 +  public void post(JSONArray body) {
    1.75 +    // Let's do this the trivial way for now.
    1.76 +    try {
    1.77 +      this.resource.post(body);
    1.78 +    } catch (UnsupportedEncodingException e) {
    1.79 +      this.delegate.handleRequestError(e);
    1.80 +    }
    1.81 +  }
    1.82 +
    1.83 +  public void put(JSONObject body) {
    1.84 +    // Let's do this the trivial way for now.
    1.85 +    try {
    1.86 +      this.resource.put(body);
    1.87 +    } catch (UnsupportedEncodingException e) {
    1.88 +      this.delegate.handleRequestError(e);
    1.89 +    }
    1.90 +  }
    1.91 +
    1.92 +  public void post(CryptoRecord record) {
    1.93 +    this.post(record.toJSONObject());
    1.94 +  }
    1.95 +
    1.96 +  public void put(CryptoRecord record) {
    1.97 +    this.put(record.toJSONObject());
    1.98 +  }
    1.99 +
   1.100 +  public void deferGet() {
   1.101 +    final SyncStorageRecordRequest self = this;
   1.102 +    ThreadPool.run(new Runnable() {
   1.103 +      @Override
   1.104 +      public void run() {
   1.105 +        self.get();
   1.106 +      }});
   1.107 +  }
   1.108 +
   1.109 +  public void deferPut(final JSONObject body) {
   1.110 +    final SyncStorageRecordRequest self = this;
   1.111 +    ThreadPool.run(new Runnable() {
   1.112 +      @Override
   1.113 +      public void run() {
   1.114 +        self.put(body);
   1.115 +      }});
   1.116 +  }
   1.117 +}

mercurial