SynchronizeFileOperation.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* ownCloud Android client application
  2. * Copyright (C) 2012 Bartek Przybylski
  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 as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. */
  18. package com.owncloud.android.operations;
  19. import org.apache.http.HttpStatus;
  20. import org.apache.jackrabbit.webdav.MultiStatus;
  21. import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
  22. import android.accounts.Account;
  23. import android.util.Log;
  24. import com.owncloud.android.datamodel.DataStorageManager;
  25. import com.owncloud.android.datamodel.OCFile;
  26. import eu.alefzero.webdav.WebdavClient;
  27. import eu.alefzero.webdav.WebdavEntry;
  28. import eu.alefzero.webdav.WebdavUtils;
  29. public class SynchronizeFileOperation extends RemoteOperation {
  30. private String TAG = SynchronizeFileOperation.class.getSimpleName();
  31. private String mRemotePath;
  32. private DataStorageManager mStorageManager;
  33. private Account mAccount;
  34. public SynchronizeFileOperation(
  35. String remotePath,
  36. DataStorageManager dataStorageManager,
  37. Account account) {
  38. mRemotePath = remotePath;
  39. mStorageManager = dataStorageManager;
  40. mAccount = account;
  41. }
  42. @Override
  43. protected RemoteOperationResult run(WebdavClient client) {
  44. PropFindMethod propfind = null;
  45. RemoteOperationResult result = null;
  46. try {
  47. propfind = new PropFindMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath));
  48. int status = client.executeMethod(propfind);
  49. boolean isMultiStatus = status == HttpStatus.SC_MULTI_STATUS;
  50. Boolean isConflict = Boolean.FALSE;
  51. if (isMultiStatus) {
  52. MultiStatus resp = propfind.getResponseBodyAsMultiStatus();
  53. WebdavEntry we = new WebdavEntry(resp.getResponses()[0],
  54. client.getBaseUri().getPath());
  55. OCFile file = fillOCFile(we);
  56. OCFile oldFile = mStorageManager.getFileByPath(file.getRemotePath());
  57. if (oldFile.getFileLength() != file.getFileLength() ||
  58. oldFile.getModificationTimestamp() != file.getModificationTimestamp()) {
  59. isConflict = Boolean.TRUE;
  60. }
  61. } else {
  62. client.exhaustResponse(propfind.getResponseBodyAsStream());
  63. }
  64. result = new RemoteOperationResult(isMultiStatus, status);
  65. result.setExtraData(isConflict);
  66. Log.i(TAG, "Synchronizing " + mAccount.name + ", file " + mRemotePath + ": " + result.getLogMessage());
  67. } catch (Exception e) {
  68. result = new RemoteOperationResult(e);
  69. Log.e(TAG, "Synchronizing " + mAccount.name + ", file " + mRemotePath + ": " + result.getLogMessage(), result.getException());
  70. } finally {
  71. if (propfind != null)
  72. propfind.releaseConnection();
  73. }
  74. return result;
  75. }
  76. /**
  77. * Creates and populates a new {@link OCFile} object with the data read from the server.
  78. *
  79. * @param we WebDAV entry read from the server for a WebDAV resource (remote file or folder).
  80. * @return New OCFile instance representing the remote resource described by we.
  81. */
  82. private OCFile fillOCFile(WebdavEntry we) {
  83. OCFile file = new OCFile(we.decodedPath());
  84. file.setCreationTimestamp(we.createTimestamp());
  85. file.setFileLength(we.contentLength());
  86. file.setMimetype(we.contentType());
  87. file.setModificationTimestamp(we.modifiedTimesamp());
  88. file.setLastSyncDate(System.currentTimeMillis());
  89. return file;
  90. }
  91. }