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
.DataStorageManager
;
27 import com
.owncloud
.android
.datamodel
.OCFile
;
29 import eu
.alefzero
.webdav
.WebdavClient
;
30 import eu
.alefzero
.webdav
.WebdavUtils
;
33 * Remote operation performing the creation of a new folder in the ownCloud server.
35 * @author David A. Velasco
37 public class CreateFolderOperation
extends RemoteOperation
{
39 private static final String TAG
= CreateFolderOperation
.class.getSimpleName();
41 private static final int READ_TIMEOUT
= 10000;
42 private static final int CONNECTION_TIMEOUT
= 5000;
44 protected String mRemotePath
;
45 protected boolean mCreateFullPath
;
46 protected DataStorageManager mStorageManager
;
51 * @param remotePath Full path to the new directory to create in the remote server.
52 * @param createFullPath 'True' means that all the ancestor folders should be created if don't exist yet.
53 * @param storageManager Reference to the local database corresponding to the account where the file is contained.
55 public CreateFolderOperation(String remotePath
, boolean createFullPath
, DataStorageManager storageManager
) {
56 mRemotePath
= remotePath
;
57 mCreateFullPath
= createFullPath
;
58 mStorageManager
= storageManager
;
63 * Performs the operation
65 * @param client Client object to communicate with the remote ownCloud server.
68 protected RemoteOperationResult
run(WebdavClient client
) {
69 RemoteOperationResult result
= null
;
70 MkColMethod mkcol
= null
;
72 mkcol
= new MkColMethod(client
.getBaseUri() + WebdavUtils
.encodePath(mRemotePath
));
73 int status
= client
.executeMethod(mkcol
, READ_TIMEOUT
, CONNECTION_TIMEOUT
);
74 if (!mkcol
.succeeded() && mkcol
.getStatusCode() == HttpStatus
.SC_CONFLICT
&& mCreateFullPath
) {
75 result
= createParentFolder(getParentPath(), client
);
76 status
= client
.executeMethod(mkcol
, READ_TIMEOUT
, CONNECTION_TIMEOUT
); // second (and last) try
78 if (mkcol
.succeeded()) {
79 // Save new directory in local database
80 OCFile newDir
= new OCFile(mRemotePath
);
81 newDir
.setMimetype("DIR");
82 long parentId
= mStorageManager
.getFileByPath(getParentPath()).getFileId();
83 newDir
.setParentId(parentId
);
84 newDir
.setModificationTimestamp(System
.currentTimeMillis());
85 mStorageManager
.saveFile(newDir
);
87 result
= new RemoteOperationResult(mkcol
.succeeded(), status
, mkcol
.getResponseHeaders());
88 Log_OC
.d(TAG
, "Create directory " + mRemotePath
+ ": " + result
.getLogMessage());
89 client
.exhaustResponse(mkcol
.getResponseBodyAsStream());
91 } catch (Exception e
) {
92 result
= new RemoteOperationResult(e
);
93 Log_OC
.e(TAG
, "Create directory " + mRemotePath
+ ": " + result
.getLogMessage(), e
);
97 mkcol
.releaseConnection();
103 private String
getParentPath() {
104 String parentPath
= new File(mRemotePath
).getParent();
105 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
: parentPath
+ OCFile
.PATH_SEPARATOR
;
110 private RemoteOperationResult
createParentFolder(String parentPath
, WebdavClient client
) {
111 RemoteOperation operation
= new CreateFolderOperation( parentPath
,
114 return operation
.execute(client
);