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.

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

mercurial