mirror of
https://github.com/owncloud/android-library.git
synced 2025-06-08 00:16:09 +00:00
Upload [WIP]
This commit is contained in:
parent
a0478826b4
commit
c676f694d6
@ -28,6 +28,7 @@ import java.io.IOException;
|
|||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
|
||||||
import okhttp3.Headers;
|
import okhttp3.Headers;
|
||||||
|
import okhttp3.HttpUrl;
|
||||||
import okhttp3.OkHttpClient;
|
import okhttp3.OkHttpClient;
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
import okhttp3.Response;
|
import okhttp3.Response;
|
||||||
@ -45,8 +46,11 @@ public abstract class HttpBaseMethod {
|
|||||||
protected Request mRequest;
|
protected Request mRequest;
|
||||||
protected Response mResponse;
|
protected Response mResponse;
|
||||||
|
|
||||||
public HttpBaseMethod () {
|
public HttpBaseMethod (HttpUrl httpUrl) {
|
||||||
mOkHttpClient = HttpClient.getOkHttpClient();
|
mOkHttpClient = HttpClient.getOkHttpClient();
|
||||||
|
mRequest = new Request.Builder()
|
||||||
|
.url(httpUrl)
|
||||||
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Request
|
// Request
|
||||||
@ -54,6 +58,18 @@ public abstract class HttpBaseMethod {
|
|||||||
return mRequest.headers();
|
return mRequest.headers();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Request headers
|
||||||
|
public void addRequestHeader(String name, String value) {
|
||||||
|
mRequest.newBuilder()
|
||||||
|
.addHeader(name, value)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRequestHeader(String name, String value){
|
||||||
|
mRequest.newBuilder()
|
||||||
|
.header(name, value);
|
||||||
|
}
|
||||||
|
|
||||||
// Response
|
// Response
|
||||||
public int getStatusCode() {
|
public int getStatusCode() {
|
||||||
return mResponse.code();
|
return mResponse.code();
|
||||||
|
@ -38,30 +38,8 @@ import okhttp3.Request;
|
|||||||
*/
|
*/
|
||||||
public abstract class HttpMethod extends HttpBaseMethod {
|
public abstract class HttpMethod extends HttpBaseMethod {
|
||||||
|
|
||||||
public HttpMethod(String httpUrl) {
|
|
||||||
super();
|
|
||||||
mRequest = new Request.Builder()
|
|
||||||
.url(httpUrl)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
public HttpMethod(HttpUrl httpUrl) {
|
public HttpMethod(HttpUrl httpUrl) {
|
||||||
super();
|
super(httpUrl);
|
||||||
mRequest = new Request.Builder()
|
|
||||||
.url(httpUrl)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Request headers
|
|
||||||
public void addRequestHeader(String name, String value) {
|
|
||||||
mRequest.newBuilder()
|
|
||||||
.addHeader(name, value)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setRequestHeader(String name, String value){
|
|
||||||
mRequest.newBuilder()
|
|
||||||
.header(name, value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int executeRequest() throws IOException {
|
public int executeRequest() throws IOException {
|
||||||
|
@ -36,7 +36,7 @@ import okhttp3.HttpUrl;
|
|||||||
*/
|
*/
|
||||||
public abstract class DavMethod extends HttpBaseMethod {
|
public abstract class DavMethod extends HttpBaseMethod {
|
||||||
|
|
||||||
protected DavResource mDavResource;
|
protected DavOCResource mDavResource;
|
||||||
|
|
||||||
public DavMethod(HttpUrl httpUrl) {
|
public DavMethod(HttpUrl httpUrl) {
|
||||||
super();
|
super();
|
||||||
|
@ -0,0 +1,73 @@
|
|||||||
|
/* ownCloud Android Library is available under MIT license
|
||||||
|
* Copyright (C) 2018 ownCloud GmbH.
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||||
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||||
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||||
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.owncloud.android.lib.common.http.methods.webdav;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import at.bitfire.dav4android.exception.DavException;
|
||||||
|
import at.bitfire.dav4android.exception.HttpException;
|
||||||
|
import at.bitfire.dav4android.exception.UnauthorizedException;
|
||||||
|
import okhttp3.HttpUrl;
|
||||||
|
import okhttp3.RequestBody;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Put calls wrapper
|
||||||
|
* @author David González Verdugo
|
||||||
|
*/
|
||||||
|
public class PutMethod extends DavMethod {
|
||||||
|
|
||||||
|
private RequestBody mRequestBody;
|
||||||
|
private String mIfMatchETag;
|
||||||
|
private boolean mIfNoneMatch;
|
||||||
|
private String mContentType;
|
||||||
|
private String mOcTotalLength;
|
||||||
|
private String mOcXOcMtimeHeader;
|
||||||
|
|
||||||
|
public PutMethod(HttpUrl httpUrl) {
|
||||||
|
super(httpUrl);
|
||||||
|
};
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int execute() throws IOException, HttpException, DavException {
|
||||||
|
try {
|
||||||
|
mDavResource.put(
|
||||||
|
mRequestBody,
|
||||||
|
mIfMatchETag,
|
||||||
|
mIfNoneMatch,
|
||||||
|
mContentType,
|
||||||
|
mOcTotalLength,
|
||||||
|
mOcXOcMtimeHeader
|
||||||
|
);
|
||||||
|
|
||||||
|
mRequest = mDavResource.getRequest();
|
||||||
|
mResponse = mDavResource.getResponse();
|
||||||
|
|
||||||
|
} catch (UnauthorizedException davException) {
|
||||||
|
// Do nothing, we will use the 401 code to handle the situation
|
||||||
|
}
|
||||||
|
|
||||||
|
return super.getStatusCode();
|
||||||
|
}
|
||||||
|
}
|
@ -99,8 +99,7 @@ public class ReadRemoteFolderOperation extends RemoteOperation {
|
|||||||
result = new RemoteOperationResult(OK);
|
result = new RemoteOperationResult(OK);
|
||||||
result.setData(mFolderAndFiles);
|
result.setData(mFolderAndFiles);
|
||||||
|
|
||||||
} else {
|
} else { // synchronization failed
|
||||||
// synchronization failed
|
|
||||||
result = new RemoteOperationResult(propfindMethod);
|
result = new RemoteOperationResult(propfindMethod);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,6 +25,7 @@
|
|||||||
package com.owncloud.android.lib.resources.files;
|
package com.owncloud.android.lib.resources.files;
|
||||||
|
|
||||||
import com.owncloud.android.lib.common.OwnCloudClient;
|
import com.owncloud.android.lib.common.OwnCloudClient;
|
||||||
|
import com.owncloud.android.lib.common.http.HttpUtils;
|
||||||
import com.owncloud.android.lib.common.network.FileRequestEntity;
|
import com.owncloud.android.lib.common.network.FileRequestEntity;
|
||||||
import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
|
import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
|
||||||
import com.owncloud.android.lib.common.network.ProgressiveDataTransferer;
|
import com.owncloud.android.lib.common.network.ProgressiveDataTransferer;
|
||||||
@ -32,10 +33,10 @@ import com.owncloud.android.lib.common.network.WebdavUtils;
|
|||||||
import com.owncloud.android.lib.common.operations.OperationCancelledException;
|
import com.owncloud.android.lib.common.operations.OperationCancelledException;
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperation;
|
import com.owncloud.android.lib.common.operations.RemoteOperation;
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
|
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
|
||||||
|
import com.owncloud.android.lib.common.http.methods.webdav.PutMethod;
|
||||||
|
|
||||||
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
|
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
|
||||||
import org.apache.commons.httpclient.HttpStatus;
|
import org.apache.commons.httpclient.HttpStatus;
|
||||||
import org.apache.commons.httpclient.methods.PutMethod;
|
|
||||||
import org.apache.commons.httpclient.methods.RequestEntity;
|
import org.apache.commons.httpclient.methods.RequestEntity;
|
||||||
import org.apache.commons.httpclient.params.HttpMethodParams;
|
import org.apache.commons.httpclient.params.HttpMethodParams;
|
||||||
|
|
||||||
@ -45,6 +46,8 @@ import java.util.HashSet;
|
|||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
|
import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remote operation performing the upload of a remote file to the ownCloud server.
|
* Remote operation performing the upload of a remote file to the ownCloud server.
|
||||||
*
|
*
|
||||||
@ -90,17 +93,20 @@ public class UploadRemoteFileOperation extends RemoteOperation {
|
|||||||
@Override
|
@Override
|
||||||
protected RemoteOperationResult run(OwnCloudClient client) {
|
protected RemoteOperationResult run(OwnCloudClient client) {
|
||||||
RemoteOperationResult result = null;
|
RemoteOperationResult result = null;
|
||||||
DefaultHttpMethodRetryHandler oldRetryHandler =
|
|
||||||
(DefaultHttpMethodRetryHandler) client.getParams().getParameter(HttpMethodParams.RETRY_HANDLER);
|
// DefaultHttpMethodRetryHandler oldRetryHandler =
|
||||||
|
// (DefaultHttpMethodRetryHandler) client.getParams().getParameter(HttpMethodParams.RETRY_HANDLER);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// TODO
|
||||||
// prevent that uploads are retried automatically by network library
|
// prevent that uploads are retried automatically by network library
|
||||||
client.getParams().setParameter(
|
// client.getParams().setParameter(
|
||||||
HttpMethodParams.RETRY_HANDLER,
|
// HttpMethodParams.RETRY_HANDLER,
|
||||||
new DefaultHttpMethodRetryHandler(0, false)
|
// new DefaultHttpMethodRetryHandler(0, false)
|
||||||
);
|
// );
|
||||||
|
|
||||||
mPutMethod = new PutMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath));
|
mPutMethod = new PutMethod(
|
||||||
|
HttpUtils.stringUrlToHttpUrl(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath)));
|
||||||
|
|
||||||
if (mCancellationRequested.get()) {
|
if (mCancellationRequested.get()) {
|
||||||
// the operation was cancelled before getting it's turn to be executed in the queue of uploads
|
// the operation was cancelled before getting it's turn to be executed in the queue of uploads
|
||||||
@ -112,18 +118,18 @@ public class UploadRemoteFileOperation extends RemoteOperation {
|
|||||||
}
|
}
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
if (mPutMethod != null && mPutMethod.isAborted()) {
|
// if (mPutMethod != null && mPutMethod.isAborted()) {
|
||||||
result = new RemoteOperationResult(new OperationCancelledException());
|
// result = new RemoteOperationResult(new OperationCancelledException());
|
||||||
|
//
|
||||||
} else {
|
// } else {
|
||||||
result = new RemoteOperationResult(e);
|
// result = new RemoteOperationResult(e);
|
||||||
}
|
// }
|
||||||
} finally {
|
} finally {
|
||||||
// reset previous retry handler
|
// reset previous retry handler
|
||||||
client.getParams().setParameter(
|
// client.getParams().setParameter(
|
||||||
HttpMethodParams.RETRY_HANDLER,
|
// HttpMethodParams.RETRY_HANDLER,
|
||||||
oldRetryHandler
|
// oldRetryHandler
|
||||||
);
|
// );
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -134,33 +140,37 @@ public class UploadRemoteFileOperation extends RemoteOperation {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected RemoteOperationResult uploadFile(OwnCloudClient client) throws IOException {
|
protected RemoteOperationResult uploadFile(OwnCloudClient client) throws IOException {
|
||||||
int status;
|
|
||||||
RemoteOperationResult result;
|
RemoteOperationResult result;
|
||||||
try {
|
try {
|
||||||
File f = new File(mLocalPath);
|
File f = new File(mLocalPath);
|
||||||
mEntity = new FileRequestEntity(f, mMimeType);
|
|
||||||
synchronized (mDataTransferListeners) {
|
// TODO
|
||||||
((ProgressiveDataTransferer)mEntity)
|
// synchronized (mDataTransferListeners) {
|
||||||
.addDatatransferProgressListeners(mDataTransferListeners);
|
// ((ProgressiveDataTransferer)mEntity)
|
||||||
}
|
// .addDatatransferProgressListeners(mDataTransferListeners);
|
||||||
|
// }
|
||||||
|
|
||||||
if (mRequiredEtag != null && mRequiredEtag.length() > 0) {
|
if (mRequiredEtag != null && mRequiredEtag.length() > 0) {
|
||||||
mPutMethod.addRequestHeader(IF_MATCH_HEADER, "\"" + mRequiredEtag + "\"");
|
mPutMethod.addRequestHeader(IF_MATCH_HEADER, "\"" + mRequiredEtag + "\"");
|
||||||
}
|
}
|
||||||
|
|
||||||
mPutMethod.addRequestHeader(OC_TOTAL_LENGTH_HEADER, String.valueOf(f.length()));
|
mPutMethod.addRequestHeader(OC_TOTAL_LENGTH_HEADER, String.valueOf(f.length()));
|
||||||
|
|
||||||
mPutMethod.addRequestHeader(OC_X_OC_MTIME_HEADER, mFileLastModifTimestamp);
|
mPutMethod.addRequestHeader(OC_X_OC_MTIME_HEADER, mFileLastModifTimestamp);
|
||||||
|
|
||||||
mPutMethod.setRequestEntity(mEntity);
|
int status = client.executeHttpMethod(mPutMethod);
|
||||||
status = client.executeMethod(mPutMethod);
|
|
||||||
|
|
||||||
result = new RemoteOperationResult(
|
if (isSuccess(status)) {
|
||||||
isSuccess(status),
|
result = new RemoteOperationResult(OK);
|
||||||
mPutMethod
|
|
||||||
);
|
|
||||||
client.exhaustResponse(mPutMethod.getResponseBodyAsStream());
|
|
||||||
|
|
||||||
} finally {
|
} else { // synchronization failed
|
||||||
mPutMethod.releaseConnection(); // let the connection available for other methods
|
result = new RemoteOperationResult(mPutMethod);
|
||||||
|
}
|
||||||
|
|
||||||
|
client.exhaustResponse(mPutMethod.getResponseAsStream());
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
result = new RemoteOperationResult(e);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -188,10 +198,10 @@ public class UploadRemoteFileOperation extends RemoteOperation {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void cancel() {
|
public void cancel() {
|
||||||
synchronized (mCancellationRequested) {
|
// synchronized (mCancellationRequested) {
|
||||||
mCancellationRequested.set(true);
|
// mCancellationRequested.set(true);
|
||||||
if (mPutMethod != null)
|
// if (mPutMethod != null)
|
||||||
mPutMethod.abort();
|
// mPutMethod.abort();
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user