7bc5f0cc18ba9cec5a20552a4fdf0bcddadb2050
[pub/Android/ownCloud.git] / oc_framework / src / com / owncloud / android / lib / operations / remote / RenameRemoteFileOperation.java
1 /* ownCloud Android Library is available under MIT license
2 * Copyright (C) 2014 ownCloud (http://www.owncloud.org/)
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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
21 * THE SOFTWARE.
22 *
23 */
24
25 package com.owncloud.android.lib.operations.remote;
26
27 import java.io.File;
28
29 import org.apache.jackrabbit.webdav.client.methods.DavMethodBase;
30
31 import android.util.Log;
32
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;
39
40
41 /**
42 * Remote operation performing the rename of a remote file or folder in the ownCloud server.
43 *
44 * @author David A. Velasco
45 * @author masensio
46 */
47 public class RenameRemoteFileOperation extends RemoteOperation {
48
49 private static final String TAG = RenameRemoteFileOperation.class.getSimpleName();
50
51 private static final int RENAME_READ_TIMEOUT = 10000;
52 private static final int RENAME_CONNECTION_TIMEOUT = 5000;
53
54 private String mOldName;
55 private String mOldRemotePath;
56 private String mNewName;
57 private String mNewRemotePath;
58
59
60 /**
61 * Constructor
62 *
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
67 */
68 public RenameRemoteFileOperation(String oldName, String oldRemotePath, String newName, boolean isFolder) {
69 mOldName = oldName;
70 mOldRemotePath = oldRemotePath;
71 mNewName = newName;
72
73 String parent = (new File(mOldRemotePath)).getParent();
74 parent = (parent.endsWith(FileUtils.PATH_SEPARATOR)) ? parent : parent + FileUtils.PATH_SEPARATOR;
75 mNewRemotePath = parent + mNewName;
76 if (isFolder) {
77 mNewRemotePath += FileUtils.PATH_SEPARATOR;
78 }
79 }
80
81 /**
82 * Performs the rename operation.
83 *
84 * @param client Client object to communicate with the remote ownCloud server.
85 */
86 @Override
87 protected RemoteOperationResult run(OwnCloudClient client) {
88 RemoteOperationResult result = null;
89
90 LocalMoveMethod move = null;
91
92 boolean noInvalidChars = FileUtils.isValidPath(mNewRemotePath);
93
94 if (noInvalidChars) {
95 try {
96
97 if (mNewName.equals(mOldName)) {
98 return new RemoteOperationResult(ResultCode.OK);
99 }
100
101
102 // check if a file with the new name already exists
103 if (client.existsFile(mNewRemotePath)) {
104 return new RemoteOperationResult(ResultCode.INVALID_OVERWRITE);
105 }
106
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);
110
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());
114
115 } catch (Exception e) {
116 result = new RemoteOperationResult(e);
117 Log.e(TAG, "Rename " + mOldRemotePath + " to " + ((mNewRemotePath==null) ? mNewName : mNewRemotePath) + ": " + result.getLogMessage(), e);
118
119 } finally {
120 if (move != null)
121 move.releaseConnection();
122 }
123 } else {
124 result = new RemoteOperationResult(ResultCode.INVALID_CHARACTER_IN_NAME);
125 }
126
127 return result;
128 }
129
130 /**
131 * Move operation
132 *
133 */
134 private class LocalMoveMethod extends DavMethodBase {
135
136 public LocalMoveMethod(String uri, String dest) {
137 super(uri);
138 addRequestHeader(new org.apache.commons.httpclient.Header("Destination", dest));
139 }
140
141 @Override
142 public String getName() {
143 return "MOVE";
144 }
145
146 @Override
147 protected boolean isSuccess(int status) {
148 return status == 201 || status == 204;
149 }
150
151 }
152
153 }