baf24234eca7072f9103e53ae49c815003ef2811
[pub/Android/ownCloud.git] / oc_framework / src / com / owncloud / android / oc_framework / operations / remote / RemoveRemoteFileOperation.java
1 package com.owncloud.android.oc_framework.operations.remote;
2
3 import org.apache.commons.httpclient.HttpStatus;
4 import org.apache.jackrabbit.webdav.client.methods.DeleteMethod;
5
6 import android.util.Log;
7
8 import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
9 import com.owncloud.android.oc_framework.network.webdav.WebdavUtils;
10 import com.owncloud.android.oc_framework.operations.RemoteOperation;
11 import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
12
13 /**
14 * Remote operation performing the removal of a remote file or folder in the ownCloud server.
15 *
16 * @author David A. Velasco
17 * @author masensio
18 */
19 public class RemoveRemoteFileOperation extends RemoteOperation {
20 private static final String TAG = RemoveRemoteFileOperation.class.getSimpleName();
21
22 private static final int REMOVE_READ_TIMEOUT = 10000;
23 private static final int REMOVE_CONNECTION_TIMEOUT = 5000;
24
25 private String mRemotePath;
26
27 /**
28 * Constructor
29 *
30 * @param remotePath RemotePath of the remote file or folder to remove from the server
31 */
32 public RemoveRemoteFileOperation(String remotePath) {
33 mRemotePath = remotePath;
34 }
35
36 /**
37 * Performs the rename operation.
38 *
39 * @param client Client object to communicate with the remote ownCloud server.
40 */
41 @Override
42 protected RemoteOperationResult run(WebdavClient client) {
43 RemoteOperationResult result = null;
44 DeleteMethod delete = null;
45
46 try {
47 delete = new DeleteMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath));
48 int status = client.executeMethod(delete, REMOVE_READ_TIMEOUT, REMOVE_CONNECTION_TIMEOUT);
49
50 delete.getResponseBodyAsString(); // exhaust the response, although not interesting
51 result = new RemoteOperationResult((delete.succeeded() || status == HttpStatus.SC_NOT_FOUND), status, delete.getResponseHeaders());
52 Log.i(TAG, "Remove " + mRemotePath + ": " + result.getLogMessage());
53
54 } catch (Exception e) {
55 result = new RemoteOperationResult(e);
56 Log.e(TAG, "Remove " + mRemotePath + ": " + result.getLogMessage(), e);
57
58 } finally {
59 if (delete != null)
60 delete.releaseConnection();
61 }
62
63 return result;
64 }
65
66 }