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

Commit ca20617

Browse files
committed
create logic scaffold for connection validator get status.php with it
1 parent 7e4b43e commit ca20617

File tree

2 files changed

+87
-4
lines changed

2 files changed

+87
-4
lines changed
Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,94 @@
11
package com.owncloud.android.lib.common
22

3+
import com.owncloud.android.lib.common.authentication.OwnCloudCredentials
4+
import com.owncloud.android.lib.common.authentication.OwnCloudCredentialsFactory
5+
import com.owncloud.android.lib.common.http.HttpConstants
36
import com.owncloud.android.lib.common.http.methods.HttpBaseMethod
4-
import com.owncloud.android.lib.common.http.methods.nonwebdav.HttpMethod
7+
import com.owncloud.android.lib.common.operations.RemoteOperationResult
8+
import com.owncloud.android.lib.resources.status.GetRemoteStatusOperation
9+
import com.owncloud.android.lib.resources.status.RemoteServerInfo
10+
import org.apache.commons.lang3.exception.ExceptionUtils
511
import timber.log.Timber
12+
import java.lang.Exception
613

714
class ConnectionValidator {
815

9-
fun validate(method: HttpBaseMethod, client: OwnCloudClient) {
10-
Timber.d("hello world")
16+
fun validate(method: HttpBaseMethod, baseClient: OwnCloudClient): Boolean {
17+
try {
18+
var validationRetryCount = 0
19+
val client = OwnCloudClient(baseClient.baseUri, null, false)
20+
client.credentials = baseClient.credentials
21+
client.setFollowRedirects(true)
22+
while (validationRetryCount < 5) {
23+
var successCounter = 0
24+
var failCounter = 0
25+
26+
if (isOnwCloudStatusOk(client)) {
27+
successCounter++
28+
} else {
29+
failCounter++
30+
}
31+
32+
val contentReply = accessRootFolder()
33+
if (contentReply == HttpConstants.HTTP_OK) {
34+
if (isRootFolderOk(contentReply)) {
35+
successCounter++
36+
} else {
37+
failCounter++
38+
}
39+
} else {
40+
failCounter++
41+
if (contentReply == HttpConstants.HTTP_UNAUTHORIZED) {
42+
triggerAuthRefresh()
43+
}
44+
}
45+
if(successCounter >= failCounter) {
46+
//update credentials in client
47+
return true
48+
}
49+
validationRetryCount++
50+
}
51+
Timber.d("Could not authenticate or get valid data from owncloud")
52+
} catch (e: Exception) {
53+
Timber.d(ExceptionUtils.getStackTrace(e))
54+
}
55+
return false
56+
}
57+
58+
private fun isOnwCloudStatusOk(client: OwnCloudClient): Boolean {
59+
//TODO: Implement me
60+
val reply = getOwnCloudStatus(client)
61+
return if (reply.httpCode == HttpConstants.HTTP_OK) {
62+
isOCStatusReplyValid(reply.data)
63+
} else {
64+
false
65+
}
66+
}
67+
68+
private fun getOwnCloudStatus(client: OwnCloudClient): RemoteOperationResult<RemoteServerInfo> {
69+
val remoteStatusOperation = GetRemoteStatusOperation()
70+
//TODO: follow redirects only 5 times
71+
return remoteStatusOperation.execute(client)
72+
}
73+
74+
private fun isOCStatusReplyValid(info: RemoteServerInfo): Boolean {
75+
//TODO: Implement me
76+
Timber.d("owncloud version %s", info.ownCloudVersion.version)
77+
return true
78+
}
79+
80+
private fun triggerAuthRefresh(): OwnCloudCredentials {
81+
//TODO: Implement me
82+
return OwnCloudCredentialsFactory.getAnonymousCredentials()
83+
}
84+
85+
private fun accessRootFolder(): Int {
86+
//TODO: Implement me
87+
return 55
88+
}
89+
90+
private fun isRootFolderOk(content: Int): Boolean {
91+
//TODO: Implement me
92+
return true
1193
}
1294
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ private int saveExecuteHttpMethod(HttpBaseMethod method) throws Exception {
133133
status = method.execute();
134134
stacklog(status, method);
135135

136-
if (status == HttpConstants.HTTP_MOVED_TEMPORARILY) {
136+
if (mConnectionValidator != null &&
137+
status == HttpConstants.HTTP_MOVED_TEMPORARILY) {
137138
mConnectionValidator.validate(method, this);
138139
retry = true;
139140
}

0 commit comments

Comments
 (0)