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 | 121 ------ .../kotlin/pl/treksoft/kvision/remote/KVServer.kt | 2 +- .../pl/treksoft/kvision/remote/KVServiceManager.kt | 460 +++++++++++++++++++++ .../kotlin/pl/treksoft/kvision/remote/Security.kt | 2 +- .../kvision/remote/SpringServiceManager.kt | 455 -------------------- 5 files changed, 462 insertions(+), 578 deletions(-) delete mode 100644 kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt create mode 100644 kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt delete mode 100644 kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt (limited to 'kvision-modules/kvision-server-spring-boot/src') 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 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 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 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 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 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 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 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>) +actual open class KVServer(val services: List>) /** * A server request. diff --git a/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt b/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt new file mode 100644 index 00000000..45c5c565 --- /dev/null +++ b/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt @@ -0,0 +1,460 @@ +/* + * 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.slf4j.Logger +import org.slf4j.LoggerFactory +import java.text.SimpleDateFormat +import javax.servlet.http.HttpServletResponse +import kotlin.reflect.KClass + +/** + * Multiplatform service manager for Spring Boot. + */ +@UseExperimental(ExperimentalCoroutinesApi::class) +actual open class KVServiceManager actual constructor(val serviceClass: KClass) : ServiceManager { + + companion object { + val LOG: Logger = LoggerFactory.getLogger(KVServiceManager::class.java.name) + } + + val postRequests: MutableMap Unit> = mutableMapOf() + val putRequests: MutableMap Unit> = mutableMapOf() + val deleteRequests: MutableMap Unit> = mutableMapOf() + val optionsRequests: MutableMap Unit> = mutableMapOf() + + 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++}" + addRoute(method, "/kv/$routeDef") { req, res -> + val service = SpringContext.getBean(serviceClass.java) + val jsonRpcRequest = mapper.readValue(req.inputStream, JsonRpcRequest::class.java) + GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) { + try { + val result = function.invoke(service) + res.writeJSON( + mapper.writeValueAsString( + JsonRpcResponse( + id = jsonRpcRequest.id, + result = mapper.writeValueAsString(result) + ) + ) + ) + } catch (e: Exception) { + LOG.error(e.message, e) + res.writeJSON( + mapper.writeValueAsString( + JsonRpcResponse( + id = jsonRpcRequest.id, + error = e.message ?: "Error" + ) + ) + ) + } + } + } + } + + /** + * 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++}" + addRoute(method, "/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(jsonRpcRequest.params[0]) + GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) { + try { + val result = function.invoke(service, param) + res.writeJSON( + mapper.writeValueAsString( + JsonRpcResponse( + id = jsonRpcRequest.id, + result = mapper.writeValueAsString(result) + ) + ) + ) + } catch (e: Exception) { + LOG.error(e.message, e) + res.writeJSON( + mapper.writeValueAsString( + JsonRpcResponse( + id = jsonRpcRequest.id, + error = e.message ?: "Error" + ) + ) + ) + } + } + } else { + res.writeJSON( + mapper.writeValueAsString( + JsonRpcResponse( + id = jsonRpcRequest.id, + error = "Invalid parameters" + ) + ) + ) + } + } + } + + /** + * 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++}" + addRoute(method, "/kv/$routeDef") { req, res -> + val service = SpringContext.getBean(serviceClass.java) + val jsonRpcRequest = mapper.readValue(req.inputStream, 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) + res.writeJSON( + mapper.writeValueAsString( + JsonRpcResponse( + id = jsonRpcRequest.id, + result = mapper.writeValueAsString(result) + ) + ) + ) + } catch (e: Exception) { + LOG.error(e.message, e) + res.writeJSON( + mapper.writeValueAsString( + JsonRpcResponse( + id = jsonRpcRequest.id, + error = e.message ?: "Error" + ) + ) + ) + } + } + } else { + res.writeJSON( + mapper.writeValueAsString( + JsonRpcResponse( + id = jsonRpcRequest.id, + error = "Invalid parameters" + ) + ) + ) + } + } + } + + /** + * 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++}" + addRoute(method, "/kv/$routeDef") { req, res -> + val service = SpringContext.getBean(serviceClass.java) + val jsonRpcRequest = mapper.readValue(req.inputStream, 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) + res.writeJSON( + mapper.writeValueAsString( + JsonRpcResponse( + id = jsonRpcRequest.id, + result = mapper.writeValueAsString(result) + ) + ) + ) + } catch (e: Exception) { + LOG.error(e.message, e) + res.writeJSON( + mapper.writeValueAsString( + JsonRpcResponse( + id = jsonRpcRequest.id, + error = e.message ?: "Error" + ) + ) + ) + } + } + } else { + res.writeJSON( + mapper.writeValueAsString( + JsonRpcResponse( + id = jsonRpcRequest.id, + error = "Invalid parameters" + ) + ) + ) + } + } + } + + /** + * 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++}" + addRoute(method, "/kv/$routeDef") { req, res -> + val service = SpringContext.getBean(serviceClass.java) + val jsonRpcRequest = mapper.readValue(req.inputStream, 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) + res.writeJSON( + mapper.writeValueAsString( + JsonRpcResponse( + id = jsonRpcRequest.id, + result = mapper.writeValueAsString(result) + ) + ) + ) + } catch (e: Exception) { + LOG.error(e.message, e) + res.writeJSON( + mapper.writeValueAsString( + JsonRpcResponse( + id = jsonRpcRequest.id, + error = e.message ?: "Error" + ) + ) + ) + } + } + } else { + res.writeJSON( + mapper.writeValueAsString( + JsonRpcResponse( + id = jsonRpcRequest.id, + error = "Invalid parameters" + ) + ) + ) + } + } + } + + /** + * 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++}" + addRoute(method, "/kv/$routeDef") { req, res -> + val service = SpringContext.getBean(serviceClass.java) + val jsonRpcRequest = mapper.readValue(req.inputStream, 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) + res.writeJSON( + mapper.writeValueAsString( + JsonRpcResponse( + id = jsonRpcRequest.id, + result = mapper.writeValueAsString(result) + ) + ) + ) + } catch (e: Exception) { + LOG.error(e.message, e) + res.writeJSON( + mapper.writeValueAsString( + JsonRpcResponse( + id = jsonRpcRequest.id, + error = e.message ?: "Error" + ) + ) + ) + } + } + } else { + res.writeJSON( + mapper.writeValueAsString( + JsonRpcResponse( + id = jsonRpcRequest.id, + error = "Invalid parameters" + ) + ) + ) + } + } + } + + /** + * 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++}" + 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 == 2) { + val param1 = getParameter(jsonRpcRequest.params[0]) + val param2 = getParameter(jsonRpcRequest.params[1]) + try { + val result = function.invoke(service, param1, param2) + res.writeJSON( + mapper.writeValueAsString( + JsonRpcResponse( + id = jsonRpcRequest.id, + result = mapper.writeValueAsString(result) + ) + ) + ) + } catch (e: Exception) { + LOG.error(e.message, e) + res.writeJSON( + mapper.writeValueAsString( + JsonRpcResponse( + id = jsonRpcRequest.id, + error = e.message ?: "Error" + ) + ) + ) + } + } else { + res.writeJSON( + mapper.writeValueAsString( + JsonRpcResponse( + id = jsonRpcRequest.id, + error = "Invalid parameters" + ) + ) + ) + } + } + } + + fun addRoute( + method: RpcHttpMethod, + path: String, + handler: (Request, HttpServletResponse) -> Unit + ) { + when (method) { + RpcHttpMethod.POST -> postRequests[path] = handler + RpcHttpMethod.PUT -> putRequests[path] = handler + RpcHttpMethod.DELETE -> deleteRequests[path] = handler + RpcHttpMethod.OPTIONS -> optionsRequests[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. + */ + actual fun applyRoutes(k: KVServer) {} +} + +fun HttpServletResponse.writeJSON(json: String) { + val out = this.outputStream + this.contentType = "application/json" + this.characterEncoding = "UTF-8" + out.write(json.toByteArray()) + out.flush() +} 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>) { +fun InterceptorRegistration.addPathPatternsFromServices(services: List>) { val paths = services.flatMap { it.postRequests.keys + it.putRequests.keys + it.optionsRequests.keys + it.optionsRequests.keys } 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/SpringServiceManager.kt deleted file mode 100644 index 162b92dd..00000000 --- a/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt +++ /dev/null @@ -1,455 +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.slf4j.Logger -import org.slf4j.LoggerFactory -import java.text.SimpleDateFormat -import javax.servlet.http.HttpServletResponse -import kotlin.reflect.KClass - -/** - * Multiplatform service manager for Spring Boot. - */ -@UseExperimental(ExperimentalCoroutinesApi::class) -actual open class SpringServiceManager actual constructor(val serviceClass: KClass) : ServiceManager { - - companion object { - val LOG: Logger = LoggerFactory.getLogger(JoobyServiceManager::class.java.name) - } - - val postRequests: MutableMap Unit> = mutableMapOf() - val putRequests: MutableMap Unit> = mutableMapOf() - val deleteRequests: MutableMap Unit> = mutableMapOf() - val optionsRequests: MutableMap Unit> = mutableMapOf() - - 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++}" - addRoute(method, "/kv/$routeDef") { req, res -> - val service = SpringContext.getBean(serviceClass.java) - val jsonRpcRequest = mapper.readValue(req.inputStream, JsonRpcRequest::class.java) - GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) { - try { - val result = function.invoke(service) - res.writeJSON( - mapper.writeValueAsString( - JsonRpcResponse( - id = jsonRpcRequest.id, - result = mapper.writeValueAsString(result) - ) - ) - ) - } catch (e: Exception) { - LOG.error(e.message, e) - res.writeJSON( - mapper.writeValueAsString( - JsonRpcResponse( - id = jsonRpcRequest.id, - error = e.message ?: "Error" - ) - ) - ) - } - } - } - } - - /** - * 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++}" - addRoute(method, "/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(jsonRpcRequest.params[0]) - GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) { - try { - val result = function.invoke(service, param) - res.writeJSON( - mapper.writeValueAsString( - JsonRpcResponse( - id = jsonRpcRequest.id, - result = mapper.writeValueAsString(result) - ) - ) - ) - } catch (e: Exception) { - LOG.error(e.message, e) - res.writeJSON( - mapper.writeValueAsString( - JsonRpcResponse( - id = jsonRpcRequest.id, - error = e.message ?: "Error" - ) - ) - ) - } - } - } else { - res.writeJSON( - mapper.writeValueAsString( - JsonRpcResponse( - id = jsonRpcRequest.id, - error = "Invalid parameters" - ) - ) - ) - } - } - } - - /** - * 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++}" - addRoute(method, "/kv/$routeDef") { req, res -> - val service = SpringContext.getBean(serviceClass.java) - val jsonRpcRequest = mapper.readValue(req.inputStream, 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) - res.writeJSON( - mapper.writeValueAsString( - JsonRpcResponse( - id = jsonRpcRequest.id, - result = mapper.writeValueAsString(result) - ) - ) - ) - } catch (e: Exception) { - LOG.error(e.message, e) - res.writeJSON( - mapper.writeValueAsString( - JsonRpcResponse( - id = jsonRpcRequest.id, - error = e.message ?: "Error" - ) - ) - ) - } - } - } else { - res.writeJSON( - mapper.writeValueAsString( - JsonRpcResponse( - id = jsonRpcRequest.id, - error = "Invalid parameters" - ) - ) - ) - } - } - } - - /** - * 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++}" - addRoute(method, "/kv/$routeDef") { req, res -> - val service = SpringContext.getBean(serviceClass.java) - val jsonRpcRequest = mapper.readValue(req.inputStream, 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) - res.writeJSON( - mapper.writeValueAsString( - JsonRpcResponse( - id = jsonRpcRequest.id, - result = mapper.writeValueAsString(result) - ) - ) - ) - } catch (e: Exception) { - LOG.error(e.message, e) - res.writeJSON( - mapper.writeValueAsString( - JsonRpcResponse( - id = jsonRpcRequest.id, - error = e.message ?: "Error" - ) - ) - ) - } - } - } else { - res.writeJSON( - mapper.writeValueAsString( - JsonRpcResponse( - id = jsonRpcRequest.id, - error = "Invalid parameters" - ) - ) - ) - } - } - } - - /** - * 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++}" - addRoute(method, "/kv/$routeDef") { req, res -> - val service = SpringContext.getBean(serviceClass.java) - val jsonRpcRequest = mapper.readValue(req.inputStream, 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) - res.writeJSON( - mapper.writeValueAsString( - JsonRpcResponse( - id = jsonRpcRequest.id, - result = mapper.writeValueAsString(result) - ) - ) - ) - } catch (e: Exception) { - LOG.error(e.message, e) - res.writeJSON( - mapper.writeValueAsString( - JsonRpcResponse( - id = jsonRpcRequest.id, - error = e.message ?: "Error" - ) - ) - ) - } - } - } else { - res.writeJSON( - mapper.writeValueAsString( - JsonRpcResponse( - id = jsonRpcRequest.id, - error = "Invalid parameters" - ) - ) - ) - } - } - } - - /** - * 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++}" - addRoute(method, "/kv/$routeDef") { req, res -> - val service = SpringContext.getBean(serviceClass.java) - val jsonRpcRequest = mapper.readValue(req.inputStream, 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) - res.writeJSON( - mapper.writeValueAsString( - JsonRpcResponse( - id = jsonRpcRequest.id, - result = mapper.writeValueAsString(result) - ) - ) - ) - } catch (e: Exception) { - LOG.error(e.message, e) - res.writeJSON( - mapper.writeValueAsString( - JsonRpcResponse( - id = jsonRpcRequest.id, - error = e.message ?: "Error" - ) - ) - ) - } - } - } else { - res.writeJSON( - mapper.writeValueAsString( - JsonRpcResponse( - id = jsonRpcRequest.id, - error = "Invalid parameters" - ) - ) - ) - } - } - } - - /** - * 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) -> List - ) { - 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(jsonRpcRequest.params[0]) - try { - val result = function.invoke(service, param) - res.writeJSON( - mapper.writeValueAsString( - JsonRpcResponse( - id = jsonRpcRequest.id, - result = mapper.writeValueAsString(result) - ) - ) - ) - } catch (e: Exception) { - LOG.error(e.message, e) - res.writeJSON( - mapper.writeValueAsString( - JsonRpcResponse( - id = jsonRpcRequest.id, - error = e.message ?: "Error" - ) - ) - ) - } - } else { - res.writeJSON( - mapper.writeValueAsString( - JsonRpcResponse( - id = jsonRpcRequest.id, - error = "Invalid parameters" - ) - ) - ) - } - } - } - - fun addRoute( - method: RpcHttpMethod, - path: String, - handler: (Request, HttpServletResponse) -> Unit - ) { - when (method) { - RpcHttpMethod.POST -> postRequests[path] = handler - RpcHttpMethod.PUT -> putRequests[path] = handler - RpcHttpMethod.DELETE -> deleteRequests[path] = handler - RpcHttpMethod.OPTIONS -> optionsRequests[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 - } -} - -fun HttpServletResponse.writeJSON(json: String) { - val out = this.outputStream - this.contentType = "application/json" - this.characterEncoding = "UTF-8" - out.write(json.toByteArray()) - out.flush() -} -- cgit