1 package com
.owncloud
.android
.oc_framework
.operations
.remote
;
3 import org
.apache
.jackrabbit
.webdav
.client
.methods
.DavMethodBase
;
5 import android
.util
.Log
;
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
;
16 * Remote operation performing the rename of a remote file or folder in the ownCloud server.
18 * @author David A. Velasco
21 public class RenameRemoteFileOperation
extends RemoteOperation
{
23 private static final String TAG
= RenameRemoteFileOperation
.class.getSimpleName();
25 private static final int RENAME_READ_TIMEOUT
= 10000;
26 private static final int RENAME_CONNECTION_TIMEOUT
= 5000;
28 private String mOldName
;
29 private String mOldRemotePath
;
30 private String mNewName
;
31 private String mNewRemotePath
;
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 "/"
42 public RenameRemoteFileOperation(String oldName
, String oldRemotePath
, String newName
, String newRemotePath
) {
44 mOldRemotePath
= oldRemotePath
;
46 mNewRemotePath
= newRemotePath
;
50 * Performs the rename operation.
52 * @param client Client object to communicate with the remote ownCloud server.
55 protected RemoteOperationResult
run(WebdavClient client
) {
56 RemoteOperationResult result
= null
;
58 LocalMoveMethod move
= null
;
60 boolean noInvalidChars
= FileUtils
.isValidPath(mNewRemotePath
);
65 if (mNewName
.equals(mOldName
)) {
66 return new RemoteOperationResult(ResultCode
.OK
);
69 // check if a file with the new name already exists
70 if (client
.existsFile(mNewRemotePath
)) {
71 return new RemoteOperationResult(ResultCode
.INVALID_OVERWRITE
);
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
);
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());
82 } catch (Exception e
) {
83 result
= new RemoteOperationResult(e
);
84 Log
.e(TAG
, "Rename " + mOldRemotePath
+ " to " + ((mNewRemotePath
==null
) ? mNewName
: mNewRemotePath
) + ": " + result
.getLogMessage(), e
);
88 move
.releaseConnection();
91 result
= new RemoteOperationResult(ResultCode
.INVALID_CHARACTER_IN_NAME
);
101 private class LocalMoveMethod
extends DavMethodBase
{
103 public LocalMoveMethod(String uri
, String dest
) {
105 addRequestHeader(new org
.apache
.commons
.httpclient
.Header("Destination", dest
));
109 public String
getName() {
114 protected boolean isSuccess(int status
) {
115 return status
== 201 || status
== 204;