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

Commit fc4ae27

Browse files
theScrabiabelgardep
authored andcommitted
remove redundant intercept of httpclient with httpmethod
1 parent 344c1e1 commit fc4ae27

39 files changed

+109
-276
lines changed

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,10 @@ private int saveExecuteHttpMethod(HttpBaseMethod method) throws Exception {
135135
method.setRequestHeader(AUTHORIZATION_HEADER, mCredentials.getHeaderAuth());
136136
}
137137

138-
status = method.execute();
138+
status = method.execute(this);
139139

140140
if (shouldConnectionValidatorBeCalled(method, status)) {
141-
retry = mConnectionValidator.validate(this, mSingleSessionManager); // retry on success fail on no success
141+
retry = mConnectionValidator.validate(this, mSingleSessionManager, getContext()); // retry on success fail on no success
142142
} else if(method.getFollowPermanentRedirects() && status == HTTP_MOVED_PERMANENTLY) {
143143
retry = true;
144144
method.setFollowRedirects(true);

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

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,40 @@ import java.net.MalformedURLException
3737
import java.net.URL
3838
import java.util.concurrent.TimeUnit
3939

40-
abstract class HttpBaseMethod constructor(clientWrapper: HttpClient, url: URL) {
41-
var okHttpClient: OkHttpClient
40+
abstract class HttpBaseMethod constructor(url: URL) {
4241
var httpUrl: HttpUrl = url.toHttpUrlOrNull() ?: throw MalformedURLException()
4342
var request: Request
4443
private var _followPermanentRedirects = false
4544
abstract var response: Response
46-
4745
var call: Call? = null
4846

47+
var followRedirects: Boolean? = true
48+
var retryOnConnectionFailure: Boolean? = false
49+
var connectionTimeoutVal: Long? = null
50+
var connectionTimeoutUnit: TimeUnit? = null
51+
var readTimeoutVal: Long? = null
52+
var readTimeoutUnit: TimeUnit? = null
53+
4954
init {
50-
okHttpClient = clientWrapper.okHttpClient
5155
request = Request.Builder()
5256
.url(httpUrl)
5357
.build()
5458
}
5559

5660
@Throws(Exception::class)
57-
open fun execute(): Int {
58-
return onExecute()
61+
open fun execute(httpClient: HttpClient): Int {
62+
val okHttpClient = httpClient.okHttpClient.newBuilder().apply {
63+
retryOnConnectionFailure?.let { retryOnConnectionFailure(it) }
64+
followRedirects?.let { followRedirects(it) }
65+
readTimeoutUnit?.let { unit ->
66+
readTimeoutVal?.let { readTimeout(it, unit) }
67+
}
68+
connectionTimeoutUnit?.let { unit ->
69+
connectionTimeoutVal?.let { connectTimeout(it, unit) }
70+
}
71+
}.build()
72+
73+
return onExecute(okHttpClient)
5974
}
6075

6176
open fun setUrl(url: HttpUrl) {
@@ -137,42 +152,26 @@ abstract class HttpBaseMethod constructor(clientWrapper: HttpClient, url: URL) {
137152
// Setter
138153
//////////////////////////////
139154
// Connection parameters
155+
/*
140156
open fun setRetryOnConnectionFailure(retryOnConnectionFailure: Boolean) {
141-
okHttpClient = okHttpClient.newBuilder()
142-
.retryOnConnectionFailure(retryOnConnectionFailure)
143-
.build()
157+
retryOnConnectionFailureVal = true
144158
}
145159
160+
*/
161+
146162
open fun setReadTimeout(readTimeout: Long, timeUnit: TimeUnit) {
147-
okHttpClient = okHttpClient.newBuilder()
148-
.readTimeout(readTimeout, timeUnit)
149-
.build()
163+
readTimeoutVal = readTimeout
164+
readTimeoutUnit = timeUnit
150165
}
151166

152167
open fun setConnectionTimeout(
153168
connectionTimeout: Long,
154169
timeUnit: TimeUnit
155170
) {
156-
okHttpClient = okHttpClient.newBuilder()
157-
.readTimeout(connectionTimeout, timeUnit)
158-
.build()
171+
connectionTimeoutVal = connectionTimeout
172+
connectionTimeoutUnit = timeUnit
159173
}
160174

161-
open fun setFollowRedirects(followRedirects: Boolean) {
162-
okHttpClient = okHttpClient.newBuilder()
163-
.followRedirects(followRedirects)
164-
.build()
165-
}
166-
167-
open fun getFollowRedirects() = okHttpClient.followRedirects
168-
169-
open fun setFollowPermanentRedirects(followRedirects: Boolean) {
170-
_followPermanentRedirects = followRedirects
171-
}
172-
173-
open fun getFollowPermanentRedirects() = _followPermanentRedirects
174-
175-
176175
/************
177176
*** Call ***
178177
************/
@@ -187,5 +186,5 @@ abstract class HttpBaseMethod constructor(clientWrapper: HttpClient, url: URL) {
187186
// For override
188187
//////////////////////////////
189188
@Throws(Exception::class)
190-
protected abstract fun onExecute(): Int
189+
protected abstract fun onExecute(okHttpClient: OkHttpClient): Int
191190
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424
package com.owncloud.android.lib.common.http.methods.nonwebdav
2525

26-
import com.owncloud.android.lib.common.http.HttpClient
26+
import okhttp3.OkHttpClient
2727
import java.io.IOException
2828
import java.net.URL
2929

@@ -32,12 +32,12 @@ import java.net.URL
3232
*
3333
* @author David González Verdugo
3434
*/
35-
class DeleteMethod(httpClient: HttpClient, url: URL) : HttpMethod(httpClient, url) {
35+
class DeleteMethod(url: URL) : HttpMethod(url) {
3636
@Throws(IOException::class)
37-
override fun onExecute(): Int {
37+
override fun onExecute(okHttpClient: OkHttpClient): Int {
3838
request = request.newBuilder()
3939
.delete()
4040
.build()
41-
return super.onExecute()
41+
return super.onExecute(okHttpClient)
4242
}
4343
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424
package com.owncloud.android.lib.common.http.methods.nonwebdav
2525

26-
import com.owncloud.android.lib.common.http.HttpClient
26+
import okhttp3.OkHttpClient
2727
import java.io.IOException
2828
import java.net.URL
2929

@@ -32,12 +32,12 @@ import java.net.URL
3232
*
3333
* @author David González Verdugo
3434
*/
35-
class GetMethod(httpClient: HttpClient, url: URL) : HttpMethod(httpClient, url) {
35+
class GetMethod(url: URL) : HttpMethod(url) {
3636
@Throws(IOException::class)
37-
override fun onExecute(): Int {
37+
override fun onExecute(okHttpClient: OkHttpClient): Int {
3838
request = request.newBuilder()
3939
.get()
4040
.build()
41-
return super.onExecute()
41+
return super.onExecute(okHttpClient)
4242
}
4343
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
*/
2424
package com.owncloud.android.lib.common.http.methods.nonwebdav
2525

26-
import com.owncloud.android.lib.common.http.HttpClient
2726
import com.owncloud.android.lib.common.http.methods.HttpBaseMethod
27+
import okhttp3.OkHttpClient
2828
import okhttp3.Response
2929
import java.net.URL
3030

@@ -34,13 +34,12 @@ import java.net.URL
3434
* @author David González Verdugo
3535
*/
3636
abstract class HttpMethod(
37-
httpClient: HttpClient,
3837
url: URL
39-
) : HttpBaseMethod(httpClient, url) {
38+
) : HttpBaseMethod(url) {
4039

4140
override lateinit var response: Response
4241

43-
public override fun onExecute(): Int {
42+
public override fun onExecute(okHttpClient: OkHttpClient): Int {
4443
call = okHttpClient.newCall(request)
4544
call?.let { response = it.execute() }
4645
return super.statusCode

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424
package com.owncloud.android.lib.common.http.methods.nonwebdav
2525

26-
import com.owncloud.android.lib.common.http.HttpClient
26+
import okhttp3.OkHttpClient
2727
import okhttp3.RequestBody
2828
import java.io.IOException
2929
import java.net.URL
@@ -34,15 +34,14 @@ import java.net.URL
3434
* @author David González Verdugo
3535
*/
3636
class PostMethod(
37-
httpClient: HttpClient,
3837
url: URL,
3938
private val postRequestBody: RequestBody
40-
) : HttpMethod(httpClient, url) {
39+
) : HttpMethod(url) {
4140
@Throws(IOException::class)
42-
override fun onExecute(): Int {
41+
override fun onExecute(okHttpClient: OkHttpClient): Int {
4342
request = request.newBuilder()
4443
.post(postRequestBody)
4544
.build()
46-
return super.onExecute()
45+
return super.onExecute(okHttpClient)
4746
}
4847
}

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424
package com.owncloud.android.lib.common.http.methods.nonwebdav
2525

26-
import com.owncloud.android.lib.common.http.HttpClient
26+
import okhttp3.OkHttpClient
2727
import okhttp3.RequestBody
2828
import java.io.IOException
2929
import java.net.URL
@@ -34,15 +34,14 @@ import java.net.URL
3434
* @author David González Verdugo
3535
*/
3636
class PutMethod(
37-
httpClient: HttpClient,
3837
url: URL,
3938
private val putRequestBody: RequestBody
40-
) : HttpMethod(httpClient, url) {
39+
) : HttpMethod(url) {
4140
@Throws(IOException::class)
42-
override fun onExecute(): Int {
41+
override fun onExecute(okHttpClient: OkHttpClient): Int {
4342
request = request.newBuilder()
4443
.put(putRequestBody)
4544
.build()
46-
return super.onExecute()
45+
return super.onExecute(okHttpClient)
4746
}
4847
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424
package com.owncloud.android.lib.common.http.methods.webdav
2525

26-
import com.owncloud.android.lib.common.http.HttpClient
26+
import at.bitfire.dav4jvm.DavOCResource
2727
import okhttp3.Response
2828
import java.net.URL
2929

@@ -34,13 +34,12 @@ import java.net.URL
3434
* @author David González Verdugo
3535
*/
3636
class CopyMethod(
37-
httpClient: HttpClient,
3837
val url: URL,
3938
private val destinationUrl: String,
4039
private val forceOverride: Boolean
41-
) : DavMethod(httpClient, url) {
40+
) : DavMethod(url) {
4241
@Throws(Exception::class)
43-
public override fun onExecute(): Int {
42+
public override fun onDavExecute(davResource: DavOCResource): Int {
4443
davResource.copy(
4544
destinationUrl,
4645
forceOverride,

0 commit comments

Comments
 (0)