1 package com
.owncloud
.android
.operations
;
3 import org
.apache
.http
.HttpStatus
;
4 import org
.apache
.jackrabbit
.webdav
.MultiStatus
;
5 import org
.apache
.jackrabbit
.webdav
.client
.methods
.PropFindMethod
;
7 import android
.accounts
.Account
;
8 import android
.content
.Context
;
9 import android
.util
.Log
;
11 import com
.owncloud
.android
.datamodel
.DataStorageManager
;
12 import com
.owncloud
.android
.datamodel
.OCFile
;
14 import eu
.alefzero
.webdav
.WebdavClient
;
15 import eu
.alefzero
.webdav
.WebdavEntry
;
16 import eu
.alefzero
.webdav
.WebdavUtils
;
18 public class SynchronizeFileOperation
extends RemoteOperation
{
20 private String TAG
= SynchronizeFileOperation
.class.getSimpleName();
22 private String mRemotePath
;
24 private DataStorageManager mStorageManager
;
26 private Account mAccount
;
28 public SynchronizeFileOperation(
30 DataStorageManager dataStorageManager
,
33 mRemotePath
= remotePath
;
34 mStorageManager
= dataStorageManager
;
39 protected RemoteOperationResult
run(WebdavClient client
) {
40 PropFindMethod propfind
= null
;
41 RemoteOperationResult result
= null
;
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
;
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
;
59 client
.exhaustResponse(propfind
.getResponseBodyAsStream());
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());
71 propfind
.releaseConnection();
77 * Creates and populates a new {@link OCFile} object with the data read from the server.
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.
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());