mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-10-31 02:17:41 +00:00 
			
		
		
		
	Implement basic upload using the new wrapper
This commit is contained in:
		
							parent
							
								
									524dc587db
								
							
						
					
					
						commit
						e4e8a5a5e3
					
				| @ -29,6 +29,20 @@ package com.owncloud.android.lib.common.http; | ||||
|  */ | ||||
| public class HttpConstants { | ||||
| 
 | ||||
|     /*********************************************************************************************************** | ||||
|      *************************************************** HEADERS *********************************************** | ||||
|      ***********************************************************************************************************/ | ||||
| 
 | ||||
|     public static final String IF_MATCH_HEADER = "If-Match"; | ||||
|     public static final String IF_NONE_MATCH_HEADER = "If-None-Match"; | ||||
|     public static final String CONTENT_TYPE_HEADER = "Content-Type"; | ||||
|     public static final String OC_TOTAL_LENGTH_HEADER = "OC-Total-Length"; | ||||
|     public static final String OC_X_OC_MTIME_HEADER = "X-OC-Mtime"; | ||||
| 
 | ||||
|     /*********************************************************************************************************** | ||||
|      ************************************************ STATUS CODES ********************************************* | ||||
|      ***********************************************************************************************************/ | ||||
| 
 | ||||
|     /** | ||||
|      * 1xx Informational | ||||
|      */ | ||||
|  | ||||
| @ -33,6 +33,7 @@ import okhttp3.Headers; | ||||
| import okhttp3.HttpUrl; | ||||
| import okhttp3.OkHttpClient; | ||||
| import okhttp3.Request; | ||||
| import okhttp3.RequestBody; | ||||
| import okhttp3.Response; | ||||
| 
 | ||||
| /** | ||||
| @ -46,6 +47,7 @@ public abstract class HttpBaseMethod { | ||||
|     public abstract int execute() throws Exception; | ||||
|     protected OkHttpClient mOkHttpClient; | ||||
|     protected Request mRequest; | ||||
|     protected RequestBody mRequestBody; | ||||
|     protected Response mResponse; | ||||
| 
 | ||||
|     protected HttpBaseMethod (HttpUrl httpUrl) { | ||||
| @ -56,19 +58,28 @@ public abstract class HttpBaseMethod { | ||||
|     } | ||||
| 
 | ||||
|     // Request | ||||
|     public String getRequestHeader(String name) { | ||||
|         return mRequest.header(name); | ||||
|     } | ||||
| 
 | ||||
|     public Headers getRequestHeaders() { | ||||
|         return mRequest.headers(); | ||||
|     } | ||||
| 
 | ||||
|     public void addRequestHeader(String name, String value) { | ||||
|         mRequest.newBuilder() | ||||
|         mRequest = mRequest.newBuilder() | ||||
|                 .addHeader(name, value) | ||||
|                 .build(); | ||||
|     } | ||||
| 
 | ||||
|     public void setRequestHeader(String name, String value){ | ||||
|         mRequest.newBuilder() | ||||
|                 .header(name, value); | ||||
|     public void setRequestHeader(String name, String value) { | ||||
|         mRequest = mRequest.newBuilder() | ||||
|                 .header(name, value) | ||||
|                 .build(); | ||||
|     } | ||||
| 
 | ||||
|     public void setRequestBody(RequestBody requestBody) { | ||||
|         mRequestBody = requestBody; | ||||
|     } | ||||
| 
 | ||||
|     // Response | ||||
|  | ||||
| @ -35,11 +35,8 @@ import okhttp3.RequestBody; | ||||
|  */ | ||||
| public class PostMethod extends HttpMethod { | ||||
| 
 | ||||
|     private RequestBody mRequestBody; | ||||
| 
 | ||||
|     public PostMethod(HttpUrl httpUrl, RequestBody requestBody){ | ||||
|     public PostMethod(HttpUrl httpUrl){ | ||||
|         super(httpUrl); | ||||
|         mRequestBody = requestBody; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|  | ||||
| @ -31,11 +31,8 @@ import okhttp3.RequestBody; | ||||
| 
 | ||||
| public class PutMethod extends HttpMethod{ | ||||
| 
 | ||||
|     private RequestBody mRequestBody; | ||||
| 
 | ||||
|     public PutMethod(HttpUrl httpUrl, RequestBody requestBody){ | ||||
|     public PutMethod(HttpUrl httpUrl){ | ||||
|         super(httpUrl); | ||||
|         mRequestBody = requestBody; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|  | ||||
| @ -24,6 +24,8 @@ | ||||
| 
 | ||||
| package com.owncloud.android.lib.common.http.methods.webdav; | ||||
| 
 | ||||
| import com.owncloud.android.lib.common.http.HttpConstants; | ||||
| 
 | ||||
| import java.io.IOException; | ||||
| 
 | ||||
| import at.bitfire.dav4android.exception.DavException; | ||||
| @ -38,13 +40,6 @@ import okhttp3.RequestBody; | ||||
|  */ | ||||
| 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); | ||||
|     }; | ||||
| @ -54,11 +49,13 @@ public class PutMethod extends DavMethod { | ||||
|         try { | ||||
|             mDavResource.put( | ||||
|                     mRequestBody, | ||||
|                     mIfMatchETag, | ||||
|                     mIfNoneMatch, | ||||
|                     mContentType, | ||||
|                     mOcTotalLength, | ||||
|                     mOcXOcMtimeHeader | ||||
|                     super.getRequestHeader(HttpConstants.IF_MATCH_HEADER), | ||||
|                     // Save a file not known to exist, guaranteeing that another upload didn't happen | ||||
|                     // before, losing the data of the previous put | ||||
|                     true, | ||||
|                     super.getRequestHeader(HttpConstants.CONTENT_TYPE_HEADER), | ||||
|                     super.getRequestHeader(HttpConstants.OC_TOTAL_LENGTH_HEADER), | ||||
|                     super.getRequestHeader(HttpConstants.OC_X_OC_MTIME_HEADER) | ||||
|             ); | ||||
| 
 | ||||
|             mRequest = mDavResource.getRequest(); | ||||
|  | ||||
| @ -47,6 +47,9 @@ import java.util.HashSet; | ||||
| import java.util.Set; | ||||
| import java.util.concurrent.atomic.AtomicBoolean; | ||||
| 
 | ||||
| import okhttp3.MediaType; | ||||
| import okhttp3.RequestBody; | ||||
| 
 | ||||
| import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK; | ||||
| 
 | ||||
| /** | ||||
| @ -61,10 +64,6 @@ public class UploadRemoteFileOperation extends RemoteOperation { | ||||
| 
 | ||||
|     private static final String TAG = UploadRemoteFileOperation.class.getSimpleName(); | ||||
| 
 | ||||
|     protected static final String OC_TOTAL_LENGTH_HEADER = "OC-Total-Length"; | ||||
|     protected static final String OC_X_OC_MTIME_HEADER = "X-OC-Mtime"; | ||||
|     protected static final String IF_MATCH_HEADER = "If-Match"; | ||||
| 
 | ||||
|     protected String mLocalPath; | ||||
|     protected String mRemotePath; | ||||
|     protected String mMimeType; | ||||
| @ -107,7 +106,7 @@ public class UploadRemoteFileOperation extends RemoteOperation { | ||||
| //            ); | ||||
| 
 | ||||
|             mPutMethod = new PutMethod( | ||||
|                     HttpUtils.stringUrlToHttpUrl(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath))); | ||||
|                     HttpUtils.stringUrlToHttpUrl(client.getNewWebDavUri() + WebdavUtils.encodePath(mRemotePath))); | ||||
| 
 | ||||
|             if (mCancellationRequested.get()) { | ||||
|                 // the operation was cancelled before getting it's turn to be executed in the queue of uploads | ||||
| @ -138,8 +137,8 @@ public class UploadRemoteFileOperation extends RemoteOperation { | ||||
|     protected RemoteOperationResult uploadFile(OwnCloudClient client) throws IOException { | ||||
|         RemoteOperationResult result; | ||||
|         try { | ||||
|             File f = new File(mLocalPath); | ||||
| 
 | ||||
|             // Headers | ||||
|             File fileToUpload = new File(mLocalPath); | ||||
|             // TODO | ||||
| //            synchronized (mDataTransferListeners) { | ||||
| //                ((ProgressiveDataTransferer)mEntity) | ||||
| @ -147,12 +146,19 @@ public class UploadRemoteFileOperation extends RemoteOperation { | ||||
| //            } | ||||
| 
 | ||||
|             if (mRequiredEtag != null && mRequiredEtag.length() > 0) { | ||||
|                 mPutMethod.addRequestHeader(IF_MATCH_HEADER, "\"" + mRequiredEtag + "\""); | ||||
|                 mPutMethod.addRequestHeader(HttpConstants.IF_MATCH_HEADER, "\"" + mRequiredEtag + "\""); | ||||
|             } | ||||
| 
 | ||||
|             mPutMethod.addRequestHeader(OC_TOTAL_LENGTH_HEADER, String.valueOf(f.length())); | ||||
|             mPutMethod.addRequestHeader(HttpConstants.OC_TOTAL_LENGTH_HEADER, String.valueOf(fileToUpload.length())); | ||||
| 
 | ||||
|             mPutMethod.addRequestHeader(OC_X_OC_MTIME_HEADER, mFileLastModifTimestamp); | ||||
|             mPutMethod.addRequestHeader(HttpConstants.OC_X_OC_MTIME_HEADER, mFileLastModifTimestamp); | ||||
| 
 | ||||
|             // Request body | ||||
|             MediaType mediaType = MediaType.parse(mMimeType); | ||||
| 
 | ||||
|             mPutMethod.setRequestBody( | ||||
|                     RequestBody.create(mediaType, fileToUpload) | ||||
|             ); | ||||
| 
 | ||||
|             int status = client.executeHttpMethod(mPutMethod); | ||||
| 
 | ||||
|  | ||||
| @ -226,17 +226,16 @@ public class CreateRemoteShareOperation extends RemoteOperation { | ||||
|                 formBodyBuilder.add(PARAM_PERMISSIONS, Integer.toString(mPermissions)); | ||||
|             } | ||||
| 
 | ||||
|             FormBody formBody = formBodyBuilder.build(); | ||||
| 
 | ||||
|             Uri requestUri = client.getBaseUri(); | ||||
|             Uri.Builder uriBuilder = requestUri.buildUpon(); | ||||
|             uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH); | ||||
| 
 | ||||
|             PostMethod postMethod = new PostMethod( | ||||
|                     HttpUtils.stringUrlToHttpUrl(uriBuilder.build().toString()), | ||||
|                     formBody | ||||
|                     HttpUtils.stringUrlToHttpUrl(uriBuilder.build().toString()) | ||||
|             ); | ||||
| 
 | ||||
|             postMethod.setRequestBody(formBodyBuilder.build()); | ||||
| 
 | ||||
|             postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); | ||||
|             postMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); | ||||
| 
 | ||||
|  | ||||
| @ -206,10 +206,11 @@ public class UpdateRemoteShareOperation extends RemoteOperation { | ||||
|             uriBuilder.appendEncodedPath(Long.toString(mRemoteId)); | ||||
| 
 | ||||
|             PutMethod putMethod = new PutMethod( | ||||
|                     HttpUtils.stringUrlToHttpUrl(uriBuilder.build().toString()), | ||||
|                     formBody | ||||
|                     HttpUtils.stringUrlToHttpUrl(uriBuilder.build().toString()) | ||||
|             ); | ||||
| 
 | ||||
|             putMethod.setRequestBody(formBodyBuilder.build()); | ||||
| 
 | ||||
|             putMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); | ||||
|             putMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); | ||||
| 
 | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user