RichDocumentsUrlOperation.java 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Nextcloud - Android Client
  3. *
  4. * SPDX-FileCopyrightText: 2018 Tobias Kaminsky <tobias@kaminsky.me>
  5. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH
  6. * SPDX-License-Identifier: AGPL-3.0-or-later
  7. */
  8. package com.owncloud.android.operations;
  9. import com.owncloud.android.lib.common.OwnCloudClient;
  10. import com.owncloud.android.lib.common.operations.RemoteOperation;
  11. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  12. import com.owncloud.android.lib.common.utils.Log_OC;
  13. import com.owncloud.android.utils.NextcloudServer;
  14. import org.apache.commons.httpclient.HttpStatus;
  15. import org.apache.commons.httpclient.methods.Utf8PostMethod;
  16. import org.json.JSONObject;
  17. /**
  18. * Edit a file with Richdocuments. Returns URL which can be shown in WebView.
  19. */
  20. public class RichDocumentsUrlOperation extends RemoteOperation {
  21. /**
  22. * TODO move to library
  23. */
  24. private static final String TAG = RichDocumentsUrlOperation.class.getSimpleName();
  25. private static final int SYNC_READ_TIMEOUT = 40000;
  26. private static final int SYNC_CONNECTION_TIMEOUT = 5000;
  27. private static final String DOCUMENT_URL = "/ocs/v2.php/apps/richdocuments/api/v1/document";
  28. private static final String FILE_ID = "fileId";
  29. // JSON node names
  30. private static final String NODE_OCS = "ocs";
  31. private static final String NODE_DATA = "data";
  32. private static final String NODE_URL = "url";
  33. private static final String JSON_FORMAT = "?format=json";
  34. private final long fileId;
  35. public RichDocumentsUrlOperation(long fileID) {
  36. this.fileId = fileID;
  37. }
  38. @NextcloudServer(max = 18)
  39. protected RemoteOperationResult run(OwnCloudClient client) {
  40. RemoteOperationResult result;
  41. Utf8PostMethod postMethod = null;
  42. try {
  43. postMethod = new Utf8PostMethod(client.getBaseUri() + DOCUMENT_URL + JSON_FORMAT);
  44. postMethod.setParameter(FILE_ID, String.valueOf(fileId));
  45. // remote request
  46. postMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
  47. int status = client.executeMethod(postMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT);
  48. if (status == HttpStatus.SC_OK) {
  49. String response = postMethod.getResponseBodyAsString();
  50. // Parse the response
  51. JSONObject respJSON = new JSONObject(response);
  52. String url = respJSON.getJSONObject(NODE_OCS).getJSONObject(NODE_DATA).getString(NODE_URL);
  53. result = new RemoteOperationResult(true, postMethod);
  54. result.setSingleData(url);
  55. } else {
  56. result = new RemoteOperationResult(false, postMethod);
  57. client.exhaustResponse(postMethod.getResponseBodyAsStream());
  58. }
  59. } catch (Exception e) {
  60. result = new RemoteOperationResult(e);
  61. Log_OC.e(TAG, "Get rich document url for file with id " + fileId + " failed: " + result.getLogMessage(),
  62. result.getException());
  63. } finally {
  64. if (postMethod != null) {
  65. postMethod.releaseConnection();
  66. }
  67. }
  68. return result;
  69. }
  70. }