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 { | 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 |      * 1xx Informational | ||||||
|      */ |      */ | ||||||
|  | |||||||
| @ -33,6 +33,7 @@ import okhttp3.Headers; | |||||||
| import okhttp3.HttpUrl; | import okhttp3.HttpUrl; | ||||||
| import okhttp3.OkHttpClient; | import okhttp3.OkHttpClient; | ||||||
| import okhttp3.Request; | import okhttp3.Request; | ||||||
|  | import okhttp3.RequestBody; | ||||||
| import okhttp3.Response; | import okhttp3.Response; | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
| @ -46,6 +47,7 @@ public abstract class HttpBaseMethod { | |||||||
|     public abstract int execute() throws Exception; |     public abstract int execute() throws Exception; | ||||||
|     protected OkHttpClient mOkHttpClient; |     protected OkHttpClient mOkHttpClient; | ||||||
|     protected Request mRequest; |     protected Request mRequest; | ||||||
|  |     protected RequestBody mRequestBody; | ||||||
|     protected Response mResponse; |     protected Response mResponse; | ||||||
| 
 | 
 | ||||||
|     protected HttpBaseMethod (HttpUrl httpUrl) { |     protected HttpBaseMethod (HttpUrl httpUrl) { | ||||||
| @ -56,19 +58,28 @@ public abstract class HttpBaseMethod { | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     // Request |     // Request | ||||||
|  |     public String getRequestHeader(String name) { | ||||||
|  |         return mRequest.header(name); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     public Headers getRequestHeaders() { |     public Headers getRequestHeaders() { | ||||||
|         return mRequest.headers(); |         return mRequest.headers(); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public void addRequestHeader(String name, String value) { |     public void addRequestHeader(String name, String value) { | ||||||
|         mRequest.newBuilder() |         mRequest = mRequest.newBuilder() | ||||||
|                 .addHeader(name, value) |                 .addHeader(name, value) | ||||||
|                 .build(); |                 .build(); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public void setRequestHeader(String name, String value){ |     public void setRequestHeader(String name, String value) { | ||||||
|         mRequest.newBuilder() |         mRequest = mRequest.newBuilder() | ||||||
|                 .header(name, value); |                 .header(name, value) | ||||||
|  |                 .build(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void setRequestBody(RequestBody requestBody) { | ||||||
|  |         mRequestBody = requestBody; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     // Response |     // Response | ||||||
|  | |||||||
| @ -35,11 +35,8 @@ import okhttp3.RequestBody; | |||||||
|  */ |  */ | ||||||
| public class PostMethod extends HttpMethod { | public class PostMethod extends HttpMethod { | ||||||
| 
 | 
 | ||||||
|     private RequestBody mRequestBody; |     public PostMethod(HttpUrl httpUrl){ | ||||||
| 
 |  | ||||||
|     public PostMethod(HttpUrl httpUrl, RequestBody requestBody){ |  | ||||||
|         super(httpUrl); |         super(httpUrl); | ||||||
|         mRequestBody = requestBody; |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
|  | |||||||
| @ -31,11 +31,8 @@ import okhttp3.RequestBody; | |||||||
| 
 | 
 | ||||||
| public class PutMethod extends HttpMethod{ | public class PutMethod extends HttpMethod{ | ||||||
| 
 | 
 | ||||||
|     private RequestBody mRequestBody; |     public PutMethod(HttpUrl httpUrl){ | ||||||
| 
 |  | ||||||
|     public PutMethod(HttpUrl httpUrl, RequestBody requestBody){ |  | ||||||
|         super(httpUrl); |         super(httpUrl); | ||||||
|         mRequestBody = requestBody; |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
|  | |||||||
| @ -24,6 +24,8 @@ | |||||||
| 
 | 
 | ||||||
| package com.owncloud.android.lib.common.http.methods.webdav; | package com.owncloud.android.lib.common.http.methods.webdav; | ||||||
| 
 | 
 | ||||||
|  | import com.owncloud.android.lib.common.http.HttpConstants; | ||||||
|  | 
 | ||||||
| import java.io.IOException; | import java.io.IOException; | ||||||
| 
 | 
 | ||||||
| import at.bitfire.dav4android.exception.DavException; | import at.bitfire.dav4android.exception.DavException; | ||||||
| @ -38,13 +40,6 @@ import okhttp3.RequestBody; | |||||||
|  */ |  */ | ||||||
| public class PutMethod extends DavMethod { | 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) { |     public PutMethod(HttpUrl httpUrl) { | ||||||
|         super(httpUrl); |         super(httpUrl); | ||||||
|     }; |     }; | ||||||
| @ -54,11 +49,13 @@ public class PutMethod extends DavMethod { | |||||||
|         try { |         try { | ||||||
|             mDavResource.put( |             mDavResource.put( | ||||||
|                     mRequestBody, |                     mRequestBody, | ||||||
|                     mIfMatchETag, |                     super.getRequestHeader(HttpConstants.IF_MATCH_HEADER), | ||||||
|                     mIfNoneMatch, |                     // Save a file not known to exist, guaranteeing that another upload didn't happen | ||||||
|                     mContentType, |                     // before, losing the data of the previous put | ||||||
|                     mOcTotalLength, |                     true, | ||||||
|                     mOcXOcMtimeHeader |                     super.getRequestHeader(HttpConstants.CONTENT_TYPE_HEADER), | ||||||
|  |                     super.getRequestHeader(HttpConstants.OC_TOTAL_LENGTH_HEADER), | ||||||
|  |                     super.getRequestHeader(HttpConstants.OC_X_OC_MTIME_HEADER) | ||||||
|             ); |             ); | ||||||
| 
 | 
 | ||||||
|             mRequest = mDavResource.getRequest(); |             mRequest = mDavResource.getRequest(); | ||||||
|  | |||||||
| @ -47,6 +47,9 @@ 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 okhttp3.MediaType; | ||||||
|  | import okhttp3.RequestBody; | ||||||
|  | 
 | ||||||
| import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK; | 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(); |     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 mLocalPath; | ||||||
|     protected String mRemotePath; |     protected String mRemotePath; | ||||||
|     protected String mMimeType; |     protected String mMimeType; | ||||||
| @ -107,7 +106,7 @@ public class UploadRemoteFileOperation extends RemoteOperation { | |||||||
| //            ); | //            ); | ||||||
| 
 | 
 | ||||||
|             mPutMethod = new PutMethod( |             mPutMethod = new PutMethod( | ||||||
|                     HttpUtils.stringUrlToHttpUrl(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath))); |                     HttpUtils.stringUrlToHttpUrl(client.getNewWebDavUri() + 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 | ||||||
| @ -138,8 +137,8 @@ public class UploadRemoteFileOperation extends RemoteOperation { | |||||||
|     protected RemoteOperationResult uploadFile(OwnCloudClient client) throws IOException { |     protected RemoteOperationResult uploadFile(OwnCloudClient client) throws IOException { | ||||||
|         RemoteOperationResult result; |         RemoteOperationResult result; | ||||||
|         try { |         try { | ||||||
|             File f = new File(mLocalPath); |             // Headers | ||||||
| 
 |             File fileToUpload = new File(mLocalPath); | ||||||
|             // TODO |             // TODO | ||||||
| //            synchronized (mDataTransferListeners) { | //            synchronized (mDataTransferListeners) { | ||||||
| //                ((ProgressiveDataTransferer)mEntity) | //                ((ProgressiveDataTransferer)mEntity) | ||||||
| @ -147,12 +146,19 @@ public class UploadRemoteFileOperation extends RemoteOperation { | |||||||
| //            } | //            } | ||||||
| 
 | 
 | ||||||
|             if (mRequiredEtag != null && mRequiredEtag.length() > 0) { |             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); |             int status = client.executeHttpMethod(mPutMethod); | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -226,17 +226,16 @@ public class CreateRemoteShareOperation extends RemoteOperation { | |||||||
|                 formBodyBuilder.add(PARAM_PERMISSIONS, Integer.toString(mPermissions)); |                 formBodyBuilder.add(PARAM_PERMISSIONS, Integer.toString(mPermissions)); | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|             FormBody formBody = formBodyBuilder.build(); |  | ||||||
| 
 |  | ||||||
|             Uri requestUri = client.getBaseUri(); |             Uri requestUri = client.getBaseUri(); | ||||||
|             Uri.Builder uriBuilder = requestUri.buildUpon(); |             Uri.Builder uriBuilder = requestUri.buildUpon(); | ||||||
|             uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH); |             uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH); | ||||||
| 
 | 
 | ||||||
|             PostMethod postMethod = new PostMethod( |             PostMethod postMethod = new PostMethod( | ||||||
|                     HttpUtils.stringUrlToHttpUrl(uriBuilder.build().toString()), |                     HttpUtils.stringUrlToHttpUrl(uriBuilder.build().toString()) | ||||||
|                     formBody |  | ||||||
|             ); |             ); | ||||||
| 
 | 
 | ||||||
|  |             postMethod.setRequestBody(formBodyBuilder.build()); | ||||||
|  | 
 | ||||||
|             postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); |             postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); | ||||||
|             postMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); |             postMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -206,10 +206,11 @@ public class UpdateRemoteShareOperation extends RemoteOperation { | |||||||
|             uriBuilder.appendEncodedPath(Long.toString(mRemoteId)); |             uriBuilder.appendEncodedPath(Long.toString(mRemoteId)); | ||||||
| 
 | 
 | ||||||
|             PutMethod putMethod = new PutMethod( |             PutMethod putMethod = new PutMethod( | ||||||
|                     HttpUtils.stringUrlToHttpUrl(uriBuilder.build().toString()), |                     HttpUtils.stringUrlToHttpUrl(uriBuilder.build().toString()) | ||||||
|                     formBody |  | ||||||
|             ); |             ); | ||||||
| 
 | 
 | ||||||
|  |             putMethod.setRequestBody(formBodyBuilder.build()); | ||||||
|  | 
 | ||||||
|             putMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); |             putMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); | ||||||
|             putMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); |             putMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user