4e756da79fa8818b48e037964b2614940ca9f2d2
[pub/Android/ownCloud.git] /
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 org.apache.commons.httpclient.HttpStatus;
21 import org.apache.jackrabbit.webdav.client.methods.MkColMethod;
22
23 import android.util.Log;
24
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;
31
32
33
34 /**
35 * Remote operation performing the creation of a new folder in the ownCloud server.
36 *
37 * @author David A. Velasco
38 * @author masensio
39 *
40 */
41 public class CreateRemoteFolderOperation extends RemoteOperation {
42
43 private static final String TAG = CreateRemoteFolderOperation.class.getSimpleName();
44
45 private static final int READ_TIMEOUT = 10000;
46 private static final int CONNECTION_TIMEOUT = 5000;
47
48
49 protected String mRemotePath;
50 protected boolean mCreateFullPath;
51
52 /**
53 * Constructor
54 *
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.
57 */
58 public CreateRemoteFolderOperation(String remotePath, boolean createFullPath) {
59 mRemotePath = remotePath;
60 mCreateFullPath = createFullPath;
61 }
62
63 /**
64 * Performs the operation
65 *
66 * @param client Client object to communicate with the remote ownCloud server.
67 */
68 @Override
69 protected RemoteOperationResult run(WebdavClient client) {
70 RemoteOperationResult result = null;
71 MkColMethod mkcol = null;
72
73 boolean noInvalidChars = FileUtils.isValidPath(mRemotePath);
74 if (noInvalidChars) {
75 try {
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
81 }
82
83 result = new RemoteOperationResult(mkcol.succeeded(), status, mkcol.getResponseHeaders());
84 Log.d(TAG, "Create directory " + mRemotePath + ": " + result.getLogMessage());
85 client.exhaustResponse(mkcol.getResponseBodyAsStream());
86
87 } catch (Exception e) {
88 result = new RemoteOperationResult(e);
89 Log.e(TAG, "Create directory " + mRemotePath + ": " + result.getLogMessage(), e);
90
91 } finally {
92 if (mkcol != null)
93 mkcol.releaseConnection();
94 }
95 } else {
96 result = new RemoteOperationResult(ResultCode.INVALID_CHARACTER_IN_NAME);
97 }
98
99 return result;
100 }
101
102
103 private RemoteOperationResult createParentFolder(String parentPath, WebdavClient client) {
104 RemoteOperation operation = new CreateRemoteFolderOperation(parentPath,
105 mCreateFullPath);
106 return operation.execute(client);
107 }
108
109
110
111 }