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
25 package com.owncloud.android.oc_framework.operations.remote;
27 import java.io.BufferedInputStream;
29 import java.io.FileOutputStream;
30 import java.io.IOException;
31 import java.util.Date;
32 import java.util.HashSet;
33 import java.util.Iterator;
35 import java.util.concurrent.atomic.AtomicBoolean;
37 import org.apache.commons.httpclient.Header;
38 import org.apache.commons.httpclient.HttpException;
39 import org.apache.commons.httpclient.methods.GetMethod;
40 import org.apache.http.HttpStatus;
42 import android.util.Log;
44 import com.owncloud.android.oc_framework.network.webdav.OnDatatransferProgressListener;
45 import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
46 import com.owncloud.android.oc_framework.network.webdav.WebdavUtils;
47 import com.owncloud.android.oc_framework.operations.OperationCancelledException;
48 import com.owncloud.android.oc_framework.operations.RemoteFile;
49 import com.owncloud.android.oc_framework.operations.RemoteOperation;
50 import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
53 * Remote operation performing the download of a remote file in the ownCloud server.
55 * @author David A. Velasco
59 public class DownloadRemoteFileOperation extends RemoteOperation {
61 private static final String TAG = DownloadRemoteFileOperation.class.getSimpleName();
63 private Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<OnDatatransferProgressListener>();
64 private final AtomicBoolean mCancellationRequested = new AtomicBoolean(false);
65 private long mModificationTimestamp = 0;
66 private GetMethod mGet;
68 private RemoteFile mRemoteFile;
69 private String mTemporalFolder;
71 public DownloadRemoteFileOperation(RemoteFile remoteFile, String temporalFolder) {
72 mRemoteFile = remoteFile;
73 mTemporalFolder = temporalFolder;
77 protected RemoteOperationResult run(WebdavClient client) {
78 RemoteOperationResult result = null;
80 /// download will be performed to a temporal file, then moved to the final location
81 File tmpFile = new File(getTmpPath());
83 /// perform the download
85 tmpFile.getParentFile().mkdirs();
86 int status = downloadFile(client, tmpFile);
87 result = new RemoteOperationResult(isSuccess(status), status, (mGet != null ? mGet.getResponseHeaders() : null));
88 Log.i(TAG, "Download of " + mRemoteFile.getRemotePath() + " to " + getTmpPath() + ": " + result.getLogMessage());
90 } catch (Exception e) {
91 result = new RemoteOperationResult(e);
92 Log.e(TAG, "Download of " + mRemoteFile.getRemotePath() + " to " + getTmpPath() + ": " + result.getLogMessage(), e);
99 protected int downloadFile(WebdavClient client, File targetFile) throws HttpException, IOException, OperationCancelledException {
101 boolean savedFile = false;
102 mGet = new GetMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemoteFile.getRemotePath()));
103 Iterator<OnDatatransferProgressListener> it = null;
105 FileOutputStream fos = null;
107 status = client.executeMethod(mGet);
108 if (isSuccess(status)) {
109 targetFile.createNewFile();
110 BufferedInputStream bis = new BufferedInputStream(mGet.getResponseBodyAsStream());
111 fos = new FileOutputStream(targetFile);
112 long transferred = 0;
114 byte[] bytes = new byte[4096];
116 while ((readResult = bis.read(bytes)) != -1) {
117 synchronized(mCancellationRequested) {
118 if (mCancellationRequested.get()) {
120 throw new OperationCancelledException();
123 fos.write(bytes, 0, readResult);
124 transferred += readResult;
125 synchronized (mDataTransferListeners) {
126 it = mDataTransferListeners.iterator();
127 while (it.hasNext()) {
128 it.next().onTransferProgress(readResult, transferred, mRemoteFile.getLength(), targetFile.getName());
133 Header modificationTime = mGet.getResponseHeader("Last-Modified");
134 if (modificationTime != null) {
135 Date d = WebdavUtils.parseResponseDate((String) modificationTime.getValue());
136 mModificationTimestamp = (d != null) ? d.getTime() : 0;
140 client.exhaustResponse(mGet.getResponseBodyAsStream());
144 if (fos != null) fos.close();
145 if (!savedFile && targetFile.exists()) {
148 mGet.releaseConnection(); // let the connection available for other methods
153 private boolean isSuccess(int status) {
154 return (status == HttpStatus.SC_OK);
157 private String getTmpPath() {
158 return mTemporalFolder + mRemoteFile.getRemotePath();
161 public void addDatatransferProgressListener (OnDatatransferProgressListener listener) {
162 synchronized (mDataTransferListeners) {
163 mDataTransferListeners.add(listener);
167 public void removeDatatransferProgressListener(OnDatatransferProgressListener listener) {
168 synchronized (mDataTransferListeners) {
169 mDataTransferListeners.remove(listener);
173 public void cancel() {
174 mCancellationRequested.set(true); // atomic set; there is no need of synchronizing it