799ce2f155082d090e0359544ab92508830e8591
[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 private boolean mIsFolder;
33
34
35 /**
36 * Constructor
37 *
38 * @param oldName Old name of the file.
39 * @param oldRemotePath Old remote path of the file.
40 * @param newName New name to set as the name of file.
41 * @param newRemotePath New remote path to move the file, for folders it starts and ends by "/"
42 */
43 public RenameRemoteFileOperation(String oldName, String oldRemotePath, String newName, String newRemotePath, boolean isFolder) {
44 mOldName = oldName;
45 mOldRemotePath = oldRemotePath;
46 mNewName = newName;
47 mNewRemotePath = newRemotePath;
48 mIsFolder = isFolder;
49 }
50
51 /**
52 * Performs the rename operation.
53 *
54 * @param client Client object to communicate with the remote ownCloud server.
55 */
56 @Override
57 protected RemoteOperationResult run(WebdavClient client) {
58 RemoteOperationResult result = null;
59
60 LocalMoveMethod move = null;
61
62 boolean noInvalidChars = true;
63
64 if (mIsFolder)
65 noInvalidChars = FileUtils.validateName(mNewRemotePath, mIsFolder);
66 else
67 noInvalidChars = FileUtils.validateName(mNewName, mIsFolder);
68
69 if (noInvalidChars) {
70 try {
71
72 if (mNewName.equals(mOldName)) {
73 return new RemoteOperationResult(ResultCode.OK);
74 }
75
76 move = new LocalMoveMethod( client.getBaseUri() + WebdavUtils.encodePath(mOldRemotePath),
77 client.getBaseUri() + WebdavUtils.encodePath(mNewRemotePath));
78 int status = client.executeMethod(move, RENAME_READ_TIMEOUT, RENAME_CONNECTION_TIMEOUT);
79
80 move.getResponseBodyAsString(); // exhaust response, although not interesting
81 result = new RemoteOperationResult(move.succeeded(), status, move.getResponseHeaders());
82 Log.i(TAG, "Rename " + mOldRemotePath + " to " + mNewRemotePath + ": " + result.getLogMessage());
83
84 } catch (Exception e) {
85 result = new RemoteOperationResult(e);
86 Log.e(TAG, "Rename " + mOldRemotePath + " to " + ((mNewRemotePath==null) ? mNewName : mNewRemotePath) + ": " + result.getLogMessage(), e);
87
88 } finally {
89 if (move != null)
90 move.releaseConnection();
91 }
92 } else {
93 result = new RemoteOperationResult(ResultCode.INVALID_CHARACTER_IN_NAME);
94 }
95
96 return result;
97 }
98
99 /**
100 * Move operation
101 *
102 */
103 private class LocalMoveMethod extends DavMethodBase {
104
105 public LocalMoveMethod(String uri, String dest) {
106 super(uri);
107 addRequestHeader(new org.apache.commons.httpclient.Header("Destination", dest));
108 }
109
110 @Override
111 public String getName() {
112 return "MOVE";
113 }
114
115 @Override
116 protected boolean isSuccess(int status) {
117 return status == 201 || status == 204;
118 }
119
120 }
121
122 }