@@ -35,7 +35,6 @@ import org.json.JSONObject
35
35
import timber.log.Timber
36
36
import java.net.URL
37
37
import java.util.concurrent.TimeUnit
38
- import javax.net.ssl.SSLException
39
38
40
39
/* *
41
40
* Checks if the server is valid
@@ -46,26 +45,18 @@ import javax.net.ssl.SSLException
46
45
* @author Abel García de Prada
47
46
*/
48
47
class GetRemoteStatusOperation : RemoteOperation <OwnCloudVersion >() {
49
- private lateinit var latestResult: RemoteOperationResult <OwnCloudVersion >
50
-
51
48
override fun run (client : OwnCloudClient ): RemoteOperationResult <OwnCloudVersion > {
52
-
53
- val baseUriStr = client.baseUri.toString()
54
- if (baseUriStr.startsWith(HTTP_PREFIX ) || baseUriStr.startsWith(
55
- HTTPS_PREFIX
56
- )
57
- ) {
58
- tryConnection(client)
59
- } else {
60
- client.baseUri = Uri .parse(HTTPS_PREFIX + baseUriStr)
61
- val httpsSuccess = tryConnection(client)
62
- if (! httpsSuccess && ! latestResult.isSslRecoverableException) {
63
- Timber .d(" Establishing secure connection failed, trying non secure connection" )
64
- client.baseUri = Uri .parse(HTTP_PREFIX + baseUriStr)
65
- tryConnection(client)
66
- }
49
+ if (client.baseUri.scheme.isNullOrEmpty())
50
+ client.baseUri = Uri .parse(HTTPS_SCHEME + " ://" + client.baseUri.toString())
51
+
52
+ var result = tryToConnect(client)
53
+ if (result.code != ResultCode .OK_SSL && ! result.isSslRecoverableException) {
54
+ Timber .d(" Establishing secure connection failed, trying non secure connection" )
55
+ client.baseUri = client.baseUri.buildUpon().scheme(HTTP_SCHEME ).build()
56
+ result = tryToConnect(client)
67
57
}
68
- return latestResult
58
+
59
+ return result
69
60
}
70
61
71
62
fun updateLocationWithRedirectPath (oldLocation : String , redirectedLocation : String ): String {
@@ -81,7 +72,7 @@ class GetRemoteStatusOperation : RemoteOperation<OwnCloudVersion>() {
81
72
redirectedUrl : String
82
73
): Boolean {
83
74
return isConnectionSecure ||
84
- (baseUrl.startsWith(HTTPS_PREFIX ) && redirectedUrl.startsWith(HTTP_PREFIX ))
75
+ (baseUrl.startsWith(HTTPS_SCHEME ) && redirectedUrl.startsWith(HTTP_SCHEME ))
85
76
}
86
77
87
78
private fun getGetMethod (url : String ): GetMethod {
@@ -126,7 +117,10 @@ class GetRemoteStatusOperation : RemoteOperation<OwnCloudVersion>() {
126
117
}
127
118
}
128
119
129
- private fun handleRequestResult (requestResult : RequestResult , baseUrl : String ): RemoteOperationResult <OwnCloudVersion > {
120
+ private fun handleRequestResult (
121
+ requestResult : RequestResult ,
122
+ baseUrl : String
123
+ ): RemoteOperationResult <OwnCloudVersion > {
130
124
if (! isSuccess(requestResult.status))
131
125
return RemoteOperationResult (requestResult.getMethod)
132
126
@@ -142,30 +136,23 @@ class GetRemoteStatusOperation : RemoteOperation<OwnCloudVersion>() {
142
136
if (requestResult.redirectedToUnsecureLocation) {
143
137
RemoteOperationResult <OwnCloudVersion >(ResultCode .OK_REDIRECT_TO_NON_SECURE_CONNECTION )
144
138
} else {
145
- if (baseUrl.startsWith(HTTPS_PREFIX )) RemoteOperationResult (ResultCode .OK_SSL )
139
+ if (baseUrl.startsWith(HTTPS_SCHEME )) RemoteOperationResult (ResultCode .OK_SSL )
146
140
else RemoteOperationResult (ResultCode .OK_NO_SSL )
147
141
}
148
142
result.data = ocVersion
149
143
return result
150
144
}
151
145
152
- private fun tryConnection (client : OwnCloudClient ): Boolean {
146
+ private fun tryToConnect (client : OwnCloudClient ): RemoteOperationResult < OwnCloudVersion > {
153
147
val baseUrl = client.baseUri.toString()
154
- try {
155
- client.setFollowRedirects(false )
156
-
148
+ client.setFollowRedirects(false )
149
+ return try {
157
150
val requestResult = requestAndFollowRedirects(baseUrl)
158
- val operationResult = handleRequestResult(requestResult, baseUrl)
159
- return operationResult.code == ResultCode .OK_SSL || operationResult.code == ResultCode .OK_NO_SSL
151
+ handleRequestResult(requestResult, baseUrl)
160
152
} catch (e: JSONException ) {
161
- latestResult = RemoteOperationResult (ResultCode .INSTANCE_NOT_CONFIGURED )
162
- return false
153
+ RemoteOperationResult (ResultCode .INSTANCE_NOT_CONFIGURED )
163
154
} catch (e: Exception ) {
164
- latestResult = RemoteOperationResult (e)
165
- return false
166
- } catch (sslE: SSLException ) {
167
- latestResult = RemoteOperationResult (sslE)
168
- return false
155
+ RemoteOperationResult (e)
169
156
}
170
157
}
171
158
@@ -179,7 +166,7 @@ class GetRemoteStatusOperation : RemoteOperation<OwnCloudVersion>() {
179
166
private const val TRY_CONNECTION_TIMEOUT : Long = 5000
180
167
private const val NODE_INSTALLED = " installed"
181
168
private const val NODE_VERSION = " version"
182
- private const val HTTPS_PREFIX = " https:// "
183
- private const val HTTP_PREFIX = " http:// "
169
+ private const val HTTPS_SCHEME = " https"
170
+ private const val HTTP_SCHEME = " http"
184
171
}
185
172
}
0 commit comments