1 /* ownCloud webDAV Library for Android is available under MIT license
2 * Copyright (C) 2014 ownCloud (http://www.owncloud.org/)
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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
25 package com
.owncloud
.android
.oc_framework
.operations
.remote
;
28 import java
.io
.IOException
;
29 import java
.util
.HashSet
;
31 import java
.util
.concurrent
.atomic
.AtomicBoolean
;
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
;
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
;
48 * Remote operation performing the upload of a remote file to the ownCloud server.
50 * @author David A. Velasco
54 public class UploadRemoteFileOperation
extends RemoteOperation
{
57 protected String mStoragePath
;
58 protected String mRemotePath
;
59 protected String mMimeType
;
60 protected PutMethod mPutMethod
= null
;
62 private final AtomicBoolean mCancellationRequested
= new AtomicBoolean(false
);
63 protected Set
<OnDatatransferProgressListener
> mDataTransferListeners
= new HashSet
<OnDatatransferProgressListener
>();
65 protected RequestEntity mEntity
= null
;
67 public UploadRemoteFileOperation(String storagePath
, String remotePath
, String mimeType
) {
68 mStoragePath
= storagePath
;
69 mRemotePath
= remotePath
;
74 protected RemoteOperationResult
run(WebdavClient client
) {
75 RemoteOperationResult result
= null
;
78 // / perform the upload
79 synchronized (mCancellationRequested
) {
80 if (mCancellationRequested
.get()) {
81 throw new OperationCancelledException();
83 mPutMethod
= new PutMethod(client
.getBaseUri() + WebdavUtils
.encodePath(mRemotePath
));
87 int status
= uploadFile(client
);
89 result
= new RemoteOperationResult(isSuccess(status
), status
, (mPutMethod
!= null ? mPutMethod
.getResponseHeaders() : null
));
91 } catch (Exception e
) {
92 // TODO something cleaner with cancellations
93 if (mCancellationRequested
.get()) {
94 result
= new RemoteOperationResult(new OperationCancelledException());
96 result
= new RemoteOperationResult(e
);
102 public boolean isSuccess(int status
) {
103 return ((status
== HttpStatus
.SC_OK
|| status
== HttpStatus
.SC_CREATED
|| status
== HttpStatus
.SC_NO_CONTENT
));
106 protected int uploadFile(WebdavClient client
) throws HttpException
, IOException
, OperationCancelledException
{
109 File f
= new File(mStoragePath
);
110 mEntity
= new FileRequestEntity(f
, mMimeType
);
111 synchronized (mDataTransferListeners
) {
112 ((ProgressiveDataTransferer
)mEntity
).addDatatransferProgressListeners(mDataTransferListeners
);
114 mPutMethod
.setRequestEntity(mEntity
);
115 status
= client
.executeMethod(mPutMethod
);
116 client
.exhaustResponse(mPutMethod
.getResponseBodyAsStream());
119 mPutMethod
.releaseConnection(); // let the connection available for other methods
124 public Set
<OnDatatransferProgressListener
> getDataTransferListeners() {
125 return mDataTransferListeners
;
128 public void addDatatransferProgressListener (OnDatatransferProgressListener listener
) {
129 synchronized (mDataTransferListeners
) {
130 mDataTransferListeners
.add(listener
);
132 if (mEntity
!= null
) {
133 ((ProgressiveDataTransferer
)mEntity
).addDatatransferProgressListener(listener
);
137 public void removeDatatransferProgressListener(OnDatatransferProgressListener listener
) {
138 synchronized (mDataTransferListeners
) {
139 mDataTransferListeners
.remove(listener
);
141 if (mEntity
!= null
) {
142 ((ProgressiveDataTransferer
)mEntity
).removeDatatransferProgressListener(listener
);
146 public void cancel() {
147 synchronized (mCancellationRequested
) {
148 mCancellationRequested
.set(true
);
149 if (mPutMethod
!= null
)