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/>.
18 package com.owncloud.android.oc_framework.operations.remote;
20 import java.io.BufferedInputStream;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.util.Date;
25 import java.util.HashSet;
26 import java.util.Iterator;
28 import java.util.concurrent.atomic.AtomicBoolean;
30 import org.apache.commons.httpclient.Header;
31 import org.apache.commons.httpclient.HttpException;
32 import org.apache.commons.httpclient.methods.GetMethod;
33 import org.apache.http.HttpStatus;
35 import android.util.Log;
37 import com.owncloud.android.oc_framework.network.webdav.OnDatatransferProgressListener;
38 import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
39 import com.owncloud.android.oc_framework.network.webdav.WebdavUtils;
40 import com.owncloud.android.oc_framework.operations.OperationCancelledException;
41 import com.owncloud.android.oc_framework.operations.RemoteFile;
42 import com.owncloud.android.oc_framework.operations.RemoteOperation;
43 import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
46 * Remote operation performing the download of a remote file in the ownCloud server.
48 * @author David A. Velasco
52 public class DownloadRemoteFileOperation extends RemoteOperation {
54 private static final String TAG = DownloadRemoteFileOperation.class.getSimpleName();
56 private Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<OnDatatransferProgressListener>();
57 private final AtomicBoolean mCancellationRequested = new AtomicBoolean(false);
58 private long mModificationTimestamp = 0;
59 private GetMethod mGet;
61 private RemoteFile mRemoteFile;
62 private String mTemporalFolder;
64 public DownloadRemoteFileOperation(RemoteFile remoteFile, String temporalFolder) {
65 mRemoteFile = remoteFile;
66 mTemporalFolder = temporalFolder;
70 protected RemoteOperationResult run(WebdavClient client) {
71 RemoteOperationResult result = null;
73 /// download will be performed to a temporal file, then moved to the final location
74 File tmpFile = new File(getTmpPath());
76 /// perform the download
78 tmpFile.getParentFile().mkdirs();
79 int status = downloadFile(client, tmpFile);
80 result = new RemoteOperationResult(isSuccess(status), status, (mGet != null ? mGet.getResponseHeaders() : null));
81 Log.i(TAG, "Download of " + mRemoteFile.getRemotePath() + " to " + getTmpPath() + ": " + result.getLogMessage());
83 } catch (Exception e) {
84 result = new RemoteOperationResult(e);
85 Log.e(TAG, "Download of " + mRemoteFile.getRemotePath() + " to " + getTmpPath() + ": " + result.getLogMessage(), e);
92 protected int downloadFile(WebdavClient client, File targetFile) throws HttpException, IOException, OperationCancelledException {
94 boolean savedFile = false;
95 mGet = new GetMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemoteFile.getRemotePath()));
96 Iterator<OnDatatransferProgressListener> it = null;
98 FileOutputStream fos = null;
100 status = client.executeMethod(mGet);
101 if (isSuccess(status)) {
102 targetFile.createNewFile();
103 BufferedInputStream bis = new BufferedInputStream(mGet.getResponseBodyAsStream());
104 fos = new FileOutputStream(targetFile);
105 long transferred = 0;
107 byte[] bytes = new byte[4096];
109 while ((readResult = bis.read(bytes)) != -1) {
110 synchronized(mCancellationRequested) {
111 if (mCancellationRequested.get()) {
113 throw new OperationCancelledException();
116 fos.write(bytes, 0, readResult);
117 transferred += readResult;
118 synchronized (mDataTransferListeners) {
119 it = mDataTransferListeners.iterator();
120 while (it.hasNext()) {
121 it.next().onTransferProgress(readResult, transferred, mRemoteFile.getLength(), targetFile.getName());
126 Header modificationTime = mGet.getResponseHeader("Last-Modified");
127 if (modificationTime != null) {
128 Date d = WebdavUtils.parseResponseDate((String) modificationTime.getValue());
129 mModificationTimestamp = (d != null) ? d.getTime() : 0;
133 client.exhaustResponse(mGet.getResponseBodyAsStream());
137 if (fos != null) fos.close();
138 if (!savedFile && targetFile.exists()) {
141 mGet.releaseConnection(); // let the connection available for other methods
146 private boolean isSuccess(int status) {
147 return (status == HttpStatus.SC_OK);
150 private String getTmpPath() {
151 return mTemporalFolder + mRemoteFile.getRemotePath();
154 public void addDatatransferProgressListener (OnDatatransferProgressListener listener) {
155 synchronized (mDataTransferListeners) {
156 mDataTransferListeners.add(listener);
160 public void removeDatatransferProgressListener(OnDatatransferProgressListener listener) {
161 synchronized (mDataTransferListeners) {
162 mDataTransferListeners.remove(listener);
166 public void cancel() {
167 mCancellationRequested.set(true); // atomic set; there is no need of synchronizing it