91e21b494d0e7480a95cce9f909fc1dab19fd161
[pub/Android/ownCloud.git] / oc_framework / src / com / owncloud / android / oc_framework / operations / remote / UploadRemoteFileOperation.java
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.File;
21 import java.io.IOException;
22 import java.util.HashSet;
23 import java.util.Set;
24 import java.util.concurrent.atomic.AtomicBoolean;
25
26 import org.apache.commons.httpclient.HttpException;
27 import org.apache.commons.httpclient.methods.PutMethod;
28 import org.apache.commons.httpclient.methods.RequestEntity;
29 import org.apache.http.HttpStatus;
30
31 import com.owncloud.android.oc_framework.network.ProgressiveDataTransferer;
32 import com.owncloud.android.oc_framework.network.webdav.FileRequestEntity;
33 import com.owncloud.android.oc_framework.network.webdav.OnDatatransferProgressListener;
34 import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
35 import com.owncloud.android.oc_framework.network.webdav.WebdavUtils;
36 import com.owncloud.android.oc_framework.operations.OperationCancelledException;
37 import com.owncloud.android.oc_framework.operations.RemoteOperation;
38 import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
39
40 /**
41 * Remote operation performing the upload of a remote file to the ownCloud server.
42 *
43 * @author David A. Velasco
44 * @author masensio
45 */
46
47 public class UploadRemoteFileOperation extends RemoteOperation {
48
49
50 protected String mStoragePath;
51 protected String mRemotePath;
52 protected String mMimeType;
53 protected PutMethod mPutMethod = null;
54
55 private final AtomicBoolean mCancellationRequested = new AtomicBoolean(false);
56 protected Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<OnDatatransferProgressListener>();
57
58 protected RequestEntity mEntity = null;
59
60 public UploadRemoteFileOperation(String storagePath, String remotePath, String mimeType) {
61 mStoragePath = storagePath;
62 mRemotePath = remotePath;
63 mMimeType = mimeType;
64 }
65
66 @Override
67 protected RemoteOperationResult run(WebdavClient client) {
68 RemoteOperationResult result = null;
69
70 try {
71 // / perform the upload
72 synchronized (mCancellationRequested) {
73 if (mCancellationRequested.get()) {
74 throw new OperationCancelledException();
75 } else {
76 mPutMethod = new PutMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath));
77 }
78 }
79
80 int status = uploadFile(client);
81
82 result = new RemoteOperationResult(isSuccess(status), status, (mPutMethod != null ? mPutMethod.getResponseHeaders() : null));
83
84 } catch (Exception e) {
85 // TODO something cleaner with cancellations
86 if (mCancellationRequested.get()) {
87 result = new RemoteOperationResult(new OperationCancelledException());
88 } else {
89 result = new RemoteOperationResult(e);
90 }
91 }
92 return result;
93 }
94
95 public boolean isSuccess(int status) {
96 return ((status == HttpStatus.SC_OK || status == HttpStatus.SC_CREATED || status == HttpStatus.SC_NO_CONTENT));
97 }
98
99 protected int uploadFile(WebdavClient client) throws HttpException, IOException, OperationCancelledException {
100 int status = -1;
101 try {
102 File f = new File(mStoragePath);
103 mEntity = new FileRequestEntity(f, mMimeType);
104 synchronized (mDataTransferListeners) {
105 ((ProgressiveDataTransferer)mEntity).addDatatransferProgressListeners(mDataTransferListeners);
106 }
107 mPutMethod.setRequestEntity(mEntity);
108 status = client.executeMethod(mPutMethod);
109 client.exhaustResponse(mPutMethod.getResponseBodyAsStream());
110
111 } finally {
112 mPutMethod.releaseConnection(); // let the connection available for other methods
113 }
114 return status;
115 }
116
117 public Set<OnDatatransferProgressListener> getDataTransferListeners() {
118 return mDataTransferListeners;
119 }
120
121 public void addDatatransferProgressListener (OnDatatransferProgressListener listener) {
122 synchronized (mDataTransferListeners) {
123 mDataTransferListeners.add(listener);
124 }
125 if (mEntity != null) {
126 ((ProgressiveDataTransferer)mEntity).addDatatransferProgressListener(listener);
127 }
128 }
129
130 public void removeDatatransferProgressListener(OnDatatransferProgressListener listener) {
131 synchronized (mDataTransferListeners) {
132 mDataTransferListeners.remove(listener);
133 }
134 if (mEntity != null) {
135 ((ProgressiveDataTransferer)mEntity).removeDatatransferProgressListener(listener);
136 }
137 }
138
139 public void cancel() {
140 synchronized (mCancellationRequested) {
141 mCancellationRequested.set(true);
142 if (mPutMethod != null)
143 mPutMethod.abort();
144 }
145 }
146
147 }