f25c22ed57693b35ad1f54df94bce449a6e47f68
[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 mFolderName;
33 protected String mRemotePath;
34 protected boolean mCreateFullPath;
35
36 /**
37 * Constructor
38 *
39 * @param folderName Name of new directory
40 * @param remotePath Full path to the new directory to create in the remote server.
41 * @param createFullPath 'True' means that all the ancestor folders should be created if don't exist yet.
42 */
43 public CreateRemoteFolderOperation(String folderName, String remotePath, boolean createFullPath) {
44 mFolderName = folderName;
45 mRemotePath = remotePath;
46 mCreateFullPath = createFullPath;
47 }
48
49 /**
50 * Performs the operation
51 *
52 * @param client Client object to communicate with the remote ownCloud server.
53 */
54 @Override
55 protected RemoteOperationResult run(WebdavClient client) {
56 RemoteOperationResult result = null;
57 MkColMethod mkcol = null;
58
59 boolean noInvalidChars = FileUtils.validateName(mFolderName);
60 if (noInvalidChars) {
61 try {
62 mkcol = new MkColMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath));
63 int status = client.executeMethod(mkcol, READ_TIMEOUT, CONNECTION_TIMEOUT);
64 if (!mkcol.succeeded() && mkcol.getStatusCode() == HttpStatus.SC_CONFLICT && mCreateFullPath) {
65 result = createParentFolder(FileUtils.getParentPath(mRemotePath), client);
66 status = client.executeMethod(mkcol, READ_TIMEOUT, CONNECTION_TIMEOUT); // second (and last) try
67 }
68
69 result = new RemoteOperationResult(mkcol.succeeded(), status, mkcol.getResponseHeaders());
70 Log.d(TAG, "Create directory " + mRemotePath + ": " + result.getLogMessage());
71 client.exhaustResponse(mkcol.getResponseBodyAsStream());
72
73 } catch (Exception e) {
74 result = new RemoteOperationResult(e);
75 Log.e(TAG, "Create directory " + mRemotePath + ": " + result.getLogMessage(), e);
76
77 } finally {
78 if (mkcol != null)
79 mkcol.releaseConnection();
80 }
81 } else {
82 result = new RemoteOperationResult(ResultCode.INVALID_CHARACTER_IN_NAME);
83 }
84
85 return result;
86 }
87
88
89 private RemoteOperationResult createParentFolder(String parentPath, WebdavClient client) {
90 RemoteOperation operation = new CreateRemoteFolderOperation("", parentPath,
91 mCreateFullPath);
92 return operation.execute(client);
93 }
94
95
96
97 }