7e623769ea87752c4f24d79b33b1a47c3b082207
[pub/Android/ownCloud.git] / oc_framework / src / com / owncloud / android / oc_framework / operations / remote / ReadRemoteFileOperation.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17 package com.owncloud.android.oc_framework.operations.remote;
18
19 import java.util.ArrayList;
20
21 import org.apache.http.HttpStatus;
22 import org.apache.jackrabbit.webdav.DavConstants;
23 import org.apache.jackrabbit.webdav.MultiStatus;
24 import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
25
26 import android.util.Log;
27
28 import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
29 import com.owncloud.android.oc_framework.network.webdav.WebdavEntry;
30 import com.owncloud.android.oc_framework.network.webdav.WebdavUtils;
31 import com.owncloud.android.oc_framework.operations.RemoteFile;
32 import com.owncloud.android.oc_framework.operations.RemoteOperation;
33 import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
34 import com.owncloud.android.oc_framework.utils.FileUtils;
35
36
37 /**
38 * Remote operation performing the read a file from the ownCloud server.
39 *
40 * @author David A. Velasco
41 * @author masensio
42 */
43
44 public class ReadRemoteFileOperation extends RemoteOperation {
45
46 private static final String TAG = ReadRemoteFileOperation.class.getSimpleName();
47 private static final int SYNC_READ_TIMEOUT = 10000;
48 private static final int SYNC_CONNECTION_TIMEOUT = 5000;
49
50 private String mRemotePath;
51
52
53 /**
54 * Constructor
55 *
56 * @param remotePath Remote path of the file.
57 */
58 public ReadRemoteFileOperation(String remotePath) {
59 mRemotePath = remotePath;
60 }
61
62 /**
63 * Performs the read operation.
64 *
65 * @param client Client object to communicate with the remote ownCloud server.
66 */
67 @Override
68 protected RemoteOperationResult run(WebdavClient client) {
69 PropFindMethod propfind = null;
70 RemoteOperationResult result = null;
71
72 /// take the duty of check the server for the current state of the file there
73 try {
74 propfind = new PropFindMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath),
75 DavConstants.PROPFIND_ALL_PROP,
76 DavConstants.DEPTH_0);
77 int status;
78 status = client.executeMethod(propfind, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT);
79
80 boolean isMultiStatus = status == HttpStatus.SC_MULTI_STATUS;
81 if (isMultiStatus) {
82 MultiStatus resp = propfind.getResponseBodyAsMultiStatus();
83 // Result of the operation
84 result = new RemoteOperationResult(true, status, propfind.getResponseHeaders());
85
86 // Add data to the result
87 if (result.isSuccess()) {
88 WebdavEntry we = new WebdavEntry(resp.getResponses()[0], client.getBaseUri().getPath());
89 RemoteFile remoteFile = FileUtils.fillOCFile(we);
90 ArrayList<RemoteFile> files = new ArrayList<RemoteFile>();
91 files.add(remoteFile);
92 result.setData(files);
93 }
94
95 } else {
96 client.exhaustResponse(propfind.getResponseBodyAsStream());
97 result = new RemoteOperationResult(false, status, propfind.getResponseHeaders());
98 }
99
100 } catch (Exception e) {
101 result = new RemoteOperationResult(e);
102 e.printStackTrace();
103 Log.e(TAG, "Synchronizing file " + mRemotePath + ": " + result.getLogMessage(), result.getException());
104 } finally {
105 if (propfind != null)
106 propfind.releaseConnection();
107 }
108 return result;
109 }
110
111 }