b1714ae757bbd6d2fcb1fff6b212f2608fe1ecd3
[pub/Android/ownCloud.git] / oc_framework / src / com / owncloud / android / oc_framework / operations / remote / RenameRemoteFileOperation.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17
18 package com.owncloud.android.oc_framework.operations.remote;
19
20 import java.io.File;
21
22 import org.apache.jackrabbit.webdav.client.methods.DavMethodBase;
23
24 import android.util.Log;
25
26 import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
27 import com.owncloud.android.oc_framework.network.webdav.WebdavUtils;
28 import com.owncloud.android.oc_framework.operations.RemoteOperation;
29 import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
30 import com.owncloud.android.oc_framework.operations.RemoteOperationResult.ResultCode;
31 import com.owncloud.android.oc_framework.utils.FileUtils;
32
33
34 /**
35 * Remote operation performing the rename of a remote file or folder in the ownCloud server.
36 *
37 * @author David A. Velasco
38 * @author masensio
39 */
40 public class RenameRemoteFileOperation extends RemoteOperation {
41
42 private static final String TAG = RenameRemoteFileOperation.class.getSimpleName();
43
44 private static final int RENAME_READ_TIMEOUT = 10000;
45 private static final int RENAME_CONNECTION_TIMEOUT = 5000;
46
47 private String mOldName;
48 private String mOldRemotePath;
49 private String mNewName;
50 private String mNewRemotePath;
51
52
53 /**
54 * Constructor
55 *
56 * @param oldName Old name of the file.
57 * @param oldRemotePath Old remote path of the file.
58 * @param newName New name to set as the name of file.
59 * @param isFolder 'true' for folder and 'false' for files
60 */
61 public RenameRemoteFileOperation(String oldName, String oldRemotePath, String newName, boolean isFolder) {
62 mOldName = oldName;
63 mOldRemotePath = oldRemotePath;
64 mNewName = newName;
65
66 String parent = (new File(mOldRemotePath)).getParent();
67 parent = (parent.endsWith(FileUtils.PATH_SEPARATOR)) ? parent : parent + FileUtils.PATH_SEPARATOR;
68 mNewRemotePath = parent + mNewName;
69 if (isFolder) {
70 mNewRemotePath += FileUtils.PATH_SEPARATOR;
71 }
72 }
73
74 /**
75 * Performs the rename operation.
76 *
77 * @param client Client object to communicate with the remote ownCloud server.
78 */
79 @Override
80 protected RemoteOperationResult run(WebdavClient client) {
81 RemoteOperationResult result = null;
82
83 LocalMoveMethod move = null;
84
85 boolean noInvalidChars = FileUtils.isValidPath(mNewRemotePath);
86
87 if (noInvalidChars) {
88 try {
89
90 if (mNewName.equals(mOldName)) {
91 return new RemoteOperationResult(ResultCode.OK);
92 }
93
94
95 // check if a file with the new name already exists
96 if (client.existsFile(mNewRemotePath)) {
97 return new RemoteOperationResult(ResultCode.INVALID_OVERWRITE);
98 }
99
100 move = new LocalMoveMethod( client.getBaseUri() + WebdavUtils.encodePath(mOldRemotePath),
101 client.getBaseUri() + WebdavUtils.encodePath(mNewRemotePath));
102 int status = client.executeMethod(move, RENAME_READ_TIMEOUT, RENAME_CONNECTION_TIMEOUT);
103
104 move.getResponseBodyAsString(); // exhaust response, although not interesting
105 result = new RemoteOperationResult(move.succeeded(), status, move.getResponseHeaders());
106 Log.i(TAG, "Rename " + mOldRemotePath + " to " + mNewRemotePath + ": " + result.getLogMessage());
107
108 } catch (Exception e) {
109 result = new RemoteOperationResult(e);
110 Log.e(TAG, "Rename " + mOldRemotePath + " to " + ((mNewRemotePath==null) ? mNewName : mNewRemotePath) + ": " + result.getLogMessage(), e);
111
112 } finally {
113 if (move != null)
114 move.releaseConnection();
115 }
116 } else {
117 result = new RemoteOperationResult(ResultCode.INVALID_CHARACTER_IN_NAME);
118 }
119
120 return result;
121 }
122
123 /**
124 * Move operation
125 *
126 */
127 private class LocalMoveMethod extends DavMethodBase {
128
129 public LocalMoveMethod(String uri, String dest) {
130 super(uri);
131 addRequestHeader(new org.apache.commons.httpclient.Header("Destination", dest));
132 }
133
134 @Override
135 public String getName() {
136 return "MOVE";
137 }
138
139 @Override
140 protected boolean isSuccess(int status) {
141 return status == 201 || status == 204;
142 }
143
144 }
145
146 }