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
;
21 import java
.io
.IOException
;
22 import java
.util
.HashSet
;
24 import java
.util
.concurrent
.atomic
.AtomicBoolean
;
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
;
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
;
41 * Remote operation performing the upload of a remote file to the ownCloud server.
43 * @author David A. Velasco
47 public class UploadRemoteFileOperation
extends RemoteOperation
{
50 private String mStoragePath
;
51 private String mRemotePath
;
52 private String mMimeType
;
53 PutMethod mPutMethod
= null
;
54 private final AtomicBoolean mCancellationRequested
= new AtomicBoolean(false
);
55 private Set
<OnDatatransferProgressListener
> mDataTransferListeners
= new HashSet
<OnDatatransferProgressListener
>();
57 protected RequestEntity mEntity
= null
;
59 public UploadRemoteFileOperation(String storagePath
, String remotePath
, String mimeType
) {
60 mStoragePath
= storagePath
;
61 mRemotePath
= remotePath
;
66 protected RemoteOperationResult
run(WebdavClient client
) {
67 RemoteOperationResult result
= null
;
70 // / perform the upload
71 synchronized (mCancellationRequested
) {
72 if (mCancellationRequested
.get()) {
73 throw new OperationCancelledException();
75 mPutMethod
= new PutMethod(client
.getBaseUri() + WebdavUtils
.encodePath(mRemotePath
));
79 int status
= uploadFile(client
);
81 result
= new RemoteOperationResult(isSuccess(status
), status
, (mPutMethod
!= null ? mPutMethod
.getResponseHeaders() : null
));
83 } catch (Exception e
) {
84 // TODO something cleaner with cancellations
85 if (mCancellationRequested
.get()) {
86 result
= new RemoteOperationResult(new OperationCancelledException());
88 result
= new RemoteOperationResult(e
);
94 public boolean isSuccess(int status
) {
95 return ((status
== HttpStatus
.SC_OK
|| status
== HttpStatus
.SC_CREATED
|| status
== HttpStatus
.SC_NO_CONTENT
));
98 protected int uploadFile(WebdavClient client
) throws HttpException
, IOException
, OperationCancelledException
{
101 File f
= new File(mStoragePath
);
102 mEntity
= new FileRequestEntity(f
, mMimeType
);
103 synchronized (mDataTransferListeners
) {
104 ((ProgressiveDataTransferer
)mEntity
).addDatatransferProgressListeners(mDataTransferListeners
);
106 mPutMethod
.setRequestEntity(mEntity
);
107 status
= client
.executeMethod(mPutMethod
);
108 client
.exhaustResponse(mPutMethod
.getResponseBodyAsStream());
111 mPutMethod
.releaseConnection(); // let the connection available for other methods
116 public Set
<OnDatatransferProgressListener
> getDataTransferListeners() {
117 return mDataTransferListeners
;
120 public void addDatatransferProgressListener (OnDatatransferProgressListener listener
) {
121 synchronized (mDataTransferListeners
) {
122 mDataTransferListeners
.add(listener
);
124 if (mEntity
!= null
) {
125 ((ProgressiveDataTransferer
)mEntity
).addDatatransferProgressListener(listener
);
129 public void removeDatatransferProgressListener(OnDatatransferProgressListener listener
) {
130 synchronized (mDataTransferListeners
) {
131 mDataTransferListeners
.remove(listener
);
133 if (mEntity
!= null
) {
134 ((ProgressiveDataTransferer
)mEntity
).removeDatatransferProgressListener(listener
);
138 public void cancel() {
139 synchronized (mCancellationRequested
) {
140 mCancellationRequested
.set(true
);
141 if (mPutMethod
!= null
)