a73bad0cf663acc2ec3c291650932e5f4dcaa2ff
[pub/Android/ownCloud.git] / oc_framework / src / com / owncloud / android / oc_framework / operations / remote / ReadRemoteFileOperation.java
1 package com.owncloud.android.oc_framework.operations.remote;
2
3 import org.apache.http.HttpStatus;
4 import org.apache.jackrabbit.webdav.DavConstants;
5 import org.apache.jackrabbit.webdav.MultiStatus;
6 import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
7
8 import android.util.Log;
9
10 import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
11 import com.owncloud.android.oc_framework.network.webdav.WebdavUtils;
12 import com.owncloud.android.oc_framework.operations.RemoteOperation;
13 import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
14
15 /**
16 * Remote operation performing the read of remote file or folder in the ownCloud server.
17 *
18 * @author David A. Velasco
19 * @author masensio
20 */
21
22 public class ReadRemoteFileOperation extends RemoteOperation {
23
24 private static final String TAG = ReadRemoteFileOperation.class.getSimpleName();
25
26 private String mRemotePath;
27 private MultiStatus mDataInServer;
28
29 public MultiStatus getDataInServer() {
30 return mDataInServer;
31 }
32
33
34 /**
35 * Constructor
36 *
37 * @param remotePath Remote path of the file.
38 */
39 public ReadRemoteFileOperation(String remotePath) {
40 mRemotePath = remotePath;
41 mDataInServer = null;
42 }
43
44 /**
45 * Performs the read operation.
46 *
47 * @param client Client object to communicate with the remote ownCloud server.
48 */
49 @Override
50 protected RemoteOperationResult run(WebdavClient client) {
51 RemoteOperationResult result = null;
52 PropFindMethod query = null;
53
54 try {
55 // remote request
56 query = new PropFindMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath),
57 DavConstants.PROPFIND_ALL_PROP,
58 DavConstants.DEPTH_1);
59 int status = client.executeMethod(query);
60
61 // check and process response
62 if (isMultiStatus(status)) {
63 // get data from remote folder
64 mDataInServer = query.getResponseBodyAsMultiStatus();
65 result = new RemoteOperationResult(true, status, query.getResponseHeaders());
66 } else {
67 // synchronization failed
68 client.exhaustResponse(query.getResponseBodyAsStream());
69 result = new RemoteOperationResult(false, status, query.getResponseHeaders());
70 }
71
72 } catch (Exception e) {
73 result = new RemoteOperationResult(e);
74
75
76 } finally {
77 if (query != null)
78 query.releaseConnection(); // let the connection available for other methods
79 if (result.isSuccess()) {
80 Log.i(TAG, "Synchronized " + mRemotePath + ": " + result.getLogMessage());
81 } else {
82 if (result.isException()) {
83 Log.e(TAG, "Synchronized " + mRemotePath + ": " + result.getLogMessage(), result.getException());
84 } else {
85 Log.e(TAG, "Synchronized " + mRemotePath + ": " + result.getLogMessage());
86 }
87 }
88
89 }
90 return result;
91 }
92
93 public boolean isMultiStatus(int status) {
94 return (status == HttpStatus.SC_MULTI_STATUS);
95 }
96
97 }