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
;
32 private boolean mIsFolder
;
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 "/"
43 public RenameRemoteFileOperation(String oldName
, String oldRemotePath
, String newName
, String newRemotePath
, boolean isFolder
) {
45 mOldRemotePath
= oldRemotePath
;
47 mNewRemotePath
= newRemotePath
;
52 * Performs the rename operation.
54 * @param client Client object to communicate with the remote ownCloud server.
57 protected RemoteOperationResult
run(WebdavClient client
) {
58 RemoteOperationResult result
= null
;
60 LocalMoveMethod move
= null
;
62 boolean noInvalidChars
= true
;
65 noInvalidChars
= FileUtils
.validateName(mNewRemotePath
, mIsFolder
);
67 noInvalidChars
= FileUtils
.validateName(mNewName
, mIsFolder
);
72 if (mNewName
.equals(mOldName
)) {
73 return new RemoteOperationResult(ResultCode
.OK
);
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
);
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());
84 } catch (Exception e
) {
85 result
= new RemoteOperationResult(e
);
86 Log
.e(TAG
, "Rename " + mOldRemotePath
+ " to " + ((mNewRemotePath
==null
) ? mNewName
: mNewRemotePath
) + ": " + result
.getLogMessage(), e
);
90 move
.releaseConnection();
93 result
= new RemoteOperationResult(ResultCode
.INVALID_CHARACTER_IN_NAME
);
103 private class LocalMoveMethod
extends DavMethodBase
{
105 public LocalMoveMethod(String uri
, String dest
) {
107 addRequestHeader(new org
.apache
.commons
.httpclient
.Header("Destination", dest
));
111 public String
getName() {
116 protected boolean isSuccess(int status
) {
117 return status
== 201 || status
== 204;