From c3b1a2312f4110fcb344d71e83ee3f952534cdcf Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Thu, 13 Dec 2018 12:40:40 +0100 Subject: Major refactor of server side modules architecture. --- .../pl/treksoft/kvision/remote/JoobyRemoteAgent.kt | 93 +++++++++++----------- .../treksoft/kvision/remote/JoobyServiceManager.kt | 14 ++-- .../kotlin/pl/treksoft/kvision/remote/Security.kt | 5 +- .../treksoft/kvision/remote/SpringRemoteAgent.kt | 93 +++++++++++----------- .../kvision/remote/SpringServiceManager.kt | 13 ++- 5 files changed, 106 insertions(+), 112 deletions(-) (limited to 'src/main') diff --git a/src/main/kotlin/pl/treksoft/kvision/remote/JoobyRemoteAgent.kt b/src/main/kotlin/pl/treksoft/kvision/remote/JoobyRemoteAgent.kt index 30d2062d..318f77ea 100644 --- a/src/main/kotlin/pl/treksoft/kvision/remote/JoobyRemoteAgent.kt +++ b/src/main/kotlin/pl/treksoft/kvision/remote/JoobyRemoteAgent.kt @@ -21,7 +21,6 @@ */ package pl.treksoft.kvision.remote -import kotlinx.coroutines.Deferred import kotlinx.coroutines.asDeferred import kotlinx.serialization.ImplicitReflectionSerializer import kotlinx.serialization.list @@ -43,7 +42,7 @@ open class JoobyRemoteAgent(val serviceManager: JoobyServiceManager) /** * Executes defined call to a remote web service. */ - inline fun call(noinline function: T.(Request?) -> Deferred): Deferred { + suspend inline fun call(noinline function: suspend T.(Request?) -> RET): RET { val (url, method) = serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") return callAgent.jsonRpcCall(url, method = method).then { @@ -58,15 +57,15 @@ open class JoobyRemoteAgent(val serviceManager: JoobyServiceManager) JSON.nonstrict.parse(RET::class.serializer(), it) } } - }.asDeferred() + }.asDeferred().await() } /** * Executes defined call to a remote web service. */ - inline fun call( - noinline function: T.(Request?) -> Deferred> - ): Deferred> { + suspend inline fun call( + noinline function: suspend T.(Request?) -> List + ): List { val (url, method) = serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") return callAgent.jsonRpcCall(url, method = method).then { @@ -80,15 +79,15 @@ open class JoobyRemoteAgent(val serviceManager: JoobyServiceManager) JSON.nonstrict.parse(RET::class.serializer().list, it) } } - }.asDeferred() + }.asDeferred().await() } /** * Executes defined call to a remote web service. */ - inline fun call( - noinline function: T.(PAR, Request?) -> Deferred, p: PAR - ): Deferred { + suspend inline fun call( + noinline function: suspend T.(PAR, Request?) -> RET, p: PAR + ): RET { val data = serialize(p) val (url, method) = serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") @@ -104,15 +103,15 @@ open class JoobyRemoteAgent(val serviceManager: JoobyServiceManager) JSON.nonstrict.parse(RET::class.serializer(), it) } } - }.asDeferred() + }.asDeferred().await() } /** * Executes defined call to a remote web service. */ - inline fun call( - noinline function: T.(PAR, Request?) -> Deferred>, p: PAR - ): Deferred> { + suspend inline fun call( + noinline function: suspend T.(PAR, Request?) -> List, p: PAR + ): List { val data = serialize(p) val (url, method) = serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") @@ -127,15 +126,15 @@ open class JoobyRemoteAgent(val serviceManager: JoobyServiceManager) JSON.nonstrict.parse(RET::class.serializer().list, it) } } - }.asDeferred() + }.asDeferred().await() } /** * Executes defined call to a remote web service. */ - inline fun call( - noinline function: T.(PAR1, PAR2, Request?) -> Deferred, p1: PAR1, p2: PAR2 - ): Deferred { + suspend inline fun call( + noinline function: suspend T.(PAR1, PAR2, Request?) -> RET, p1: PAR1, p2: PAR2 + ): RET { val data1 = serialize(p1) val data2 = serialize(p2) val (url, method) = @@ -152,15 +151,15 @@ open class JoobyRemoteAgent(val serviceManager: JoobyServiceManager) JSON.nonstrict.parse(RET::class.serializer(), it) } } - }.asDeferred() + }.asDeferred().await() } /** * Executes defined call to a remote web service. */ - inline fun call( - noinline function: T.(PAR1, PAR2, Request?) -> Deferred>, p1: PAR1, p2: PAR2 - ): Deferred> { + suspend inline fun call( + noinline function: suspend T.(PAR1, PAR2, Request?) -> List, p1: PAR1, p2: PAR2 + ): List { val data1 = serialize(p1) val data2 = serialize(p2) val (url, method) = @@ -176,15 +175,15 @@ open class JoobyRemoteAgent(val serviceManager: JoobyServiceManager) JSON.nonstrict.parse(RET::class.serializer().list, it) } } - }.asDeferred() + }.asDeferred().await() } /** * Executes defined call to a remote web service. */ - inline fun call( - noinline function: T.(PAR1, PAR2, PAR3, Request?) -> Deferred, p1: PAR1, p2: PAR2, p3: PAR3 - ): Deferred { + suspend inline fun call( + noinline function: suspend T.(PAR1, PAR2, PAR3, Request?) -> RET, p1: PAR1, p2: PAR2, p3: PAR3 + ): RET { val data1 = serialize(p1) val data2 = serialize(p2) val data3 = serialize(p3) @@ -202,15 +201,15 @@ open class JoobyRemoteAgent(val serviceManager: JoobyServiceManager) JSON.nonstrict.parse(RET::class.serializer(), it) } } - }.asDeferred() + }.asDeferred().await() } /** * Executes defined call to a remote web service. */ - inline fun call( - noinline function: T.(PAR1, PAR2, PAR3, Request?) -> Deferred>, p1: PAR1, p2: PAR2, p3: PAR3 - ): Deferred> { + suspend inline fun call( + noinline function: suspend T.(PAR1, PAR2, PAR3, Request?) -> List, p1: PAR1, p2: PAR2, p3: PAR3 + ): List { val data1 = serialize(p1) val data2 = serialize(p2) val data3 = serialize(p3) @@ -227,15 +226,15 @@ open class JoobyRemoteAgent(val serviceManager: JoobyServiceManager) JSON.nonstrict.parse(RET::class.serializer().list, it) } } - }.asDeferred() + }.asDeferred().await() } /** * Executes defined call to a remote web service. */ - inline fun call( - noinline function: T.(PAR1, PAR2, PAR3, PAR4, Request?) -> Deferred, p1: PAR1, p2: PAR2, p3: PAR3, p4: PAR4 - ): Deferred { + suspend inline fun call( + noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, Request?) -> RET, p1: PAR1, p2: PAR2, p3: PAR3, p4: PAR4 + ): RET { val data1 = serialize(p1) val data2 = serialize(p2) val data3 = serialize(p3) @@ -254,19 +253,19 @@ open class JoobyRemoteAgent(val serviceManager: JoobyServiceManager) JSON.nonstrict.parse(RET::class.serializer(), it) } } - }.asDeferred() + }.asDeferred().await() } /** * Executes defined call to a remote web service. */ - inline fun call( - noinline function: T.(PAR1, PAR2, PAR3, PAR4, Request?) -> Deferred>, + suspend inline fun call( + noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, Request?) -> List, p1: PAR1, p2: PAR2, p3: PAR3, p4: PAR4 - ): Deferred> { + ): List { val data1 = serialize(p1) val data2 = serialize(p2) val data3 = serialize(p3) @@ -284,22 +283,22 @@ open class JoobyRemoteAgent(val serviceManager: JoobyServiceManager) JSON.nonstrict.parse(RET::class.serializer().list, it) } } - }.asDeferred() + }.asDeferred().await() } /** * Executes defined call to a remote web service. */ @Suppress("LongParameterList") - inline fun call( - noinline function: T.(PAR1, PAR2, PAR3, PAR4, PAR5, Request?) -> Deferred, + noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5, Request?) -> RET, p1: PAR1, p2: PAR2, p3: PAR3, p4: PAR4, p5: PAR5 - ): Deferred { + ): RET { val data1 = serialize(p1) val data2 = serialize(p2) val data3 = serialize(p3) @@ -319,22 +318,22 @@ open class JoobyRemoteAgent(val serviceManager: JoobyServiceManager) JSON.nonstrict.parse(RET::class.serializer(), it) } } - }.asDeferred() + }.asDeferred().await() } /** * Executes defined call to a remote web service. */ @Suppress("LongParameterList") - inline fun call( - noinline function: T.(PAR1, PAR2, PAR3, PAR4, PAR5, Request?) -> Deferred>, + noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5, Request?) -> List, p1: PAR1, p2: PAR2, p3: PAR3, p4: PAR4, p5: PAR5 - ): Deferred> { + ): List { val data1 = serialize(p1) val data2 = serialize(p2) val data3 = serialize(p3) @@ -353,7 +352,7 @@ open class JoobyRemoteAgent(val serviceManager: JoobyServiceManager) JSON.nonstrict.parse(RET::class.serializer().list, it) } } - }.asDeferred() + }.asDeferred().await() } diff --git a/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt b/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt index c9d8ba25..0d3515a1 100644 --- a/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt +++ b/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt @@ -21,8 +21,6 @@ */ package pl.treksoft.kvision.remote -import kotlinx.coroutines.Deferred - /** * Multiplatform service manager for Jooby. */ @@ -38,7 +36,7 @@ actual open class JoobyServiceManager actual constructor(service: T) : * @param method a HTTP method */ protected actual inline fun bind( - noinline function: T.(Request?) -> Deferred, + noinline function: suspend T.(Request?) -> RET, route: String?, method: RpcHttpMethod ) { val routeDef = route ?: "route${this::class.simpleName}${counter++}" @@ -52,7 +50,7 @@ actual open class JoobyServiceManager actual constructor(service: T) : * @param method a HTTP method */ protected actual inline fun bind( - noinline function: T.(PAR, Request?) -> Deferred, + noinline function: suspend T.(PAR, Request?) -> RET, route: String?, method: RpcHttpMethod ) { val routeDef = route ?: "route${this::class.simpleName}${counter++}" @@ -66,7 +64,7 @@ actual open class JoobyServiceManager actual constructor(service: T) : * @param method a HTTP method */ protected actual inline fun bind( - noinline function: T.(PAR1, PAR2, Request?) -> Deferred, + noinline function: suspend T.(PAR1, PAR2, Request?) -> RET, route: String?, method: RpcHttpMethod ) { val routeDef = route ?: "route${this::class.simpleName}${counter++}" @@ -80,7 +78,7 @@ actual open class JoobyServiceManager actual constructor(service: T) : * @param method a HTTP method */ protected actual inline fun bind( - noinline function: T.(PAR1, PAR2, PAR3, Request?) -> Deferred, + noinline function: suspend T.(PAR1, PAR2, PAR3, Request?) -> RET, route: String?, method: RpcHttpMethod ) { val routeDef = route ?: "route${this::class.simpleName}${counter++}" @@ -94,7 +92,7 @@ actual open class JoobyServiceManager actual constructor(service: T) : * @param method a HTTP method */ protected actual inline fun bind( - noinline function: T.(PAR1, PAR2, PAR3, PAR4, Request?) -> Deferred, + noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, Request?) -> RET, route: String?, method: RpcHttpMethod ) { val routeDef = route ?: "route${this::class.simpleName}${counter++}" @@ -109,7 +107,7 @@ actual open class JoobyServiceManager actual constructor(service: T) : */ protected actual inline fun bind( - noinline function: T.(PAR1, PAR2, PAR3, PAR4, PAR5, Request?) -> Deferred, + noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5, Request?) -> RET, route: String?, method: RpcHttpMethod ) { diff --git a/src/main/kotlin/pl/treksoft/kvision/remote/Security.kt b/src/main/kotlin/pl/treksoft/kvision/remote/Security.kt index bf77a06b..b485e17d 100644 --- a/src/main/kotlin/pl/treksoft/kvision/remote/Security.kt +++ b/src/main/kotlin/pl/treksoft/kvision/remote/Security.kt @@ -21,7 +21,6 @@ */ package pl.treksoft.kvision.remote -import kotlinx.coroutines.Deferred import kotlinx.coroutines.asDeferred import kotlinx.serialization.Serializable import pl.treksoft.kvision.utils.obj @@ -47,12 +46,12 @@ class LoginService { * Login with Pac4j FormClient. * @param credentials username and password credentials */ - fun login(credentials: Credentials?): Deferred = + suspend fun login(credentials: Credentials?): Boolean = if (credentials?.username != null) { loginAgent.remoteCall("callback?client_name=FormClient", obj { this.username = credentials.username this.password = credentials.password - }, HttpMethod.POST, "application/x-www-form-urlencoded").then { _: dynamic -> true }.asDeferred() + }, HttpMethod.POST, "application/x-www-form-urlencoded").then { _: dynamic -> true }.asDeferred().await() } else { throw SecurityException("Credentials cannot be empty") } diff --git a/src/main/kotlin/pl/treksoft/kvision/remote/SpringRemoteAgent.kt b/src/main/kotlin/pl/treksoft/kvision/remote/SpringRemoteAgent.kt index f329d10e..fea16b99 100644 --- a/src/main/kotlin/pl/treksoft/kvision/remote/SpringRemoteAgent.kt +++ b/src/main/kotlin/pl/treksoft/kvision/remote/SpringRemoteAgent.kt @@ -21,7 +21,6 @@ */ package pl.treksoft.kvision.remote -import kotlinx.coroutines.Deferred import kotlinx.coroutines.asDeferred import kotlinx.serialization.ImplicitReflectionSerializer import kotlinx.serialization.list @@ -43,7 +42,7 @@ open class SpringRemoteAgent(val serviceManager: SpringServiceManager call(noinline function: T.() -> Deferred): Deferred { + suspend inline fun call(noinline function: suspend T.() -> RET): RET { val (url, method) = serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") return callAgent.jsonRpcCall(url, method = method).then { @@ -58,15 +57,15 @@ open class SpringRemoteAgent(val serviceManager: SpringServiceManager call( - noinline function: T.() -> Deferred> - ): Deferred> { + suspend inline fun call( + noinline function: suspend T.() -> List + ): List { val (url, method) = serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") return callAgent.jsonRpcCall(url, method = method).then { @@ -80,15 +79,15 @@ open class SpringRemoteAgent(val serviceManager: SpringServiceManager call( - noinline function: T.(PAR) -> Deferred, p: PAR - ): Deferred { + suspend inline fun call( + noinline function: suspend T.(PAR) -> RET, p: PAR + ): RET { val data = serialize(p) val (url, method) = serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") @@ -104,15 +103,15 @@ open class SpringRemoteAgent(val serviceManager: SpringServiceManager call( - noinline function: T.(PAR) -> Deferred>, p: PAR - ): Deferred> { + suspend inline fun call( + noinline function: suspend T.(PAR) -> List, p: PAR + ): List { val data = serialize(p) val (url, method) = serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") @@ -127,15 +126,15 @@ open class SpringRemoteAgent(val serviceManager: SpringServiceManager call( - noinline function: T.(PAR1, PAR2) -> Deferred, p1: PAR1, p2: PAR2 - ): Deferred { + suspend inline fun call( + noinline function: suspend T.(PAR1, PAR2) -> RET, p1: PAR1, p2: PAR2 + ): RET { val data1 = serialize(p1) val data2 = serialize(p2) val (url, method) = @@ -152,15 +151,15 @@ open class SpringRemoteAgent(val serviceManager: SpringServiceManager call( - noinline function: T.(PAR1, PAR2) -> Deferred>, p1: PAR1, p2: PAR2 - ): Deferred> { + suspend inline fun call( + noinline function: suspend T.(PAR1, PAR2) -> List, p1: PAR1, p2: PAR2 + ): List { val data1 = serialize(p1) val data2 = serialize(p2) val (url, method) = @@ -176,15 +175,15 @@ open class SpringRemoteAgent(val serviceManager: SpringServiceManager call( - noinline function: T.(PAR1, PAR2, PAR3) -> Deferred, p1: PAR1, p2: PAR2, p3: PAR3 - ): Deferred { + suspend inline fun call( + noinline function: suspend T.(PAR1, PAR2, PAR3) -> RET, p1: PAR1, p2: PAR2, p3: PAR3 + ): RET { val data1 = serialize(p1) val data2 = serialize(p2) val data3 = serialize(p3) @@ -202,15 +201,15 @@ open class SpringRemoteAgent(val serviceManager: SpringServiceManager call( - noinline function: T.(PAR1, PAR2, PAR3) -> Deferred>, p1: PAR1, p2: PAR2, p3: PAR3 - ): Deferred> { + suspend inline fun call( + noinline function: suspend T.(PAR1, PAR2, PAR3) -> List, p1: PAR1, p2: PAR2, p3: PAR3 + ): List { val data1 = serialize(p1) val data2 = serialize(p2) val data3 = serialize(p3) @@ -227,15 +226,15 @@ open class SpringRemoteAgent(val serviceManager: SpringServiceManager call( - noinline function: T.(PAR1, PAR2, PAR3, PAR4) -> Deferred, p1: PAR1, p2: PAR2, p3: PAR3, p4: PAR4 - ): Deferred { + suspend inline fun call( + noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4) -> RET, p1: PAR1, p2: PAR2, p3: PAR3, p4: PAR4 + ): RET { val data1 = serialize(p1) val data2 = serialize(p2) val data3 = serialize(p3) @@ -254,19 +253,19 @@ open class SpringRemoteAgent(val serviceManager: SpringServiceManager call( - noinline function: T.(PAR1, PAR2, PAR3, PAR4) -> Deferred>, + suspend inline fun call( + noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4) -> List, p1: PAR1, p2: PAR2, p3: PAR3, p4: PAR4 - ): Deferred> { + ): List { val data1 = serialize(p1) val data2 = serialize(p2) val data3 = serialize(p3) @@ -284,22 +283,22 @@ open class SpringRemoteAgent(val serviceManager: SpringServiceManager call( - noinline function: T.(PAR1, PAR2, PAR3, PAR4, PAR5) -> Deferred, + noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5) -> RET, p1: PAR1, p2: PAR2, p3: PAR3, p4: PAR4, p5: PAR5 - ): Deferred { + ): RET { val data1 = serialize(p1) val data2 = serialize(p2) val data3 = serialize(p3) @@ -319,22 +318,22 @@ open class SpringRemoteAgent(val serviceManager: SpringServiceManager call( - noinline function: T.(PAR1, PAR2, PAR3, PAR4, PAR5) -> Deferred>, + noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5) -> List, p1: PAR1, p2: PAR2, p3: PAR3, p4: PAR4, p5: PAR5 - ): Deferred> { + ): List { val data1 = serialize(p1) val data2 = serialize(p2) val data3 = serialize(p3) @@ -353,7 +352,7 @@ open class SpringRemoteAgent(val serviceManager: SpringServiceManager actual constructor(serviceClass: * @param method a HTTP method */ protected actual inline fun bind( - noinline function: T.() -> Deferred, + noinline function: suspend T.() -> RET, route: String?, method: RpcHttpMethod ) { val routeDef = route ?: "route${this::class.simpleName}${counter++}" @@ -53,7 +52,7 @@ actual open class SpringServiceManager actual constructor(serviceClass: * @param method a HTTP method */ protected actual inline fun bind( - noinline function: T.(PAR) -> Deferred, + noinline function: suspend T.(PAR) -> RET, route: String?, method: RpcHttpMethod ) { val routeDef = route ?: "route${this::class.simpleName}${counter++}" @@ -67,7 +66,7 @@ actual open class SpringServiceManager actual constructor(serviceClass: * @param method a HTTP method */ protected actual inline fun bind( - noinline function: T.(PAR1, PAR2) -> Deferred, + noinline function: suspend T.(PAR1, PAR2) -> RET, route: String?, method: RpcHttpMethod ) { val routeDef = route ?: "route${this::class.simpleName}${counter++}" @@ -81,7 +80,7 @@ actual open class SpringServiceManager actual constructor(serviceClass: * @param method a HTTP method */ protected actual inline fun bind( - noinline function: T.(PAR1, PAR2, PAR3) -> Deferred, + noinline function: suspend T.(PAR1, PAR2, PAR3) -> RET, route: String?, method: RpcHttpMethod ) { val routeDef = route ?: "route${this::class.simpleName}${counter++}" @@ -95,7 +94,7 @@ actual open class SpringServiceManager actual constructor(serviceClass: * @param method a HTTP method */ protected actual inline fun bind( - noinline function: T.(PAR1, PAR2, PAR3, PAR4) -> Deferred, + noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4) -> RET, route: String?, method: RpcHttpMethod ) { val routeDef = route ?: "route${this::class.simpleName}${counter++}" @@ -110,7 +109,7 @@ actual open class SpringServiceManager actual constructor(serviceClass: */ protected actual inline fun bind( - noinline function: T.(PAR1, PAR2, PAR3, PAR4, PAR5) -> Deferred, + noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5) -> RET, route: String?, method: RpcHttpMethod ) { -- cgit