From b995f4b927c230e0ae15d51920390dc6afff6caa Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Thu, 20 Dec 2018 10:19:55 +0100 Subject: Refactor server side modules. --- .../treksoft/kvision/remote/JoobyServiceManager.kt | 324 ------------------ .../kotlin/pl/treksoft/kvision/remote/KVServer.kt | 24 +- .../pl/treksoft/kvision/remote/KVServiceManager.kt | 380 +++++++++++++++++++++ .../kvision/remote/SpringServiceManager.kt | 126 ------- 4 files changed, 387 insertions(+), 467 deletions(-) delete mode 100644 kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt create mode 100644 kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt delete mode 100644 kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt (limited to 'kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft') 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/JoobyServiceManager.kt deleted file mode 100644 index 17f51443..00000000 --- a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt +++ /dev/null @@ -1,324 +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 com.fasterxml.jackson.module.kotlin.jacksonObjectMapper -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 - -/** - * Multiplatform service manager for Jooby. - */ -@UseExperimental(ExperimentalCoroutinesApi::class) -actual open class JoobyServiceManager actual constructor(val service: T) : ServiceManager { - - companion object { - val LOG: Logger = LoggerFactory.getLogger(JoobyServiceManager::class.java.name) - } - - protected val routes: MutableList Unit> = mutableListOf() - val mapper = jacksonObjectMapper().apply { - dateFormat = SimpleDateFormat("YYYY-MM-DD HH:mm:ss") - } - 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 - */ - @Suppress("TooGenericExceptionCaught") - protected actual inline fun bind( - noinline function: suspend T.(Request?) -> 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) - GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) { - try { - val result = function.invoke(service, req) - 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")) - } - } - }.invoke(this) - } - } - - /** - * 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 - */ - @Suppress("TooGenericExceptionCaught") - protected actual inline fun bind( - noinline function: suspend T.(PAR, Request?) -> 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) - if (jsonRpcRequest.params.size == 1) { - val param = getParameter(jsonRpcRequest.params[0]) - GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) { - try { - val result = function.invoke(service, param, req) - 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 route with a function of the receiver. - * @param function a function of the receiver - * @param route a route - * @param method a HTTP method - */ - @Suppress("TooGenericExceptionCaught") - protected actual inline fun bind( - noinline function: suspend T.(PAR1, PAR2, Request?) -> 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) - if (jsonRpcRequest.params.size == 2) { - val param1 = getParameter(jsonRpcRequest.params[0]) - val param2 = getParameter(jsonRpcRequest.params[1]) - GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) { - try { - val result = function.invoke(service, param1, param2, req) - 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 route with a function of the receiver. - * @param function a function of the receiver - * @param route a route - * @param method a HTTP method - */ - @Suppress("TooGenericExceptionCaught") - protected actual inline fun bind( - noinline function: suspend T.(PAR1, PAR2, PAR3, Request?) -> 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) - @Suppress("MagicNumber") - if (jsonRpcRequest.params.size == 3) { - val param1 = getParameter(jsonRpcRequest.params[0]) - val param2 = getParameter(jsonRpcRequest.params[1]) - val param3 = getParameter(jsonRpcRequest.params[2]) - GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) { - try { - val result = function.invoke(service, param1, param2, param3, req) - 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 route with a function of the receiver. - * @param function a function of the receiver - * @param route a route - * @param method a HTTP method - */ - @Suppress("TooGenericExceptionCaught") - protected actual inline fun bind( - noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, Request?) -> 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) - @Suppress("MagicNumber") - if (jsonRpcRequest.params.size == 4) { - val param1 = getParameter(jsonRpcRequest.params[0]) - val param2 = getParameter(jsonRpcRequest.params[1]) - val param3 = getParameter(jsonRpcRequest.params[2]) - val param4 = getParameter(jsonRpcRequest.params[3]) - GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) { - try { - val result = function.invoke(service, param1, param2, param3, param4, req) - 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 route with a function of the receiver. - * @param function a function of the receiver - * @param route a route - * @param method a HTTP method - */ - @Suppress("TooGenericExceptionCaught") - protected actual inline fun bind( - noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5, Request?) -> 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) - @Suppress("MagicNumber") - if (jsonRpcRequest.params.size == 5) { - val param1 = getParameter(jsonRpcRequest.params[0]) - val param2 = getParameter(jsonRpcRequest.params[1]) - val param3 = getParameter(jsonRpcRequest.params[2]) - val param4 = getParameter(jsonRpcRequest.params[3]) - val param5 = getParameter(jsonRpcRequest.params[4]) - GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) { - try { - val result = function.invoke(service, param1, param2, param3, param4, param5, req) - 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) - } - } - - fun call( - method: RpcHttpMethod, - path: String, - handler: (Request, Response) -> Unit - ): KVServer.() -> Unit { - return { - when (method) { - RpcHttpMethod.POST -> post(path, handler) - RpcHttpMethod.PUT -> put(path, handler) - RpcHttpMethod.DELETE -> delete(path, handler) - RpcHttpMethod.OPTIONS -> options(path, handler) - } - } - } - - @Suppress("TooGenericExceptionCaught") - protected inline fun getParameter(str: String?): T { - return str?.let { - if (T::class == String::class) { - str as T - } else { - mapper.readValue(str, T::class.java) - } - } ?: null as T - } - - /** - * Applies all defined routes to the given server. - * @param k a server - */ - actual fun applyRoutes(k: KVServer) { - routes.forEach { - it.invoke(k) - } - } - -} 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 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 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 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/KVServiceManager.kt b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt new file mode 100644 index 00000000..958699f1 --- /dev/null +++ b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt @@ -0,0 +1,380 @@ +/* + * 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 com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import com.google.inject.Injector +import kotlinx.coroutines.CoroutineStart +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. + */ +actual open class KVServiceManager actual constructor(val serviceClass: KClass) : ServiceManager { + + companion object { + val LOG: Logger = LoggerFactory.getLogger(KVServiceManager::class.java.name) + } + + protected val routes: MutableList Unit> = mutableListOf() + val mapper = jacksonObjectMapper().apply { + dateFormat = SimpleDateFormat("YYYY-MM-DD HH:mm:ss") + } + 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 + */ + @Suppress("TooGenericExceptionCaught") + protected actual inline fun bind( + 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) + 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")) + } + } + }.invoke(this) + } + } + + /** + * 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 + */ + @Suppress("TooGenericExceptionCaught") + protected actual inline fun bind( + 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(jsonRpcRequest.params[0]) + GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) { + try { + val result = function.invoke(service, param) + 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 route with a function of the receiver. + * @param function a function of the receiver + * @param route a route + * @param method a HTTP method + */ + @Suppress("TooGenericExceptionCaught") + protected actual inline fun bind( + 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(jsonRpcRequest.params[0]) + val param2 = getParameter(jsonRpcRequest.params[1]) + GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) { + try { + val result = function.invoke(service, param1, param2) + 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 route with a function of the receiver. + * @param function a function of the receiver + * @param route a route + * @param method a HTTP method + */ + @Suppress("TooGenericExceptionCaught") + protected actual inline fun bind( + 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(jsonRpcRequest.params[0]) + val param2 = getParameter(jsonRpcRequest.params[1]) + val param3 = getParameter(jsonRpcRequest.params[2]) + GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) { + try { + val result = function.invoke(service, param1, param2, param3) + 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 route with a function of the receiver. + * @param function a function of the receiver + * @param route a route + * @param method a HTTP method + */ + @Suppress("TooGenericExceptionCaught") + protected actual inline fun bind( + 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(jsonRpcRequest.params[0]) + val param2 = getParameter(jsonRpcRequest.params[1]) + val param3 = getParameter(jsonRpcRequest.params[2]) + val param4 = getParameter(jsonRpcRequest.params[3]) + GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) { + try { + val result = function.invoke(service, param1, param2, param3, param4) + 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 route with a function of the receiver. + * @param function a function of the receiver + * @param route a route + * @param method a HTTP method + */ + @Suppress("TooGenericExceptionCaught") + protected actual inline fun bind( + noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5) -> 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 == 5) { + val param1 = getParameter(jsonRpcRequest.params[0]) + val param2 = getParameter(jsonRpcRequest.params[1]) + val param3 = getParameter(jsonRpcRequest.params[2]) + val param4 = getParameter(jsonRpcRequest.params[3]) + val param5 = getParameter(jsonRpcRequest.params[4]) + GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) { + try { + 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 + ) { + 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(jsonRpcRequest.params[0]) + val param2 = getParameter(jsonRpcRequest.params[1]) + GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) { + try { + val result = function.invoke(service, param1, param2) + 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) + } + } + + fun call( + method: RpcHttpMethod, + path: String, + handler: (Request, Response) -> Unit + ): KVServer.() -> Unit { + return { + when (method) { + RpcHttpMethod.POST -> post(path, handler) + RpcHttpMethod.PUT -> put(path, handler) + RpcHttpMethod.DELETE -> delete(path, handler) + RpcHttpMethod.OPTIONS -> options(path, handler) + } + } + } + + protected inline fun getParameter(str: String?): T { + return str?.let { + if (T::class == String::class) { + str as T + } else { + mapper.readValue(str, T::class.java) + } + } ?: null as T + } + + /** + * Applies all defined routes to the given server. + * @param k a server + */ + actual fun applyRoutes(k: KVServer) { + routes.forEach { + it.invoke(k) + } + } + +} 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 actual constructor(val serviceClass: KClass) : 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 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 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 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 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 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 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 - ) { - throw IllegalStateException("This class is for Spring Boot integration.") - } - -} -- cgit