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

Commit 9d4b88c

Browse files
theScrabiabelgardep
authored andcommitted
refactor run function
1 parent cbfd977 commit 9d4b88c

File tree

1 file changed

+24
-37
lines changed

1 file changed

+24
-37
lines changed

owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/GetRemoteStatusOperation.kt

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import org.json.JSONObject
3535
import timber.log.Timber
3636
import java.net.URL
3737
import java.util.concurrent.TimeUnit
38-
import javax.net.ssl.SSLException
3938

4039
/**
4140
* Checks if the server is valid
@@ -46,26 +45,18 @@ import javax.net.ssl.SSLException
4645
* @author Abel García de Prada
4746
*/
4847
class GetRemoteStatusOperation : RemoteOperation<OwnCloudVersion>() {
49-
private lateinit var latestResult: RemoteOperationResult<OwnCloudVersion>
50-
5148
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)
6757
}
68-
return latestResult
58+
59+
return result
6960
}
7061

7162
fun updateLocationWithRedirectPath(oldLocation: String, redirectedLocation: String): String {
@@ -81,7 +72,7 @@ class GetRemoteStatusOperation : RemoteOperation<OwnCloudVersion>() {
8172
redirectedUrl: String
8273
): Boolean {
8374
return isConnectionSecure ||
84-
(baseUrl.startsWith(HTTPS_PREFIX) && redirectedUrl.startsWith(HTTP_PREFIX))
75+
(baseUrl.startsWith(HTTPS_SCHEME) && redirectedUrl.startsWith(HTTP_SCHEME))
8576
}
8677

8778
private fun getGetMethod(url: String): GetMethod {
@@ -126,7 +117,10 @@ class GetRemoteStatusOperation : RemoteOperation<OwnCloudVersion>() {
126117
}
127118
}
128119

129-
private fun handleRequestResult(requestResult: RequestResult, baseUrl: String): RemoteOperationResult<OwnCloudVersion> {
120+
private fun handleRequestResult(
121+
requestResult: RequestResult,
122+
baseUrl: String
123+
): RemoteOperationResult<OwnCloudVersion> {
130124
if (!isSuccess(requestResult.status))
131125
return RemoteOperationResult(requestResult.getMethod)
132126

@@ -142,30 +136,23 @@ class GetRemoteStatusOperation : RemoteOperation<OwnCloudVersion>() {
142136
if (requestResult.redirectedToUnsecureLocation) {
143137
RemoteOperationResult<OwnCloudVersion>(ResultCode.OK_REDIRECT_TO_NON_SECURE_CONNECTION)
144138
} else {
145-
if (baseUrl.startsWith(HTTPS_PREFIX)) RemoteOperationResult(ResultCode.OK_SSL)
139+
if (baseUrl.startsWith(HTTPS_SCHEME)) RemoteOperationResult(ResultCode.OK_SSL)
146140
else RemoteOperationResult(ResultCode.OK_NO_SSL)
147141
}
148142
result.data = ocVersion
149143
return result
150144
}
151145

152-
private fun tryConnection(client: OwnCloudClient): Boolean {
146+
private fun tryToConnect(client: OwnCloudClient): RemoteOperationResult<OwnCloudVersion> {
153147
val baseUrl = client.baseUri.toString()
154-
try {
155-
client.setFollowRedirects(false)
156-
148+
client.setFollowRedirects(false)
149+
return try {
157150
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)
160152
} catch (e: JSONException) {
161-
latestResult = RemoteOperationResult(ResultCode.INSTANCE_NOT_CONFIGURED)
162-
return false
153+
RemoteOperationResult(ResultCode.INSTANCE_NOT_CONFIGURED)
163154
} catch (e: Exception) {
164-
latestResult = RemoteOperationResult(e)
165-
return false
166-
} catch (sslE: SSLException) {
167-
latestResult = RemoteOperationResult(sslE)
168-
return false
155+
RemoteOperationResult(e)
169156
}
170157
}
171158

@@ -179,7 +166,7 @@ class GetRemoteStatusOperation : RemoteOperation<OwnCloudVersion>() {
179166
private const val TRY_CONNECTION_TIMEOUT: Long = 5000
180167
private const val NODE_INSTALLED = "installed"
181168
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"
184171
}
185172
}

0 commit comments

Comments
 (0)