From 3dd84bb59a119afba9fd5bcb84c15e214daa8d3d Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Tue, 25 Feb 2020 12:10:52 +0100 Subject: Upgrade to Jooby 2.x - support for WithContext and WithSession interfaces --- .../pl/treksoft/kvision/remote/KVServiceManager.kt | 20 +++++++++++++ .../treksoft/kvision/remote/SessionInterfaces.kt | 33 ++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/SessionInterfaces.kt (limited to 'kvision-modules/kvision-server-jooby/src/main') diff --git a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt index c9a15859..dc1e0f65 100644 --- a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt +++ b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt @@ -89,6 +89,18 @@ actual open class KVServiceManager actual constructor(val serviceClass: } var counter: Int = 0 + /** + * @suppress internal function + */ + fun initializeService(service: T, ctx: Context) { + if (service is WithContext) { + service.ctx = ctx + } + if (service is WithSession) { + service.session = ctx.session() + } + } + /** * Binds a given route with a function of the receiver. * @param function a function of the receiver @@ -108,6 +120,7 @@ actual open class KVServiceManager actual constructor(val serviceClass: ctx.body() } val service = ctx.require(serviceClass.java) + initializeService(service, ctx) try { val result = function.invoke(service) JsonRpcResponse( @@ -143,6 +156,7 @@ actual open class KVServiceManager actual constructor(val serviceClass: if (jsonRpcRequest.params.size == 1) { val param = getParameter(jsonRpcRequest.params[0]) val service = ctx.require(serviceClass.java) + initializeService(service, ctx) try { val result = function.invoke(service, param) JsonRpcResponse( @@ -182,6 +196,7 @@ actual open class KVServiceManager actual constructor(val serviceClass: val param1 = getParameter(jsonRpcRequest.params[0]) val param2 = getParameter(jsonRpcRequest.params[1]) val service = ctx.require(serviceClass.java) + initializeService(service, ctx) try { val result = function.invoke(service, param1, param2) JsonRpcResponse( @@ -223,6 +238,7 @@ actual open class KVServiceManager actual constructor(val serviceClass: val param2 = getParameter(jsonRpcRequest.params[1]) val param3 = getParameter(jsonRpcRequest.params[2]) val service = ctx.require(serviceClass.java) + initializeService(service, ctx) try { val result = function.invoke(service, param1, param2, param3) JsonRpcResponse( @@ -265,6 +281,7 @@ actual open class KVServiceManager actual constructor(val serviceClass: val param3 = getParameter(jsonRpcRequest.params[2]) val param4 = getParameter(jsonRpcRequest.params[3]) val service = ctx.require(serviceClass.java) + initializeService(service, ctx) try { val result = function.invoke(service, param1, param2, param3, param4) JsonRpcResponse( @@ -309,6 +326,7 @@ actual open class KVServiceManager actual constructor(val serviceClass: val param4 = getParameter(jsonRpcRequest.params[3]) val param5 = getParameter(jsonRpcRequest.params[4]) val service = ctx.require(serviceClass.java) + initializeService(service, ctx) try { val result = function.invoke(service, param1, param2, param3, param4, param5) JsonRpcResponse( @@ -340,6 +358,7 @@ actual open class KVServiceManager actual constructor(val serviceClass: val routeDef = "route${this::class.simpleName}${counter++}" webSocketRequests["/kvws/$routeDef"] = { ctx, configurer -> val service = ctx.require(serviceClass.java) + initializeService(service, ctx) val incoming = Channel() val outgoing = Channel() configurer.onConnect { ws -> @@ -421,6 +440,7 @@ actual open class KVServiceManager actual constructor(val serviceClass: @Suppress("MagicNumber") val param5 = getParameter(jsonRpcRequest.params[4]) val service = ctx.require(serviceClass.java) + initializeService(service, ctx) try { val result = function.invoke(service, param1, param2, param3, param4, param5) JsonRpcResponse( diff --git a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/SessionInterfaces.kt b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/SessionInterfaces.kt new file mode 100644 index 00000000..06955a07 --- /dev/null +++ b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/SessionInterfaces.kt @@ -0,0 +1,33 @@ +/* + * 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 io.jooby.Context +import io.jooby.Session + +interface WithContext { + var ctx: Context +} + +interface WithSession { + var session: Session +} -- cgit