47870dec44446e9797e980384702d6d63a737207
[pub/Android/ownCloud.git] /
1 package com.owncloud.android.oc_framework.operations.remote;
2
3 import java.io.File;
4
5 import org.apache.commons.httpclient.HttpStatus;
6 import org.apache.jackrabbit.webdav.client.methods.MkColMethod;
7
8 import android.util.Log;
9
10 import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
11 import com.owncloud.android.oc_framework.network.webdav.WebdavUtils;
12 import com.owncloud.android.oc_framework.operations.RemoteOperation;
13 import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
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 private static final String PATH_SEPARATOR = "/";
32
33 protected String mRemotePath;
34 protected boolean mCreateFullPath;
35
36 /**
37 * Constructor
38 *
39 * @param remotePath Full path to the new directory to create in the remote server.
40 * @param createFullPath 'True' means that all the ancestor folders should be created if don't exist yet.
41 */
42 public CreateRemoteFolderOperation(String remotePath, boolean createFullPath) {
43 mRemotePath = remotePath;
44 mCreateFullPath = createFullPath;
45 }
46
47 /**
48 * Performs the operation
49 *
50 * @param client Client object to communicate with the remote ownCloud server.
51 */
52 @Override
53 protected RemoteOperationResult run(WebdavClient client) {
54 RemoteOperationResult result = null;
55 MkColMethod mkcol = null;
56
57 try {
58 mkcol = new MkColMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath));
59 int status = client.executeMethod(mkcol, READ_TIMEOUT, CONNECTION_TIMEOUT);
60 if (!mkcol.succeeded() && mkcol.getStatusCode() == HttpStatus.SC_CONFLICT && mCreateFullPath) {
61 result = createParentFolder(getParentPath(), client);
62 status = client.executeMethod(mkcol, READ_TIMEOUT, CONNECTION_TIMEOUT); // second (and last) try
63 }
64
65 result = new RemoteOperationResult(mkcol.succeeded(), status, mkcol.getResponseHeaders());
66 Log.d(TAG, "Create directory " + mRemotePath + ": " + result.getLogMessage());
67 client.exhaustResponse(mkcol.getResponseBodyAsStream());
68
69 } catch (Exception e) {
70 result = new RemoteOperationResult(e);
71 Log.e(TAG, "Create directory " + mRemotePath + ": " + result.getLogMessage(), e);
72
73 } finally {
74 if (mkcol != null)
75 mkcol.releaseConnection();
76 }
77 return result;
78 }
79
80
81 private RemoteOperationResult createParentFolder(String parentPath, WebdavClient client) {
82 RemoteOperation operation = new CreateRemoteFolderOperation( parentPath,
83 mCreateFullPath);
84 return operation.execute(client);
85 }
86
87 private String getParentPath() {
88 String parentPath = new File(mRemotePath).getParent();
89 parentPath = parentPath.endsWith(PATH_SEPARATOR) ? parentPath : parentPath + PATH_SEPARATOR;
90 return parentPath;
91 }
92
93 }