c6e013fef46194e2330c91277eb4d5d4f54e2d36
[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.WebdavEntry;
12 import com.owncloud.android.oc_framework.network.webdav.WebdavUtils;
13 import com.owncloud.android.oc_framework.operations.RemoteOperation;
14 import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
15
16 public class ReadRemoteFileOperation extends RemoteOperation {
17
18 private static final String TAG = ReadRemoteFileOperation.class.getSimpleName();
19
20 private String mRemotePath;
21 private WebdavEntry mWe;
22
23 public WebdavEntry getWEntry() {
24 return mWe;
25 }
26
27 public ReadRemoteFileOperation(String remotePath) {
28 mRemotePath = remotePath;
29 }
30
31 @Override
32 protected RemoteOperationResult run(WebdavClient client) {
33 RemoteOperationResult result = null;
34 PropFindMethod query = null;
35
36 try {
37 // remote request
38 query = new PropFindMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath),
39 DavConstants.PROPFIND_ALL_PROP,
40 DavConstants.DEPTH_1);
41 int status = client.executeMethod(query);
42
43 // check and process response
44 if (isMultiStatus(status)) {
45 MultiStatus dataInServer = query.getResponseBodyAsMultiStatus();
46 // parse data from remote folder
47 mWe = new WebdavEntry(dataInServer.getResponses()[0], client.getBaseUri().getPath());
48 result = new RemoteOperationResult(true, status, query.getResponseHeaders());
49 } else {
50 // synchronization failed
51 client.exhaustResponse(query.getResponseBodyAsStream());
52 result = new RemoteOperationResult(false, status, query.getResponseHeaders());
53 }
54
55 } catch (Exception e) {
56 result = new RemoteOperationResult(e);
57
58
59 } finally {
60 if (query != null)
61 query.releaseConnection(); // let the connection available for other methods
62 if (result.isSuccess()) {
63 Log.i(TAG, "Synchronized " + mRemotePath + ": " + result.getLogMessage());
64 } else {
65 if (result.isException()) {
66 Log.e(TAG, "Synchronized " + mRemotePath + ": " + result.getLogMessage(), result.getException());
67 } else {
68 Log.e(TAG, "Synchronized " + mRemotePath + ": " + result.getLogMessage());
69 }
70 }
71
72 }
73 return result;
74 }
75
76 public boolean isMultiStatus(int status) {
77 return (status == HttpStatus.SC_MULTI_STATUS);
78 }
79
80 }