mirror of
https://github.com/owncloud/android-library.git
synced 2025-06-07 16:06:08 +00:00
clean up tryConnect function
This commit is contained in:
parent
df6cd5b13a
commit
8e8d10c964
@ -69,87 +69,104 @@ class GetRemoteStatusOperation : RemoteOperation<OwnCloudVersion>() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun updateLocationWithRedirectPath(oldLocation: String, redirectedLocation: String): String {
|
fun updateLocationWithRedirectPath(oldLocation: String, redirectedLocation: String): String {
|
||||||
if(!redirectedLocation.startsWith("/"))
|
if (!redirectedLocation.startsWith("/"))
|
||||||
return redirectedLocation
|
return redirectedLocation
|
||||||
val oldLocation = URL(oldLocation)
|
val oldLocation = URL(oldLocation)
|
||||||
return URL(oldLocation.protocol, oldLocation.host, oldLocation.port, redirectedLocation).toString()
|
return URL(oldLocation.protocol, oldLocation.host, oldLocation.port, redirectedLocation).toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun tryConnection(client: OwnCloudClient): Boolean {
|
private fun checkIfConnectionIsRedirectedToNoneSecure(
|
||||||
var successfulConnection = false
|
isConnectionSecure: Boolean,
|
||||||
val baseUrlStr = client.baseUri.toString()
|
baseUrl: String,
|
||||||
try {
|
redirectedUrl: String
|
||||||
var getMethod = GetMethod(URL(baseUrlStr + OwnCloudClient.STATUS_PATH)).apply {
|
): Boolean {
|
||||||
setReadTimeout(TRY_CONNECTION_TIMEOUT, TimeUnit.SECONDS)
|
return isConnectionSecure ||
|
||||||
setConnectionTimeout(TRY_CONNECTION_TIMEOUT, TimeUnit.SECONDS)
|
(baseUrl.startsWith(HTTPS_PREFIX) && redirectedUrl.startsWith(HTTP_PREFIX))
|
||||||
}
|
}
|
||||||
client.setFollowRedirects(false)
|
|
||||||
var isRedirectToNonSecureConnection = false
|
|
||||||
var status: Int
|
|
||||||
try {
|
|
||||||
status = client.executeHttpMethod(getMethod)
|
|
||||||
latestResult =
|
|
||||||
if (isSuccess(status)) RemoteOperationResult(ResultCode.OK)
|
|
||||||
else RemoteOperationResult(getMethod)
|
|
||||||
|
|
||||||
} catch (sslE: SSLException) {
|
private fun getGetMethod(url: String): GetMethod {
|
||||||
latestResult = RemoteOperationResult(sslE)
|
return GetMethod(URL(url + OwnCloudClient.STATUS_PATH)).apply {
|
||||||
return successfulConnection
|
setReadTimeout(TRY_CONNECTION_TIMEOUT, TimeUnit.SECONDS)
|
||||||
}
|
setConnectionTimeout(TRY_CONNECTION_TIMEOUT, TimeUnit.SECONDS)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var redirectedLocation = updateLocationWithRedirectPath(baseUrlStr, latestResult.redirectedLocation)
|
data class RequestResult(
|
||||||
while (!redirectedLocation.isNullOrEmpty() && !latestResult.isSuccess) {
|
val getMethod: GetMethod,
|
||||||
isRedirectToNonSecureConnection =
|
val status: Int,
|
||||||
isRedirectToNonSecureConnection ||
|
val result: RemoteOperationResult<OwnCloudVersion>,
|
||||||
(baseUrlStr.startsWith(HTTPS_PREFIX) && redirectedLocation.startsWith(
|
val redirectedToUnsecureLocation: Boolean
|
||||||
HTTP_PREFIX
|
)
|
||||||
))
|
|
||||||
|
|
||||||
getMethod = GetMethod(URL(redirectedLocation)).apply {
|
fun requestAndFollowRedirects(baseLocation: String): RequestResult {
|
||||||
setReadTimeout(TRY_CONNECTION_TIMEOUT, TimeUnit.SECONDS)
|
var currentLocation = baseLocation
|
||||||
setConnectionTimeout(TRY_CONNECTION_TIMEOUT, TimeUnit.SECONDS)
|
var redirectedToUnsecureLocation = false
|
||||||
}
|
var status: Int
|
||||||
|
|
||||||
status = client.executeHttpMethod(getMethod)
|
while (true) {
|
||||||
latestResult = RemoteOperationResult(getMethod)
|
val getMethod = getGetMethod(currentLocation)
|
||||||
redirectedLocation = updateLocationWithRedirectPath(redirectedLocation, latestResult.redirectedLocation)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isSuccess(status)) {
|
status = client.executeHttpMethod(getMethod)
|
||||||
val respJSON = JSONObject(getMethod.getResponseBodyAsString())
|
val result =
|
||||||
if (!respJSON.getBoolean(NODE_INSTALLED)) {
|
if (isSuccess(status)) RemoteOperationResult<OwnCloudVersion>(ResultCode.OK)
|
||||||
latestResult = RemoteOperationResult(ResultCode.INSTANCE_NOT_CONFIGURED)
|
else RemoteOperationResult(getMethod)
|
||||||
} else {
|
|
||||||
val version = respJSON.getString(NODE_VERSION)
|
if (result.redirectedLocation.isNullOrEmpty() || result.isSuccess) {
|
||||||
val ocVersion = OwnCloudVersion(version)
|
return RequestResult(getMethod, status, result, redirectedToUnsecureLocation)
|
||||||
// the version object will be returned even if the version is invalid, no error code;
|
|
||||||
// every app will decide how to act if (ocVersion.isVersionValid() == false)
|
|
||||||
latestResult = if (isRedirectToNonSecureConnection) {
|
|
||||||
RemoteOperationResult(ResultCode.OK_REDIRECT_TO_NON_SECURE_CONNECTION)
|
|
||||||
} else {
|
|
||||||
if (baseUrlStr.startsWith(HTTPS_PREFIX)) RemoteOperationResult(ResultCode.OK_SSL)
|
|
||||||
else RemoteOperationResult(ResultCode.OK_NO_SSL)
|
|
||||||
}
|
|
||||||
latestResult.data = ocVersion
|
|
||||||
successfulConnection = true
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
latestResult = RemoteOperationResult(getMethod)
|
val nextLocation = updateLocationWithRedirectPath(currentLocation, result.redirectedLocation)
|
||||||
|
redirectedToUnsecureLocation =
|
||||||
|
checkIfConnectionIsRedirectedToNoneSecure(
|
||||||
|
redirectedToUnsecureLocation,
|
||||||
|
currentLocation,
|
||||||
|
nextLocation
|
||||||
|
)
|
||||||
|
currentLocation = nextLocation
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun handleRequestResult(requestResult: RequestResult, baseUrl: String): RemoteOperationResult<OwnCloudVersion> {
|
||||||
|
if (!isSuccess(requestResult.status))
|
||||||
|
return RemoteOperationResult(requestResult.getMethod)
|
||||||
|
|
||||||
|
val respJSON = JSONObject(requestResult.getMethod.getResponseBodyAsString())
|
||||||
|
if (!respJSON.getBoolean(NODE_INSTALLED))
|
||||||
|
return RemoteOperationResult(ResultCode.INSTANCE_NOT_CONFIGURED)
|
||||||
|
|
||||||
|
val version = respJSON.getString(NODE_VERSION)
|
||||||
|
val ocVersion = OwnCloudVersion(version)
|
||||||
|
// the version object will be returned even if the version is invalid, no error code;
|
||||||
|
// every app will decide how to act if (ocVersion.isVersionValid() == false)
|
||||||
|
val result =
|
||||||
|
if (requestResult.redirectedToUnsecureLocation) {
|
||||||
|
RemoteOperationResult<OwnCloudVersion>(ResultCode.OK_REDIRECT_TO_NON_SECURE_CONNECTION)
|
||||||
|
} else {
|
||||||
|
if (baseUrl.startsWith(HTTPS_PREFIX)) RemoteOperationResult(ResultCode.OK_SSL)
|
||||||
|
else RemoteOperationResult(ResultCode.OK_NO_SSL)
|
||||||
|
}
|
||||||
|
result.data = ocVersion
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun tryConnection(client: OwnCloudClient): Boolean {
|
||||||
|
val baseUrl = client.baseUri.toString()
|
||||||
|
try {
|
||||||
|
client.setFollowRedirects(false)
|
||||||
|
|
||||||
|
val requestResult = requestAndFollowRedirects(baseUrl)
|
||||||
|
val operationResult = handleRequestResult(requestResult, baseUrl)
|
||||||
|
return operationResult.code == ResultCode.OK_SSL || operationResult.code == ResultCode.OK_NO_SSL
|
||||||
} catch (e: JSONException) {
|
} catch (e: JSONException) {
|
||||||
latestResult = RemoteOperationResult(ResultCode.INSTANCE_NOT_CONFIGURED)
|
latestResult = RemoteOperationResult(ResultCode.INSTANCE_NOT_CONFIGURED)
|
||||||
|
return false
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
latestResult = RemoteOperationResult(e)
|
latestResult = RemoteOperationResult(e)
|
||||||
|
return false
|
||||||
|
} catch (sslE: SSLException) {
|
||||||
|
latestResult = RemoteOperationResult(sslE)
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
when {
|
|
||||||
latestResult.isSuccess -> Timber.i("Connection check at $baseUrlStr successful: ${latestResult.logMessage}")
|
|
||||||
|
|
||||||
latestResult.isException ->
|
|
||||||
Timber.e(latestResult.exception, "Connection check at $baseUrlStr: ${latestResult.logMessage}")
|
|
||||||
|
|
||||||
else -> Timber.e("Connection check at $baseUrlStr failed: ${latestResult.logMessage}")
|
|
||||||
}
|
|
||||||
return successfulConnection
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun isSuccess(status: Int): Boolean = status == HttpConstants.HTTP_OK
|
private fun isSuccess(status: Int): Boolean = status == HttpConstants.HTTP_OK
|
||||||
|
Loading…
x
Reference in New Issue
Block a user