RemoveFileOperation.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* ownCloud Android client application
  2. * Copyright (C) 2012-2013 ownCloud Inc.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. package com.owncloud.android.operations;
  18. import org.apache.commons.httpclient.HttpStatus;
  19. import org.apache.jackrabbit.webdav.client.methods.DeleteMethod;
  20. import com.owncloud.android.datamodel.FileDataStorageManager;
  21. import com.owncloud.android.datamodel.OCFile;
  22. import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
  23. import com.owncloud.android.oc_framework.operations.RemoteOperation;
  24. import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
  25. import com.owncloud.android.oc_framework.network.webdav.WebdavUtils;
  26. import com.owncloud.android.utils.Log_OC;
  27. /**
  28. * Remote operation performing the removal of a remote file or folder in the ownCloud server.
  29. *
  30. * @author David A. Velasco
  31. */
  32. public class RemoveFileOperation extends RemoteOperation {
  33. private static final String TAG = RemoveFileOperation.class.getSimpleName();
  34. private static final int REMOVE_READ_TIMEOUT = 10000;
  35. private static final int REMOVE_CONNECTION_TIMEOUT = 5000;
  36. OCFile mFileToRemove;
  37. boolean mDeleteLocalCopy;
  38. FileDataStorageManager mDataStorageManager;
  39. /**
  40. * Constructor
  41. *
  42. * @param fileToRemove OCFile instance describing the remote file or folder to remove from the server
  43. * @param deleteLocalCopy When 'true', and a local copy of the file exists, it is also removed.
  44. * @param storageManager Reference to the local database corresponding to the account where the file is contained.
  45. */
  46. public RemoveFileOperation(OCFile fileToRemove, boolean deleteLocalCopy, FileDataStorageManager storageManager) {
  47. mFileToRemove = fileToRemove;
  48. mDeleteLocalCopy = deleteLocalCopy;
  49. mDataStorageManager = storageManager;
  50. }
  51. /**
  52. * Getter for the file to remove (or removed, if the operation was successfully performed).
  53. *
  54. * @return File to remove or already removed.
  55. */
  56. public OCFile getFile() {
  57. return mFileToRemove;
  58. }
  59. /**
  60. * Performs the remove operation
  61. *
  62. * @param client Client object to communicate with the remote ownCloud server.
  63. */
  64. @Override
  65. protected RemoteOperationResult run(WebdavClient client) {
  66. RemoteOperationResult result = null;
  67. DeleteMethod delete = null;
  68. try {
  69. delete = new DeleteMethod(client.getBaseUri() + WebdavUtils.encodePath(mFileToRemove.getRemotePath()));
  70. int status = client.executeMethod(delete, REMOVE_READ_TIMEOUT, REMOVE_CONNECTION_TIMEOUT);
  71. if (delete.succeeded() || status == HttpStatus.SC_NOT_FOUND) {
  72. mDataStorageManager.removeFile(mFileToRemove, true, mDeleteLocalCopy);
  73. }
  74. delete.getResponseBodyAsString(); // exhaust the response, although not interesting
  75. result = new RemoteOperationResult((delete.succeeded() || status == HttpStatus.SC_NOT_FOUND), status, delete.getResponseHeaders());
  76. Log_OC.i(TAG, "Remove " + mFileToRemove.getRemotePath() + ": " + result.getLogMessage());
  77. } catch (Exception e) {
  78. result = new RemoteOperationResult(e);
  79. Log_OC.e(TAG, "Remove " + mFileToRemove.getRemotePath() + ": " + result.getLogMessage(), e);
  80. } finally {
  81. if (delete != null)
  82. delete.releaseConnection();
  83. }
  84. return result;
  85. }
  86. }