24
24
25
25
package com.owncloud.android.lib.resources.webfinger
26
26
27
+ import android.net.Uri
27
28
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
28
32
import com.owncloud.android.lib.common.operations.RemoteOperation
29
33
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
30
39
31
40
class GetOCInstanceViaWebfingerOperation (
41
+ private val lockupServerDomain : String ,
32
42
private val rel : String ,
33
43
private val resource : String
34
44
) : RemoteOperation<String>() {
35
45
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"
38
109
}
39
110
}
0 commit comments