6cdacad800a8749d1f2413a22cce44db850f7b38
[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
35
36 /**
37 * Remote operation performing the read a file from the ownCloud server.
38 *
39 * @author David A. Velasco
40 * @author masensio
41 */
42
43 public class ReadRemoteFileOperation extends RemoteOperation {
44
45 private static final String TAG = ReadRemoteFileOperation.class.getSimpleName();
46 private static final int SYNC_READ_TIMEOUT = 10000;
47 private static final int SYNC_CONNECTION_TIMEOUT = 5000;
48
49 private String mRemotePath;
50
51
52 /**
53 * Constructor
54 *
55 * @param remotePath Remote path of the file.
56 */
57 public ReadRemoteFileOperation(String remotePath) {
58 mRemotePath = remotePath;
59 }
60
61 /**
62 * Performs the read operation.
63 *
64 * @param client Client object to communicate with the remote ownCloud server.
65 */
66 @Override
67 protected RemoteOperationResult run(WebdavClient client) {
68 PropFindMethod propfind = null;
69 RemoteOperationResult result = null;
70
71 /// take the duty of check the server for the current state of the file there
72 try {
73 propfind = new PropFindMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath),
74 DavConstants.PROPFIND_ALL_PROP,
75 DavConstants.DEPTH_0);
76 int status;
77 status = client.executeMethod(propfind, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT);
78
79 boolean isMultiStatus = status == HttpStatus.SC_MULTI_STATUS;
80 if (isMultiStatus) {
81 // Parse response
82 MultiStatus resp = propfind.getResponseBodyAsMultiStatus();
83 WebdavEntry we = new WebdavEntry(resp.getResponses()[0], client.getBaseUri().getPath());
84 RemoteFile remoteFile = new RemoteFile(we);
85 ArrayList<RemoteFile> files = new ArrayList<RemoteFile>();
86 files.add(remoteFile);
87
88 // Result of the operation
89 result = new RemoteOperationResult(true, status, propfind.getResponseHeaders());
90 result.setData(files);
91
92 } else {
93 client.exhaustResponse(propfind.getResponseBodyAsStream());
94 result = new RemoteOperationResult(false, status, propfind.getResponseHeaders());
95 }
96
97 } catch (Exception e) {
98 result = new RemoteOperationResult(e);
99 e.printStackTrace();
100 Log.e(TAG, "Synchronizing file " + mRemotePath + ": " + result.getLogMessage(), result.getException());
101 } finally {
102 if (propfind != null)
103 propfind.releaseConnection();
104 }
105 return result;
106 }
107
108 }