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