mirror of
https://github.com/owncloud/android-library.git
synced 2025-06-07 07:56:19 +00:00
create logic scaffold for connection validator get status.php with it
This commit is contained in:
parent
7e4b43e7cb
commit
ca206173df
@ -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<RemoteServerInfo> {
|
||||
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
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user