mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-10-31 02:17:41 +00:00 
			
		
		
		
	Move RequestBody to constructor to avoid npe
This commit is contained in:
		
							parent
							
								
									f89cfd91a9
								
							
						
					
					
						commit
						31d3bfbde9
					
				| @ -7,7 +7,6 @@ import okhttp3.HttpUrl | |||||||
| import okhttp3.HttpUrl.Companion.toHttpUrlOrNull | import okhttp3.HttpUrl.Companion.toHttpUrlOrNull | ||||||
| import okhttp3.OkHttpClient | import okhttp3.OkHttpClient | ||||||
| import okhttp3.Request | import okhttp3.Request | ||||||
| import okhttp3.RequestBody |  | ||||||
| import okhttp3.Response | import okhttp3.Response | ||||||
| import java.io.InputStream | import java.io.InputStream | ||||||
| import java.net.MalformedURLException | import java.net.MalformedURLException | ||||||
| @ -18,7 +17,6 @@ abstract class HttpBaseMethod constructor(url: URL) { | |||||||
|     var okHttpClient: OkHttpClient |     var okHttpClient: OkHttpClient | ||||||
|     var httpUrl: HttpUrl = url.toHttpUrlOrNull() ?: throw MalformedURLException() |     var httpUrl: HttpUrl = url.toHttpUrlOrNull() ?: throw MalformedURLException() | ||||||
|     var request: Request |     var request: Request | ||||||
|     var requestBody: RequestBody? = null |  | ||||||
|     abstract var response: Response |     abstract var response: Response | ||||||
| 
 | 
 | ||||||
|     var call: Call? = null |     var call: Call? = null | ||||||
| @ -45,17 +43,13 @@ abstract class HttpBaseMethod constructor(url: URL) { | |||||||
|      *** Requests *** |      *** Requests *** | ||||||
|      ****************/ |      ****************/ | ||||||
| 
 | 
 | ||||||
|     // Headers |  | ||||||
|     val requestHeaders: Headers |  | ||||||
|         get() = request.headers |  | ||||||
| 
 |  | ||||||
|     fun getRequestHeader(name: String): String? { |     fun getRequestHeader(name: String): String? { | ||||||
|         return request.header(name) |         return request.header(name) | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     fun getRequestHeadersAsHashMap(): HashMap<String, String?> { |     fun getRequestHeadersAsHashMap(): HashMap<String, String?> { | ||||||
|         val headers: HashMap<String, String?> = HashMap() |         val headers: HashMap<String, String?> = HashMap() | ||||||
|         val superHeaders: Set<String> = requestHeaders.names() |         val superHeaders: Set<String> = request.headers.names() | ||||||
|         superHeaders.forEach { |         superHeaders.forEach { | ||||||
|             headers[it] = getRequestHeader(it) |             headers[it] = getRequestHeader(it) | ||||||
|         } |         } | ||||||
|  | |||||||
| @ -32,10 +32,12 @@ import java.net.URL | |||||||
|  * |  * | ||||||
|  * @author David González Verdugo |  * @author David González Verdugo | ||||||
|  */ |  */ | ||||||
| class PostMethod(url: URL, private val postRequestBody: RequestBody) : HttpMethod(url) { | class PostMethod( | ||||||
|  |     url: URL, | ||||||
|  |     private val postRequestBody: RequestBody | ||||||
|  | ) : HttpMethod(url) { | ||||||
|     @Throws(IOException::class) |     @Throws(IOException::class) | ||||||
|     override fun onExecute(): Int { |     override fun onExecute(): Int { | ||||||
|         requestBody = postRequestBody |  | ||||||
|         request = request.newBuilder() |         request = request.newBuilder() | ||||||
|             .post(postRequestBody) |             .post(postRequestBody) | ||||||
|             .build() |             .build() | ||||||
|  | |||||||
| @ -38,7 +38,6 @@ class PutMethod( | |||||||
| ) : HttpMethod(url) { | ) : HttpMethod(url) { | ||||||
|     @Throws(IOException::class) |     @Throws(IOException::class) | ||||||
|     override fun onExecute(): Int { |     override fun onExecute(): Int { | ||||||
|         requestBody = putRequestBody |  | ||||||
|         request = request.newBuilder() |         request = request.newBuilder() | ||||||
|             .put(putRequestBody) |             .put(putRequestBody) | ||||||
|             .build() |             .build() | ||||||
|  | |||||||
| @ -25,6 +25,7 @@ package com.owncloud.android.lib.common.http.methods.webdav | |||||||
| 
 | 
 | ||||||
| import at.bitfire.dav4jvm.exception.HttpException | import at.bitfire.dav4jvm.exception.HttpException | ||||||
| import com.owncloud.android.lib.common.http.HttpConstants | import com.owncloud.android.lib.common.http.HttpConstants | ||||||
|  | import okhttp3.RequestBody | ||||||
| import java.io.IOException | import java.io.IOException | ||||||
| import java.net.URL | import java.net.URL | ||||||
| 
 | 
 | ||||||
| @ -34,12 +35,13 @@ import java.net.URL | |||||||
|  * @author David González Verdugo |  * @author David González Verdugo | ||||||
|  */ |  */ | ||||||
| class PutMethod( | class PutMethod( | ||||||
|     url: URL |     url: URL, | ||||||
|  |     private val putRequestBody: RequestBody | ||||||
| ) : DavMethod(url) { | ) : DavMethod(url) { | ||||||
|     @Throws(IOException::class, HttpException::class) |     @Throws(IOException::class, HttpException::class) | ||||||
|     public override fun onExecute(): Int { |     public override fun onExecute(): Int { | ||||||
|         davResource.put( |         davResource.put( | ||||||
|             requestBody!!, |             putRequestBody, | ||||||
|             super.getRequestHeader(HttpConstants.IF_MATCH_HEADER), |             super.getRequestHeader(HttpConstants.IF_MATCH_HEADER), | ||||||
|             getRequestHeadersAsHashMap() |             getRequestHeadersAsHashMap() | ||||||
|         ) { callBackResponse -> |         ) { callBackResponse -> | ||||||
|  | |||||||
| @ -84,10 +84,6 @@ public class UploadRemoteFileOperation extends RemoteOperation { | |||||||
|         RemoteOperationResult result; |         RemoteOperationResult result; | ||||||
| 
 | 
 | ||||||
|         try { |         try { | ||||||
|             mPutMethod = new PutMethod( |  | ||||||
|                     new URL(client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath))); |  | ||||||
| 
 |  | ||||||
|             mPutMethod.setRetryOnConnectionFailure(false); |  | ||||||
| 
 | 
 | ||||||
|             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 | ||||||
| @ -125,6 +121,11 @@ public class UploadRemoteFileOperation extends RemoteOperation { | |||||||
|             mFileRequestBody.addDatatransferProgressListeners(mDataTransferListeners); |             mFileRequestBody.addDatatransferProgressListeners(mDataTransferListeners); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|  |         mPutMethod = new PutMethod( | ||||||
|  |                 new URL(client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath)), mFileRequestBody); | ||||||
|  | 
 | ||||||
|  |         mPutMethod.setRetryOnConnectionFailure(false); | ||||||
|  | 
 | ||||||
|         if (mRequiredEtag != null && mRequiredEtag.length() > 0) { |         if (mRequiredEtag != null && mRequiredEtag.length() > 0) { | ||||||
|             mPutMethod.addRequestHeader(HttpConstants.IF_MATCH_HEADER, mRequiredEtag); |             mPutMethod.addRequestHeader(HttpConstants.IF_MATCH_HEADER, mRequiredEtag); | ||||||
|         } |         } | ||||||
| @ -132,8 +133,6 @@ public class UploadRemoteFileOperation extends RemoteOperation { | |||||||
|         mPutMethod.addRequestHeader(HttpConstants.OC_TOTAL_LENGTH_HEADER, String.valueOf(fileToUpload.length())); |         mPutMethod.addRequestHeader(HttpConstants.OC_TOTAL_LENGTH_HEADER, String.valueOf(fileToUpload.length())); | ||||||
|         mPutMethod.addRequestHeader(HttpConstants.OC_X_OC_MTIME_HEADER, mFileLastModifTimestamp); |         mPutMethod.addRequestHeader(HttpConstants.OC_X_OC_MTIME_HEADER, mFileLastModifTimestamp); | ||||||
| 
 | 
 | ||||||
|         mPutMethod.setRequestBody(mFileRequestBody); |  | ||||||
| 
 |  | ||||||
|         int status = client.executeHttpMethod(mPutMethod); |         int status = client.executeHttpMethod(mPutMethod); | ||||||
| 
 | 
 | ||||||
|         if (isSuccess(status)) { |         if (isSuccess(status)) { | ||||||
|  | |||||||
| @ -86,11 +86,6 @@ public class ChunkedUploadRemoteFileOperation extends UploadRemoteFileOperation | |||||||
|         long chunkCount = (long) Math.ceil((double) totalLength / CHUNK_SIZE); |         long chunkCount = (long) Math.ceil((double) totalLength / CHUNK_SIZE); | ||||||
| 
 | 
 | ||||||
|         for (int chunkIndex = 0; chunkIndex < chunkCount; chunkIndex++, offset += CHUNK_SIZE) { |         for (int chunkIndex = 0; chunkIndex < chunkCount; chunkIndex++, offset += CHUNK_SIZE) { | ||||||
|             mPutMethod = new PutMethod(new URL(uriPrefix + File.separator + chunkIndex)); |  | ||||||
| 
 |  | ||||||
|             if (mRequiredEtag != null && mRequiredEtag.length() > 0) { |  | ||||||
|                 mPutMethod.addRequestHeader(IF_MATCH_HEADER, "\"" + mRequiredEtag + "\""); |  | ||||||
|             } |  | ||||||
| 
 | 
 | ||||||
|             ((ChunkFromFileRequestBody) mFileRequestBody).setOffset(offset); |             ((ChunkFromFileRequestBody) mFileRequestBody).setOffset(offset); | ||||||
| 
 | 
 | ||||||
| @ -98,6 +93,12 @@ public class ChunkedUploadRemoteFileOperation extends UploadRemoteFileOperation | |||||||
|                 result = new RemoteOperationResult<>(new OperationCancelledException()); |                 result = new RemoteOperationResult<>(new OperationCancelledException()); | ||||||
|                 break; |                 break; | ||||||
|             } else { |             } else { | ||||||
|  |                 mPutMethod = new PutMethod(new URL(uriPrefix + File.separator + chunkIndex), mFileRequestBody); | ||||||
|  | 
 | ||||||
|  |                 if (mRequiredEtag != null && mRequiredEtag.length() > 0) { | ||||||
|  |                     mPutMethod.addRequestHeader(IF_MATCH_HEADER, "\"" + mRequiredEtag + "\""); | ||||||
|  |                 } | ||||||
|  | 
 | ||||||
|                 if (chunkIndex == chunkCount - 1) { |                 if (chunkIndex == chunkCount - 1) { | ||||||
|                     // Added a high timeout to the last chunk due to when the last chunk |                     // Added a high timeout to the last chunk due to when the last chunk | ||||||
|                     // arrives to the server with the last PUT, all chunks get assembled |                     // arrives to the server with the last PUT, all chunks get assembled | ||||||
| @ -105,8 +106,6 @@ public class ChunkedUploadRemoteFileOperation extends UploadRemoteFileOperation | |||||||
|                     mPutMethod.setReadTimeout(LAST_CHUNK_TIMEOUT, TimeUnit.MILLISECONDS); |                     mPutMethod.setReadTimeout(LAST_CHUNK_TIMEOUT, TimeUnit.MILLISECONDS); | ||||||
|                 } |                 } | ||||||
| 
 | 
 | ||||||
|                 mPutMethod.setRequestBody(mFileRequestBody); |  | ||||||
| 
 |  | ||||||
|                 status = client.executeHttpMethod(mPutMethod); |                 status = client.executeHttpMethod(mPutMethod); | ||||||
| 
 | 
 | ||||||
|                 Timber.d("Upload of " + mLocalPath + " to " + mRemotePath + |                 Timber.d("Upload of " + mLocalPath + " to " + mRemotePath + | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user