mirror of
https://github.com/owncloud/android-library.git
synced 2025-06-07 07:56:19 +00:00
update base url in active client after 301 redirect
reduce validation retry count
This commit is contained in:
parent
ce761aaec2
commit
5ca99a0e69
@ -32,7 +32,7 @@ class ConnectionValidator (
|
||||
|
||||
client.account = baseClient.account
|
||||
client.credentials = baseClient.credentials
|
||||
while (validationRetryCount < 5) {
|
||||
while (validationRetryCount < VALIDATION_RETRY_COUNT) {
|
||||
Timber.d("+++++++++++++++++++++++++++++++++++++ validationRetryCout %d", validationRetryCount)
|
||||
var successCounter = 0
|
||||
var failCounter = 0
|
||||
@ -180,4 +180,8 @@ class ConnectionValidator (
|
||||
}
|
||||
return credentialsWereRefreshed
|
||||
}
|
||||
|
||||
companion object {
|
||||
val VALIDATION_RETRY_COUNT = 3
|
||||
}
|
||||
}
|
@ -48,6 +48,7 @@ import java.io.InputStream;
|
||||
import java.util.List;
|
||||
|
||||
import static com.owncloud.android.lib.common.http.HttpConstants.AUTHORIZATION_HEADER;
|
||||
import static com.owncloud.android.lib.common.http.HttpConstants.HTTP_MOVED_PERMANENTLY;
|
||||
import static com.owncloud.android.lib.common.http.HttpConstants.OC_X_REQUEST_ID;
|
||||
|
||||
public class OwnCloudClient extends HttpClient {
|
||||
@ -77,7 +78,10 @@ public class OwnCloudClient extends HttpClient {
|
||||
|
||||
private boolean mFollowRedirects = false;
|
||||
|
||||
public OwnCloudClient(Uri baseUri, ConnectionValidator connectionValidator, boolean synchronizeRequests, SingleSessionManager singleSessionManager) {
|
||||
public OwnCloudClient(Uri baseUri,
|
||||
ConnectionValidator connectionValidator,
|
||||
boolean synchronizeRequests,
|
||||
SingleSessionManager singleSessionManager) {
|
||||
if (baseUri == null) {
|
||||
throw new IllegalArgumentException("Parameter 'baseUri' cannot be NULL");
|
||||
}
|
||||
@ -113,6 +117,10 @@ public class OwnCloudClient extends HttpClient {
|
||||
int repeatCounter = 0;
|
||||
int status;
|
||||
|
||||
if(mFollowRedirects) {
|
||||
method.setFollowRedirects(true);
|
||||
}
|
||||
|
||||
boolean retry;
|
||||
do {
|
||||
repeatCounter++;
|
||||
@ -128,16 +136,19 @@ public class OwnCloudClient extends HttpClient {
|
||||
method.setRequestHeader(AUTHORIZATION_HEADER, mCredentials.getHeaderAuth());
|
||||
}
|
||||
|
||||
method.setFollowRedirects(mFollowRedirects);
|
||||
status = method.execute();
|
||||
stacklog(status, method);
|
||||
|
||||
if (!mFollowRedirects &&
|
||||
!method.getFollowRedirects() &&
|
||||
mConnectionValidator != null &&
|
||||
(status == HttpConstants.HTTP_MOVED_TEMPORARILY ||
|
||||
(!(mCredentials instanceof OwnCloudAnonymousCredentials) &&
|
||||
status == HttpConstants.HTTP_UNAUTHORIZED))) {
|
||||
retry = mConnectionValidator.validate(this, mSingleSessionManager); // retry on success fail on no success
|
||||
} else if(method.getFollowPermanentRedirects() && status == HTTP_MOVED_PERMANENTLY) {
|
||||
retry = true;
|
||||
method.setFollowRedirects(true);
|
||||
}
|
||||
|
||||
} while (retry && repeatCounter < MAX_RETRY_COUNT);
|
||||
|
@ -41,6 +41,7 @@ abstract class HttpBaseMethod constructor(url: URL) {
|
||||
var okHttpClient: OkHttpClient
|
||||
var httpUrl: HttpUrl = url.toHttpUrlOrNull() ?: throw MalformedURLException()
|
||||
var request: Request
|
||||
private var _followPermanentRedirects = false
|
||||
abstract var response: Response
|
||||
|
||||
var call: Call? = null
|
||||
@ -123,6 +124,11 @@ abstract class HttpBaseMethod constructor(url: URL) {
|
||||
return response.body?.byteStream()
|
||||
}
|
||||
|
||||
/**
|
||||
* returns the final url after following the last redirect.
|
||||
*/
|
||||
open fun getFinalUrl() = response.request.url
|
||||
|
||||
/*************************
|
||||
*** Connection Params ***
|
||||
*************************/
|
||||
@ -158,6 +164,15 @@ abstract class HttpBaseMethod constructor(url: URL) {
|
||||
.build()
|
||||
}
|
||||
|
||||
open fun getFollowRedirects() = okHttpClient.followRedirects
|
||||
|
||||
open fun setFollPermanentRedirects(followRedirects: Boolean) {
|
||||
_followPermanentRedirects = followRedirects
|
||||
}
|
||||
|
||||
open fun getFollowPermanentRedirects() = _followPermanentRedirects
|
||||
|
||||
|
||||
/************
|
||||
*** Call ***
|
||||
************/
|
||||
|
@ -48,17 +48,11 @@ class GetRemoteStatusOperation : RemoteOperation<RemoteServerInfo>() {
|
||||
if(!usesHttpOrHttps(client.baseUri)) {
|
||||
client.baseUri = buildFullHttpsUrl(client.baseUri)
|
||||
}
|
||||
return tryToConnect(client)
|
||||
}
|
||||
|
||||
var result = tryToConnect(client)
|
||||
/*
|
||||
if (!(result.code == ResultCode.OK || result.code == ResultCode.OK_SSL) && !result.isSslRecoverableException) {
|
||||
Timber.d("Establishing secure connection failed, trying non secure connection")
|
||||
client.baseUri = client.baseUri.buildUpon().scheme(HTTP_SCHEME).build()
|
||||
result = tryToConnect(client)
|
||||
}
|
||||
*/
|
||||
|
||||
return result
|
||||
private fun updateClientBaseUrl(client:OwnCloudClient, newBaseUrl:String) {
|
||||
client.baseUri = Uri.parse(newBaseUrl)
|
||||
}
|
||||
|
||||
private fun tryToConnect(client: OwnCloudClient): RemoteOperationResult<RemoteServerInfo> {
|
||||
@ -66,7 +60,9 @@ class GetRemoteStatusOperation : RemoteOperation<RemoteServerInfo>() {
|
||||
return try {
|
||||
val requester = StatusRequester()
|
||||
val requestResult = requester.request(baseUrl, client)
|
||||
requester.handleRequestResult(requestResult, baseUrl)
|
||||
val result = requester.handleRequestResult(requestResult, baseUrl)
|
||||
updateClientBaseUrl(client, result.data.baseUrl)
|
||||
return result
|
||||
} catch (e: JSONException) {
|
||||
RemoteOperationResult(ResultCode.INSTANCE_NOT_CONFIGURED)
|
||||
} catch (e: Exception) {
|
||||
|
@ -83,8 +83,10 @@ internal class StatusRequester {
|
||||
var status: Int
|
||||
|
||||
val getMethod = getGetMethod(currentLocation)
|
||||
getMethod.setFollPermanentRedirects(true)
|
||||
status = client.executeHttpMethod(getMethod)
|
||||
return RequestResult(getMethod, status, redirectedToUnsecureLocation, currentLocation)
|
||||
|
||||
return RequestResult(getMethod, status, redirectedToUnsecureLocation, getMethod.getFinalUrl().toString())
|
||||
}
|
||||
|
||||
private fun Int.isSuccess() = this == HttpConstants.HTTP_OK
|
||||
|
Loading…
x
Reference in New Issue
Block a user