8e54f2ccea42e852d01e433c03f820ba5664fb4a
[pub/Android/ownCloud.git] / oc_framework / src / com / owncloud / android / oc_framework / operations / remote / RemoveRemoteFileOperation.java
1 /* ownCloud webDAV Library for Android is available under MIT license
2 * Copyright (C) 2014 ownCloud (http://www.owncloud.org/)
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 *
23 */
24
25 package com.owncloud.android.oc_framework.operations.remote;
26
27 import org.apache.commons.httpclient.HttpStatus;
28 import org.apache.jackrabbit.webdav.client.methods.DeleteMethod;
29
30 import android.util.Log;
31
32 import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
33 import com.owncloud.android.oc_framework.network.webdav.WebdavUtils;
34 import com.owncloud.android.oc_framework.operations.RemoteOperation;
35 import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
36
37 /**
38 * Remote operation performing the removal of a remote file or folder in the ownCloud server.
39 *
40 * @author David A. Velasco
41 * @author masensio
42 */
43 public class RemoveRemoteFileOperation extends RemoteOperation {
44 private static final String TAG = RemoveRemoteFileOperation.class.getSimpleName();
45
46 private static final int REMOVE_READ_TIMEOUT = 10000;
47 private static final int REMOVE_CONNECTION_TIMEOUT = 5000;
48
49 private String mRemotePath;
50
51 /**
52 * Constructor
53 *
54 * @param remotePath RemotePath of the remote file or folder to remove from the server
55 */
56 public RemoveRemoteFileOperation(String remotePath) {
57 mRemotePath = remotePath;
58 }
59
60 /**
61 * Performs the rename operation.
62 *
63 * @param client Client object to communicate with the remote ownCloud server.
64 */
65 @Override
66 protected RemoteOperationResult run(WebdavClient client) {
67 RemoteOperationResult result = null;
68 DeleteMethod delete = null;
69
70 try {
71 delete = new DeleteMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath));
72 int status = client.executeMethod(delete, REMOVE_READ_TIMEOUT, REMOVE_CONNECTION_TIMEOUT);
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.i(TAG, "Remove " + mRemotePath + ": " + result.getLogMessage());
77
78 } catch (Exception e) {
79 result = new RemoteOperationResult(e);
80 Log.e(TAG, "Remove " + mRemotePath + ": " + result.getLogMessage(), e);
81
82 } finally {
83 if (delete != null)
84 delete.releaseConnection();
85 }
86
87 return result;
88 }
89
90 }