2ead67b316ec019c5c3791a05ba7856be5339f88
[pub/Android/ownCloud.git] / oc_framework / src / com / owncloud / android / oc_framework / operations / remote / RenameRemoteFileOperation.java
1 package com.owncloud.android.oc_framework.operations.remote;
2
3 import org.apache.jackrabbit.webdav.client.methods.DavMethodBase;
4
5 import android.util.Log;
6
7 import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
8 import com.owncloud.android.oc_framework.network.webdav.WebdavUtils;
9 import com.owncloud.android.oc_framework.operations.RemoteOperation;
10 import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
11 import com.owncloud.android.oc_framework.operations.RemoteOperationResult.ResultCode;
12 import com.owncloud.android.oc_framework.utils.FileUtils;
13
14
15 /**
16 * Remote operation performing the rename of a remote file or folder in the ownCloud server.
17 *
18 * @author David A. Velasco
19 * @author masensio
20 */
21 public class RenameRemoteFileOperation extends RemoteOperation {
22
23 private static final String TAG = RenameRemoteFileOperation.class.getSimpleName();
24
25 private static final int RENAME_READ_TIMEOUT = 10000;
26 private static final int RENAME_CONNECTION_TIMEOUT = 5000;
27
28 private String mOldName;
29 private String mOldRemotePath;
30 private String mNewName;
31 private String mNewRemotePath;
32
33
34 /**
35 * Constructor
36 *
37 * @param oldName Old name of the file.
38 * @param oldRemotePath Old remote path of the file. For folders it starts and ends by "/"
39 * @param newName New name to set as the name of file.
40 * @param newRemotePath New remote path to move the file, for folders it starts and ends by "/"
41 */
42 public RenameRemoteFileOperation(String oldName, String oldRemotePath, String newName, String newRemotePath) {
43 mOldName = oldName;
44 mOldRemotePath = oldRemotePath;
45 mNewName = newName;
46 mNewRemotePath = newRemotePath;
47 }
48
49 /**
50 * Performs the rename operation.
51 *
52 * @param client Client object to communicate with the remote ownCloud server.
53 */
54 @Override
55 protected RemoteOperationResult run(WebdavClient client) {
56 RemoteOperationResult result = null;
57
58 LocalMoveMethod move = null;
59
60 boolean noInvalidChars = FileUtils.isValidPath(mNewRemotePath);
61
62 if (noInvalidChars) {
63 try {
64
65 if (mNewName.equals(mOldName)) {
66 return new RemoteOperationResult(ResultCode.OK);
67 }
68
69 // check if a file with the new name already exists
70 if (client.existsFile(mNewRemotePath)) {
71 return new RemoteOperationResult(ResultCode.INVALID_OVERWRITE);
72 }
73
74 move = new LocalMoveMethod( client.getBaseUri() + WebdavUtils.encodePath(mOldRemotePath),
75 client.getBaseUri() + WebdavUtils.encodePath(mNewRemotePath));
76 int status = client.executeMethod(move, RENAME_READ_TIMEOUT, RENAME_CONNECTION_TIMEOUT);
77
78 move.getResponseBodyAsString(); // exhaust response, although not interesting
79 result = new RemoteOperationResult(move.succeeded(), status, move.getResponseHeaders());
80 Log.i(TAG, "Rename " + mOldRemotePath + " to " + mNewRemotePath + ": " + result.getLogMessage());
81
82 } catch (Exception e) {
83 result = new RemoteOperationResult(e);
84 Log.e(TAG, "Rename " + mOldRemotePath + " to " + ((mNewRemotePath==null) ? mNewName : mNewRemotePath) + ": " + result.getLogMessage(), e);
85
86 } finally {
87 if (move != null)
88 move.releaseConnection();
89 }
90 } else {
91 result = new RemoteOperationResult(ResultCode.INVALID_CHARACTER_IN_NAME);
92 }
93
94 return result;
95 }
96
97 /**
98 * Move operation
99 *
100 */
101 private class LocalMoveMethod extends DavMethodBase {
102
103 public LocalMoveMethod(String uri, String dest) {
104 super(uri);
105 addRequestHeader(new org.apache.commons.httpclient.Header("Destination", dest));
106 }
107
108 @Override
109 public String getName() {
110 return "MOVE";
111 }
112
113 @Override
114 protected boolean isSuccess(int status) {
115 return status == 201 || status == 204;
116 }
117
118 }
119
120 }