1 /* ownCloud Android Library is available under MIT license
2 * Copyright (C) 2014 ownCloud (http://www.owncloud.org/)
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 package com
.owncloud
.android
.lib
.operations
.remote
;
27 import org
.apache
.commons
.httpclient
.HttpStatus
;
28 import org
.apache
.jackrabbit
.webdav
.client
.methods
.MkColMethod
;
30 import android
.util
.Log
;
32 import com
.owncloud
.android
.lib
.network
.OwnCloudClient
;
33 import com
.owncloud
.android
.lib
.network
.webdav
.WebdavUtils
;
34 import com
.owncloud
.android
.lib
.operations
.common
.RemoteOperation
;
35 import com
.owncloud
.android
.lib
.operations
.common
.RemoteOperationResult
;
36 import com
.owncloud
.android
.lib
.operations
.common
.RemoteOperationResult
.ResultCode
;
37 import com
.owncloud
.android
.lib
.utils
.FileUtils
;
42 * Remote operation performing the creation of a new folder in the ownCloud server.
44 * @author David A. Velasco
48 public class CreateRemoteFolderOperation
extends RemoteOperation
{
50 private static final String TAG
= CreateRemoteFolderOperation
.class.getSimpleName();
52 private static final int READ_TIMEOUT
= 10000;
53 private static final int CONNECTION_TIMEOUT
= 5000;
56 protected String mRemotePath
;
57 protected boolean mCreateFullPath
;
62 * @param remotePath Full path to the new directory to create in the remote server.
63 * @param createFullPath 'True' means that all the ancestor folders should be created if don't exist yet.
65 public CreateRemoteFolderOperation(String remotePath
, boolean createFullPath
) {
66 mRemotePath
= remotePath
;
67 mCreateFullPath
= createFullPath
;
71 * Performs the operation
73 * @param client Client object to communicate with the remote ownCloud server.
76 protected RemoteOperationResult
run(OwnCloudClient client
) {
77 RemoteOperationResult result
= null
;
78 MkColMethod mkcol
= null
;
80 boolean noInvalidChars
= FileUtils
.isValidPath(mRemotePath
);
83 mkcol
= new MkColMethod(client
.getBaseUri() + WebdavUtils
.encodePath(mRemotePath
));
84 int status
= client
.executeMethod(mkcol
, READ_TIMEOUT
, CONNECTION_TIMEOUT
);
85 if (!mkcol
.succeeded() && mkcol
.getStatusCode() == HttpStatus
.SC_CONFLICT
&& mCreateFullPath
) {
86 result
= createParentFolder(FileUtils
.getParentPath(mRemotePath
), client
);
87 status
= client
.executeMethod(mkcol
, READ_TIMEOUT
, CONNECTION_TIMEOUT
); // second (and last) try
90 result
= new RemoteOperationResult(mkcol
.succeeded(), status
, mkcol
.getResponseHeaders());
91 Log
.d(TAG
, "Create directory " + mRemotePath
+ ": " + result
.getLogMessage());
92 client
.exhaustResponse(mkcol
.getResponseBodyAsStream());
94 } catch (Exception e
) {
95 result
= new RemoteOperationResult(e
);
96 Log
.e(TAG
, "Create directory " + mRemotePath
+ ": " + result
.getLogMessage(), e
);
100 mkcol
.releaseConnection();
103 result
= new RemoteOperationResult(ResultCode
.INVALID_CHARACTER_IN_NAME
);
110 private RemoteOperationResult
createParentFolder(String parentPath
, OwnCloudClient client
) {
111 RemoteOperation operation
= new CreateRemoteFolderOperation(parentPath
,
113 return operation
.execute(client
);