/**
- * Test to Create Folder with special characters
+ * Test to Create Folder with special characters: / \ < > : " | ? *
*/
public void testCreateFolderSpecialCharacters() {
boolean createFullPath = true;
--- /dev/null
+package com.owncloud.android.oc_framework_test_project.test;
+
+import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
+import com.owncloud.android.oc_framework.operations.RemoteOperationResult.ResultCode;
+import com.owncloud.android.oc_framework_test_project.TestActivity;
+
+import android.test.ActivityInstrumentationTestCase2;
+
+public class RenameFileTest extends ActivityInstrumentationTestCase2<TestActivity> {
+
+ /* Folder data to rename. This folder must exist on the account */
+ private final String mOldFolderName = "folderToRename";
+ private final String mOldFolderPath = "/folderToRename";
+ private final String mNewFolderName = "renamedFolder";
+ private final String mNewFolderPath = "/renamedFolder";
+
+ /* File data to rename. This file must exist on the account */
+ private final String mOldFileName = "fileToRename.png";
+ private final String mOldFilePath = "/fileToRename.png";
+ private final String mNewFileName = "renamedFile";
+ private final String mFileExtension = ".png";
+ private final String mNewFilePath ="/renamedFile.png";
+
+
+ private TestActivity mActivity;
+
+ public RenameFileTest() {
+ super(TestActivity.class);
+
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ setActivityInitialTouchMode(false);
+ mActivity = getActivity();
+ }
+
+ /**
+ * Test Rename Folder
+ */
+ public void testRenameFolder() {
+
+ RemoteOperationResult result = mActivity.renameFile(mOldFolderName, mOldFolderPath,
+ mNewFolderName, true);
+ assertTrue(result.isSuccess());
+ }
+
+ /**
+ * Test Rename Folder with forbidden characters : \ < > : " | ? *
+ */
+ public void testRenameFolderForbiddenChars() {
+
+ RemoteOperationResult result = mActivity.renameFile(mOldFolderName, mOldFolderPath,
+ mNewFolderName + "\\", true);
+ assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
+
+ result = mActivity.renameFile(mOldFolderName, mOldFolderPath,
+ mNewFolderName + "<", true);
+ assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
+
+ result = mActivity.renameFile(mOldFolderName, mOldFolderPath,
+ mNewFolderName + ">", true);
+ assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
+
+ result = mActivity.renameFile(mOldFolderName, mOldFolderPath,
+ mNewFolderName + ":", true);
+ assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
+
+ result = mActivity.renameFile(mOldFolderName, mOldFolderPath,
+ mNewFolderName + "\"", true);
+ assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
+
+ result = mActivity.renameFile(mOldFolderName, mOldFolderPath,
+ mNewFolderName + "|", true);
+ assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
+
+ result = mActivity.renameFile(mOldFolderName, mOldFolderPath,
+ mNewFolderName + "?", true);
+ assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
+
+ result = mActivity.renameFile(mOldFolderName, mOldFolderPath,
+ mNewFolderName + "*", true);
+ assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
+ }
+
+ /**
+ * Test Rename File
+ */
+ public void testRenameFile() {
+ RemoteOperationResult result = mActivity.renameFile(mOldFileName, mOldFilePath,
+ mNewFileName + mFileExtension, false);
+ assertTrue(result.isSuccess());
+ }
+
+
+ /**
+ * Test Rename Folder with forbidden characters: \ < > : " | ? *
+ */
+ public void testRenameFileForbiddenChars() {
+ RemoteOperationResult result = mActivity.renameFile(mOldFileName, mOldFilePath,
+ mNewFileName + "\\" + mFileExtension, false);
+ assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
+
+ result = mActivity.renameFile(mOldFileName, mOldFilePath,
+ mNewFileName + "<" + mFileExtension, false);
+ assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
+
+ result = mActivity.renameFile(mOldFileName, mOldFilePath,
+ mNewFileName + ">" + mFileExtension, false);
+ assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
+
+ result = mActivity.renameFile(mOldFileName, mOldFilePath,
+ mNewFileName + ":" + mFileExtension, false);
+ assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
+
+ result = mActivity.renameFile(mOldFileName, mOldFilePath,
+ mNewFileName + "\"" + mFileExtension, false);
+ assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
+
+ result = mActivity.renameFile(mOldFileName, mOldFilePath,
+ mNewFileName + "|" + mFileExtension, false);
+ assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
+
+ result = mActivity.renameFile(mOldFileName, mOldFilePath,
+ mNewFileName + "?" + mFileExtension, false);
+ assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
+
+ result = mActivity.renameFile(mOldFileName, mOldFilePath,
+ mNewFileName + "*" + mFileExtension, false);
+ assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
+
+ }
+
+
+ /**
+ * Restore initial conditions
+ */
+ public void testRestoreInitialConditions() {
+ RemoteOperationResult result = mActivity.renameFile(mNewFolderName, mNewFolderPath, mOldFolderName, true);
+ assertTrue(result.isSuccess());
+
+ result = mActivity.renameFile(mNewFileName + mFileExtension, mNewFilePath, mOldFileName, false);
+ assertTrue(result.isSuccess());
+ }
+
+}
import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
import com.owncloud.android.oc_framework.operations.remote.CreateRemoteFolderOperation;
+import com.owncloud.android.oc_framework.operations.remote.RenameRemoteFileOperation;
import android.os.AsyncTask;
import android.os.Bundle;
private static final String TAG = "TestActivity";
+ // This account must exists on the simulator / device
+ private static final String mAccountHost = "beta.owncloud.com";
+ private static final String mAccountUser = "testandroid";
+ private static final String mAccountName = mAccountUser + "@"+ mAccountHost;
+ private static final String mAccountPass = "testandroid";
+ private static final String mAccountType = "owncloud";
+
private Account mAccount = null;
private WebdavClient mClient;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
-
- // This account must exists on the simulator / device
- String accountHost = "beta.owncloud.com";
- String accountUser = "testandroid";
- String accountName = accountUser + "@"+ accountHost;
- String accountPass = "testandroid";
- String accountType = "owncloud";
AccountManager am = AccountManager.get(this);
- Account[] ocAccounts = am.getAccountsByType(accountType);
+ Account[] ocAccounts = am.getAccountsByType(mAccountType);
for (Account ac : ocAccounts) {
- if (ac.name.equals(accountName)) {
+ if (ac.name.equals(mAccountName)) {
mAccount = ac;
break;
}
* Access to the library method to Create a Folder
* @param remotePath
* @param createFullPath
+ *
* @return
*/
public RemoteOperationResult createFolder(String remotePath, boolean createFullPath) {
return result;
}
+ /**
+ * Access to the library method to Rename a File or Folder
+ * @param oldName Old name of the file.
+ * @param oldRemotePath Old remote path of the file. For folders it starts and ends by "/"
+ * @param newName New name to set as the name of file.
+ * @param isFolder 'true' for folder and 'false' for files
+ *
+ * @return
+ */
+
+ public RemoteOperationResult renameFile(String oldName, String oldRemotePath, String newName, boolean isFolder) {
+
+ RenameRemoteFileOperation renameOperation = new RenameRemoteFileOperation(oldName, oldRemotePath, newName, isFolder);
+ RemoteOperationResult result = renameOperation.execute(mClient);
+
+ return result;
+ }
+
private class AuthTask extends AsyncTask<Context, Void, WebdavClient> {
@Override
} else {
String username = account.name.substring(0, account.name.lastIndexOf('@'));
- //String password = am.getPassword(account);
- String password = am.blockingGetAuthToken(account, AccountTypeUtils.getAuthTokenTypePass(account.type), false);
+ String password = am.getPassword(account);
+ //String password = am.blockingGetAuthToken(account, AccountTypeUtils.getAuthTokenTypePass(account.type), false);
client.setBasicCredentials(username, password);
}
--- /dev/null
+package com.owncloud.android.oc_framework.operations.remote;
+
+import java.io.File;
+
+import org.apache.jackrabbit.webdav.client.methods.DavMethodBase;
+
+import android.util.Log;
+
+import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
+import com.owncloud.android.oc_framework.network.webdav.WebdavUtils;
+import com.owncloud.android.oc_framework.operations.RemoteOperation;
+import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
+import com.owncloud.android.oc_framework.operations.RemoteOperationResult.ResultCode;
+import com.owncloud.android.oc_framework.utils.FileUtils;
+
+
+/**
+ * Remote operation performing the rename of a remote file or folder in the ownCloud server.
+ *
+ * @author David A. Velasco
+ * @author masensio
+ */
+public class RenameRemoteFileOperation extends RemoteOperation {
+
+ private static final String TAG = RenameRemoteFileOperation.class.getSimpleName();
+
+ private static final int RENAME_READ_TIMEOUT = 10000;
+ private static final int RENAME_CONNECTION_TIMEOUT = 5000;
+
+ private String mOldName;
+ private String mOldRemotePath;
+ private String mNewName;
+ private String mNewRemotePath;
+
+
+ /**
+ * Constructor
+ *
+ * @param oldName Old name of the file.
+ * @param oldRemotePath Old remote path of the file.
+ * @param newName New name to set as the name of file.
+ * @param isFolder 'true' for folder and 'false' for files
+ */
+ public RenameRemoteFileOperation(String oldName, String oldRemotePath, String newName, boolean isFolder) {
+ mOldName = oldName;
+ mOldRemotePath = oldRemotePath;
+ mNewName = newName;
+
+ String parent = (new File(mOldRemotePath)).getParent();
+ parent = (parent.endsWith(FileUtils.PATH_SEPARATOR)) ? parent : parent + FileUtils.PATH_SEPARATOR;
+ mNewRemotePath = parent + mNewName;
+ if (isFolder) {
+ mNewRemotePath += FileUtils.PATH_SEPARATOR;
+ }
+ }
+
+ /**
+ * Performs the rename operation.
+ *
+ * @param client Client object to communicate with the remote ownCloud server.
+ */
+ @Override
+ protected RemoteOperationResult run(WebdavClient client) {
+ RemoteOperationResult result = null;
+
+ LocalMoveMethod move = null;
+
+ boolean noInvalidChars = FileUtils.isValidPath(mNewRemotePath);
+
+ if (noInvalidChars) {
+ try {
+
+ if (mNewName.equals(mOldName)) {
+ return new RemoteOperationResult(ResultCode.OK);
+ }
+
+
+ // check if a file with the new name already exists
+ if (client.existsFile(mNewRemotePath)) {
+ return new RemoteOperationResult(ResultCode.INVALID_OVERWRITE);
+ }
+
+ move = new LocalMoveMethod( client.getBaseUri() + WebdavUtils.encodePath(mOldRemotePath),
+ client.getBaseUri() + WebdavUtils.encodePath(mNewRemotePath));
+ int status = client.executeMethod(move, RENAME_READ_TIMEOUT, RENAME_CONNECTION_TIMEOUT);
+
+ move.getResponseBodyAsString(); // exhaust response, although not interesting
+ result = new RemoteOperationResult(move.succeeded(), status, move.getResponseHeaders());
+ Log.i(TAG, "Rename " + mOldRemotePath + " to " + mNewRemotePath + ": " + result.getLogMessage());
+
+ } catch (Exception e) {
+ result = new RemoteOperationResult(e);
+ Log.e(TAG, "Rename " + mOldRemotePath + " to " + ((mNewRemotePath==null) ? mNewName : mNewRemotePath) + ": " + result.getLogMessage(), e);
+
+ } finally {
+ if (move != null)
+ move.releaseConnection();
+ }
+ } else {
+ result = new RemoteOperationResult(ResultCode.INVALID_CHARACTER_IN_NAME);
+ }
+
+ return result;
+ }
+
+ /**
+ * Move operation
+ *
+ */
+ private class LocalMoveMethod extends DavMethodBase {
+
+ public LocalMoveMethod(String uri, String dest) {
+ super(uri);
+ addRequestHeader(new org.apache.commons.httpclient.Header("Destination", dest));
+ }
+
+ @Override
+ public String getName() {
+ return "MOVE";
+ }
+
+ @Override
+ protected boolean isSuccess(int status) {
+ return status == 201 || status == 204;
+ }
+
+ }
+
+}
import android.app.AlertDialog;\r
import android.app.Dialog;\r
import android.app.ProgressDialog;\r
-import android.content.ContentResolver;\r
import android.content.DialogInterface;\r
import android.content.Intent;\r
import android.content.SharedPreferences;\r
import org.apache.jackrabbit.webdav.MultiStatus;
import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
-import com.owncloud.android.MainApp;
import com.owncloud.android.R;
-import com.owncloud.android.authentication.AccountAuthenticator;
import com.owncloud.android.authentication.AuthenticatorActivity;
import com.owncloud.android.datamodel.FileDataStorageManager;
import com.owncloud.android.datamodel.OCFile;
import java.io.File;
import java.io.IOException;
-import org.apache.jackrabbit.webdav.client.methods.DavMethodBase;
-
+import org.apache.commons.httpclient.HttpException;
import com.owncloud.android.datamodel.FileDataStorageManager;
import com.owncloud.android.datamodel.OCFile;
import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
-import com.owncloud.android.oc_framework.network.webdav.WebdavUtils;
import com.owncloud.android.oc_framework.operations.RemoteOperation;
import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
import com.owncloud.android.oc_framework.operations.RemoteOperationResult.ResultCode;
+import com.owncloud.android.oc_framework.operations.remote.RenameRemoteFileOperation;
import com.owncloud.android.utils.FileStorageUtils;
import com.owncloud.android.utils.Log_OC;
-//import org.apache.jackrabbit.webdav.client.methods.MoveMethod;
import android.accounts.Account;
-
/**
* Remote operation performing the rename of a remote file (or folder?) in the ownCloud server.
*
public class RenameFileOperation extends RemoteOperation {
private static final String TAG = RenameFileOperation.class.getSimpleName();
-
- private static final int RENAME_READ_TIMEOUT = 10000;
- private static final int RENAME_CONNECTION_TIMEOUT = 5000;
private OCFile mFile;
protected RemoteOperationResult run(WebdavClient client) {
RemoteOperationResult result = null;
- LocalMoveMethod move = null;
- mNewRemotePath = null;
+ // check if the new name is valid in the local file system
try {
- if (mNewName.equals(mFile.getFileName())) {
- return new RemoteOperationResult(ResultCode.OK);
+ if (!isValidNewName()) {
+ return new RemoteOperationResult(ResultCode.INVALID_LOCAL_FILE_NAME);
}
-
String parent = (new File(mFile.getRemotePath())).getParent();
parent = (parent.endsWith(OCFile.PATH_SEPARATOR)) ? parent : parent + OCFile.PATH_SEPARATOR;
mNewRemotePath = parent + mNewName;
if (mFile.isFolder()) {
mNewRemotePath += OCFile.PATH_SEPARATOR;
}
-
- // check if the new name is valid in the local file system
- if (!isValidNewName()) {
- return new RemoteOperationResult(ResultCode.INVALID_LOCAL_FILE_NAME);
- }
-
- // check if a file with the new name already exists
- if (client.existsFile(mNewRemotePath) || // remote check could fail by network failure. by indeterminate behavior of HEAD for folders ...
- mStorageManager.getFileByPath(mNewRemotePath) != null) { // ... so local check is convenient
+
+ // ckeck local overwrite
+ if (mStorageManager.getFileByPath(mNewRemotePath) != null) {
return new RemoteOperationResult(ResultCode.INVALID_OVERWRITE);
}
- move = new LocalMoveMethod( client.getBaseUri() + WebdavUtils.encodePath(mFile.getRemotePath()),
- client.getBaseUri() + WebdavUtils.encodePath(mNewRemotePath));
- int status = client.executeMethod(move, RENAME_READ_TIMEOUT, RENAME_CONNECTION_TIMEOUT);
- if (move.succeeded()) {
+
+ RenameRemoteFileOperation operation = new RenameRemoteFileOperation(mFile.getFileName(), mFile.getRemotePath(),
+ mNewName, mFile.isFolder());
+ result = operation.execute(client);
+ if (result.isSuccess()) {
if (mFile.isFolder()) {
saveLocalDirectory();
-
+
} else {
saveLocalFile();
-
}
-
- /*
- *} else if (mFile.isDirectory() && (status == 207 || status >= 500)) {
- * // TODO
- * // if server fails in the rename of a folder, some children files could have been moved to a folder with the new name while some others
- * // stayed in the old folder;
- * //
- * // easiest and heaviest solution is synchronizing the parent folder (or the full account);
- * //
- * // a better solution is synchronizing the folders with the old and new names;
- *}
- */
-
}
-
- move.getResponseBodyAsString(); // exhaust response, although not interesting
- result = new RemoteOperationResult(move.succeeded(), status, move.getResponseHeaders());
- Log_OC.i(TAG, "Rename " + mFile.getRemotePath() + " to " + mNewRemotePath + ": " + result.getLogMessage());
-
- } catch (Exception e) {
- result = new RemoteOperationResult(e);
- Log_OC.e(TAG, "Rename " + mFile.getRemotePath() + " to " + ((mNewRemotePath==null) ? mNewName : mNewRemotePath) + ": " + result.getLogMessage(), e);
-
- } finally {
- if (move != null)
- move.releaseConnection();
+ } catch (HttpException e) {
+ Log_OC.e(TAG, "Rename " + mFile.getRemotePath() + " to " + ((mNewRemotePath==null) ? mNewName : mNewRemotePath) + ": " +
+ ((result!= null) ? result.getLogMessage() : ""), e);
+ e.printStackTrace();
+ } catch (IOException e) {
+ Log_OC.e(TAG, "Rename " + mFile.getRemotePath() + " to " + ((mNewRemotePath==null) ? mNewName : mNewRemotePath) + ": " +
+ ((result!= null) ? result.getLogMessage() : ""), e);
+ e.printStackTrace();
}
+
return result;
}
return result;
}
-
- // move operation
- private class LocalMoveMethod extends DavMethodBase {
-
- public LocalMoveMethod(String uri, String dest) {
- super(uri);
- addRequestHeader(new org.apache.commons.httpclient.Header("Destination", dest));
- }
-
- @Override
- public String getName() {
- return "MOVE";
- }
-
- @Override
- protected boolean isSuccess(int status) {
- return status == 201 || status == 204;
- }
-
- }
-
-
}
Toast msg = Toast.makeText(this, R.string.rename_local_fail_msg, Toast.LENGTH_LONG);
msg.show();
// TODO throw again the new rename dialog
+ } if (result.getCode().equals(ResultCode.INVALID_CHARACTER_IN_NAME)) {
+ Toast msg = Toast.makeText(this, R.string.filename_forbidden_characters, Toast.LENGTH_LONG);
+ msg.show();
} else {
Toast msg = Toast.makeText(this, R.string.rename_server_fail_msg, Toast.LENGTH_LONG);
msg.show();
Toast msg = Toast.makeText(getActivity(), R.string.rename_local_fail_msg, Toast.LENGTH_LONG);
msg.show();
// TODO throw again the new rename dialog
+ } if (result.getCode().equals(ResultCode.INVALID_CHARACTER_IN_NAME)) {
+ Toast msg = Toast.makeText(getActivity(), R.string.filename_forbidden_characters, Toast.LENGTH_LONG);
+ msg.show();
} else {
Toast msg = Toast.makeText(getActivity(), R.string.rename_server_fail_msg, Toast.LENGTH_LONG);
msg.show();