diff options
13 files changed, 113 insertions, 904 deletions
diff --git a/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt b/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt deleted file mode 100644 index cb1b44d0..00000000 --- a/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt +++ /dev/null @@ -1,107 +0,0 @@ -/* - * Copyright (c) 2017-present Robert Jaros - * - * 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 pl.treksoft.kvision.remote - -/** - * Multiplatform service manager for Jooby. - */ -expect open class JoobyServiceManager<T : Any>(service: T) : ServiceManager { - - /** - * Binds a given route with a function of the receiver. - * @param function a function of the receiver - * @param route a route - * @param method a HTTP method - */ - protected inline fun <reified RET> bind( - noinline function: suspend T.(Request?) -> RET, - route: String? = null, - method: RpcHttpMethod = RpcHttpMethod.POST - ) - - /** - * Binds a given route with a function of the receiver. - * @param function a function of the receiver - * @param route a route - * @param method a HTTP method - */ - protected inline fun <reified PAR, reified RET> bind( - noinline function: suspend T.(PAR, Request?) -> RET, - route: String? = null, - method: RpcHttpMethod = RpcHttpMethod.POST - ) - - /** - * Binds a given route with a function of the receiver. - * @param function a function of the receiver - * @param route a route - * @param method a HTTP method - */ - protected inline fun <reified PAR1, reified PAR2, reified RET> bind( - noinline function: suspend T.(PAR1, PAR2, Request?) -> RET, - route: String? = null, - method: RpcHttpMethod = RpcHttpMethod.POST - ) - - /** - * Binds a given route with a function of the receiver. - * @param function a function of the receiver - * @param route a route - * @param method a HTTP method - */ - protected inline fun <reified PAR1, reified PAR2, reified PAR3, reified RET> bind( - noinline function: suspend T.(PAR1, PAR2, PAR3, Request?) -> RET, - route: String? = null, - method: RpcHttpMethod = RpcHttpMethod.POST - ) - - /** - * Binds a given route with a function of the receiver. - * @param function a function of the receiver - * @param route a route - * @param method a HTTP method - */ - protected inline fun <reified PAR1, reified PAR2, reified PAR3, reified PAR4, reified RET> bind( - noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, Request?) -> RET, - route: String? = null, - method: RpcHttpMethod = RpcHttpMethod.POST - ) - - /** - * Binds a given route with a function of the receiver. - * @param function a function of the receiver - * @param route a route - * @param method a HTTP method - */ - protected inline fun <reified PAR1, reified PAR2, reified PAR3, reified PAR4, reified PAR5, reified RET> bind( - noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5, Request?) -> RET, - route: String? = null, - method: RpcHttpMethod = RpcHttpMethod.POST - ) - - /** - * Applies all defined routes to the given server. - * @param k a server - */ - fun applyRoutes(k: KVServer) - -} diff --git a/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt b/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt index bc45d72b..4b4fcfd2 100644 --- a/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt +++ b/kvision-modules/kvision-common-remote/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt @@ -24,9 +24,9 @@ package pl.treksoft.kvision.remote import kotlin.reflect.KClass /** - * Multiplatform service manager for Spring Boot. + * Multiplatform service manager. */ -expect open class SpringServiceManager<T : Any>(serviceClass: KClass<T>) : ServiceManager { +expect open class KVServiceManager<T : Any>(serviceClass: KClass<T>) : ServiceManager { /** * Binds a given route with a function of the receiver. @@ -105,6 +105,11 @@ expect open class SpringServiceManager<T : Any>(serviceClass: KClass<T>) : Servi * @param function a function of the receiver */ protected fun bind( - function: T.(String) -> List<RemoteSelectOption> + function: T.(String?, String?) -> List<RemoteSelectOption> ) + + /** + * Applies all defined routes to the given server. + */ + fun applyRoutes(k: KVServer) } diff --git a/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/JoobyRemoteAgent.kt b/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/JoobyRemoteAgent.kt deleted file mode 100644 index 318f77ea..00000000 --- a/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/JoobyRemoteAgent.kt +++ /dev/null @@ -1,370 +0,0 @@ -/* - * Copyright (c) 2017-present Robert Jaros - * - * 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 pl.treksoft.kvision.remote - -import kotlinx.coroutines.asDeferred -import kotlinx.serialization.ImplicitReflectionSerializer -import kotlinx.serialization.list -import kotlinx.serialization.serializer -import pl.treksoft.kvision.utils.JSON -import kotlin.js.js -import kotlin.reflect.KClass -import kotlin.js.JSON as NativeJSON - -/** - * Client side agent for JSON-RPC remote calls with Jooby. - */ -@Suppress("LargeClass", "TooManyFunctions") -@UseExperimental(ImplicitReflectionSerializer::class) -open class JoobyRemoteAgent<T : Any>(val serviceManager: JoobyServiceManager<T>) : RemoteAgent { - - val callAgent = CallAgent() - - /** - * Executes defined call to a remote web service. - */ - suspend inline fun <reified RET : Any, T> 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 { - try { - @Suppress("UNCHECKED_CAST") - deserialize<RET>(it, RET::class.js.name) - } catch (t: NotStandardTypeException) { - try { - @Suppress("UNCHECKED_CAST") - tryDeserializeEnum(RET::class as KClass<Any>, it) as RET - } catch (t: NotEnumTypeException) { - JSON.nonstrict.parse(RET::class.serializer(), it) - } - } - }.asDeferred().await() - } - - /** - * Executes defined call to a remote web service. - */ - suspend inline fun <reified RET : Any, T> call( - noinline function: suspend T.(Request?) -> List<RET> - ): List<RET> { - val (url, method) = - serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") - return callAgent.jsonRpcCall(url, method = method).then { - try { - deserializeList<RET>(it, RET::class.js.name) - } catch (t: NotStandardTypeException) { - try { - @Suppress("UNCHECKED_CAST") - tryDeserializeEnumList(RET::class as KClass<Any>, it) as List<RET> - } catch (t: NotEnumTypeException) { - JSON.nonstrict.parse(RET::class.serializer().list, it) - } - } - }.asDeferred().await() - } - - /** - * Executes defined call to a remote web service. - */ - suspend inline fun <reified PAR, reified RET : Any, T> 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!") - return callAgent.jsonRpcCall(url, listOf(data), method).then { - try { - @Suppress("UNCHECKED_CAST") - deserialize<RET>(it, RET::class.js.name) - } catch (t: NotStandardTypeException) { - try { - @Suppress("UNCHECKED_CAST") - tryDeserializeEnum(RET::class as KClass<Any>, it) as RET - } catch (t: NotEnumTypeException) { - JSON.nonstrict.parse(RET::class.serializer(), it) - } - } - }.asDeferred().await() - } - - /** - * Executes defined call to a remote web service. - */ - suspend inline fun <reified PAR, reified RET : Any, T> call( - noinline function: suspend T.(PAR, Request?) -> List<RET>, p: PAR - ): List<RET> { - val data = serialize(p) - val (url, method) = - serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") - return callAgent.jsonRpcCall(url, listOf(data), method).then { - try { - deserializeList<RET>(it, RET::class.js.name) - } catch (t: NotStandardTypeException) { - try { - @Suppress("UNCHECKED_CAST") - tryDeserializeEnumList(RET::class as KClass<Any>, it) as List<RET> - } catch (t: NotEnumTypeException) { - JSON.nonstrict.parse(RET::class.serializer().list, it) - } - } - }.asDeferred().await() - } - - /** - * Executes defined call to a remote web service. - */ - suspend inline fun <reified PAR1, reified PAR2, reified RET : Any, T> 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) = - serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") - return callAgent.jsonRpcCall(url, listOf(data1, data2), method).then { - try { - @Suppress("UNCHECKED_CAST") - deserialize<RET>(it, RET::class.js.name) - } catch (t: NotStandardTypeException) { - try { - @Suppress("UNCHECKED_CAST") - tryDeserializeEnum(RET::class as KClass<Any>, it) as RET - } catch (t: NotEnumTypeException) { - JSON.nonstrict.parse(RET::class.serializer(), it) - } - } - }.asDeferred().await() - } - - /** - * Executes defined call to a remote web service. - */ - suspend inline fun <reified PAR1, reified PAR2, reified RET : Any, T> call( - noinline function: suspend T.(PAR1, PAR2, Request?) -> List<RET>, p1: PAR1, p2: PAR2 - ): List<RET> { - val data1 = serialize(p1) - val data2 = serialize(p2) - val (url, method) = - serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") - return callAgent.jsonRpcCall(url, listOf(data1, data2), method).then { - try { - deserializeList<RET>(it, RET::class.js.name) - } catch (t: NotStandardTypeException) { - try { - @Suppress("UNCHECKED_CAST") - tryDeserializeEnumList(RET::class as KClass<Any>, it) as List<RET> - } catch (t: NotEnumTypeException) { - JSON.nonstrict.parse(RET::class.serializer().list, it) - } - } - }.asDeferred().await() - } - - /** - * Executes defined call to a remote web service. - */ - suspend inline fun <reified PAR1, reified PAR2, reified PAR3, reified RET : Any, T> 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) - val (url, method) = - serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") - return callAgent.jsonRpcCall(url, listOf(data1, data2, data3), method).then { - try { - @Suppress("UNCHECKED_CAST") - deserialize<RET>(it, RET::class.js.name) - } catch (t: NotStandardTypeException) { - try { - @Suppress("UNCHECKED_CAST") - tryDeserializeEnum(RET::class as KClass<Any>, it) as RET - } catch (t: NotEnumTypeException) { - JSON.nonstrict.parse(RET::class.serializer(), it) - } - } - }.asDeferred().await() - } - - /** - * Executes defined call to a remote web service. - */ - suspend inline fun <reified PAR1, reified PAR2, reified PAR3, reified RET : Any, T> call( - noinline function: suspend T.(PAR1, PAR2, PAR3, Request?) -> List<RET>, p1: PAR1, p2: PAR2, p3: PAR3 - ): List<RET> { - val data1 = serialize(p1) - val data2 = serialize(p2) - val data3 = serialize(p3) - val (url, method) = - serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") - return callAgent.jsonRpcCall(url, listOf(data1, data2, data3), method).then { - try { - deserializeList<RET>(it, RET::class.js.name) - } catch (t: NotStandardTypeException) { - try { - @Suppress("UNCHECKED_CAST") - tryDeserializeEnumList(RET::class as KClass<Any>, it) as List<RET> - } catch (t: NotEnumTypeException) { - JSON.nonstrict.parse(RET::class.serializer().list, it) - } - } - }.asDeferred().await() - } - - /** - * Executes defined call to a remote web service. - */ - suspend inline fun <reified PAR1, reified PAR2, reified PAR3, reified PAR4, reified RET : Any, T> 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) - val data4 = serialize(p4) - val (url, method) = - serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") - return callAgent.jsonRpcCall(url, listOf(data1, data2, data3, data4), method).then { - try { - @Suppress("UNCHECKED_CAST") - deserialize<RET>(it, RET::class.js.name) - } catch (t: NotStandardTypeException) { - try { - @Suppress("UNCHECKED_CAST") - tryDeserializeEnum(RET::class as KClass<Any>, it) as RET - } catch (t: NotEnumTypeException) { - JSON.nonstrict.parse(RET::class.serializer(), it) - } - } - }.asDeferred().await() - } - - /** - * Executes defined call to a remote web service. - */ - suspend inline fun <reified PAR1, reified PAR2, reified PAR3, reified PAR4, reified RET : Any, T> call( - noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, Request?) -> List<RET>, - p1: PAR1, - p2: PAR2, - p3: PAR3, - p4: PAR4 - ): List<RET> { - val data1 = serialize(p1) - val data2 = serialize(p2) - val data3 = serialize(p3) - val data4 = serialize(p4) - val (url, method) = - serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") - return callAgent.jsonRpcCall(url, listOf(data1, data2, data3, data4), method).then { - try { - deserializeList<RET>(it, RET::class.js.name) - } catch (t: NotStandardTypeException) { - try { - @Suppress("UNCHECKED_CAST") - tryDeserializeEnumList(RET::class as KClass<Any>, it) as List<RET> - } catch (t: NotEnumTypeException) { - JSON.nonstrict.parse(RET::class.serializer().list, it) - } - } - }.asDeferred().await() - } - - /** - * Executes defined call to a remote web service. - */ - @Suppress("LongParameterList") - suspend inline fun <reified PAR1, reified PAR2, reified PAR3, reified PAR4, reified PAR5, - reified RET : Any, T> call( - noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5, Request?) -> RET, - p1: PAR1, - p2: PAR2, - p3: PAR3, - p4: PAR4, - p5: PAR5 - ): RET { - val data1 = serialize(p1) - val data2 = serialize(p2) - val data3 = serialize(p3) - val data4 = serialize(p4) - val data5 = serialize(p5) - val (url, method) = - serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") - return callAgent.jsonRpcCall(url, listOf(data1, data2, data3, data4, data5), method).then { - try { - @Suppress("UNCHECKED_CAST") - deserialize<RET>(it, RET::class.js.name) - } catch (t: NotStandardTypeException) { - try { - @Suppress("UNCHECKED_CAST") - tryDeserializeEnum(RET::class as KClass<Any>, it) as RET - } catch (t: NotEnumTypeException) { - JSON.nonstrict.parse(RET::class.serializer(), it) - } - } - }.asDeferred().await() - } - - /** - * Executes defined call to a remote web service. - */ - @Suppress("LongParameterList") - suspend inline fun <reified PAR1, reified PAR2, reified PAR3, reified PAR4, reified PAR5, - reified RET : Any, T> call( - noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5, Request?) -> List<RET>, - p1: PAR1, - p2: PAR2, - p3: PAR3, - p4: PAR4, - p5: PAR5 - ): List<RET> { - val data1 = serialize(p1) - val data2 = serialize(p2) - val data3 = serialize(p3) - val data4 = serialize(p4) - val data5 = serialize(p5) - val (url, method) = - serviceManager.getCalls()[function.toString()] ?: throw IllegalStateException("Function not specified!") - return callAgent.jsonRpcCall(url, listOf(data1, data2, data3, data4, data5), method).then { - try { - deserializeList<RET>(it, RET::class.js.name) - } catch (t: NotStandardTypeException) { - try { - @Suppress("UNCHECKED_CAST") - tryDeserializeEnumList(RET::class as KClass<Any>, it) as List<RET> - } catch (t: NotEnumTypeException) { - JSON.nonstrict.parse(RET::class.serializer().list, it) - } - } - }.asDeferred().await() - } - - - /** - * @suppress - * Internal function - */ - inline fun <reified PAR> serialize(value: PAR): String? { - return value?.let { - @Suppress("UNCHECKED_CAST") - trySerialize((PAR::class as KClass<Any>), it as Any) - } - } - -} diff --git a/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt b/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt deleted file mode 100644 index 0d3515a1..00000000 --- a/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt +++ /dev/null @@ -1,129 +0,0 @@ -/* - * Copyright (c) 2017-present Robert Jaros - * - * 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 pl.treksoft.kvision.remote - -/** - * Multiplatform service manager for Jooby. - */ -actual open class JoobyServiceManager<T : Any> actual constructor(service: T) : ServiceManager { - - protected val calls: MutableMap<String, Pair<String, RpcHttpMethod>> = mutableMapOf() - var counter: Int = 0 - - /** - * Binds a given route with a function of the receiver. - * @param function a function of the receiver - * @param route a route - * @param method a HTTP method - */ - protected actual inline fun <reified RET> bind( - noinline function: suspend T.(Request?) -> RET, - route: String?, method: RpcHttpMethod - ) { - val routeDef = route ?: "route${this::class.simpleName}${counter++}" - calls[function.toString()] = Pair("/kv/$routeDef", method) - } - - /** - * Binds a given route with a function of the receiver. - * @param function a function of the receiver - * @param route a route - * @param method a HTTP method - */ - protected actual inline fun <reified PAR, reified RET> bind( - noinline function: suspend T.(PAR, Request?) -> RET, - route: String?, method: RpcHttpMethod - ) { - val routeDef = route ?: "route${this::class.simpleName}${counter++}" - calls[function.toString()] = Pair("/kv/$routeDef", method) - } - - /** - * Binds a given route with a function of the receiver. - * @param function a function of the receiver - * @param route a route - * @param method a HTTP method - */ - protected actual inline fun <reified PAR1, reified PAR2, reified RET> bind( - noinline function: suspend T.(PAR1, PAR2, Request?) -> RET, - route: String?, method: RpcHttpMethod - ) { - val routeDef = route ?: "route${this::class.simpleName}${counter++}" - calls[function.toString()] = Pair("/kv/$routeDef", method) - } - - /** - * Binds a given route with a function of the receiver. - * @param function a function of the receiver - * @param route a route - * @param method a HTTP method - */ - protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, reified RET> bind( - noinline function: suspend T.(PAR1, PAR2, PAR3, Request?) -> RET, - route: String?, method: RpcHttpMethod - ) { - val routeDef = route ?: "route${this::class.simpleName}${counter++}" - calls[function.toString()] = Pair("/kv/$routeDef", method) - } - - /** - * Binds a given route with a function of the receiver. - * @param function a function of the receiver - * @param route a route - * @param method a HTTP method - */ - protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, reified PAR4, reified RET> bind( - noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, Request?) -> RET, - route: String?, method: RpcHttpMethod - ) { - val routeDef = route ?: "route${this::class.simpleName}${counter++}" - calls[function.toString()] = Pair("/kv/$routeDef", method) - } - - /** - * Binds a given route with a function of the receiver. - * @param function a function of the receiver - * @param route a route - * @param method a HTTP method - */ - protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, - reified PAR4, reified PAR5, reified RET> bind( - noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5, Request?) -> RET, - route: String?, - method: RpcHttpMethod - ) { - val routeDef = route ?: "route${this::class.simpleName}${counter++}" - calls[function.toString()] = Pair("/kv/$routeDef", method) - } - - /** - * Applies all defined routes to the given server. - * Not used on the js platform. - */ - actual fun applyRoutes(k: KVServer) { - } - - /** - * Returns the map of defined paths. - */ - override fun getCalls(): Map<String, Pair<String, RpcHttpMethod>> = calls -} diff --git a/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/SpringRemoteAgent.kt b/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/KVRemoteAgent.kt index 2f6b765a..a157a313 100644 --- a/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/SpringRemoteAgent.kt +++ b/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/KVRemoteAgent.kt @@ -31,11 +31,11 @@ import kotlin.reflect.KClass import kotlin.js.JSON as NativeJSON /** - * Client side agent for JSON-RPC remote calls with Spring Boot. + * Client side agent for JSON-RPC remote calls. */ @Suppress("LargeClass", "TooManyFunctions") @UseExperimental(ImplicitReflectionSerializer::class) -open class SpringRemoteAgent<T : Any>(val serviceManager: SpringServiceManager<T>) : RemoteAgent { +open class KVRemoteAgent<T : Any>(val serviceManager: KVServiceManager<T>) : RemoteAgent { val callAgent = CallAgent() diff --git a/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt b/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt index 61ae32ba..383e8bb2 100644 --- a/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt +++ b/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt @@ -24,9 +24,9 @@ package pl.treksoft.kvision.remote import kotlin.reflect.KClass /** - * Multiplatform service manager for Spring Boot. + * Multiplatform service manager. */ -actual open class SpringServiceManager<T : Any> actual constructor(serviceClass: KClass<T>) : ServiceManager { +actual open class KVServiceManager<T : Any> actual constructor(serviceClass: KClass<T>) : ServiceManager { protected val calls: MutableMap<String, Pair<String, RpcHttpMethod>> = mutableMapOf() var counter: Int = 0 @@ -122,7 +122,7 @@ actual open class SpringServiceManager<T : Any> actual constructor(serviceClass: * @param function a function of the receiver */ protected actual fun bind( - function: T.(String) -> List<RemoteSelectOption> + function: T.(String?, String?) -> List<RemoteSelectOption> ) { val routeDef = "route${this::class.simpleName}${counter++}" calls[function.toString().replace("\\s".toRegex(), "")] = Pair("/kv/$routeDef", RpcHttpMethod.POST) @@ -133,4 +133,10 @@ actual open class SpringServiceManager<T : Any> actual constructor(serviceClass: */ override fun getCalls(): Map<String, Pair<String, RpcHttpMethod>> = calls + /** + * Applies all defined routes to the given server. + * Not used on the js platform. + */ + actual fun applyRoutes(k: KVServer) { + } } diff --git a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt index c2321911..79a02e87 100644 --- a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt +++ b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt @@ -23,7 +23,6 @@ package pl.treksoft.kvision.remote import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import org.jooby.Kooby -import org.jooby.Session import org.jooby.json.Jackson import org.pac4j.core.profile.CommonProfile import java.text.SimpleDateFormat @@ -59,20 +58,11 @@ actual typealias Request = org.jooby.Request actual typealias Profile = CommonProfile /** - * A helper extension function for asynchronous request processing with session. + * A helper extension function for processing with authenticated user profile. */ -fun <RESP> Request?.withSession(block: (Request, Session) -> RESP): RESP = this?.let { req -> - val session = req.session() - block(req, session) -} ?: throw IllegalStateException("Request not set!") - -/** - * A helper extension function for asynchronous request processing with session and user profile. - */ -fun <RESP> Request?.withProfile(block: (Request, Session, Profile) -> RESP): RESP = this?.let { req -> - val session = req.session() - val profile = req.require(CommonProfile::class.java) as CommonProfile? - profile?.let { - block(req, session, profile) - } -} ?: throw IllegalStateException("Request or profile not set!") +fun <RESP> Request.withProfile(block: (Profile) -> RESP): RESP { + val profile = this.require(CommonProfile::class.java) as CommonProfile? + return profile?.let { + block(profile) + } ?: throw IllegalStateException("Profile not set!") +} diff --git a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt index 17f51443..958699f1 100644 --- a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt +++ b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt @@ -22,23 +22,23 @@ package pl.treksoft.kvision.remote import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import com.google.inject.Injector import kotlinx.coroutines.CoroutineStart -import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch import org.jooby.Response import org.slf4j.Logger import org.slf4j.LoggerFactory import java.text.SimpleDateFormat +import kotlin.reflect.KClass /** * Multiplatform service manager for Jooby. */ -@UseExperimental(ExperimentalCoroutinesApi::class) -actual open class JoobyServiceManager<T : Any> actual constructor(val service: T) : ServiceManager { +actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: KClass<T>) : ServiceManager { companion object { - val LOG: Logger = LoggerFactory.getLogger(JoobyServiceManager::class.java.name) + val LOG: Logger = LoggerFactory.getLogger(KVServiceManager::class.java.name) } protected val routes: MutableList<KVServer.() -> Unit> = mutableListOf() @@ -55,16 +55,19 @@ actual open class JoobyServiceManager<T : Any> actual constructor(val service: T */ @Suppress("TooGenericExceptionCaught") protected actual inline fun <reified RET> bind( - noinline function: suspend T.(Request?) -> RET, + noinline function: suspend T.() -> RET, route: String?, method: RpcHttpMethod ) { val routeDef = route ?: "route${this::class.simpleName}${counter++}" routes.add { call(method, "/kv/$routeDef") { req, res -> val jsonRpcRequest = req.body(JsonRpcRequest::class.java) + val service = req.require(serviceClass.java) + val injector = req.require(Injector::class.java) + injector.injectMembers(service) GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) { try { - val result = function.invoke(service, req) + val result = function.invoke(service) res.send( JsonRpcResponse( id = jsonRpcRequest.id, @@ -88,18 +91,21 @@ actual open class JoobyServiceManager<T : Any> actual constructor(val service: T */ @Suppress("TooGenericExceptionCaught") protected actual inline fun <reified PAR, reified RET> bind( - noinline function: suspend T.(PAR, Request?) -> RET, + noinline function: suspend T.(PAR) -> RET, route: String?, method: RpcHttpMethod ) { val routeDef = route ?: "route${this::class.simpleName}${counter++}" routes.add { call(method, "/kv/$routeDef") { req, res -> val jsonRpcRequest = req.body(JsonRpcRequest::class.java) + val service = req.require(serviceClass.java) + val injector = req.require(Injector::class.java) + injector.injectMembers(service) if (jsonRpcRequest.params.size == 1) { val param = getParameter<PAR>(jsonRpcRequest.params[0]) GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) { try { - val result = function.invoke(service, param, req) + val result = function.invoke(service, param) res.send( JsonRpcResponse( id = jsonRpcRequest.id, @@ -126,19 +132,22 @@ actual open class JoobyServiceManager<T : Any> actual constructor(val service: T */ @Suppress("TooGenericExceptionCaught") protected actual inline fun <reified PAR1, reified PAR2, reified RET> bind( - noinline function: suspend T.(PAR1, PAR2, Request?) -> RET, + noinline function: suspend T.(PAR1, PAR2) -> RET, route: String?, method: RpcHttpMethod ) { val routeDef = route ?: "route${this::class.simpleName}${counter++}" routes.add { call(method, "/kv/$routeDef") { req, res -> val jsonRpcRequest = req.body(JsonRpcRequest::class.java) + val service = req.require(serviceClass.java) + val injector = req.require(Injector::class.java) + injector.injectMembers(service) if (jsonRpcRequest.params.size == 2) { val param1 = getParameter<PAR1>(jsonRpcRequest.params[0]) val param2 = getParameter<PAR2>(jsonRpcRequest.params[1]) GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) { try { - val result = function.invoke(service, param1, param2, req) + val result = function.invoke(service, param1, param2) res.send( JsonRpcResponse( id = jsonRpcRequest.id, @@ -165,13 +174,16 @@ actual open class JoobyServiceManager<T : Any> actual constructor(val service: T */ @Suppress("TooGenericExceptionCaught") protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, reified RET> bind( - noinline function: suspend T.(PAR1, PAR2, PAR3, Request?) -> RET, + noinline function: suspend T.(PAR1, PAR2, PAR3) -> RET, route: String?, method: RpcHttpMethod ) { val routeDef = route ?: "route${this::class.simpleName}${counter++}" routes.add { call(method, "/kv/$routeDef") { req, res -> val jsonRpcRequest = req.body(JsonRpcRequest::class.java) + val service = req.require(serviceClass.java) + val injector = req.require(Injector::class.java) + injector.injectMembers(service) @Suppress("MagicNumber") if (jsonRpcRequest.params.size == 3) { val param1 = getParameter<PAR1>(jsonRpcRequest.params[0]) @@ -179,7 +191,7 @@ actual open class JoobyServiceManager<T : Any> actual constructor(val service: T val param3 = getParameter<PAR3>(jsonRpcRequest.params[2]) GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) { try { - val result = function.invoke(service, param1, param2, param3, req) + val result = function.invoke(service, param1, param2, param3) res.send( JsonRpcResponse( id = jsonRpcRequest.id, @@ -206,13 +218,16 @@ actual open class JoobyServiceManager<T : Any> actual constructor(val service: T */ @Suppress("TooGenericExceptionCaught") protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, reified PAR4, reified RET> bind( - noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, Request?) -> RET, + noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4) -> RET, route: String?, method: RpcHttpMethod ) { val routeDef = route ?: "route${this::class.simpleName}${counter++}" routes.add { call(method, "/kv/$routeDef") { req, res -> val jsonRpcRequest = req.body(JsonRpcRequest::class.java) + val service = req.require(serviceClass.java) + val injector = req.require(Injector::class.java) + injector.injectMembers(service) @Suppress("MagicNumber") if (jsonRpcRequest.params.size == 4) { val param1 = getParameter<PAR1>(jsonRpcRequest.params[0]) @@ -221,7 +236,7 @@ actual open class JoobyServiceManager<T : Any> actual constructor(val service: T val param4 = getParameter<PAR4>(jsonRpcRequest.params[3]) GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) { try { - val result = function.invoke(service, param1, param2, param3, param4, req) + val result = function.invoke(service, param1, param2, param3, param4) res.send( JsonRpcResponse( id = jsonRpcRequest.id, @@ -249,7 +264,7 @@ actual open class JoobyServiceManager<T : Any> actual constructor(val service: T @Suppress("TooGenericExceptionCaught") protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, reified PAR4, reified PAR5, reified RET> bind( - noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5, Request?) -> RET, + noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5) -> RET, route: String?, method: RpcHttpMethod ) { @@ -257,6 +272,9 @@ actual open class JoobyServiceManager<T : Any> actual constructor(val service: T routes.add { call(method, "/kv/$routeDef") { req, res -> val jsonRpcRequest = req.body(JsonRpcRequest::class.java) + val service = req.require(serviceClass.java) + val injector = req.require(Injector::class.java) + injector.injectMembers(service) @Suppress("MagicNumber") if (jsonRpcRequest.params.size == 5) { val param1 = getParameter<PAR1>(jsonRpcRequest.params[0]) @@ -266,7 +284,46 @@ actual open class JoobyServiceManager<T : Any> actual constructor(val service: T val param5 = getParameter<PAR5>(jsonRpcRequest.params[4]) GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) { try { - val result = function.invoke(service, param1, param2, param3, param4, param5, req) + val result = function.invoke(service, param1, param2, param3, param4, param5) + res.send( + JsonRpcResponse( + id = jsonRpcRequest.id, + result = mapper.writeValueAsString(result) + ) + ) + } catch (e: Exception) { + LOG.error(e.message, e) + res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error")) + } + } + } else { + res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = "Invalid parameters")) + } + }.invoke(this) + } + } + + /** + * Binds a given function of the receiver as a select options source + * @param function a function of the receiver + */ + @Suppress("TooGenericExceptionCaught") + protected actual fun bind( + function: T.(String?, String?) -> List<RemoteSelectOption> + ) { + val routeDef = "route${this::class.simpleName}${counter++}" + routes.add { + call(RpcHttpMethod.POST, "/kv/$routeDef") { req, res -> + val jsonRpcRequest = req.body(JsonRpcRequest::class.java) + val service = req.require(serviceClass.java) + val injector = req.require(Injector::class.java) + injector.injectMembers(service) + if (jsonRpcRequest.params.size == 2) { + val param1 = getParameter<String?>(jsonRpcRequest.params[0]) + val param2 = getParameter<String?>(jsonRpcRequest.params[1]) + GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) { + try { + val result = function.invoke(service, param1, param2) res.send( JsonRpcResponse( id = jsonRpcRequest.id, @@ -300,7 +357,6 @@ actual open class JoobyServiceManager<T : Any> actual constructor(val service: T } } - @Suppress("TooGenericExceptionCaught") protected inline fun <reified T> getParameter(str: String?): T { return str?.let { if (T::class == String::class) { diff --git a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt deleted file mode 100644 index dae150ba..00000000 --- a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Copyright (c) 2017-present Robert Jaros - * - * 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 pl.treksoft.kvision.remote - -import kotlin.reflect.KClass - -/** - * Multiplatform service manager for Spring Boot. - * Not to be used in this module. - */ -actual open class SpringServiceManager<T : Any> actual constructor(val serviceClass: KClass<T>) : ServiceManager { - /** - * Binds a given route with a function of the receiver. - * @param function a function of the receiver - * @param route a route - * @param method a HTTP method - */ - protected actual inline fun <reified RET> bind( - noinline function: suspend T.() -> RET, - route: String?, - method: RpcHttpMethod - ) { - throw IllegalStateException("This class is for Spring Boot integration.") - } - - /** - * Binds a given route with a function of the receiver. - * @param function a function of the receiver - * @param route a route - * @param method a HTTP method - */ - protected actual inline fun <reified PAR, reified RET> bind( - noinline function: suspend T.(PAR) -> RET, - route: String?, - method: RpcHttpMethod - ) { - throw IllegalStateException("This class is for Spring Boot integration.") - } - - /** - * Binds a given route with a function of the receiver. - * @param function a function of the receiver - * @param route a route - * @param method a HTTP method - */ - protected actual inline fun <reified PAR1, reified PAR2, reified RET> bind( - noinline function: suspend T.(PAR1, PAR2) -> RET, - route: String?, - method: RpcHttpMethod - ) { - throw IllegalStateException("This class is for Spring Boot integration.") - } - - /** - * Binds a given route with a function of the receiver. - * @param function a function of the receiver - * @param route a route - * @param method a HTTP method - */ - protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, reified RET> bind( - noinline function: suspend T.(PAR1, PAR2, PAR3) -> RET, - route: String?, - method: RpcHttpMethod - ) { - throw IllegalStateException("This class is for Spring Boot integration.") - } - - /** - * Binds a given route with a function of the receiver. - * @param function a function of the receiver - * @param route a route - * @param method a HTTP method - */ - protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, reified PAR4, reified RET> bind( - noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4) -> RET, - route: String?, - method: RpcHttpMethod - ) { - throw IllegalStateException("This class is for Spring Boot integration.") - } - - /** - * Binds a given route with a function of the receiver. - * @param function a function of the receiver - * @param route a route - * @param method a HTTP method - */ - protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, - reified PAR4, reified PAR5, reified RET> bind( - noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5) -> RET, - route: String?, - method: RpcHttpMethod - ) { - throw IllegalStateException("This class is for Spring Boot integration.") - } - - /** - * Binds a given function of the receiver as a select options source - * @param function a function of the receiver - */ - protected actual fun bind( - function: T.(String) -> List<RemoteSelectOption> - ) { - throw IllegalStateException("This class is for Spring Boot integration.") - } - -} diff --git a/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt b/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt deleted file mode 100644 index 422c97ba..00000000 --- a/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt +++ /dev/null @@ -1,121 +0,0 @@ -/* - * Copyright (c) 2017-present Robert Jaros - * - * 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 pl.treksoft.kvision.remote - -/** - * Multiplatform service manager for Jooby. - * Not to be used in this module. - */ -actual open class JoobyServiceManager<T : Any> actual constructor(val service: T) : ServiceManager { - /** - * Binds a given route with a function of the receiver. - * @param function a function of the receiver - * @param route a route - * @param method a HTTP method - */ - protected actual inline fun <reified RET> bind( - noinline function: suspend T.(Request?) -> RET, - route: String?, - method: RpcHttpMethod - ) { - throw IllegalStateException("This class is for Jooby integration.") - } - - /** - * Binds a given route with a function of the receiver. - * @param function a function of the receiver - * @param route a route - * @param method a HTTP method - */ - protected actual inline fun <reified PAR, reified RET> bind( - noinline function: suspend T.(PAR, Request?) -> RET, - route: String?, - method: RpcHttpMethod - ) { - throw IllegalStateException("This class is for Jooby integration.") - } - - /** - * Binds a given route with a function of the receiver. - * @param function a function of the receiver - * @param route a route - * @param method a HTTP method - */ - protected actual inline fun <reified PAR1, reified PAR2, reified RET> bind( - noinline function: suspend T.(PAR1, PAR2, Request?) -> RET, - route: String?, - method: RpcHttpMethod - ) { - throw IllegalStateException("This class is for Jooby integration.") - } - - /** - * Binds a given route with a function of the receiver. - * @param function a function of the receiver - * @param route a route - * @param method a HTTP method - */ - protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, reified RET> bind( - noinline function: suspend T.(PAR1, PAR2, PAR3, Request?) -> RET, - route: String?, - method: RpcHttpMethod - ) { - throw IllegalStateException("This class is for Jooby integration.") - } - - /** - * Binds a given route with a function of the receiver. - * @param function a function of the receiver - * @param route a route - * @param method a HTTP method - */ - protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, reified PAR4, reified RET> bind( - noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, Request?) -> RET, - route: String?, - method: RpcHttpMethod - ) { - throw IllegalStateException("This class is for Jooby integration.") - } - - /** - * Binds a given route with a function of the receiver. - * @param function a function of the receiver - * @param route a route - * @param method a HTTP method - */ - protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, - reified PAR4, reified PAR5, reified RET> bind( - noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5, Request?) -> RET, - route: String?, - method: RpcHttpMethod - ) { - throw IllegalStateException("This class is for Jooby integration.") - } - - /** - * Applies all defined routes to the given server. - * @param k a server - */ - actual fun applyRoutes(k: KVServer) { - throw IllegalStateException("This class is for Jooby integration.") - } -} diff --git a/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt b/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt index f2e3e951..ba7264e8 100644 --- a/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt +++ b/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt @@ -33,7 +33,7 @@ import kotlinx.coroutines.async as coroutinesAsync /** * A Spring boot based server. */ -actual open class KVServer(val services: List<SpringServiceManager<*>>) +actual open class KVServer(val services: List<KVServiceManager<*>>) /** * A server request. diff --git a/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt b/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt index 162b92dd..45c5c565 100644 --- a/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt +++ b/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt @@ -36,10 +36,10 @@ import kotlin.reflect.KClass * Multiplatform service manager for Spring Boot. */ @UseExperimental(ExperimentalCoroutinesApi::class) -actual open class SpringServiceManager<T : Any> actual constructor(val serviceClass: KClass<T>) : ServiceManager { +actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: KClass<T>) : ServiceManager { companion object { - val LOG: Logger = LoggerFactory.getLogger(JoobyServiceManager::class.java.name) + val LOG: Logger = LoggerFactory.getLogger(KVServiceManager::class.java.name) } val postRequests: MutableMap<String, (Request, HttpServletResponse) -> Unit> = mutableMapOf() @@ -379,16 +379,17 @@ actual open class SpringServiceManager<T : Any> actual constructor(val serviceCl */ @Suppress("TooGenericExceptionCaught") protected actual fun bind( - function: T.(String) -> List<RemoteSelectOption> + function: T.(String?, String?) -> List<RemoteSelectOption> ) { val routeDef = "route${this::class.simpleName}${counter++}" addRoute(RpcHttpMethod.POST, "/kv/$routeDef") { req, res -> val service = SpringContext.getBean(serviceClass.java) val jsonRpcRequest = mapper.readValue(req.inputStream, JsonRpcRequest::class.java) - if (jsonRpcRequest.params.size == 1) { - val param = getParameter<String>(jsonRpcRequest.params[0]) + if (jsonRpcRequest.params.size == 2) { + val param1 = getParameter<String?>(jsonRpcRequest.params[0]) + val param2 = getParameter<String?>(jsonRpcRequest.params[1]) try { - val result = function.invoke(service, param) + val result = function.invoke(service, param1, param2) res.writeJSON( mapper.writeValueAsString( JsonRpcResponse( @@ -434,7 +435,6 @@ actual open class SpringServiceManager<T : Any> actual constructor(val serviceCl } } - @Suppress("TooGenericExceptionCaught") protected inline fun <reified T> getParameter(str: String?): T { return str?.let { if (T::class == String::class) { @@ -444,6 +444,11 @@ actual open class SpringServiceManager<T : Any> actual constructor(val serviceCl } } ?: null as T } + + /** + * Applies all defined routes to the given server. + */ + actual fun applyRoutes(k: KVServer) {} } fun HttpServletResponse.writeJSON(json: String) { diff --git a/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/Security.kt b/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/Security.kt index 9d80beb0..abfef934 100644 --- a/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/Security.kt +++ b/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/Security.kt @@ -23,7 +23,7 @@ package pl.treksoft.kvision.remote import org.springframework.web.servlet.config.annotation.InterceptorRegistration -fun InterceptorRegistration.addPathPatternsFromServices(services: List<SpringServiceManager<*>>) { +fun InterceptorRegistration.addPathPatternsFromServices(services: List<KVServiceManager<*>>) { val paths = services.flatMap { it.postRequests.keys + it.putRequests.keys + it.optionsRequests.keys + it.optionsRequests.keys } |