Skip to content
This repository was archived by the owner on Mar 19, 2024. It is now read-only.

Commit 31d3bfb

Browse files
committed
Move RequestBody to constructor to avoid npe
1 parent f89cfd9 commit 31d3bfb

File tree

6 files changed

+20
-25
lines changed

6 files changed

+20
-25
lines changed

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/HttpBaseMethod.kt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import okhttp3.HttpUrl
77
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
88
import okhttp3.OkHttpClient
99
import okhttp3.Request
10-
import okhttp3.RequestBody
1110
import okhttp3.Response
1211
import java.io.InputStream
1312
import java.net.MalformedURLException
@@ -18,7 +17,6 @@ abstract class HttpBaseMethod constructor(url: URL) {
1817
var okHttpClient: OkHttpClient
1918
var httpUrl: HttpUrl = url.toHttpUrlOrNull() ?: throw MalformedURLException()
2019
var request: Request
21-
var requestBody: RequestBody? = null
2220
abstract var response: Response
2321

2422
var call: Call? = null
@@ -45,17 +43,13 @@ abstract class HttpBaseMethod constructor(url: URL) {
4543
*** Requests ***
4644
****************/
4745

48-
// Headers
49-
val requestHeaders: Headers
50-
get() = request.headers
51-
5246
fun getRequestHeader(name: String): String? {
5347
return request.header(name)
5448
}
5549

5650
fun getRequestHeadersAsHashMap(): HashMap<String, String?> {
5751
val headers: HashMap<String, String?> = HashMap()
58-
val superHeaders: Set<String> = requestHeaders.names()
52+
val superHeaders: Set<String> = request.headers.names()
5953
superHeaders.forEach {
6054
headers[it] = getRequestHeader(it)
6155
}

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PostMethod.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ import java.net.URL
3232
*
3333
* @author David González Verdugo
3434
*/
35-
class PostMethod(url: URL, private val postRequestBody: RequestBody) : HttpMethod(url) {
35+
class PostMethod(
36+
url: URL,
37+
private val postRequestBody: RequestBody
38+
) : HttpMethod(url) {
3639
@Throws(IOException::class)
3740
override fun onExecute(): Int {
38-
requestBody = postRequestBody
3941
request = request.newBuilder()
4042
.post(postRequestBody)
4143
.build()

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/nonwebdav/PutMethod.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ class PutMethod(
3838
) : HttpMethod(url) {
3939
@Throws(IOException::class)
4040
override fun onExecute(): Int {
41-
requestBody = putRequestBody
4241
request = request.newBuilder()
4342
.put(putRequestBody)
4443
.build()

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/methods/webdav/PutMethod.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ package com.owncloud.android.lib.common.http.methods.webdav
2525

2626
import at.bitfire.dav4jvm.exception.HttpException
2727
import com.owncloud.android.lib.common.http.HttpConstants
28+
import okhttp3.RequestBody
2829
import java.io.IOException
2930
import java.net.URL
3031

@@ -34,12 +35,13 @@ import java.net.URL
3435
* @author David González Verdugo
3536
*/
3637
class PutMethod(
37-
url: URL
38+
url: URL,
39+
private val putRequestBody: RequestBody
3840
) : DavMethod(url) {
3941
@Throws(IOException::class, HttpException::class)
4042
public override fun onExecute(): Int {
4143
davResource.put(
42-
requestBody!!,
44+
putRequestBody,
4345
super.getRequestHeader(HttpConstants.IF_MATCH_HEADER),
4446
getRequestHeadersAsHashMap()
4547
) { callBackResponse ->

owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/UploadRemoteFileOperation.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,6 @@ protected RemoteOperationResult run(OwnCloudClient client) {
8484
RemoteOperationResult result;
8585

8686
try {
87-
mPutMethod = new PutMethod(
88-
new URL(client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath)));
89-
90-
mPutMethod.setRetryOnConnectionFailure(false);
9187

9288
if (mCancellationRequested.get()) {
9389
// the operation was cancelled before getting it's turn to be executed in the queue of uploads
@@ -125,15 +121,18 @@ protected RemoteOperationResult<?> uploadFile(OwnCloudClient client) throws Exce
125121
mFileRequestBody.addDatatransferProgressListeners(mDataTransferListeners);
126122
}
127123

124+
mPutMethod = new PutMethod(
125+
new URL(client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath)), mFileRequestBody);
126+
127+
mPutMethod.setRetryOnConnectionFailure(false);
128+
128129
if (mRequiredEtag != null && mRequiredEtag.length() > 0) {
129130
mPutMethod.addRequestHeader(HttpConstants.IF_MATCH_HEADER, mRequiredEtag);
130131
}
131132

132133
mPutMethod.addRequestHeader(HttpConstants.OC_TOTAL_LENGTH_HEADER, String.valueOf(fileToUpload.length()));
133134
mPutMethod.addRequestHeader(HttpConstants.OC_X_OC_MTIME_HEADER, mFileLastModifTimestamp);
134135

135-
mPutMethod.setRequestBody(mFileRequestBody);
136-
137136
int status = client.executeHttpMethod(mPutMethod);
138137

139138
if (isSuccess(status)) {

owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/chunks/ChunkedUploadRemoteFileOperation.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,27 +86,26 @@ protected RemoteOperationResult uploadFile(OwnCloudClient client) throws Excepti
8686
long chunkCount = (long) Math.ceil((double) totalLength / CHUNK_SIZE);
8787

8888
for (int chunkIndex = 0; chunkIndex < chunkCount; chunkIndex++, offset += CHUNK_SIZE) {
89-
mPutMethod = new PutMethod(new URL(uriPrefix + File.separator + chunkIndex));
90-
91-
if (mRequiredEtag != null && mRequiredEtag.length() > 0) {
92-
mPutMethod.addRequestHeader(IF_MATCH_HEADER, "\"" + mRequiredEtag + "\"");
93-
}
9489

9590
((ChunkFromFileRequestBody) mFileRequestBody).setOffset(offset);
9691

9792
if (mCancellationRequested.get()) {
9893
result = new RemoteOperationResult<>(new OperationCancelledException());
9994
break;
10095
} else {
96+
mPutMethod = new PutMethod(new URL(uriPrefix + File.separator + chunkIndex), mFileRequestBody);
97+
98+
if (mRequiredEtag != null && mRequiredEtag.length() > 0) {
99+
mPutMethod.addRequestHeader(IF_MATCH_HEADER, "\"" + mRequiredEtag + "\"");
100+
}
101+
101102
if (chunkIndex == chunkCount - 1) {
102103
// Added a high timeout to the last chunk due to when the last chunk
103104
// arrives to the server with the last PUT, all chunks get assembled
104105
// within that PHP request, so last one takes longer.
105106
mPutMethod.setReadTimeout(LAST_CHUNK_TIMEOUT, TimeUnit.MILLISECONDS);
106107
}
107108

108-
mPutMethod.setRequestBody(mFileRequestBody);
109-
110109
status = client.executeHttpMethod(mPutMethod);
111110

112111
Timber.d("Upload of " + mLocalPath + " to " + mRemotePath +

0 commit comments

Comments
 (0)