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