1 package com
.owncloud
.android
.oc_framework
.operations
.remote
;
3 import java
.util
.ArrayList
;
5 import org
.apache
.http
.HttpStatus
;
6 import org
.apache
.jackrabbit
.webdav
.DavConstants
;
7 import org
.apache
.jackrabbit
.webdav
.MultiStatus
;
8 import org
.apache
.jackrabbit
.webdav
.client
.methods
.PropFindMethod
;
10 import android
.util
.Log
;
12 import com
.owncloud
.android
.oc_framework
.network
.webdav
.WebdavClient
;
13 import com
.owncloud
.android
.oc_framework
.network
.webdav
.WebdavEntry
;
14 import com
.owncloud
.android
.oc_framework
.network
.webdav
.WebdavUtils
;
15 import com
.owncloud
.android
.oc_framework
.operations
.RemoteFile
;
16 import com
.owncloud
.android
.oc_framework
.operations
.RemoteOperation
;
17 import com
.owncloud
.android
.oc_framework
.operations
.RemoteOperationResult
;
20 * Remote operation performing the read of remote file or folder in the ownCloud server.
22 * @author David A. Velasco
26 public class ReadRemoteFolderOperation
extends RemoteOperation
{
28 private static final String TAG
= ReadRemoteFolderOperation
.class.getSimpleName();
30 private String mRemotePath
;
31 private ArrayList
<RemoteFile
> mFolderAndFiles
;
36 * @param remotePath Remote path of the file.
38 public ReadRemoteFolderOperation(String remotePath
) {
39 mRemotePath
= remotePath
;
43 * Performs the read operation.
45 * @param client Client object to communicate with the remote ownCloud server.
48 protected RemoteOperationResult
run(WebdavClient client
) {
49 RemoteOperationResult result
= null
;
50 PropFindMethod query
= null
;
54 query
= new PropFindMethod(client
.getBaseUri() + WebdavUtils
.encodePath(mRemotePath
),
55 DavConstants
.PROPFIND_ALL_PROP
,
56 DavConstants
.DEPTH_1
);
57 int status
= client
.executeMethod(query
);
59 // check and process response
60 if (isMultiStatus(status
)) {
61 // get data from remote folder
62 MultiStatus dataInServer
= query
.getResponseBodyAsMultiStatus();
63 readData(dataInServer
, client
);
65 // Result of the operation
66 result
= new RemoteOperationResult(true
, status
, query
.getResponseHeaders());
67 // Add data to the result
68 if (result
.isSuccess()) {
69 result
.setData(mFolderAndFiles
);
72 // synchronization failed
73 client
.exhaustResponse(query
.getResponseBodyAsStream());
74 result
= new RemoteOperationResult(false
, status
, query
.getResponseHeaders());
77 } catch (Exception e
) {
78 result
= new RemoteOperationResult(e
);
83 query
.releaseConnection(); // let the connection available for other methods
84 if (result
.isSuccess()) {
85 Log
.i(TAG
, "Synchronized " + mRemotePath
+ ": " + result
.getLogMessage());
87 if (result
.isException()) {
88 Log
.e(TAG
, "Synchronized " + mRemotePath
+ ": " + result
.getLogMessage(), result
.getException());
90 Log
.e(TAG
, "Synchronized " + mRemotePath
+ ": " + result
.getLogMessage());
98 public boolean isMultiStatus(int status
) {
99 return (status
== HttpStatus
.SC_MULTI_STATUS
);
103 * Read the data retrieved from the server about the contents of the target folder
106 * @param dataInServer Full response got from the server with the data of the target
107 * folder and its direct children.
108 * @param client Client instance to the remote server where the data were
112 private void readData(MultiStatus dataInServer
, WebdavClient client
) {
113 mFolderAndFiles
= new ArrayList
<RemoteFile
>();
115 // parse data from remote folder
116 WebdavEntry we
= new WebdavEntry(dataInServer
.getResponses()[0], client
.getBaseUri().getPath());
117 mFolderAndFiles
.add(fillOCFile(we
));
119 // loop to update every child
120 RemoteFile remoteFile
= null
;
121 for (int i
= 1; i
< dataInServer
.getResponses().length
; ++i
) {
122 /// new OCFile instance with the data from the server
123 we
= new WebdavEntry(dataInServer
.getResponses()[i
], client
.getBaseUri().getPath());
124 remoteFile
= fillOCFile(we
);
125 mFolderAndFiles
.add(remoteFile
);
131 * Creates and populates a new {@link RemoteFile} object with the data read from the server.
133 * @param we WebDAV entry read from the server for a WebDAV resource (remote file or folder).
134 * @return New OCFile instance representing the remote resource described by we.
136 private RemoteFile
fillOCFile(WebdavEntry we
) {
137 RemoteFile file
= new RemoteFile(we
.decodedPath());
138 file
.setCreationTimestamp(we
.createTimestamp());
139 file
.setLength(we
.contentLength());
140 file
.setMimeType(we
.contentType());
141 file
.setModifiedTimestamp(we
.modifiedTimestamp());
142 file
.setEtag(we
.etag());