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 e4ee8483..4ac6009d 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 @@ -1,12 +1,94 @@ package com.owncloud.android.lib.common +import com.owncloud.android.lib.common.authentication.OwnCloudCredentials +import com.owncloud.android.lib.common.authentication.OwnCloudCredentialsFactory +import com.owncloud.android.lib.common.http.HttpConstants import com.owncloud.android.lib.common.http.methods.HttpBaseMethod -import com.owncloud.android.lib.common.http.methods.nonwebdav.HttpMethod +import com.owncloud.android.lib.common.operations.RemoteOperationResult +import com.owncloud.android.lib.resources.status.GetRemoteStatusOperation +import com.owncloud.android.lib.resources.status.RemoteServerInfo +import org.apache.commons.lang3.exception.ExceptionUtils import timber.log.Timber +import java.lang.Exception class ConnectionValidator { - fun validate(method: HttpBaseMethod, client: OwnCloudClient) { - Timber.d("hello world") + fun validate(method: HttpBaseMethod, baseClient: OwnCloudClient): Boolean { + try { + var validationRetryCount = 0 + val client = OwnCloudClient(baseClient.baseUri, null, false) + client.credentials = baseClient.credentials + client.setFollowRedirects(true) + while (validationRetryCount < 5) { + var successCounter = 0 + var failCounter = 0 + + if (isOnwCloudStatusOk(client)) { + successCounter++ + } else { + failCounter++ + } + + val contentReply = accessRootFolder() + if (contentReply == HttpConstants.HTTP_OK) { + if (isRootFolderOk(contentReply)) { + successCounter++ + } else { + failCounter++ + } + } else { + failCounter++ + if (contentReply == HttpConstants.HTTP_UNAUTHORIZED) { + triggerAuthRefresh() + } + } + if(successCounter >= failCounter) { + //update credentials in client + return true + } + validationRetryCount++ + } + Timber.d("Could not authenticate or get valid data from owncloud") + } catch (e: Exception) { + Timber.d(ExceptionUtils.getStackTrace(e)) + } + return false + } + + private fun isOnwCloudStatusOk(client: OwnCloudClient): Boolean { + //TODO: Implement me + val reply = getOwnCloudStatus(client) + return if (reply.httpCode == HttpConstants.HTTP_OK) { + isOCStatusReplyValid(reply.data) + } else { + false + } + } + + private fun getOwnCloudStatus(client: OwnCloudClient): RemoteOperationResult { + val remoteStatusOperation = GetRemoteStatusOperation() + //TODO: follow redirects only 5 times + return remoteStatusOperation.execute(client) + } + + private fun isOCStatusReplyValid(info: RemoteServerInfo): Boolean { + //TODO: Implement me + Timber.d("owncloud version %s", info.ownCloudVersion.version) + return true + } + + private fun triggerAuthRefresh(): OwnCloudCredentials { + //TODO: Implement me + return OwnCloudCredentialsFactory.getAnonymousCredentials() + } + + private fun accessRootFolder(): Int { + //TODO: Implement me + return 55 + } + + private fun isRootFolderOk(content: Int): Boolean { + //TODO: Implement me + return true } } \ No newline at end of file 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 5941f9e9..2a0152ba 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 @@ -133,7 +133,8 @@ public class OwnCloudClient extends HttpClient { status = method.execute(); stacklog(status, method); - if (status == HttpConstants.HTTP_MOVED_TEMPORARILY) { + if (mConnectionValidator != null && + status == HttpConstants.HTTP_MOVED_TEMPORARILY) { mConnectionValidator.validate(method, this); retry = true; }