From 1afa124b7d705a4871bfc0ae0d3f5932876b9bfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Abel=20Garc=C3=ADa=20de=20Prada?= Date: Mon, 10 Oct 2022 13:07:11 +0200 Subject: [PATCH] Add webfinger service and update the remote operation --- .../GetOCInstanceViaWebfingerOperation.kt | 20 +++++----- .../webfinger/services/WebfingerService.kt | 29 ++++++++++++++ .../implementation/OCWebfingerService.kt | 38 +++++++++++++++++++ 3 files changed, 76 insertions(+), 11 deletions(-) create mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/webfinger/services/WebfingerService.kt create mode 100644 owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/webfinger/services/implementation/OCWebfingerService.kt diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/webfinger/GetOCInstanceViaWebfingerOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/webfinger/GetOCInstanceViaWebfingerOperation.kt index ee6c7e95..42ee4b12 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/webfinger/GetOCInstanceViaWebfingerOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/webfinger/GetOCInstanceViaWebfingerOperation.kt @@ -31,22 +31,20 @@ import com.owncloud.android.lib.common.http.methods.nonwebdav.GetMethod import com.owncloud.android.lib.common.http.methods.nonwebdav.HttpMethod import com.owncloud.android.lib.common.operations.RemoteOperation import com.owncloud.android.lib.common.operations.RemoteOperationResult -import com.owncloud.android.lib.resources.status.HttpScheme import com.owncloud.android.lib.resources.webfinger.responses.WebfingerJrdResponse import com.squareup.moshi.Moshi import timber.log.Timber import java.net.URL class GetOCInstanceViaWebfingerOperation( - private val lockupServerDomain:String, - private val rel:String, - private val resource:String + private val lockupServerDomain: String, + private val rel: String, + private val resource: String, ) : RemoteOperation() { private fun buildRequestUri() = Uri.parse(lockupServerDomain).buildUpon() - .scheme(HttpScheme.HTTPS_SCHEME) - .path(WEBFINGER_PATH) + .path(ENDPOINT_WEBFINGER_PATH) .appendQueryParameter("rel", rel) .appendQueryParameter("resource", resource) .build() @@ -73,9 +71,9 @@ class GetOCInstanceViaWebfingerOperation( return RemoteOperationResult(method) } - private fun onRequestSuccessful(rawResponse:String): RemoteOperationResult { + private fun onRequestSuccessful(rawResponse: String): RemoteOperationResult { val response = parseResponse(rawResponse) - for(i in response.links) { + for (i in response.links) { if (i.rel == rel) { val operationResult = RemoteOperationResult(RemoteOperationResult.ResultCode.OK) operationResult.data = i.href @@ -98,13 +96,13 @@ class GetOCInstanceViaWebfingerOperation( } else { onResultUnsuccessful(getMethod, response, status) } - } catch(e: Exception) { + } catch (e: Exception) { Timber.e(e, "Requesting webfinger info failed") RemoteOperationResult(e) } } companion object { - val WEBFINGER_PATH = "/.well-known/webfinger" + private const val ENDPOINT_WEBFINGER_PATH = "/.well-known/webfinger" } -} \ No newline at end of file +} diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/webfinger/services/WebfingerService.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/webfinger/services/WebfingerService.kt new file mode 100644 index 00000000..4e057beb --- /dev/null +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/webfinger/services/WebfingerService.kt @@ -0,0 +1,29 @@ +/** + * ownCloud Android client application + * + * Copyright (C) 2022 ownCloud GmbH. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.owncloud.android.lib.resources.webfinger.services + +import com.owncloud.android.lib.common.OwnCloudClient +import com.owncloud.android.lib.common.operations.RemoteOperationResult + +interface WebfingerService { + fun getInstanceFromWebfinger( + lookupServer: String, + username: String, + client: OwnCloudClient, + ): RemoteOperationResult +} diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/webfinger/services/implementation/OCWebfingerService.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/webfinger/services/implementation/OCWebfingerService.kt new file mode 100644 index 00000000..3eab83d4 --- /dev/null +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/webfinger/services/implementation/OCWebfingerService.kt @@ -0,0 +1,38 @@ +/** + * ownCloud Android client application + * + * Copyright (C) 2022 ownCloud GmbH. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.owncloud.android.lib.resources.webfinger.services.implementation + +import com.owncloud.android.lib.common.OwnCloudClient +import com.owncloud.android.lib.common.operations.RemoteOperationResult +import com.owncloud.android.lib.resources.webfinger.GetOCInstanceViaWebfingerOperation +import com.owncloud.android.lib.resources.webfinger.services.WebfingerService + +class OCWebfingerService : WebfingerService { + + override fun getInstanceFromWebfinger( + lookupServer: String, + username: String, + client: OwnCloudClient, + ): RemoteOperationResult = + GetOCInstanceViaWebfingerOperation(lookupServer, OWNCLOUD_REL, username).execute(client) + + companion object { + private const val OWNCLOUD_REL = "http://webfinger.owncloud/rel/server-instance" + } + +}