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

Commit 5ca99a0

Browse files
committed
update base url in active client after 301 redirect
reduce validation retry count
1 parent ce761aa commit 5ca99a0

File tree

5 files changed

+43
-15
lines changed

5 files changed

+43
-15
lines changed

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/ConnectionValidator.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class ConnectionValidator (
3232

3333
client.account = baseClient.account
3434
client.credentials = baseClient.credentials
35-
while (validationRetryCount < 5) {
35+
while (validationRetryCount < VALIDATION_RETRY_COUNT) {
3636
Timber.d("+++++++++++++++++++++++++++++++++++++ validationRetryCout %d", validationRetryCount)
3737
var successCounter = 0
3838
var failCounter = 0
@@ -180,4 +180,8 @@ class ConnectionValidator (
180180
}
181181
return credentialsWereRefreshed
182182
}
183+
184+
companion object {
185+
val VALIDATION_RETRY_COUNT = 3
186+
}
183187
}

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import java.util.List;
4949

5050
import static com.owncloud.android.lib.common.http.HttpConstants.AUTHORIZATION_HEADER;
51+
import static com.owncloud.android.lib.common.http.HttpConstants.HTTP_MOVED_PERMANENTLY;
5152
import static com.owncloud.android.lib.common.http.HttpConstants.OC_X_REQUEST_ID;
5253

5354
public class OwnCloudClient extends HttpClient {
@@ -77,7 +78,10 @@ public class OwnCloudClient extends HttpClient {
7778

7879
private boolean mFollowRedirects = false;
7980

80-
public OwnCloudClient(Uri baseUri, ConnectionValidator connectionValidator, boolean synchronizeRequests, SingleSessionManager singleSessionManager) {
81+
public OwnCloudClient(Uri baseUri,
82+
ConnectionValidator connectionValidator,
83+
boolean synchronizeRequests,
84+
SingleSessionManager singleSessionManager) {
8185
if (baseUri == null) {
8286
throw new IllegalArgumentException("Parameter 'baseUri' cannot be NULL");
8387
}
@@ -113,6 +117,10 @@ private int saveExecuteHttpMethod(HttpBaseMethod method) throws Exception {
113117
int repeatCounter = 0;
114118
int status;
115119

120+
if(mFollowRedirects) {
121+
method.setFollowRedirects(true);
122+
}
123+
116124
boolean retry;
117125
do {
118126
repeatCounter++;
@@ -128,16 +136,19 @@ private int saveExecuteHttpMethod(HttpBaseMethod method) throws Exception {
128136
method.setRequestHeader(AUTHORIZATION_HEADER, mCredentials.getHeaderAuth());
129137
}
130138

131-
method.setFollowRedirects(mFollowRedirects);
132139
status = method.execute();
133140
stacklog(status, method);
134141

135142
if (!mFollowRedirects &&
143+
!method.getFollowRedirects() &&
136144
mConnectionValidator != null &&
137145
(status == HttpConstants.HTTP_MOVED_TEMPORARILY ||
138146
(!(mCredentials instanceof OwnCloudAnonymousCredentials) &&
139147
status == HttpConstants.HTTP_UNAUTHORIZED))) {
140148
retry = mConnectionValidator.validate(this, mSingleSessionManager); // retry on success fail on no success
149+
} else if(method.getFollowPermanentRedirects() && status == HTTP_MOVED_PERMANENTLY) {
150+
retry = true;
151+
method.setFollowRedirects(true);
141152
}
142153

143154
} while (retry && repeatCounter < MAX_RETRY_COUNT);

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ abstract class HttpBaseMethod constructor(url: URL) {
4141
var okHttpClient: OkHttpClient
4242
var httpUrl: HttpUrl = url.toHttpUrlOrNull() ?: throw MalformedURLException()
4343
var request: Request
44+
private var _followPermanentRedirects = false
4445
abstract var response: Response
4546

4647
var call: Call? = null
@@ -123,6 +124,11 @@ abstract class HttpBaseMethod constructor(url: URL) {
123124
return response.body?.byteStream()
124125
}
125126

127+
/**
128+
* returns the final url after following the last redirect.
129+
*/
130+
open fun getFinalUrl() = response.request.url
131+
126132
/*************************
127133
*** Connection Params ***
128134
*************************/
@@ -158,6 +164,15 @@ abstract class HttpBaseMethod constructor(url: URL) {
158164
.build()
159165
}
160166

167+
open fun getFollowRedirects() = okHttpClient.followRedirects
168+
169+
open fun setFollPermanentRedirects(followRedirects: Boolean) {
170+
_followPermanentRedirects = followRedirects
171+
}
172+
173+
open fun getFollowPermanentRedirects() = _followPermanentRedirects
174+
175+
161176
/************
162177
*** Call ***
163178
************/

owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/GetRemoteStatusOperation.kt

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,21 @@ class GetRemoteStatusOperation : RemoteOperation<RemoteServerInfo>() {
4848
if(!usesHttpOrHttps(client.baseUri)) {
4949
client.baseUri = buildFullHttpsUrl(client.baseUri)
5050
}
51+
return tryToConnect(client)
52+
}
5153

52-
var result = tryToConnect(client)
53-
/*
54-
if (!(result.code == ResultCode.OK || result.code == ResultCode.OK_SSL) && !result.isSslRecoverableException) {
55-
Timber.d("Establishing secure connection failed, trying non secure connection")
56-
client.baseUri = client.baseUri.buildUpon().scheme(HTTP_SCHEME).build()
57-
result = tryToConnect(client)
58-
}
59-
*/
60-
61-
return result
54+
private fun updateClientBaseUrl(client:OwnCloudClient, newBaseUrl:String) {
55+
client.baseUri = Uri.parse(newBaseUrl)
6256
}
6357

6458
private fun tryToConnect(client: OwnCloudClient): RemoteOperationResult<RemoteServerInfo> {
6559
val baseUrl = client.baseUri.toString()
6660
return try {
6761
val requester = StatusRequester()
6862
val requestResult = requester.request(baseUrl, client)
69-
requester.handleRequestResult(requestResult, baseUrl)
63+
val result = requester.handleRequestResult(requestResult, baseUrl)
64+
updateClientBaseUrl(client, result.data.baseUrl)
65+
return result
7066
} catch (e: JSONException) {
7167
RemoteOperationResult(ResultCode.INSTANCE_NOT_CONFIGURED)
7268
} catch (e: Exception) {

owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/StatusRequester.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,10 @@ internal class StatusRequester {
8383
var status: Int
8484

8585
val getMethod = getGetMethod(currentLocation)
86+
getMethod.setFollPermanentRedirects(true)
8687
status = client.executeHttpMethod(getMethod)
87-
return RequestResult(getMethod, status, redirectedToUnsecureLocation, currentLocation)
88+
89+
return RequestResult(getMethod, status, redirectedToUnsecureLocation, getMethod.getFinalUrl().toString())
8890
}
8991

9092
private fun Int.isSuccess() = this == HttpConstants.HTTP_OK

0 commit comments

Comments
 (0)