CreateFolderOperation.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* ownCloud Android client application
  2. * Copyright (C) 2012 ownCloud Inc.
  3. *
  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.
  7. *
  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.
  12. *
  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/>.
  15. *
  16. */
  17. package com.owncloud.android.operations;
  18. import java.io.File;
  19. import org.apache.commons.httpclient.HttpStatus;
  20. import org.apache.jackrabbit.webdav.client.methods.MkColMethod;
  21. import com.owncloud.android.Log_OC;
  22. import com.owncloud.android.datamodel.FileDataStorageManager;
  23. import com.owncloud.android.datamodel.OCFile;
  24. import eu.alefzero.webdav.WebdavClient;
  25. import eu.alefzero.webdav.WebdavUtils;
  26. /**
  27. * Remote operation performing the creation of a new folder in the ownCloud server.
  28. *
  29. * @author David A. Velasco
  30. */
  31. public class CreateFolderOperation extends RemoteOperation {
  32. private static final String TAG = CreateFolderOperation.class.getSimpleName();
  33. private static final int READ_TIMEOUT = 10000;
  34. private static final int CONNECTION_TIMEOUT = 5000;
  35. protected String mRemotePath;
  36. protected boolean mCreateFullPath;
  37. protected FileDataStorageManager mStorageManager;
  38. /**
  39. * Constructor
  40. *
  41. * @param remotePath Full path to the new directory to create in the remote server.
  42. * @param createFullPath 'True' means that all the ancestor folders should be created if don't exist yet.
  43. * @param storageManager Reference to the local database corresponding to the account where the file is contained.
  44. */
  45. public CreateFolderOperation(String remotePath, boolean createFullPath, FileDataStorageManager storageManager) {
  46. mRemotePath = remotePath;
  47. mCreateFullPath = createFullPath;
  48. mStorageManager = storageManager;
  49. }
  50. /**
  51. * Performs the operation
  52. *
  53. * @param client Client object to communicate with the remote ownCloud server.
  54. */
  55. @Override
  56. protected RemoteOperationResult run(WebdavClient client) {
  57. RemoteOperationResult result = null;
  58. MkColMethod mkcol = null;
  59. try {
  60. mkcol = new MkColMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath));
  61. int status = client.executeMethod(mkcol, READ_TIMEOUT, CONNECTION_TIMEOUT);
  62. if (!mkcol.succeeded() && mkcol.getStatusCode() == HttpStatus.SC_CONFLICT && mCreateFullPath) {
  63. result = createParentFolder(getParentPath(), client);
  64. status = client.executeMethod(mkcol, READ_TIMEOUT, CONNECTION_TIMEOUT); // second (and last) try
  65. }
  66. if (mkcol.succeeded()) {
  67. // Save new directory in local database
  68. OCFile newDir = new OCFile(mRemotePath);
  69. newDir.setMimetype("DIR");
  70. long parentId = mStorageManager.getFileByPath(getParentPath()).getFileId();
  71. newDir.setParentId(parentId);
  72. newDir.setModificationTimestamp(System.currentTimeMillis());
  73. mStorageManager.saveFile(newDir);
  74. }
  75. result = new RemoteOperationResult(mkcol.succeeded(), status, mkcol.getResponseHeaders());
  76. Log_OC.d(TAG, "Create directory " + mRemotePath + ": " + result.getLogMessage());
  77. client.exhaustResponse(mkcol.getResponseBodyAsStream());
  78. } catch (Exception e) {
  79. result = new RemoteOperationResult(e);
  80. Log_OC.e(TAG, "Create directory " + mRemotePath + ": " + result.getLogMessage(), e);
  81. } finally {
  82. if (mkcol != null)
  83. mkcol.releaseConnection();
  84. }
  85. return result;
  86. }
  87. private String getParentPath() {
  88. String parentPath = new File(mRemotePath).getParent();
  89. parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath : parentPath + OCFile.PATH_SEPARATOR;
  90. return parentPath;
  91. }
  92. private RemoteOperationResult createParentFolder(String parentPath, WebdavClient client) {
  93. RemoteOperation operation = new CreateFolderOperation( parentPath,
  94. mCreateFullPath,
  95. mStorageManager );
  96. return operation.execute(client);
  97. }
  98. }