1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
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.
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.
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/>.
17 package com
.owncloud
.android
.oc_framework
.operations
.remote
;
19 import java
.util
.ArrayList
;
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
;
26 import android
.util
.Log
;
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
;
37 * Remote operation performing the read a file from the ownCloud server.
39 * @author David A. Velasco
43 public class ReadRemoteFileOperation
extends RemoteOperation
{
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;
49 private String mRemotePath
;
55 * @param remotePath Remote path of the file.
57 public ReadRemoteFileOperation(String remotePath
) {
58 mRemotePath
= remotePath
;
62 * Performs the read operation.
64 * @param client Client object to communicate with the remote ownCloud server.
67 protected RemoteOperationResult
run(WebdavClient client
) {
68 PropFindMethod propfind
= null
;
69 RemoteOperationResult result
= null
;
71 /// take the duty of check the server for the current state of the file there
73 propfind
= new PropFindMethod(client
.getBaseUri() + WebdavUtils
.encodePath(mRemotePath
),
74 DavConstants
.PROPFIND_ALL_PROP
,
75 DavConstants
.DEPTH_0
);
77 status
= client
.executeMethod(propfind
, SYNC_READ_TIMEOUT
, SYNC_CONNECTION_TIMEOUT
);
79 boolean isMultiStatus
= status
== HttpStatus
.SC_MULTI_STATUS
;
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
);
88 // Result of the operation
89 result
= new RemoteOperationResult(true
, status
, propfind
.getResponseHeaders());
90 result
.setData(files
);
93 client
.exhaustResponse(propfind
.getResponseBodyAsStream());
94 result
= new RemoteOperationResult(false
, status
, propfind
.getResponseHeaders());
97 } catch (Exception e
) {
98 result
= new RemoteOperationResult(e
);
100 Log
.e(TAG
, "Synchronizing file " + mRemotePath
+ ": " + result
.getLogMessage(), result
.getException());
102 if (propfind
!= null
)
103 propfind
.releaseConnection();