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 RemoteFile mFolder
;
32 private ArrayList
<RemoteFile
> mFiles
;
37 * @param remotePath Remote path of the file.
39 public ReadRemoteFolderOperation(String remotePath
) {
40 mRemotePath
= remotePath
;
44 * Performs the read operation.
46 * @param client Client object to communicate with the remote ownCloud server.
49 protected RemoteOperationResult
run(WebdavClient client
) {
50 RemoteOperationResult result
= null
;
51 PropFindMethod query
= null
;
55 query
= new PropFindMethod(client
.getBaseUri() + WebdavUtils
.encodePath(mRemotePath
),
56 DavConstants
.PROPFIND_ALL_PROP
,
57 DavConstants
.DEPTH_1
);
58 int status
= client
.executeMethod(query
);
60 // check and process response
61 if (isMultiStatus(status
)) {
62 // get data from remote folder
63 MultiStatus dataInServer
= query
.getResponseBodyAsMultiStatus();
64 readData(dataInServer
, client
);
66 // Result of the operation
67 result
= new RemoteOperationResult(true
, status
, query
.getResponseHeaders());
68 // Add data to the result
69 if (result
.isSuccess()) {
70 result
.setFile(mFolder
);
71 result
.setData(mFiles
);
74 // synchronization failed
75 client
.exhaustResponse(query
.getResponseBodyAsStream());
76 result
= new RemoteOperationResult(false
, status
, query
.getResponseHeaders());
79 } catch (Exception e
) {
80 result
= new RemoteOperationResult(e
);
85 query
.releaseConnection(); // let the connection available for other methods
86 if (result
.isSuccess()) {
87 Log
.i(TAG
, "Synchronized " + mRemotePath
+ ": " + result
.getLogMessage());
89 if (result
.isException()) {
90 Log
.e(TAG
, "Synchronized " + mRemotePath
+ ": " + result
.getLogMessage(), result
.getException());
92 Log
.e(TAG
, "Synchronized " + mRemotePath
+ ": " + result
.getLogMessage());
100 public boolean isMultiStatus(int status
) {
101 return (status
== HttpStatus
.SC_MULTI_STATUS
);
105 * Read the data retrieved from the server about the contents of the target folder
108 * @param dataInServer Full response got from the server with the data of the target
109 * folder and its direct children.
110 * @param client Client instance to the remote server where the data were
114 private void readData(MultiStatus dataInServer
, WebdavClient client
) {
115 // parse data from remote folder
116 WebdavEntry we
= new WebdavEntry(dataInServer
.getResponses()[0], client
.getBaseUri().getPath());
117 mFolder
= fillOCFile(we
);
120 // loop to update every child
121 RemoteFile remoteFile
= null
;
122 mFiles
= new ArrayList
<RemoteFile
>();
123 for (int i
= 1; i
< dataInServer
.getResponses().length
; ++i
) {
124 /// new OCFile instance with the data from the server
125 we
= new WebdavEntry(dataInServer
.getResponses()[i
], client
.getBaseUri().getPath());
126 remoteFile
= fillOCFile(we
);
128 mFiles
.add(remoteFile
);
134 * Creates and populates a new {@link RemoteFile} object with the data read from the server.
136 * @param we WebDAV entry read from the server for a WebDAV resource (remote file or folder).
137 * @return New OCFile instance representing the remote resource described by we.
139 private RemoteFile
fillOCFile(WebdavEntry we
) {
140 RemoteFile file
= new RemoteFile(we
.decodedPath());
141 file
.setCreationTimestamp(we
.createTimestamp());
142 file
.setLength(we
.contentLength());
143 file
.setMimeType(we
.contentType());
144 file
.setModifiedTimestamp(we
.modifiedTimestamp());
145 file
.setEtag(we
.etag());