d1899945246bd8bb775ab9510af17eb68bd628f7
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / SynchronizeFileOperation.java
1 package com.owncloud.android.operations;
2
3 import org.apache.http.HttpStatus;
4 import org.apache.jackrabbit.webdav.MultiStatus;
5 import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
6
7 import android.accounts.Account;
8 import android.content.Context;
9 import android.util.Log;
10
11 import com.owncloud.android.datamodel.DataStorageManager;
12 import com.owncloud.android.datamodel.OCFile;
13
14 import eu.alefzero.webdav.WebdavClient;
15 import eu.alefzero.webdav.WebdavEntry;
16 import eu.alefzero.webdav.WebdavUtils;
17
18 public class SynchronizeFileOperation extends RemoteOperation {
19
20 private String TAG = SynchronizeFileOperation.class.getSimpleName();
21
22 private String mRemotePath;
23
24 private DataStorageManager mStorageManager;
25
26 private Account mAccount;
27
28 public SynchronizeFileOperation(
29 String remotePath,
30 DataStorageManager dataStorageManager,
31 Account account,
32 Context context ) {
33 mRemotePath = remotePath;
34 mStorageManager = dataStorageManager;
35 mAccount = account;
36 }
37
38 @Override
39 protected RemoteOperationResult run(WebdavClient client) {
40 PropFindMethod propfind = null;
41 RemoteOperationResult result = null;
42 try {
43 propfind = new PropFindMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath));
44 int status = client.executeMethod(propfind);
45 boolean isMultiStatus = status == HttpStatus.SC_MULTI_STATUS;
46 Boolean isConflict = Boolean.FALSE;
47 if (isMultiStatus) {
48 MultiStatus resp = propfind.getResponseBodyAsMultiStatus();
49 WebdavEntry we = new WebdavEntry(resp.getResponses()[0],
50 client.getBaseUri().getPath());
51 OCFile file = fillOCFile(we);
52 OCFile oldFile = mStorageManager.getFileByPath(file.getRemotePath());
53 if (oldFile.getFileLength() != file.getFileLength() ||
54 oldFile.getModificationTimestamp() != file.getModificationTimestamp()) {
55 isConflict = Boolean.TRUE;
56 }
57
58 } else {
59 client.exhaustResponse(propfind.getResponseBodyAsStream());
60 }
61
62 result = new RemoteOperationResult(isMultiStatus, status);
63 result.setExtraData(isConflict);
64 Log.i(TAG, "Synchronizing " + mAccount.name + ", file " + mRemotePath + ": " + result.getLogMessage());
65 } catch (Exception e) {
66 result = new RemoteOperationResult(e);
67 Log.e(TAG, "Synchronizing " + mAccount.name + ", file " + mRemotePath + ": " + result.getLogMessage(), result.getException());
68
69 } finally {
70 if (propfind != null)
71 propfind.releaseConnection();
72 }
73 return result;
74 }
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
92 }