e3e7ee07354bbc5a401984e9d0c4321ee4fba321
[pub/Android/ownCloud.git] /
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
3 *
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.
7 *
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.
12 *
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/>.
15 *
16 */
17
18 package com.owncloud.android.oc_framework.operations.remote;
19
20 import java.io.BufferedInputStream;
21 import java.io.File;
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;
27 import java.util.Set;
28 import java.util.concurrent.atomic.AtomicBoolean;
29
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;
34
35 import android.util.Log;
36
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;
44
45 /**
46 * Remote operation performing the download of a remote file in the ownCloud server.
47 *
48 * @author David A. Velasco
49 * @author masensio
50 */
51
52 public class DownloadRemoteFileOperation extends RemoteOperation {
53
54 private static final String TAG = DownloadRemoteFileOperation.class.getSimpleName();
55
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;
60
61 private RemoteFile mRemoteFile;
62 private String mTemporalFolder;
63
64 public DownloadRemoteFileOperation(RemoteFile remoteFile, String temporalFolder) {
65 mRemoteFile = remoteFile;
66 mTemporalFolder = temporalFolder;
67 }
68
69 @Override
70 protected RemoteOperationResult run(WebdavClient client) {
71 RemoteOperationResult result = null;
72
73 /// download will be performed to a temporal file, then moved to the final location
74 File tmpFile = new File(getTmpPath());
75
76 /// perform the download
77 try {
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());
82
83 } catch (Exception e) {
84 result = new RemoteOperationResult(e);
85 Log.e(TAG, "Download of " + mRemoteFile.getRemotePath() + " to " + getTmpPath() + ": " + result.getLogMessage(), e);
86 }
87
88 return result;
89 }
90
91
92 protected int downloadFile(WebdavClient client, File targetFile) throws HttpException, IOException, OperationCancelledException {
93 int status = -1;
94 boolean savedFile = false;
95 mGet = new GetMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemoteFile.getRemotePath()));
96 Iterator<OnDatatransferProgressListener> it = null;
97
98 FileOutputStream fos = null;
99 try {
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;
106
107 byte[] bytes = new byte[4096];
108 int readResult = 0;
109 while ((readResult = bis.read(bytes)) != -1) {
110 synchronized(mCancellationRequested) {
111 if (mCancellationRequested.get()) {
112 mGet.abort();
113 throw new OperationCancelledException();
114 }
115 }
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());
122 }
123 }
124 }
125 savedFile = true;
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;
130 }
131
132 } else {
133 client.exhaustResponse(mGet.getResponseBodyAsStream());
134 }
135
136 } finally {
137 if (fos != null) fos.close();
138 if (!savedFile && targetFile.exists()) {
139 targetFile.delete();
140 }
141 mGet.releaseConnection(); // let the connection available for other methods
142 }
143 return status;
144 }
145
146 private boolean isSuccess(int status) {
147 return (status == HttpStatus.SC_OK);
148 }
149
150 private String getTmpPath() {
151 return mTemporalFolder + mRemoteFile.getRemotePath();
152 }
153
154 public void addDatatransferProgressListener (OnDatatransferProgressListener listener) {
155 synchronized (mDataTransferListeners) {
156 mDataTransferListeners.add(listener);
157 }
158 }
159
160 public void removeDatatransferProgressListener(OnDatatransferProgressListener listener) {
161 synchronized (mDataTransferListeners) {
162 mDataTransferListeners.remove(listener);
163 }
164 }
165
166 public void cancel() {
167 mCancellationRequested.set(true); // atomic set; there is no need of synchronizing it
168 }
169 }