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 protected String mStoragePath
;
51 protected String mRemotePath
;
52 protected String mMimeType
;
53 protected PutMethod mPutMethod
= null
;
55 private final AtomicBoolean mCancellationRequested
= new AtomicBoolean(false
);
56 protected Set
<OnDatatransferProgressListener
> mDataTransferListeners
= new HashSet
<OnDatatransferProgressListener
>();
58 protected RequestEntity mEntity
= null
;
60 public UploadRemoteFileOperation(String storagePath
, String remotePath
, String mimeType
) {
61 mStoragePath
= storagePath
;
62 mRemotePath
= remotePath
;
67 protected RemoteOperationResult
run(WebdavClient client
) {
68 RemoteOperationResult result
= null
;
71 // / perform the upload
72 synchronized (mCancellationRequested
) {
73 if (mCancellationRequested
.get()) {
74 throw new OperationCancelledException();
76 mPutMethod
= new PutMethod(client
.getBaseUri() + WebdavUtils
.encodePath(mRemotePath
));
80 int status
= uploadFile(client
);
82 result
= new RemoteOperationResult(isSuccess(status
), status
, (mPutMethod
!= null ? mPutMethod
.getResponseHeaders() : null
));
84 } catch (Exception e
) {
85 // TODO something cleaner with cancellations
86 if (mCancellationRequested
.get()) {
87 result
= new RemoteOperationResult(new OperationCancelledException());
89 result
= new RemoteOperationResult(e
);
95 public boolean isSuccess(int status
) {
96 return ((status
== HttpStatus
.SC_OK
|| status
== HttpStatus
.SC_CREATED
|| status
== HttpStatus
.SC_NO_CONTENT
));
99 protected int uploadFile(WebdavClient client
) throws HttpException
, IOException
, OperationCancelledException
{
102 File f
= new File(mStoragePath
);
103 mEntity
= new FileRequestEntity(f
, mMimeType
);
104 synchronized (mDataTransferListeners
) {
105 ((ProgressiveDataTransferer
)mEntity
).addDatatransferProgressListeners(mDataTransferListeners
);
107 mPutMethod
.setRequestEntity(mEntity
);
108 status
= client
.executeMethod(mPutMethod
);
109 client
.exhaustResponse(mPutMethod
.getResponseBodyAsStream());
112 mPutMethod
.releaseConnection(); // let the connection available for other methods
117 public Set
<OnDatatransferProgressListener
> getDataTransferListeners() {
118 return mDataTransferListeners
;
121 public void addDatatransferProgressListener (OnDatatransferProgressListener listener
) {
122 synchronized (mDataTransferListeners
) {
123 mDataTransferListeners
.add(listener
);
125 if (mEntity
!= null
) {
126 ((ProgressiveDataTransferer
)mEntity
).addDatatransferProgressListener(listener
);
130 public void removeDatatransferProgressListener(OnDatatransferProgressListener listener
) {
131 synchronized (mDataTransferListeners
) {
132 mDataTransferListeners
.remove(listener
);
134 if (mEntity
!= null
) {
135 ((ProgressiveDataTransferer
)mEntity
).removeDatatransferProgressListener(listener
);
139 public void cancel() {
140 synchronized (mCancellationRequested
) {
141 mCancellationRequested
.set(true
);
142 if (mPutMethod
!= null
)