/src/main/java/org/mozilla/gecko/sync/net/SyncStorageRecordRequest.java

https://github.com/mozilla-services/android-sync · Java · 114 lines · 71 code · 15 blank · 28 comment · 0 complexity · 3efdf26afdc8760b759bce09027018e0 MD5 · raw file

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