From b2f6d7f3b15d361578774f0885b0c769c612229d Mon Sep 17 00:00:00 2001 From: Christian Schabesberger Date: Fri, 10 Sep 2021 16:39:02 +0200 Subject: [PATCH] make initial check with apm work though connection validator --- .../android/lib/common/ConnectionValidator.kt | 34 +++++++++++-------- .../android/lib/common/OwnCloudClient.java | 16 +-------- .../android/lib/common/http/HttpClient.java | 8 +++-- 3 files changed, 27 insertions(+), 31 deletions(-) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/ConnectionValidator.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/ConnectionValidator.kt index 3284a482..a052924f 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/ConnectionValidator.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/ConnectionValidator.kt @@ -14,7 +14,7 @@ import java.lang.Exception class ConnectionValidator ( val clearCookiesOnValidation: Boolean - ){ +){ fun validate(baseClient: OwnCloudClient): Boolean { try { @@ -28,6 +28,7 @@ class ConnectionValidator ( client.credentials = baseClient.credentials while (validationRetryCount < 5) { + Timber.d("+++++++++++++++++++++++++++++++++++++ validationRetryCout %d", validationRetryCount) var successCounter = 0 var failCounter = 0 @@ -38,21 +39,24 @@ class ConnectionValidator ( failCounter++ } - client.setFollowRedirects(false) - val contentReply = canAccessRootFolder(client) - if (contentReply.httpCode == HttpConstants.HTTP_OK) { - if (contentReply.data == true) { //if data is true it means that the content reply was ok - successCounter++ + // Skip the part where we try to check if we can access the parts where we have to be logged in... if we are not logged in + if(baseClient.credentials !is OwnCloudCredentialsFactory.OwnCloudAnonymousCredentials) { + client.setFollowRedirects(false) + val contentReply = canAccessRootFolder(client) + if (contentReply.httpCode == HttpConstants.HTTP_OK) { + if (contentReply.data == true) { //if data is true it means that the content reply was ok + successCounter++ + } else { + failCounter++ + } } else { failCounter++ - } - } else { - failCounter++ - if (contentReply.hashCode() == HttpConstants.HTTP_UNAUTHORIZED) { - triggerAuthRefresh() + if (contentReply.hashCode() == HttpConstants.HTTP_UNAUTHORIZED) { + triggerAuthRefresh() + } } } - if(successCounter >= failCounter) { + if (successCounter >= failCounter) { //update credentials in client return true } @@ -67,8 +71,10 @@ class ConnectionValidator ( private fun isOnwCloudStatusOk(client: OwnCloudClient): Boolean { val reply = getOwnCloudStatus(client) - return reply.httpCode == HttpConstants.HTTP_OK && - !reply.isException && + // dont check status code. It currently relais on the broken redirect code of the owncloud client + // TODO: Use okhttp redirect and add this check again + // return reply.httpCode == HttpConstants.HTTP_OK && + return !reply.isException && reply.data != null } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClient.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClient.java index b3aeac12..eb3432b2 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClient.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClient.java @@ -131,7 +131,6 @@ public class OwnCloudClient extends HttpClient { } status = method.execute(); - Timber.d("-------------------------------------"); stacklog(status, method); if (mConnectionValidator != null && @@ -163,7 +162,7 @@ public class OwnCloudClient extends HttpClient { "\nobject: " + this.toString() + "\nMethod: " + method.toString() + "\nUrl: " + method.getHttpUrl() + - "\nCookeis: " + getCookiesString() + + "\nCookeis: " + getCookiesForBaseUri().toString() + "\ntrace: " + ExceptionUtils.getStackTrace(e) + "---------------------------"); } @@ -329,19 +328,6 @@ public class OwnCloudClient extends HttpClient { } } - public String getCookiesString() { - StringBuilder cookiesString = new StringBuilder(); - List cookieList = getCookiesForBaseUri(); - - if (cookieList != null) { - for (Cookie cookie : cookieList) { - cookiesString.append(cookie.toString()).append(";"); - } - } - - return cookiesString.toString(); - } - public void setCookiesForBaseUri(List cookies) { getOkHttpClient().cookieJar().saveFromResponse( HttpUrl.parse(mBaseUri.toString()), diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java index 910e0936..b231ffed 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java @@ -57,6 +57,7 @@ import java.util.concurrent.TimeUnit; public class HttpClient { private static OkHttpClient sOkHttpClient; private static Context sContext; + private static HashMap> sCookieStore = new HashMap<>(); private static LogInterceptor sLogInterceptor; private static Interceptor sDebugInterceptor; @@ -67,10 +68,11 @@ public class HttpClient { NetworkUtils.getKnownServersStore(sContext)); final SSLSocketFactory sslSocketFactory = getNewSslSocketFactory(trustManager); // Automatic cookie handling, NOT PERSISTENT + final CookieJar cookieJar = new CookieJarImpl(sCookieStore); // TODO: Not verifying the hostname against certificate. ask owncloud security human if this is ok. //.hostnameVerifier(new BrowserCompatHostnameVerifier()); - sOkHttpClient = buildNewOkHttpClient(sslSocketFactory, trustManager); + sOkHttpClient = buildNewOkHttpClient(sslSocketFactory, trustManager, cookieJar); } catch (Exception e) { Timber.e(e, "Could not setup SSL system."); @@ -107,7 +109,8 @@ public class HttpClient { return sslContext.getSocketFactory(); } - private static OkHttpClient buildNewOkHttpClient(SSLSocketFactory sslSocketFactory, X509TrustManager trustManager){ + private static OkHttpClient buildNewOkHttpClient(SSLSocketFactory sslSocketFactory, X509TrustManager trustManager, + CookieJar cookieJar) { return new OkHttpClient.Builder() .addNetworkInterceptor(getLogInterceptor()) .addNetworkInterceptor(DebugInterceptorFactory.INSTANCE.getInterceptor()) @@ -118,6 +121,7 @@ public class HttpClient { .followRedirects(false) .sslSocketFactory(sslSocketFactory, trustManager) .hostnameVerifier((asdf, usdf) -> true) + .cookieJar(cookieJar) .build(); }