1 package com
.owncloud
.android
.oc_framework
.operations
.remote
;
5 import org
.apache
.jackrabbit
.webdav
.client
.methods
.DavMethodBase
;
7 import android
.util
.Log
;
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
;
18 * Remote operation performing the rename of a remote file or folder in the ownCloud server.
20 * @author David A. Velasco
23 public class RenameRemoteFileOperation
extends RemoteOperation
{
25 private static final String TAG
= RenameRemoteFileOperation
.class.getSimpleName();
27 private static final int RENAME_READ_TIMEOUT
= 10000;
28 private static final int RENAME_CONNECTION_TIMEOUT
= 5000;
30 private String mOldName
;
31 private String mOldRemotePath
;
32 private String mNewName
;
33 private String mNewRemotePath
;
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
44 public RenameRemoteFileOperation(String oldName
, String oldRemotePath
, String newName
, boolean isFolder
) {
46 mOldRemotePath
= oldRemotePath
;
49 String parent
= (new File(mOldRemotePath
)).getParent();
50 parent
= (parent
.endsWith(FileUtils
.PATH_SEPARATOR
)) ? parent
: parent
+ FileUtils
.PATH_SEPARATOR
;
51 mNewRemotePath
= parent
+ mNewName
;
53 mNewRemotePath
+= FileUtils
.PATH_SEPARATOR
;
58 * Performs the rename operation.
60 * @param client Client object to communicate with the remote ownCloud server.
63 protected RemoteOperationResult
run(WebdavClient client
) {
64 RemoteOperationResult result
= null
;
66 LocalMoveMethod move
= null
;
68 boolean noInvalidChars
= FileUtils
.isValidPath(mNewRemotePath
);
73 if (mNewName
.equals(mOldName
)) {
74 return new RemoteOperationResult(ResultCode
.OK
);
78 // check if a file with the new name already exists
79 if (client
.existsFile(mNewRemotePath
)) {
80 return new RemoteOperationResult(ResultCode
.INVALID_OVERWRITE
);
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
);
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());
91 } catch (Exception e
) {
92 result
= new RemoteOperationResult(e
);
93 Log
.e(TAG
, "Rename " + mOldRemotePath
+ " to " + ((mNewRemotePath
==null
) ? mNewName
: mNewRemotePath
) + ": " + result
.getLogMessage(), e
);
97 move
.releaseConnection();
100 result
= new RemoteOperationResult(ResultCode
.INVALID_CHARACTER_IN_NAME
);
110 private class LocalMoveMethod
extends DavMethodBase
{
112 public LocalMoveMethod(String uri
, String dest
) {
114 addRequestHeader(new org
.apache
.commons
.httpclient
.Header("Destination", dest
));
118 public String
getName() {
123 protected boolean isSuccess(int status
) {
124 return status
== 201 || status
== 204;