1 /* ownCloud Android Library is available under MIT license
2 * Copyright (C) 2014 ownCloud (http://www.owncloud.org/)
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 package com
.owncloud
.android
.lib
.operations
.remote
;
29 import org
.apache
.jackrabbit
.webdav
.client
.methods
.DavMethodBase
;
31 import android
.util
.Log
;
33 import com
.owncloud
.android
.lib
.network
.OwnCloudClient
;
34 import com
.owncloud
.android
.lib
.network
.webdav
.WebdavUtils
;
35 import com
.owncloud
.android
.lib
.operations
.common
.RemoteOperation
;
36 import com
.owncloud
.android
.lib
.operations
.common
.RemoteOperationResult
;
37 import com
.owncloud
.android
.lib
.operations
.common
.RemoteOperationResult
.ResultCode
;
38 import com
.owncloud
.android
.lib
.utils
.FileUtils
;
42 * Remote operation performing the rename of a remote file or folder in the ownCloud server.
44 * @author David A. Velasco
47 public class RenameRemoteFileOperation
extends RemoteOperation
{
49 private static final String TAG
= RenameRemoteFileOperation
.class.getSimpleName();
51 private static final int RENAME_READ_TIMEOUT
= 10000;
52 private static final int RENAME_CONNECTION_TIMEOUT
= 5000;
54 private String mOldName
;
55 private String mOldRemotePath
;
56 private String mNewName
;
57 private String mNewRemotePath
;
63 * @param oldName Old name of the file.
64 * @param oldRemotePath Old remote path of the file.
65 * @param newName New name to set as the name of file.
66 * @param isFolder 'true' for folder and 'false' for files
68 public RenameRemoteFileOperation(String oldName
, String oldRemotePath
, String newName
, boolean isFolder
) {
70 mOldRemotePath
= oldRemotePath
;
73 String parent
= (new File(mOldRemotePath
)).getParent();
74 parent
= (parent
.endsWith(FileUtils
.PATH_SEPARATOR
)) ? parent
: parent
+ FileUtils
.PATH_SEPARATOR
;
75 mNewRemotePath
= parent
+ mNewName
;
77 mNewRemotePath
+= FileUtils
.PATH_SEPARATOR
;
82 * Performs the rename operation.
84 * @param client Client object to communicate with the remote ownCloud server.
87 protected RemoteOperationResult
run(OwnCloudClient client
) {
88 RemoteOperationResult result
= null
;
90 LocalMoveMethod move
= null
;
92 boolean noInvalidChars
= FileUtils
.isValidPath(mNewRemotePath
);
97 if (mNewName
.equals(mOldName
)) {
98 return new RemoteOperationResult(ResultCode
.OK
);
102 // check if a file with the new name already exists
103 if (client
.existsFile(mNewRemotePath
)) {
104 return new RemoteOperationResult(ResultCode
.INVALID_OVERWRITE
);
107 move
= new LocalMoveMethod( client
.getBaseUri() + WebdavUtils
.encodePath(mOldRemotePath
),
108 client
.getBaseUri() + WebdavUtils
.encodePath(mNewRemotePath
));
109 int status
= client
.executeMethod(move
, RENAME_READ_TIMEOUT
, RENAME_CONNECTION_TIMEOUT
);
111 move
.getResponseBodyAsString(); // exhaust response, although not interesting
112 result
= new RemoteOperationResult(move
.succeeded(), status
, move
.getResponseHeaders());
113 Log
.i(TAG
, "Rename " + mOldRemotePath
+ " to " + mNewRemotePath
+ ": " + result
.getLogMessage());
115 } catch (Exception e
) {
116 result
= new RemoteOperationResult(e
);
117 Log
.e(TAG
, "Rename " + mOldRemotePath
+ " to " + ((mNewRemotePath
==null
) ? mNewName
: mNewRemotePath
) + ": " + result
.getLogMessage(), e
);
121 move
.releaseConnection();
124 result
= new RemoteOperationResult(ResultCode
.INVALID_CHARACTER_IN_NAME
);
134 private class LocalMoveMethod
extends DavMethodBase
{
136 public LocalMoveMethod(String uri
, String dest
) {
138 addRequestHeader(new org
.apache
.commons
.httpclient
.Header("Destination", dest
));
142 public String
getName() {
147 protected boolean isSuccess(int status
) {
148 return status
== 201 || status
== 204;