f0a325ed3bbe075147e7998125a0098e36585d86
[pub/Android/ownCloud.git] / oc_framework / src / com / owncloud / android / lib / operations / remote / CreateRemoteFolderOperation.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 org.apache.commons.httpclient.HttpStatus;
28 import org.apache.jackrabbit.webdav.client.methods.MkColMethod;
29
30 import android.util.Log;
31
32 import com.owncloud.android.lib.network.OwnCloudClient;
33 import com.owncloud.android.lib.network.webdav.WebdavUtils;
34 import com.owncloud.android.lib.operations.common.RemoteOperation;
35 import com.owncloud.android.lib.operations.common.RemoteOperationResult;
36 import com.owncloud.android.lib.operations.common.RemoteOperationResult.ResultCode;
37 import com.owncloud.android.lib.utils.FileUtils;
38
39
40
41 /**
42 * Remote operation performing the creation of a new folder in the ownCloud server.
43 *
44 * @author David A. Velasco
45 * @author masensio
46 *
47 */
48 public class CreateRemoteFolderOperation extends RemoteOperation {
49
50 private static final String TAG = CreateRemoteFolderOperation.class.getSimpleName();
51
52 private static final int READ_TIMEOUT = 10000;
53 private static final int CONNECTION_TIMEOUT = 5000;
54
55
56 protected String mRemotePath;
57 protected boolean mCreateFullPath;
58
59 /**
60 * Constructor
61 *
62 * @param remotePath Full path to the new directory to create in the remote server.
63 * @param createFullPath 'True' means that all the ancestor folders should be created if don't exist yet.
64 */
65 public CreateRemoteFolderOperation(String remotePath, boolean createFullPath) {
66 mRemotePath = remotePath;
67 mCreateFullPath = createFullPath;
68 }
69
70 /**
71 * Performs the operation
72 *
73 * @param client Client object to communicate with the remote ownCloud server.
74 */
75 @Override
76 protected RemoteOperationResult run(OwnCloudClient client) {
77 RemoteOperationResult result = null;
78 MkColMethod mkcol = null;
79
80 boolean noInvalidChars = FileUtils.isValidPath(mRemotePath);
81 if (noInvalidChars) {
82 try {
83 mkcol = new MkColMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath));
84 int status = client.executeMethod(mkcol, READ_TIMEOUT, CONNECTION_TIMEOUT);
85 if (!mkcol.succeeded() && mkcol.getStatusCode() == HttpStatus.SC_CONFLICT && mCreateFullPath) {
86 result = createParentFolder(FileUtils.getParentPath(mRemotePath), client);
87 status = client.executeMethod(mkcol, READ_TIMEOUT, CONNECTION_TIMEOUT); // second (and last) try
88 }
89
90 result = new RemoteOperationResult(mkcol.succeeded(), status, mkcol.getResponseHeaders());
91 Log.d(TAG, "Create directory " + mRemotePath + ": " + result.getLogMessage());
92 client.exhaustResponse(mkcol.getResponseBodyAsStream());
93
94 } catch (Exception e) {
95 result = new RemoteOperationResult(e);
96 Log.e(TAG, "Create directory " + mRemotePath + ": " + result.getLogMessage(), e);
97
98 } finally {
99 if (mkcol != null)
100 mkcol.releaseConnection();
101 }
102 } else {
103 result = new RemoteOperationResult(ResultCode.INVALID_CHARACTER_IN_NAME);
104 }
105
106 return result;
107 }
108
109
110 private RemoteOperationResult createParentFolder(String parentPath, OwnCloudClient client) {
111 RemoteOperation operation = new CreateRemoteFolderOperation(parentPath,
112 mCreateFullPath);
113 return operation.execute(client);
114 }
115
116
117
118 }