7bbf7b7f914bcf10c6e44d15d226de9b08ae768e
[pub/Android/ownCloud.git] / oc_framework / src / com / owncloud / android / oc_framework / operations / remote / ReadRemoteFolderOperation.java
1 package com.owncloud.android.oc_framework.operations.remote;
2
3 import java.util.ArrayList;
4
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;
9
10 import android.util.Log;
11
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;
18
19 /**
20 * Remote operation performing the read of remote file or folder in the ownCloud server.
21 *
22 * @author David A. Velasco
23 * @author masensio
24 */
25
26 public class ReadRemoteFolderOperation extends RemoteOperation {
27
28 private static final String TAG = ReadRemoteFolderOperation.class.getSimpleName();
29
30 private String mRemotePath;
31 private ArrayList<RemoteFile> mFolderAndFiles;
32
33 /**
34 * Constructor
35 *
36 * @param remotePath Remote path of the file.
37 */
38 public ReadRemoteFolderOperation(String remotePath) {
39 mRemotePath = remotePath;
40 }
41
42 /**
43 * Performs the read operation.
44 *
45 * @param client Client object to communicate with the remote ownCloud server.
46 */
47 @Override
48 protected RemoteOperationResult run(WebdavClient client) {
49 RemoteOperationResult result = null;
50 PropFindMethod query = null;
51
52 try {
53 // remote request
54 query = new PropFindMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath),
55 DavConstants.PROPFIND_ALL_PROP,
56 DavConstants.DEPTH_1);
57 int status = client.executeMethod(query);
58
59 // check and process response
60 if (isMultiStatus(status)) {
61 // get data from remote folder
62 MultiStatus dataInServer = query.getResponseBodyAsMultiStatus();
63 readData(dataInServer, client);
64
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);
70 }
71 } else {
72 // synchronization failed
73 client.exhaustResponse(query.getResponseBodyAsStream());
74 result = new RemoteOperationResult(false, status, query.getResponseHeaders());
75 }
76
77 } catch (Exception e) {
78 result = new RemoteOperationResult(e);
79
80
81 } finally {
82 if (query != null)
83 query.releaseConnection(); // let the connection available for other methods
84 if (result.isSuccess()) {
85 Log.i(TAG, "Synchronized " + mRemotePath + ": " + result.getLogMessage());
86 } else {
87 if (result.isException()) {
88 Log.e(TAG, "Synchronized " + mRemotePath + ": " + result.getLogMessage(), result.getException());
89 } else {
90 Log.e(TAG, "Synchronized " + mRemotePath + ": " + result.getLogMessage());
91 }
92 }
93
94 }
95 return result;
96 }
97
98 public boolean isMultiStatus(int status) {
99 return (status == HttpStatus.SC_MULTI_STATUS);
100 }
101
102 /**
103 * Read the data retrieved from the server about the contents of the target folder
104 *
105 *
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
109 * retrieved.
110 * @return
111 */
112 private void readData(MultiStatus dataInServer, WebdavClient client) {
113 mFolderAndFiles = new ArrayList<RemoteFile>();
114
115 // parse data from remote folder
116 WebdavEntry we = new WebdavEntry(dataInServer.getResponses()[0], client.getBaseUri().getPath());
117 mFolderAndFiles.add(fillOCFile(we));
118
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);
126 }
127
128 }
129
130 /**
131 * Creates and populates a new {@link RemoteFile} object with the data read from the server.
132 *
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.
135 */
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());
143 return file;
144 }
145 }