1
1
package com.owncloud.android.lib.common
2
2
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
3
6
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
5
11
import timber.log.Timber
12
+ import java.lang.Exception
6
13
7
14
class ConnectionValidator {
8
15
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
11
93
}
12
94
}
0 commit comments