@@ -25,16 +25,12 @@ package com.owncloud.android.lib.resources.status
25
25
26
26
import android.net.Uri
27
27
import com.owncloud.android.lib.common.OwnCloudClient
28
- import com.owncloud.android.lib.common.http.HttpConstants
29
- import com.owncloud.android.lib.common.http.methods.nonwebdav.GetMethod
30
28
import com.owncloud.android.lib.common.operations.RemoteOperation
31
29
import com.owncloud.android.lib.common.operations.RemoteOperationResult
32
30
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode
33
31
import org.json.JSONException
34
- import org.json.JSONObject
35
32
import timber.log.Timber
36
- import java.net.URL
37
- import java.util.concurrent.TimeUnit
33
+
38
34
39
35
/* *
40
36
* Checks if the server is valid
@@ -45,6 +41,11 @@ import java.util.concurrent.TimeUnit
45
41
* @author Abel García de Prada
46
42
*/
47
43
class GetRemoteStatusOperation : RemoteOperation <OwnCloudVersion >() {
44
+ companion object {
45
+ const val HTTPS_SCHEME = " https"
46
+ const val HTTP_SCHEME = " http"
47
+ }
48
+
48
49
override fun run (client : OwnCloudClient ): RemoteOperationResult <OwnCloudVersion > {
49
50
if (client.baseUri.scheme.isNullOrEmpty())
50
51
client.baseUri = Uri .parse(HTTPS_SCHEME + " ://" + client.baseUri.toString())
@@ -59,114 +60,17 @@ class GetRemoteStatusOperation : RemoteOperation<OwnCloudVersion>() {
59
60
return result
60
61
}
61
62
62
- fun updateLocationWithRedirectPath (oldLocation : String , redirectedLocation : String ): String {
63
- if (! redirectedLocation.startsWith(" /" ))
64
- return redirectedLocation
65
- val oldLocation = URL (oldLocation)
66
- return URL (oldLocation.protocol, oldLocation.host, oldLocation.port, redirectedLocation).toString()
67
- }
68
-
69
- private fun checkIfConnectionIsRedirectedToNoneSecure (
70
- isConnectionSecure : Boolean ,
71
- baseUrl : String ,
72
- redirectedUrl : String
73
- ): Boolean {
74
- return isConnectionSecure ||
75
- (baseUrl.startsWith(HTTPS_SCHEME ) && redirectedUrl.startsWith(HTTP_SCHEME ))
76
- }
77
-
78
- private fun getGetMethod (url : String ): GetMethod {
79
- return GetMethod (URL (url + OwnCloudClient .STATUS_PATH )).apply {
80
- setReadTimeout(TRY_CONNECTION_TIMEOUT , TimeUnit .SECONDS )
81
- setConnectionTimeout(TRY_CONNECTION_TIMEOUT , TimeUnit .SECONDS )
82
- }
83
- }
84
-
85
- data class RequestResult (
86
- val getMethod : GetMethod ,
87
- val status : Int ,
88
- val result : RemoteOperationResult <OwnCloudVersion >,
89
- val redirectedToUnsecureLocation : Boolean
90
- )
91
-
92
- fun requestAndFollowRedirects (baseLocation : String ): RequestResult {
93
- var currentLocation = baseLocation
94
- var redirectedToUnsecureLocation = false
95
- var status: Int
96
-
97
- while (true ) {
98
- val getMethod = getGetMethod(currentLocation)
99
-
100
- status = client.executeHttpMethod(getMethod)
101
- val result =
102
- if (isSuccess(status)) RemoteOperationResult <OwnCloudVersion >(ResultCode .OK )
103
- else RemoteOperationResult (getMethod)
104
-
105
- if (result.redirectedLocation.isNullOrEmpty() || result.isSuccess) {
106
- return RequestResult (getMethod, status, result, redirectedToUnsecureLocation)
107
- } else {
108
- val nextLocation = updateLocationWithRedirectPath(currentLocation, result.redirectedLocation)
109
- redirectedToUnsecureLocation =
110
- checkIfConnectionIsRedirectedToNoneSecure(
111
- redirectedToUnsecureLocation,
112
- currentLocation,
113
- nextLocation
114
- )
115
- currentLocation = nextLocation
116
- }
117
- }
118
- }
119
-
120
- private fun handleRequestResult (
121
- requestResult : RequestResult ,
122
- baseUrl : String
123
- ): RemoteOperationResult <OwnCloudVersion > {
124
- if (! isSuccess(requestResult.status))
125
- return RemoteOperationResult (requestResult.getMethod)
126
-
127
- val respJSON = JSONObject (requestResult.getMethod.getResponseBodyAsString())
128
- if (! respJSON.getBoolean(NODE_INSTALLED ))
129
- return RemoteOperationResult (ResultCode .INSTANCE_NOT_CONFIGURED )
130
-
131
- val version = respJSON.getString(NODE_VERSION )
132
- val ocVersion = OwnCloudVersion (version)
133
- // the version object will be returned even if the version is invalid, no error code;
134
- // every app will decide how to act if (ocVersion.isVersionValid() == false)
135
- val result =
136
- if (requestResult.redirectedToUnsecureLocation) {
137
- RemoteOperationResult <OwnCloudVersion >(ResultCode .OK_REDIRECT_TO_NON_SECURE_CONNECTION )
138
- } else {
139
- if (baseUrl.startsWith(HTTPS_SCHEME )) RemoteOperationResult (ResultCode .OK_SSL )
140
- else RemoteOperationResult (ResultCode .OK_NO_SSL )
141
- }
142
- result.data = ocVersion
143
- return result
144
- }
145
-
146
63
private fun tryToConnect (client : OwnCloudClient ): RemoteOperationResult <OwnCloudVersion > {
147
64
val baseUrl = client.baseUri.toString()
148
65
client.setFollowRedirects(false )
149
66
return try {
150
- val requestResult = requestAndFollowRedirects(baseUrl)
151
- handleRequestResult(requestResult, baseUrl)
67
+ val requestor = StatusRequestor ()
68
+ val requestResult = requestor.requestAndFollowRedirects(baseUrl, client)
69
+ requestor.handleRequestResult(requestResult, baseUrl)
152
70
} catch (e: JSONException ) {
153
71
RemoteOperationResult (ResultCode .INSTANCE_NOT_CONFIGURED )
154
72
} catch (e: Exception ) {
155
73
RemoteOperationResult (e)
156
74
}
157
75
}
158
-
159
- private fun isSuccess (status : Int ): Boolean = status == HttpConstants .HTTP_OK
160
-
161
- companion object {
162
- /* *
163
- * Maximum time to wait for a response from the server when the connection is being tested,
164
- * in MILLISECONDs.
165
- */
166
- private const val TRY_CONNECTION_TIMEOUT : Long = 5000
167
- private const val NODE_INSTALLED = " installed"
168
- private const val NODE_VERSION = " version"
169
- private const val HTTPS_SCHEME = " https"
170
- private const val HTTP_SCHEME = " http"
171
- }
172
76
}
0 commit comments