1 package com.owncloud.android.oc_framework.operations.remote;
3 import org.apache.commons.httpclient.HttpStatus;
4 import org.apache.jackrabbit.webdav.client.methods.MkColMethod;
6 import android.util.Log;
8 import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
9 import com.owncloud.android.oc_framework.network.webdav.WebdavUtils;
10 import com.owncloud.android.oc_framework.operations.RemoteOperation;
11 import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
12 import com.owncloud.android.oc_framework.operations.RemoteOperationResult.ResultCode;
13 import com.owncloud.android.oc_framework.utils.FileUtils;
18 * Remote operation performing the creation of a new folder in the ownCloud server.
20 * @author David A. Velasco
24 public class CreateRemoteFolderOperation extends RemoteOperation {
26 private static final String TAG = CreateRemoteFolderOperation.class.getSimpleName();
28 private static final int READ_TIMEOUT = 10000;
29 private static final int CONNECTION_TIMEOUT = 5000;
33 protected String mRemotePath;
34 protected boolean mCreateFullPath;
39 * @param remotePath Full path to the new directory to create in the remote server.
40 * @param createFullPath 'True' means that all the ancestor folders should be created if don't exist yet.
42 public CreateRemoteFolderOperation(String remotePath, boolean createFullPath) {
43 mRemotePath = remotePath;
44 mCreateFullPath = createFullPath;
48 * Performs the operation
50 * @param client Client object to communicate with the remote ownCloud server.
53 protected RemoteOperationResult run(WebdavClient client) {
54 RemoteOperationResult result = null;
55 MkColMethod mkcol = null;
57 boolean noInvalidChars = FileUtils.validateName(mRemotePath);
60 mkcol = new MkColMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath));
61 int status = client.executeMethod(mkcol, READ_TIMEOUT, CONNECTION_TIMEOUT);
62 if (!mkcol.succeeded() && mkcol.getStatusCode() == HttpStatus.SC_CONFLICT && mCreateFullPath) {
63 result = createParentFolder(FileUtils.getParentPath(mRemotePath), client);
64 status = client.executeMethod(mkcol, READ_TIMEOUT, CONNECTION_TIMEOUT); // second (and last) try
67 result = new RemoteOperationResult(mkcol.succeeded(), status, mkcol.getResponseHeaders());
68 Log.d(TAG, "Create directory " + mRemotePath + ": " + result.getLogMessage());
69 client.exhaustResponse(mkcol.getResponseBodyAsStream());
71 } catch (Exception e) {
72 result = new RemoteOperationResult(e);
73 Log.e(TAG, "Create directory " + mRemotePath + ": " + result.getLogMessage(), e);
77 mkcol.releaseConnection();
80 result = new RemoteOperationResult(ResultCode.INVALID_CHARACTER_IN_NAME);
87 private RemoteOperationResult createParentFolder(String parentPath, WebdavClient client) {
88 RemoteOperation operation = new CreateRemoteFolderOperation( parentPath,
90 return operation.execute(client);