1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-07-17 06:55:48 +00:00

Remove ugly dependency from GetRemoteStatusOperation

Create AnonymousService for operations that not require authentication
This commit is contained in:
abelgardep 2020-01-27 22:50:10 +01:00 committed by davigonz
parent ad533c8307
commit ec5c9fc4aa
5 changed files with 56 additions and 40 deletions

View File

@ -329,5 +329,7 @@ public class AccountUtils {
* OAuth2 scope * OAuth2 scope
*/ */
public static final String KEY_OAUTH2_SCOPE = "oc_oauth2_scope"; public static final String KEY_OAUTH2_SCOPE = "oc_oauth2_scope";
public static final String WEBDAV_PATH_4_0_AND_LATER = "/remote.php/dav";
} }
} }

View File

@ -0,0 +1,33 @@
/* ownCloud Android Library is available under MIT license
* Copyright (C) 2020 ownCloud GmbH.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
package com.owncloud.android.lib.resources.server
import com.owncloud.android.lib.common.operations.RemoteOperationResult
import com.owncloud.android.lib.resources.status.OwnCloudVersion
interface AnonymousService {
fun checkPathExistence(path: String, isUserLogged: Boolean): RemoteOperationResult<Any>
fun getRemoteStatus(path: String): RemoteOperationResult<OwnCloudVersion>
}

View File

@ -56,7 +56,7 @@ class CheckPathExistenceOperation(
* *
* @return Sequence of redirections followed, if any, or NULL if the operation was not executed. * @return Sequence of redirections followed, if any, or NULL if the operation was not executed.
*/ */
var redirectionPath: RedirectionPath? = null lateinit var redirectionPath: RedirectionPath
private set private set
override fun run(client: OwnCloudClient): RemoteOperationResult<Any> { override fun run(client: OwnCloudClient): RemoteOperationResult<Any> {
@ -75,7 +75,7 @@ class CheckPathExistenceOperation(
var status = client.executeHttpMethod(propFindMethod) var status = client.executeHttpMethod(propFindMethod)
if (previousFollowRedirects) { if (previousFollowRedirects) {
redirectionPath = client.followRedirection(propFindMethod) redirectionPath = client.followRedirection(propFindMethod)
status = redirectionPath?.lastStatus!! status = redirectionPath.lastStatus
} }
/* PROPFIND method /* PROPFIND method
* 404 NOT FOUND: path doesn't exist, * 404 NOT FOUND: path doesn't exist,
@ -103,10 +103,9 @@ class CheckPathExistenceOperation(
/** /**
* @return 'True' if the operation was executed and at least one redirection was followed. * @return 'True' if the operation was executed and at least one redirection was followed.
*/ */
fun wasRedirected() = redirectionPath != null && redirectionPath!!.redirectionsCount > 0 fun wasRedirected() = redirectionPath.redirectionsCount > 0
private fun isSuccess(status: Int) = private fun isSuccess(status: Int) = status == HttpConstants.HTTP_OK || status == HttpConstants.HTTP_MULTI_STATUS
status == HttpConstants.HTTP_OK || status == HttpConstants.HTTP_MULTI_STATUS || status == HttpConstants.HTTP_MULTI_STATUS
companion object { companion object {
/** /**

View File

@ -23,9 +23,6 @@
*/ */
package com.owncloud.android.lib.resources.server package com.owncloud.android.lib.resources.server
import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkInfo
import android.net.Uri import android.net.Uri
import com.owncloud.android.lib.common.OwnCloudClient import com.owncloud.android.lib.common.OwnCloudClient
import com.owncloud.android.lib.common.http.HttpConstants import com.owncloud.android.lib.common.http.HttpConstants
@ -42,21 +39,18 @@ import java.util.concurrent.TimeUnit
import javax.net.ssl.SSLException import javax.net.ssl.SSLException
/** /**
* Checks if the server is valid and if the server supports the Share API * Checks if the server is valid
* *
* @author David A. Velasco * @author David A. Velasco
* @author masensio * @author masensio
* @author David González Verdugo * @author David González Verdugo
* @author Abel García de Prada
*/ */
class GetRemoteStatusOperation(private var context: Context) : RemoteOperation<OwnCloudVersion>() { class GetRemoteStatusOperation : RemoteOperation<OwnCloudVersion>() {
private lateinit var latestResult: RemoteOperationResult<OwnCloudVersion> private lateinit var latestResult: RemoteOperationResult<OwnCloudVersion>
override fun run(client: OwnCloudClient): RemoteOperationResult<OwnCloudVersion> { override fun run(client: OwnCloudClient): RemoteOperationResult<OwnCloudVersion> {
if (!isOnline) {
return RemoteOperationResult(ResultCode.NO_NETWORK_CONNECTION)
}
val baseUriStr = client.baseUri.toString() val baseUriStr = client.baseUri.toString()
if (baseUriStr.startsWith(HTTP_PREFIX) || baseUriStr.startsWith(HTTPS_PREFIX)) { if (baseUriStr.startsWith(HTTP_PREFIX) || baseUriStr.startsWith(HTTPS_PREFIX)) {
tryConnection(client) tryConnection(client)
@ -73,7 +67,7 @@ class GetRemoteStatusOperation(private var context: Context) : RemoteOperation<O
} }
private fun tryConnection(client: OwnCloudClient): Boolean { private fun tryConnection(client: OwnCloudClient): Boolean {
var retval = false var successfulConnection = false
val baseUrlSt = client.baseUri.toString() val baseUrlSt = client.baseUri.toString()
try { try {
var getMethod = GetMethod(URL(baseUrlSt + OwnCloudClient.STATUS_PATH)).apply { var getMethod = GetMethod(URL(baseUrlSt + OwnCloudClient.STATUS_PATH)).apply {
@ -90,10 +84,10 @@ class GetRemoteStatusOperation(private var context: Context) : RemoteOperation<O
else RemoteOperationResult(getMethod) else RemoteOperationResult(getMethod)
} catch (sslE: SSLException) { } catch (sslE: SSLException) {
latestResult = RemoteOperationResult(sslE) latestResult = RemoteOperationResult(sslE)
return false return successfulConnection
} }
var redirectedLocation = latestResult.redirectedLocation var redirectedLocation = latestResult.redirectedLocation
while (redirectedLocation != null && redirectedLocation.isNotEmpty() && !latestResult.isSuccess) { while (!redirectedLocation.isNullOrEmpty() && !latestResult.isSuccess) {
isRedirectToNonSecureConnection = isRedirectToNonSecureConnection =
isRedirectToNonSecureConnection or isRedirectToNonSecureConnection or
(baseUrlSt.startsWith(HTTPS_PREFIX) && redirectedLocation.startsWith(HTTP_PREFIX)) (baseUrlSt.startsWith(HTTPS_PREFIX) && redirectedLocation.startsWith(HTTP_PREFIX))
@ -108,7 +102,6 @@ class GetRemoteStatusOperation(private var context: Context) : RemoteOperation<O
} }
if (isSuccess(status)) { if (isSuccess(status)) {
Timber.d(getMethod.responseBodyAsString)
val respJSON = JSONObject(getMethod.responseBodyAsString) val respJSON = JSONObject(getMethod.responseBodyAsString)
if (!respJSON.getBoolean(NODE_INSTALLED)) { if (!respJSON.getBoolean(NODE_INSTALLED)) {
latestResult = RemoteOperationResult(ResultCode.INSTANCE_NOT_CONFIGURED) latestResult = RemoteOperationResult(ResultCode.INSTANCE_NOT_CONFIGURED)
@ -120,10 +113,11 @@ class GetRemoteStatusOperation(private var context: Context) : RemoteOperation<O
latestResult = if (isRedirectToNonSecureConnection) { latestResult = if (isRedirectToNonSecureConnection) {
RemoteOperationResult(ResultCode.OK_REDIRECT_TO_NON_SECURE_CONNECTION) RemoteOperationResult(ResultCode.OK_REDIRECT_TO_NON_SECURE_CONNECTION)
} else { } else {
RemoteOperationResult(if (baseUrlSt.startsWith(HTTPS_PREFIX)) ResultCode.OK_SSL else ResultCode.OK_NO_SSL) if (baseUrlSt.startsWith(HTTPS_PREFIX)) RemoteOperationResult(ResultCode.OK_SSL)
else RemoteOperationResult(ResultCode.OK_NO_SSL)
} }
latestResult.data = ocVersion latestResult.data = ocVersion
retval = true successfulConnection = true
} }
} else { } else {
latestResult = RemoteOperationResult(getMethod) latestResult = RemoteOperationResult(getMethod)
@ -134,25 +128,15 @@ class GetRemoteStatusOperation(private var context: Context) : RemoteOperation<O
latestResult = RemoteOperationResult(e) latestResult = RemoteOperationResult(e)
} }
when { when {
latestResult.isSuccess -> { latestResult.isSuccess -> Timber.i("Connection check at $baseUrlSt: ${latestResult.logMessage}")
Timber.i("Connection check at $baseUrlSt: ${latestResult.logMessage}")
}
latestResult.exception != null -> {
Timber.e(latestResult.exception, "Connection check at $baseUrlSt: ${latestResult.logMessage}")
}
else -> {
Timber.e("Connection check at $baseUrlSt: ${latestResult.logMessage}")
}
}
return retval
}
private val isOnline: Boolean latestResult.isException ->
get() { Timber.e(latestResult.exception, "Connection check at $baseUrlSt: ${latestResult.logMessage}")
val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val activeNetwork: NetworkInfo? = cm.activeNetworkInfo else -> Timber.e("Connection check at $baseUrlSt: ${latestResult.logMessage}")
return activeNetwork?.isConnected == true
} }
return successfulConnection
}
private fun isSuccess(status: Int): Boolean = status == HttpConstants.HTTP_OK private fun isSuccess(status: Int): Boolean = status == HttpConstants.HTTP_OK
@ -161,7 +145,7 @@ class GetRemoteStatusOperation(private var context: Context) : RemoteOperation<O
* Maximum time to wait for a response from the server when the connection is being tested, * Maximum time to wait for a response from the server when the connection is being tested,
* in MILLISECONDs. * in MILLISECONDs.
*/ */
const val TRY_CONNECTION_TIMEOUT: Long = 5000 private const val TRY_CONNECTION_TIMEOUT: Long = 5000
private const val NODE_INSTALLED = "installed" private const val NODE_INSTALLED = "installed"
private const val NODE_VERSION = "version" private const val NODE_VERSION = "version"
private const val HTTPS_PREFIX = "https://" private const val HTTPS_PREFIX = "https://"

View File

@ -21,7 +21,6 @@
* THE SOFTWARE. * THE SOFTWARE.
* *
*/ */
package com.owncloud.android.lib.resources.server package com.owncloud.android.lib.resources.server
import com.owncloud.android.lib.common.operations.RemoteOperationResult import com.owncloud.android.lib.common.operations.RemoteOperationResult
@ -29,5 +28,4 @@ import com.owncloud.android.lib.resources.Service
interface ServerService: Service { interface ServerService: Service {
fun checkPathExistence(path: String, isUserLogged: Boolean): RemoteOperationResult<Any> fun checkPathExistence(path: String, isUserLogged: Boolean): RemoteOperationResult<Any>
} }