CommentFileOperation.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Tobias Kaminsky
  5. * Copyright (C) 2018 Tobias Kaminsky
  6. * Copyright (C) 2018 Nextcloud GmbH.
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. package com.owncloud.android.operations;
  22. import android.util.Log;
  23. import com.google.gson.GsonBuilder;
  24. import com.owncloud.android.lib.common.OwnCloudClient;
  25. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  26. import com.owncloud.android.operations.common.SyncOperation;
  27. import org.apache.commons.httpclient.HttpStatus;
  28. import org.apache.commons.httpclient.methods.PostMethod;
  29. import org.apache.commons.httpclient.methods.StringRequestEntity;
  30. import java.io.IOException;
  31. import java.util.HashMap;
  32. import java.util.Map;
  33. /**
  34. * Restore a {@link com.owncloud.android.lib.resources.files.FileVersion}.
  35. */
  36. public class CommentFileOperation extends SyncOperation {
  37. private static final String TAG = CommentFileOperation.class.getSimpleName();
  38. private static final int POST_READ_TIMEOUT = 30000;
  39. private static final int POST_CONNECTION_TIMEOUT = 5000;
  40. private static final String ACTOR_ID = "actorId";
  41. private static final String ACTOR_TYPE = "actorType";
  42. private static final String ACTOR_TYPE_VALUE = "users";
  43. private static final String VERB = "verb";
  44. private static final String VERB_VALUE = "comment";
  45. private static final String MESSAGE = "message";
  46. private String message;
  47. private String fileId;
  48. private String userId;
  49. /**
  50. * Constructor
  51. *
  52. * @param message Comment to store
  53. * @param userId userId to access correct dav endpoint
  54. */
  55. public CommentFileOperation(String message, String fileId, String userId) {
  56. this.message = message;
  57. this.fileId = fileId;
  58. this.userId = userId;
  59. }
  60. /**
  61. * Performs the operation.
  62. *
  63. * @param client Client object to communicate with the remote ownCloud server.
  64. */
  65. @Override
  66. protected RemoteOperationResult run(OwnCloudClient client) {
  67. RemoteOperationResult result;
  68. try {
  69. String url = client.getNewWebdavUri() + "/comments/files/" + fileId;
  70. PostMethod postMethod = new PostMethod(url);
  71. postMethod.addRequestHeader("Content-type", "application/json");
  72. Map<String, String> values = new HashMap<>();
  73. values.put(ACTOR_ID, userId);
  74. values.put(ACTOR_TYPE, ACTOR_TYPE_VALUE);
  75. values.put(VERB, VERB_VALUE);
  76. values.put(MESSAGE, message);
  77. String json = new GsonBuilder().create().toJson(values, Map.class);
  78. postMethod.setRequestEntity(new StringRequestEntity(json));
  79. int status = client.executeMethod(postMethod, POST_READ_TIMEOUT, POST_CONNECTION_TIMEOUT);
  80. result = new RemoteOperationResult(isSuccess(status), postMethod);
  81. client.exhaustResponse(postMethod.getResponseBodyAsStream());
  82. } catch (IOException e) {
  83. result = new RemoteOperationResult(e);
  84. Log.e(TAG, "Post comment to file with id " + fileId + " failed: " + result.getLogMessage(), e);
  85. }
  86. return result;
  87. }
  88. private boolean isSuccess(int status) {
  89. return status == HttpStatus.SC_CREATED;
  90. }
  91. }