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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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/. */
     5 package org.mozilla.gecko.sync.net;
     7 import java.io.UnsupportedEncodingException;
     8 import java.net.URI;
     9 import java.net.URISyntaxException;
    11 import org.json.simple.JSONArray;
    12 import org.json.simple.JSONObject;
    13 import org.mozilla.gecko.sync.CryptoRecord;
    14 import org.mozilla.gecko.sync.ThreadPool;
    16 /**
    17  * Resource class that implements expected headers and processing for Sync.
    18  * Accepts a simplified delegate.
    19  *
    20  * Includes:
    21  * * Basic Auth headers (via Resource)
    22  * * Error responses:
    23  *   * 401
    24  *   * 503
    25  * * Headers:
    26  *   * Retry-After
    27  *   * X-Weave-Backoff
    28  *   * X-Backoff
    29  *   * X-Weave-Records?
    30  *   * ...
    31  * * Timeouts
    32  * * Network errors
    33  * * application/newlines
    34  * * JSON parsing
    35  * * Content-Type and Content-Length validation.
    36  */
    37 public class SyncStorageRecordRequest extends SyncStorageRequest {
    39   public class SyncStorageRecordResourceDelegate extends SyncStorageResourceDelegate {
    40     SyncStorageRecordResourceDelegate(SyncStorageRequest request) {
    41       super(request);
    42     }
    43   }
    45   public SyncStorageRecordRequest(URI uri) {
    46     super(uri);
    47   }
    49   public SyncStorageRecordRequest(String url) throws URISyntaxException {
    50     this(new URI(url));
    51   }
    53   @Override
    54   protected BaseResourceDelegate makeResourceDelegate(SyncStorageRequest request) {
    55     return new SyncStorageRecordResourceDelegate(request);
    56   }
    58   @SuppressWarnings("unchecked")
    59   public void post(JSONObject body) {
    60     // Let's do this the trivial way for now.
    61     // Note that POSTs should be an array, so we wrap here.
    62     final JSONArray toPOST = new JSONArray();
    63     toPOST.add(body);
    64     try {
    65       this.resource.post(toPOST);
    66     } catch (UnsupportedEncodingException e) {
    67       this.delegate.handleRequestError(e);
    68     }
    69   }
    71   public void post(JSONArray body) {
    72     // Let's do this the trivial way for now.
    73     try {
    74       this.resource.post(body);
    75     } catch (UnsupportedEncodingException e) {
    76       this.delegate.handleRequestError(e);
    77     }
    78   }
    80   public void put(JSONObject body) {
    81     // Let's do this the trivial way for now.
    82     try {
    83       this.resource.put(body);
    84     } catch (UnsupportedEncodingException e) {
    85       this.delegate.handleRequestError(e);
    86     }
    87   }
    89   public void post(CryptoRecord record) {
    90     this.post(record.toJSONObject());
    91   }
    93   public void put(CryptoRecord record) {
    94     this.put(record.toJSONObject());
    95   }
    97   public void deferGet() {
    98     final SyncStorageRecordRequest self = this;
    99     ThreadPool.run(new Runnable() {
   100       @Override
   101       public void run() {
   102         self.get();
   103       }});
   104   }
   106   public void deferPut(final JSONObject body) {
   107     final SyncStorageRecordRequest self = this;
   108     ThreadPool.run(new Runnable() {
   109       @Override
   110       public void run() {
   111         self.put(body);
   112       }});
   113   }
   114 }

mercurial