mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-10-31 02:17:41 +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 okhttp3.Headers; | ||||
| import okhttp3.HttpUrl; | ||||
| import okhttp3.OkHttpClient; | ||||
| import okhttp3.Request; | ||||
| import okhttp3.Response; | ||||
| @ -45,8 +46,11 @@ public abstract class HttpBaseMethod { | ||||
|     protected Request mRequest; | ||||
|     protected Response mResponse; | ||||
| 
 | ||||
|     public HttpBaseMethod () { | ||||
|     public HttpBaseMethod (HttpUrl httpUrl) { | ||||
|         mOkHttpClient = HttpClient.getOkHttpClient(); | ||||
|         mRequest = new Request.Builder() | ||||
|                 .url(httpUrl) | ||||
|                 .build(); | ||||
|     } | ||||
| 
 | ||||
|     // Request | ||||
| @ -54,6 +58,18 @@ public abstract class HttpBaseMethod { | ||||
|         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 | ||||
|     public int getStatusCode() { | ||||
|         return mResponse.code(); | ||||
|  | ||||
| @ -38,30 +38,8 @@ import okhttp3.Request; | ||||
|  */ | ||||
| public abstract class HttpMethod extends HttpBaseMethod { | ||||
| 
 | ||||
|     public HttpMethod(String httpUrl) { | ||||
|         super(); | ||||
|         mRequest = new Request.Builder() | ||||
|                 .url(httpUrl) | ||||
|                 .build(); | ||||
|     } | ||||
| 
 | ||||
|     public HttpMethod(HttpUrl httpUrl) { | ||||
|         super(); | ||||
|         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); | ||||
|         super(httpUrl); | ||||
|     } | ||||
| 
 | ||||
|     public int executeRequest() throws IOException { | ||||
|  | ||||
| @ -36,7 +36,7 @@ import okhttp3.HttpUrl; | ||||
|  */ | ||||
| public abstract class DavMethod extends HttpBaseMethod { | ||||
| 
 | ||||
|     protected DavResource mDavResource; | ||||
|     protected DavOCResource mDavResource; | ||||
| 
 | ||||
|     public DavMethod(HttpUrl httpUrl) { | ||||
|         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.setData(mFolderAndFiles); | ||||
| 
 | ||||
|             } else { | ||||
|                 // synchronization failed | ||||
|             } else { // synchronization failed | ||||
|                 result = new RemoteOperationResult(propfindMethod); | ||||
|             } | ||||
| 
 | ||||
|  | ||||
| @ -25,6 +25,7 @@ | ||||
| package com.owncloud.android.lib.resources.files; | ||||
| 
 | ||||
| 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.OnDatatransferProgressListener; | ||||
| 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.RemoteOperation; | ||||
| 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.HttpStatus; | ||||
| import org.apache.commons.httpclient.methods.PutMethod; | ||||
| import org.apache.commons.httpclient.methods.RequestEntity; | ||||
| import org.apache.commons.httpclient.params.HttpMethodParams; | ||||
| 
 | ||||
| @ -45,6 +46,8 @@ import java.util.HashSet; | ||||
| import java.util.Set; | ||||
| 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. | ||||
|  *  | ||||
| @ -90,17 +93,20 @@ public class UploadRemoteFileOperation extends RemoteOperation { | ||||
|     @Override | ||||
|     protected RemoteOperationResult run(OwnCloudClient client) { | ||||
|         RemoteOperationResult result = null; | ||||
|         DefaultHttpMethodRetryHandler oldRetryHandler = | ||||
|             (DefaultHttpMethodRetryHandler) client.getParams().getParameter(HttpMethodParams.RETRY_HANDLER); | ||||
| 
 | ||||
| //        DefaultHttpMethodRetryHandler oldRetryHandler = | ||||
| //            (DefaultHttpMethodRetryHandler) client.getParams().getParameter(HttpMethodParams.RETRY_HANDLER); | ||||
| 
 | ||||
|         try { | ||||
|             // TODO | ||||
|             // prevent that uploads are retried automatically by network library | ||||
|             client.getParams().setParameter( | ||||
|                 HttpMethodParams.RETRY_HANDLER, | ||||
|                 new DefaultHttpMethodRetryHandler(0, false) | ||||
|             ); | ||||
| //            client.getParams().setParameter( | ||||
| //                HttpMethodParams.RETRY_HANDLER, | ||||
| //                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()) { | ||||
|                 // 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) { | ||||
|             if (mPutMethod != null && mPutMethod.isAborted()) { | ||||
|                 result = new RemoteOperationResult(new OperationCancelledException()); | ||||
| 
 | ||||
|             } else { | ||||
|                 result = new RemoteOperationResult(e); | ||||
|             } | ||||
| //            if (mPutMethod != null && mPutMethod.isAborted()) { | ||||
| //                result = new RemoteOperationResult(new OperationCancelledException()); | ||||
| // | ||||
| //            } else { | ||||
| //                result = new RemoteOperationResult(e); | ||||
| //            } | ||||
|         } finally { | ||||
|             // reset previous retry handler | ||||
|             client.getParams().setParameter( | ||||
|                 HttpMethodParams.RETRY_HANDLER, | ||||
|                 oldRetryHandler | ||||
|             ); | ||||
| //            client.getParams().setParameter( | ||||
| //                HttpMethodParams.RETRY_HANDLER, | ||||
| //                oldRetryHandler | ||||
| //            ); | ||||
|         } | ||||
|         return result; | ||||
|     } | ||||
| @ -134,33 +140,37 @@ public class UploadRemoteFileOperation extends RemoteOperation { | ||||
|     } | ||||
| 
 | ||||
|     protected RemoteOperationResult uploadFile(OwnCloudClient client) throws IOException { | ||||
|         int status; | ||||
|         RemoteOperationResult result; | ||||
|         try { | ||||
|             File f = new File(mLocalPath); | ||||
|             mEntity  = new FileRequestEntity(f, mMimeType); | ||||
|             synchronized (mDataTransferListeners) { | ||||
|                 ((ProgressiveDataTransferer)mEntity) | ||||
|                         .addDatatransferProgressListeners(mDataTransferListeners); | ||||
|             } | ||||
| 
 | ||||
|             // TODO | ||||
| //            synchronized (mDataTransferListeners) { | ||||
| //                ((ProgressiveDataTransferer)mEntity) | ||||
| //                        .addDatatransferProgressListeners(mDataTransferListeners); | ||||
| //            } | ||||
| 
 | ||||
|             if (mRequiredEtag != null && mRequiredEtag.length() > 0) { | ||||
|                 mPutMethod.addRequestHeader(IF_MATCH_HEADER, "\"" + mRequiredEtag + "\""); | ||||
|             } | ||||
| 
 | ||||
|             mPutMethod.addRequestHeader(OC_TOTAL_LENGTH_HEADER, String.valueOf(f.length())); | ||||
| 
 | ||||
|             mPutMethod.addRequestHeader(OC_X_OC_MTIME_HEADER, mFileLastModifTimestamp); | ||||
| 
 | ||||
|             mPutMethod.setRequestEntity(mEntity); | ||||
|             status = client.executeMethod(mPutMethod); | ||||
|             int status = client.executeHttpMethod(mPutMethod); | ||||
| 
 | ||||
|             result = new RemoteOperationResult( | ||||
|                 isSuccess(status), | ||||
|                 mPutMethod | ||||
|             ); | ||||
|             client.exhaustResponse(mPutMethod.getResponseBodyAsStream()); | ||||
|             if (isSuccess(status)) { | ||||
|                 result = new RemoteOperationResult(OK); | ||||
| 
 | ||||
|         } finally { | ||||
|             mPutMethod.releaseConnection(); // let the connection available for other methods | ||||
|             } else { // synchronization failed | ||||
|                 result = new RemoteOperationResult(mPutMethod); | ||||
|             } | ||||
| 
 | ||||
|             client.exhaustResponse(mPutMethod.getResponseAsStream()); | ||||
| 
 | ||||
|         } catch (Exception e) { | ||||
|             result = new RemoteOperationResult(e); | ||||
|         } | ||||
|         return result; | ||||
|     } | ||||
| @ -188,10 +198,10 @@ public class UploadRemoteFileOperation extends RemoteOperation { | ||||
|     } | ||||
|      | ||||
|     public void cancel() { | ||||
|         synchronized (mCancellationRequested) { | ||||
|             mCancellationRequested.set(true); | ||||
|             if (mPutMethod != null) | ||||
|                 mPutMethod.abort(); | ||||
|         } | ||||
| //        synchronized (mCancellationRequested) { | ||||
| //            mCancellationRequested.set(true); | ||||
| //            if (mPutMethod != null) | ||||
| //                mPutMethod.abort(); | ||||
| //        } | ||||
|     } | ||||
| } | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user