RichDocumentsCreateAssetOperation.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Nextcloud - Android Client
  3. *
  4. * SPDX-FileCopyrightText: 2018 Tobias Kaminsky
  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 org.apache.commons.httpclient.HttpStatus;
  14. import org.apache.commons.httpclient.methods.Utf8PostMethod;
  15. import org.json.JSONObject;
  16. /**
  17. * Create asset for RichDocuments app from file, which is already stored on Nextcloud server
  18. */
  19. public class RichDocumentsCreateAssetOperation extends RemoteOperation {
  20. private static final String TAG = RichDocumentsCreateAssetOperation.class.getSimpleName();
  21. private static final int SYNC_READ_TIMEOUT = 40000;
  22. private static final int SYNC_CONNECTION_TIMEOUT = 5000;
  23. private static final String ASSET_URL = "/index.php/apps/richdocuments/assets";
  24. private static final String NODE_URL = "url";
  25. private static final String PARAMETER_PATH = "path";
  26. private static final String PARAMETER_FORMAT = "format";
  27. private static final String PARAMETER_FORMAT_VALUE = "json";
  28. private String path;
  29. public RichDocumentsCreateAssetOperation(String path) {
  30. this.path = path;
  31. }
  32. protected RemoteOperationResult run(OwnCloudClient client) {
  33. RemoteOperationResult result;
  34. Utf8PostMethod postMethod = null;
  35. try {
  36. postMethod = new Utf8PostMethod(client.getBaseUri() + ASSET_URL);
  37. postMethod.setParameter(PARAMETER_PATH, path);
  38. postMethod.setParameter(PARAMETER_FORMAT, PARAMETER_FORMAT_VALUE);
  39. // remote request
  40. postMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
  41. int status = client.executeMethod(postMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT);
  42. if (status == HttpStatus.SC_OK) {
  43. String response = postMethod.getResponseBodyAsString();
  44. // Parse the response
  45. JSONObject respJSON = new JSONObject(response);
  46. String url = respJSON.getString(NODE_URL);
  47. result = new RemoteOperationResult(true, postMethod);
  48. result.setSingleData(url);
  49. } else {
  50. result = new RemoteOperationResult(false, postMethod);
  51. client.exhaustResponse(postMethod.getResponseBodyAsStream());
  52. }
  53. } catch (Exception e) {
  54. result = new RemoteOperationResult(e);
  55. Log_OC.e(TAG, "Create asset for richdocuments with path " + path + " failed: " + result.getLogMessage(),
  56. result.getException());
  57. } finally {
  58. if (postMethod != null) {
  59. postMethod.releaseConnection();
  60. }
  61. }
  62. return result;
  63. }
  64. }