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