1 package com
.owncloud
.android
.operations
.remote
;
3 import org
.apache
.commons
.httpclient
.HttpStatus
;
4 import org
.apache
.jackrabbit
.webdav
.client
.methods
.MkColMethod
;
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
;
15 * Remote operation performing the creation of a new folder in the ownCloud server.
17 * @author David A. Velasco
21 public class CreateRemoteFolderOperation
extends RemoteOperation
{
23 private static final String TAG
= CreateRemoteFolderOperation
.class.getSimpleName();
25 private static final int READ_TIMEOUT
= 10000;
26 private static final int CONNECTION_TIMEOUT
= 5000;
28 protected String mRemotePath
;
29 protected boolean mCreateFullPath
;
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.
37 public CreateRemoteFolderOperation(String remotePath
, boolean createFullPath
) {
38 mRemotePath
= remotePath
;
39 mCreateFullPath
= createFullPath
;
43 * Performs the operation
45 * @param client Client object to communicate with the remote ownCloud server.
48 protected RemoteOperationResult
run(WebdavClient client
) {
49 RemoteOperationResult result
= null
;
50 MkColMethod mkcol
= null
;
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
60 result
= new RemoteOperationResult(mkcol
.succeeded(), status
, mkcol
.getResponseHeaders());
61 Log_OC
.d(TAG
, "Create directory " + mRemotePath
+ ": " + result
.getLogMessage());
62 client
.exhaustResponse(mkcol
.getResponseBodyAsStream());
64 } catch (Exception e
) {
65 result
= new RemoteOperationResult(e
);
66 Log_OC
.e(TAG
, "Create directory " + mRemotePath
+ ": " + result
.getLogMessage(), e
);
70 mkcol
.releaseConnection();
76 private RemoteOperationResult
createParentFolder(String parentPath
, WebdavClient client
) {
77 RemoteOperation operation
= new CreateRemoteFolderOperation( parentPath
,
79 return operation
.execute(client
);