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