1 /* ownCloud Android client application
2 * Copyright (C) 2012 ownCloud Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package com
.owncloud
.android
.operations
;
22 import org
.apache
.commons
.httpclient
.HttpStatus
;
23 import org
.apache
.jackrabbit
.webdav
.client
.methods
.MkColMethod
;
25 import com
.owncloud
.android
.Log_OC
;
26 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
27 import com
.owncloud
.android
.datamodel
.OCFile
;
30 import eu
.alefzero
.webdav
.WebdavClient
;
31 import eu
.alefzero
.webdav
.WebdavUtils
;
34 * Remote operation performing the creation of a new folder in the ownCloud server.
36 * @author David A. Velasco
38 public class CreateFolderOperation
extends RemoteOperation
{
40 private static final String TAG
= CreateFolderOperation
.class.getSimpleName();
42 private static final int READ_TIMEOUT
= 10000;
43 private static final int CONNECTION_TIMEOUT
= 5000;
45 protected String mRemotePath
;
46 protected boolean mCreateFullPath
;
47 protected FileDataStorageManager mStorageManager
;
52 * @param remotePath Full path to the new directory to create in the remote server.
53 * @param createFullPath 'True' means that all the ancestor folders should be created if don't exist yet.
54 * @param storageManager Reference to the local database corresponding to the account where the file is contained.
56 public CreateFolderOperation(String remotePath
, boolean createFullPath
, FileDataStorageManager storageManager
) {
57 mRemotePath
= remotePath
;
58 mCreateFullPath
= createFullPath
;
59 mStorageManager
= storageManager
;
64 * Performs the operation
66 * @param client Client object to communicate with the remote ownCloud server.
69 protected RemoteOperationResult
run(WebdavClient client
) {
70 RemoteOperationResult result
= null
;
71 MkColMethod mkcol
= null
;
73 mkcol
= new MkColMethod(client
.getBaseUri() + WebdavUtils
.encodePath(mRemotePath
));
74 int status
= client
.executeMethod(mkcol
, READ_TIMEOUT
, CONNECTION_TIMEOUT
);
75 if (!mkcol
.succeeded() && mkcol
.getStatusCode() == HttpStatus
.SC_CONFLICT
&& mCreateFullPath
) {
76 result
= createParentFolder(getParentPath(), client
);
77 status
= client
.executeMethod(mkcol
, READ_TIMEOUT
, CONNECTION_TIMEOUT
); // second (and last) try
79 if (mkcol
.succeeded()) {
80 // Save new directory in local database
81 OCFile newDir
= new OCFile(mRemotePath
);
82 newDir
.setMimetype("DIR");
83 long parentId
= mStorageManager
.getFileByPath(getParentPath()).getFileId();
84 newDir
.setParentId(parentId
);
85 newDir
.setModificationTimestamp(System
.currentTimeMillis());
86 mStorageManager
.saveFile(newDir
);
88 result
= new RemoteOperationResult(mkcol
.succeeded(), status
, mkcol
.getResponseHeaders());
89 Log_OC
.d(TAG
, "Create directory " + mRemotePath
+ ": " + result
.getLogMessage());
90 client
.exhaustResponse(mkcol
.getResponseBodyAsStream());
92 } catch (Exception e
) {
93 result
= new RemoteOperationResult(e
);
94 Log_OC
.e(TAG
, "Create directory " + mRemotePath
+ ": " + result
.getLogMessage(), e
);
98 mkcol
.releaseConnection();
104 private String
getParentPath() {
105 String parentPath
= new File(mRemotePath
).getParent();
106 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
: parentPath
+ OCFile
.PATH_SEPARATOR
;
111 private RemoteOperationResult
createParentFolder(String parentPath
, WebdavClient client
) {
112 RemoteOperation operation
= new CreateFolderOperation( parentPath
,
115 return operation
.execute(client
);