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