1 /* ownCloud webDAV Library for Android is available under MIT license
2 * Copyright (C) 2014 ownCloud (http://www.owncloud.org/)
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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
24 package com
.owncloud
.android
.oc_framework
.operations
.remote
;
26 import java
.util
.ArrayList
;
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
;
33 import android
.util
.Log
;
35 import com
.owncloud
.android
.oc_framework
.network
.webdav
.WebdavClient
;
36 import com
.owncloud
.android
.oc_framework
.network
.webdav
.WebdavEntry
;
37 import com
.owncloud
.android
.oc_framework
.network
.webdav
.WebdavUtils
;
38 import com
.owncloud
.android
.oc_framework
.operations
.RemoteFile
;
39 import com
.owncloud
.android
.oc_framework
.operations
.RemoteOperation
;
40 import com
.owncloud
.android
.oc_framework
.operations
.RemoteOperationResult
;
44 * Remote operation performing the read a file from the ownCloud server.
46 * @author David A. Velasco
50 public class ReadRemoteFileOperation
extends RemoteOperation
{
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;
56 private String mRemotePath
;
62 * @param remotePath Remote path of the file.
64 public ReadRemoteFileOperation(String remotePath
) {
65 mRemotePath
= remotePath
;
69 * Performs the read operation.
71 * @param client Client object to communicate with the remote ownCloud server.
74 protected RemoteOperationResult
run(WebdavClient client
) {
75 PropFindMethod propfind
= null
;
76 RemoteOperationResult result
= null
;
78 /// take the duty of check the server for the current state of the file there
80 propfind
= new PropFindMethod(client
.getBaseUri() + WebdavUtils
.encodePath(mRemotePath
),
81 DavConstants
.PROPFIND_ALL_PROP
,
82 DavConstants
.DEPTH_0
);
84 status
= client
.executeMethod(propfind
, SYNC_READ_TIMEOUT
, SYNC_CONNECTION_TIMEOUT
);
86 boolean isMultiStatus
= status
== HttpStatus
.SC_MULTI_STATUS
;
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
);
95 // Result of the operation
96 result
= new RemoteOperationResult(true
, status
, propfind
.getResponseHeaders());
97 result
.setData(files
);
100 client
.exhaustResponse(propfind
.getResponseBodyAsStream());
101 result
= new RemoteOperationResult(false
, status
, propfind
.getResponseHeaders());
104 } catch (Exception e
) {
105 result
= new RemoteOperationResult(e
);
107 Log
.e(TAG
, "Synchronizing file " + mRemotePath
+ ": " + result
.getLogMessage(), result
.getException());
109 if (propfind
!= null
)
110 propfind
.releaseConnection();