1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
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.
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.
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/>.
18 package com.owncloud.android.oc_framework.operations.remote;
20 import org.apache.commons.httpclient.HttpStatus;
21 import org.apache.jackrabbit.webdav.client.methods.MkColMethod;
23 import android.util.Log;
25 import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
26 import com.owncloud.android.oc_framework.network.webdav.WebdavUtils;
27 import com.owncloud.android.oc_framework.operations.RemoteOperation;
28 import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
29 import com.owncloud.android.oc_framework.operations.RemoteOperationResult.ResultCode;
30 import com.owncloud.android.oc_framework.utils.FileUtils;
35 * Remote operation performing the creation of a new folder in the ownCloud server.
37 * @author David A. Velasco
41 public class CreateRemoteFolderOperation extends RemoteOperation {
43 private static final String TAG = CreateRemoteFolderOperation.class.getSimpleName();
45 private static final int READ_TIMEOUT = 10000;
46 private static final int CONNECTION_TIMEOUT = 5000;
49 protected String mRemotePath;
50 protected boolean mCreateFullPath;
55 * @param remotePath Full path to the new directory to create in the remote server.
56 * @param createFullPath 'True' means that all the ancestor folders should be created if don't exist yet.
58 public CreateRemoteFolderOperation(String remotePath, boolean createFullPath) {
59 mRemotePath = remotePath;
60 mCreateFullPath = createFullPath;
64 * Performs the operation
66 * @param client Client object to communicate with the remote ownCloud server.
69 protected RemoteOperationResult run(WebdavClient client) {
70 RemoteOperationResult result = null;
71 MkColMethod mkcol = null;
73 boolean noInvalidChars = FileUtils.isValidPath(mRemotePath);
76 mkcol = new MkColMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath));
77 int status = client.executeMethod(mkcol, READ_TIMEOUT, CONNECTION_TIMEOUT);
78 if (!mkcol.succeeded() && mkcol.getStatusCode() == HttpStatus.SC_CONFLICT && mCreateFullPath) {
79 result = createParentFolder(FileUtils.getParentPath(mRemotePath), client);
80 status = client.executeMethod(mkcol, READ_TIMEOUT, CONNECTION_TIMEOUT); // second (and last) try
83 result = new RemoteOperationResult(mkcol.succeeded(), status, mkcol.getResponseHeaders());
84 Log.d(TAG, "Create directory " + mRemotePath + ": " + result.getLogMessage());
85 client.exhaustResponse(mkcol.getResponseBodyAsStream());
87 } catch (Exception e) {
88 result = new RemoteOperationResult(e);
89 Log.e(TAG, "Create directory " + mRemotePath + ": " + result.getLogMessage(), e);
93 mkcol.releaseConnection();
96 result = new RemoteOperationResult(ResultCode.INVALID_CHARACTER_IN_NAME);
103 private RemoteOperationResult createParentFolder(String parentPath, WebdavClient client) {
104 RemoteOperation operation = new CreateRemoteFolderOperation(parentPath,
106 return operation.execute(client);