1 /* ownCloud Android Library 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 
  25 package com
.owncloud
.android
.oc_framework
.operations
.remote
; 
  27 import java
.util
.ArrayList
; 
  29 import org
.apache
.http
.HttpStatus
; 
  30 import org
.apache
.jackrabbit
.webdav
.DavConstants
; 
  31 import org
.apache
.jackrabbit
.webdav
.MultiStatus
; 
  32 import org
.apache
.jackrabbit
.webdav
.client
.methods
.PropFindMethod
; 
  34 import android
.util
.Log
; 
  36 import com
.owncloud
.android
.oc_framework
.network
.webdav
.WebdavClient
; 
  37 import com
.owncloud
.android
.oc_framework
.network
.webdav
.WebdavEntry
; 
  38 import com
.owncloud
.android
.oc_framework
.network
.webdav
.WebdavUtils
; 
  39 import com
.owncloud
.android
.oc_framework
.operations
.RemoteFile
; 
  40 import com
.owncloud
.android
.oc_framework
.operations
.RemoteOperation
; 
  41 import com
.owncloud
.android
.oc_framework
.operations
.RemoteOperationResult
; 
  44  * Remote operation performing the read of remote file or folder in the ownCloud server. 
  46  * @author David A. Velasco 
  50 public class ReadRemoteFolderOperation 
extends RemoteOperation 
{ 
  52         private static final String TAG 
= ReadRemoteFolderOperation
.class.getSimpleName(); 
  54         private String mRemotePath
; 
  55         private ArrayList
<RemoteFile
> mFolderAndFiles
; 
  60      * @param remotePath                Remote path of the file.  
  62         public ReadRemoteFolderOperation(String remotePath
) { 
  63                 mRemotePath 
= remotePath
; 
  67      * Performs the read operation. 
  69      * @param   client      Client object to communicate with the remote ownCloud server. 
  72         protected RemoteOperationResult 
run(WebdavClient client
) { 
  73                 RemoteOperationResult result 
= null
; 
  74         PropFindMethod query 
= null
; 
  78             query 
= new PropFindMethod(client
.getBaseUri() + WebdavUtils
.encodePath(mRemotePath
), 
  79                     DavConstants
.PROPFIND_ALL_PROP
, 
  80                     DavConstants
.DEPTH_1
); 
  81             int status 
= client
.executeMethod(query
); 
  83             // check and process response 
  84             if (isMultiStatus(status
)) { 
  85                 // get data from remote folder  
  86                 MultiStatus dataInServer 
= query
.getResponseBodyAsMultiStatus(); 
  87                 readData(dataInServer
, client
); 
  89                 // Result of the operation 
  90                 result 
= new RemoteOperationResult(true
, status
, query
.getResponseHeaders()); 
  91                 // Add data to the result 
  92                 if (result
.isSuccess()) { 
  93                         result
.setData(mFolderAndFiles
); 
  96                 // synchronization failed 
  97                 client
.exhaustResponse(query
.getResponseBodyAsStream()); 
  98                 result 
= new RemoteOperationResult(false
, status
, query
.getResponseHeaders()); 
 101         } catch (Exception e
) { 
 102             result 
= new RemoteOperationResult(e
); 
 107                 query
.releaseConnection();  // let the connection available for other methods 
 108             if (result
.isSuccess()) { 
 109                 Log
.i(TAG
, "Synchronized "  + mRemotePath 
+ ": " + result
.getLogMessage()); 
 111                 if (result
.isException()) { 
 112                     Log
.e(TAG
, "Synchronized " + mRemotePath  
+ ": " + result
.getLogMessage(), result
.getException()); 
 114                     Log
.e(TAG
, "Synchronized " + mRemotePath 
+ ": " + result
.getLogMessage()); 
 122     public boolean isMultiStatus(int status
) { 
 123         return (status 
== HttpStatus
.SC_MULTI_STATUS
);  
 127      *  Read the data retrieved from the server about the contents of the target folder  
 130      *  @param dataInServer     Full response got from the server with the data of the target  
 131      *                          folder and its direct children. 
 132      *  @param client           Client instance to the remote server where the data were  
 136     private void readData(MultiStatus dataInServer
, WebdavClient client
) {       
 137         mFolderAndFiles 
= new ArrayList
<RemoteFile
>(); 
 139         // parse data from remote folder  
 140         WebdavEntry we 
= new WebdavEntry(dataInServer
.getResponses()[0], client
.getBaseUri().getPath()); 
 141         mFolderAndFiles
.add(fillOCFile(we
)); 
 143         // loop to update every child 
 144         RemoteFile remoteFile 
= null
; 
 145         for (int i 
= 1; i 
< dataInServer
.getResponses().length
; ++i
) { 
 146             /// new OCFile instance with the data from the server 
 147             we 
= new WebdavEntry(dataInServer
.getResponses()[i
], client
.getBaseUri().getPath());                         
 148             remoteFile 
= fillOCFile(we
); 
 149             mFolderAndFiles
.add(remoteFile
); 
 155      * Creates and populates a new {@link RemoteFile} object with the data read from the server. 
 157      * @param we        WebDAV entry read from the server for a WebDAV resource (remote file or folder). 
 158      * @return          New OCFile instance representing the remote resource described by we. 
 160     private RemoteFile 
fillOCFile(WebdavEntry we
) { 
 161         RemoteFile file 
= new RemoteFile(we
.decodedPath()); 
 162         file
.setCreationTimestamp(we
.createTimestamp()); 
 163         file
.setLength(we
.contentLength()); 
 164         file
.setMimeType(we
.contentType()); 
 165         file
.setModifiedTimestamp(we
.modifiedTimestamp()); 
 166         file
.setEtag(we
.etag());