fb03449c9616d8673eaee1dc483bd6482a3febd3
[pub/Android/ownCloud.git] / oc_framework / src / com / owncloud / android / oc_framework / operations / remote / ReadRemoteFolderOperation.java
1 /* ownCloud Android Library is available under MIT license
2 * Copyright (C) 2014 ownCloud (http://www.owncloud.org/)
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 *
23 */
24
25 package com.owncloud.android.oc_framework.operations.remote;
26
27 import java.util.ArrayList;
28
29 import org.apache.http.HttpStatus;
30 import org.apache.jackrabbit.webdav.DavConstants;
31 import org.apache.jackrabbit.webdav.MultiStatus;
32 import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
33
34 import android.util.Log;
35
36 import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
37 import com.owncloud.android.oc_framework.network.webdav.WebdavEntry;
38 import com.owncloud.android.oc_framework.network.webdav.WebdavUtils;
39 import com.owncloud.android.oc_framework.operations.RemoteFile;
40 import com.owncloud.android.oc_framework.operations.RemoteOperation;
41 import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
42
43 /**
44 * Remote operation performing the read of remote file or folder in the ownCloud server.
45 *
46 * @author David A. Velasco
47 * @author masensio
48 */
49
50 public class ReadRemoteFolderOperation extends RemoteOperation {
51
52 private static final String TAG = ReadRemoteFolderOperation.class.getSimpleName();
53
54 private String mRemotePath;
55 private ArrayList<RemoteFile> mFolderAndFiles;
56
57 /**
58 * Constructor
59 *
60 * @param remotePath Remote path of the file.
61 */
62 public ReadRemoteFolderOperation(String remotePath) {
63 mRemotePath = remotePath;
64 }
65
66 /**
67 * Performs the read operation.
68 *
69 * @param client Client object to communicate with the remote ownCloud server.
70 */
71 @Override
72 protected RemoteOperationResult run(WebdavClient client) {
73 RemoteOperationResult result = null;
74 PropFindMethod query = null;
75
76 try {
77 // remote request
78 query = new PropFindMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath),
79 DavConstants.PROPFIND_ALL_PROP,
80 DavConstants.DEPTH_1);
81 int status = client.executeMethod(query);
82
83 // check and process response
84 if (isMultiStatus(status)) {
85 // get data from remote folder
86 MultiStatus dataInServer = query.getResponseBodyAsMultiStatus();
87 readData(dataInServer, client);
88
89 // Result of the operation
90 result = new RemoteOperationResult(true, status, query.getResponseHeaders());
91 // Add data to the result
92 if (result.isSuccess()) {
93 result.setData(mFolderAndFiles);
94 }
95 } else {
96 // synchronization failed
97 client.exhaustResponse(query.getResponseBodyAsStream());
98 result = new RemoteOperationResult(false, status, query.getResponseHeaders());
99 }
100
101 } catch (Exception e) {
102 result = new RemoteOperationResult(e);
103
104
105 } finally {
106 if (query != null)
107 query.releaseConnection(); // let the connection available for other methods
108 if (result.isSuccess()) {
109 Log.i(TAG, "Synchronized " + mRemotePath + ": " + result.getLogMessage());
110 } else {
111 if (result.isException()) {
112 Log.e(TAG, "Synchronized " + mRemotePath + ": " + result.getLogMessage(), result.getException());
113 } else {
114 Log.e(TAG, "Synchronized " + mRemotePath + ": " + result.getLogMessage());
115 }
116 }
117
118 }
119 return result;
120 }
121
122 public boolean isMultiStatus(int status) {
123 return (status == HttpStatus.SC_MULTI_STATUS);
124 }
125
126 /**
127 * Read the data retrieved from the server about the contents of the target folder
128 *
129 *
130 * @param dataInServer Full response got from the server with the data of the target
131 * folder and its direct children.
132 * @param client Client instance to the remote server where the data were
133 * retrieved.
134 * @return
135 */
136 private void readData(MultiStatus dataInServer, WebdavClient client) {
137 mFolderAndFiles = new ArrayList<RemoteFile>();
138
139 // parse data from remote folder
140 WebdavEntry we = new WebdavEntry(dataInServer.getResponses()[0], client.getBaseUri().getPath());
141 mFolderAndFiles.add(fillOCFile(we));
142
143 // loop to update every child
144 RemoteFile remoteFile = null;
145 for (int i = 1; i < dataInServer.getResponses().length; ++i) {
146 /// new OCFile instance with the data from the server
147 we = new WebdavEntry(dataInServer.getResponses()[i], client.getBaseUri().getPath());
148 remoteFile = fillOCFile(we);
149 mFolderAndFiles.add(remoteFile);
150 }
151
152 }
153
154 /**
155 * Creates and populates a new {@link RemoteFile} object with the data read from the server.
156 *
157 * @param we WebDAV entry read from the server for a WebDAV resource (remote file or folder).
158 * @return New OCFile instance representing the remote resource described by we.
159 */
160 private RemoteFile fillOCFile(WebdavEntry we) {
161 RemoteFile file = new RemoteFile(we.decodedPath());
162 file.setCreationTimestamp(we.createTimestamp());
163 file.setLength(we.contentLength());
164 file.setMimeType(we.contentType());
165 file.setModifiedTimestamp(we.modifiedTimestamp());
166 file.setEtag(we.etag());
167 return file;
168 }
169 }