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

Commit f19b235

Browse files
theScrabiabelgardep
authored andcommitted
create scaffold for webfinger request operation
1 parent c966bf1 commit f19b235

File tree

2 files changed

+73
-13
lines changed

2 files changed

+73
-13
lines changed

owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/webfinger/GetOCInstanceViaWebfingerOperation.kt

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,87 @@
2424

2525
package com.owncloud.android.lib.resources.webfinger
2626

27+
import android.net.Uri
2728
import com.owncloud.android.lib.common.OwnCloudClient
29+
import com.owncloud.android.lib.common.http.HttpConstants
30+
import com.owncloud.android.lib.common.http.methods.nonwebdav.GetMethod
31+
import com.owncloud.android.lib.common.http.methods.nonwebdav.HttpMethod
2832
import com.owncloud.android.lib.common.operations.RemoteOperation
2933
import com.owncloud.android.lib.common.operations.RemoteOperationResult
34+
import com.owncloud.android.lib.resources.status.HttpScheme
35+
import com.owncloud.android.lib.resources.webfinger.responses.WebfingerJrdResponse
36+
import com.squareup.moshi.Moshi
37+
import timber.log.Timber
38+
import java.net.URL
3039

3140
class GetOCInstanceViaWebfingerOperation(
41+
private val lockupServerDomain:String,
3242
private val rel:String,
3343
private val resource:String
3444
) : RemoteOperation<String>() {
3545

36-
override fun run(client: OwnCloudClient?): RemoteOperationResult<String> {
37-
TODO("Not yet implemented")
46+
private fun buildRequestUri() =
47+
Uri.parse(lockupServerDomain).buildUpon()
48+
.scheme(HttpScheme.HTTPS_SCHEME)
49+
.path(WEBFINGER_PATH)
50+
.appendQueryParameter("rel", rel)
51+
.appendQueryParameter("resource", resource)
52+
.build()
53+
54+
private fun isSuccess(status: Int): Boolean = status == HttpConstants.HTTP_OK
55+
56+
private fun parseResponse(response: String): WebfingerJrdResponse {
57+
val moshi = Moshi.Builder().build()
58+
val adapter = moshi.adapter(WebfingerJrdResponse::class.java)
59+
return adapter.fromJson(response)!!
60+
}
61+
62+
private fun onResultUnsuccessful(
63+
method: HttpMethod,
64+
response: String?,
65+
status: Int
66+
): RemoteOperationResult<String> {
67+
Timber.e("Failed requesting webfinger info")
68+
if (response != null) {
69+
Timber.e("*** status code: $status; response message: $response")
70+
} else {
71+
Timber.e("*** status code: $status")
72+
}
73+
return RemoteOperationResult<String>(method)
74+
}
75+
76+
private fun onRequestSuccessful(rawResponse:String): RemoteOperationResult<String> {
77+
val response = parseResponse(rawResponse)
78+
for(i in response.links) {
79+
if (i.rel == rel) {
80+
val operationResult = RemoteOperationResult<String>(RemoteOperationResult.ResultCode.OK)
81+
operationResult.data = i.href
82+
return operationResult
83+
}
84+
}
85+
Timber.e("Could not find ownCloud relevant information in webfinger response: $rawResponse")
86+
throw java.lang.Exception("Could not find ownCloud relevant information in webfinger response")
87+
}
88+
89+
override fun run(client: OwnCloudClient): RemoteOperationResult<String> {
90+
val requestUri = buildRequestUri()
91+
val getMethod = GetMethod(URL(requestUri.toString()))
92+
return try {
93+
val status = client.executeHttpMethod(getMethod)
94+
val response = getMethod.getResponseBodyAsString()!!
95+
96+
if (isSuccess(status)) {
97+
onRequestSuccessful(response)
98+
} else {
99+
onResultUnsuccessful(getMethod, response, status)
100+
}
101+
} catch(e: Exception) {
102+
Timber.e(e, "Requesting webfinger info failed")
103+
RemoteOperationResult<String>(e)
104+
}
105+
}
106+
107+
companion object {
108+
val WEBFINGER_PATH = "/.well-known/webfinger"
38109
}
39110
}

owncloudComLibrary/src/test/java/com/owncloud/android/lib/resources/webfinger/GetOCInstanceViaWebfingerOperationTest.kt

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)