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