├── NOTICE ├── README.md ├── src └── main │ ├── kotlin │ └── com │ │ └── nordicapis │ │ └── kotlin_spark_sample │ │ ├── Noncomposer.kt │ │ ├── ControllerResult.kt │ │ ├── Composable.kt │ │ ├── main.kt │ │ ├── TokenController.kt │ │ ├── ContainerComposer.kt │ │ ├── LoginController.kt │ │ ├── AuthorizeController.kt │ │ ├── Controllable.kt │ │ ├── Application.kt │ │ └── Router.kt │ └── resources │ ├── log4j.properties │ ├── authorize.vm │ └── login.vm ├── .gitignore ├── pom.xml └── LICENSE /NOTICE: -------------------------------------------------------------------------------- 1 | Nordic APIs Kotlin / Spark Sample 2 | Copyright (C) 2015 Nordic APIs AB 3 | 4 | This sample was developed by Nordic APIs AB, a Swedish corporation. 5 | For more information, refer to http://nordicapis.com/kotlin_spark_sample. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Kotlin / Spark Sample 2 | 3 | A port of the [Java-based Spark demo][1] that was presented at the Java user group meeting in Stockholm in December 2014. 4 | 5 | This sample does not show how to use [Spark][3] (refer to the [Java version of the sample for that][1]). Instead, 6 | it shows how the [Kotlin programming language][2] can be used together with [Spark][3] to create a more real-world API. 7 | 8 | For more information about this sample, refer to the Nordic APIs [blog post series about it](http://nordicapis.com/building-apis-on-the-jvm-using-kotlin-and-spark-part-1/). 9 | 10 | [1]: https://github.com/travisspencer/stockholm-java-meetup-java-spark-demo#more-complex-example 11 | [2]: http://kotlinlang.org/ 12 | [3]: http://sparkjava.com/ 13 | -------------------------------------------------------------------------------- /src/main/kotlin/com/nordicapis/kotlin_spark_sample/Noncomposer.kt: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2015 Nordic APIs AB 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | 15 | Noncomposer.kt - A trivial no-op composer for cases where DI isn't needed or desired 16 | */ 17 | 18 | package com.nordicapis.kotlin_spark_sample 19 | 20 | // For when DI isn't used 21 | class Noncomposer() : Composable -------------------------------------------------------------------------------- /src/main/kotlin/com/nordicapis/kotlin_spark_sample/ControllerResult.kt: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2015 Nordic APIs AB 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | 15 | ControllerResult.kt - Simple data class that contains the information about a route 16 | */ 17 | 18 | package com.nordicapis.kotlin_spark_sample 19 | 20 | import java.util.* 21 | 22 | data class ControllerResult( 23 | val continueProcessing: Boolean = true, 24 | val model: Map = emptyMap()) -------------------------------------------------------------------------------- /src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2015 Nordic APIs AB 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # http://www.apache.org/licenses/LICENSE-2.0 7 | # 8 | # Unless required by applicable law or agreed to in writing, software 9 | # distributed under the License is distributed on an "AS IS" BASIS, 10 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | # See the License for the specific language governing permissions and 12 | # limitations under the License. 13 | # 14 | # log4j.properties - Log4j settings files 15 | 16 | log4j.rootLogger=INFO,stdout 17 | 18 | log4j.category.com.nordicapis=TRACE 19 | log4j.category.spark=DEBUG 20 | 21 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender 22 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 23 | log4j.appender.stdout.layout.ConversionPattern=%d{yyy-MM-dd HH:mm:s} %-5p {%t} %C:%L - %m%n -------------------------------------------------------------------------------- /src/main/kotlin/com/nordicapis/kotlin_spark_sample/Composable.kt: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2015 Nordic APIs AB 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | 15 | Composable.kt - The interface used to compose the dependencies of applications 16 | */ 17 | 18 | package com.nordicapis.kotlin_spark_sample 19 | 20 | import org.picocontainer.MutablePicoContainer 21 | 22 | interface Composable 23 | { 24 | fun composeApplication(appContainer: MutablePicoContainer) { } 25 | 26 | fun composeRequest(container: MutablePicoContainer) { } 27 | } -------------------------------------------------------------------------------- /src/main/resources/authorize.vm: -------------------------------------------------------------------------------- 1 | 17 | 18 | 19 | 20 | Test 21 | 22 | 23 |

Spark Test

24 | 25 |

User: $!user

26 | 27 |
28 | #foreach ($e in $data.entrySet()) 29 |
$e.key
30 |
$e.value
31 | #end 32 |
33 | 34 | -------------------------------------------------------------------------------- /src/main/resources/login.vm: -------------------------------------------------------------------------------- 1 | 17 | 18 | 19 | 20 | Login 21 | 22 | 23 |

Spark Test

24 |

Login

25 | 26 |
27 |

28 |

29 | 30 |
31 | 32 | -------------------------------------------------------------------------------- /src/main/kotlin/com/nordicapis/kotlin_spark_sample/main.kt: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2015 Nordic APIs AB 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | 15 | main.kt - The entry point of the application 16 | */ 17 | 18 | package com.nordicapis.kotlin_spark_sample 19 | 20 | fun main(args: Array) = api(composer = ContainerComposer()) 21 | { 22 | route( 23 | path("/login", to = LoginController::class, renderWith = "login.vm"), 24 | path("/authorize", to = AuthorizeController::class, renderWith = "authorize.vm"), 25 | path("/token", to = TokenController::class)) 26 | } 27 | -------------------------------------------------------------------------------- /src/main/kotlin/com/nordicapis/kotlin_spark_sample/TokenController.kt: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2015 Nordic APIs AB 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | 15 | TokenController.kt - The controller class that provides the logic for the token endpoint 16 | */ 17 | 18 | package com.nordicapis.kotlin_spark_sample 19 | 20 | import spark.Request 21 | import spark.Response 22 | 23 | public class TokenController : Controllable() 24 | { 25 | public override fun get(request: Request, response: Response): ControllerResult 26 | { 27 | response.body("my good token") 28 | 29 | return ControllerResult() 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/kotlin/com/nordicapis/kotlin_spark_sample/ContainerComposer.kt: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2015 Nordic APIs AB 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | 15 | ContainerComposer.kt - The composer that wires up the dependencies of this application 16 | */ 17 | 18 | package com.nordicapis.kotlin_spark_sample 19 | 20 | import org.picocontainer.DefaultPicoContainer 21 | import org.picocontainer.MutablePicoContainer 22 | import kotlin.reflect.jvm.java 23 | 24 | class ContainerComposer : Composable 25 | { 26 | public override fun composeApplication(appContainer: MutablePicoContainer) 27 | { 28 | appContainer.addComponent(javaClass()) 29 | appContainer.addComponent(javaClass()) 30 | appContainer.addComponent(javaClass()) 31 | } 32 | 33 | public override fun composeRequest(container: MutablePicoContainer) 34 | { 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2015 Nordic APIs AB 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # http://www.apache.org/licenses/LICENSE-2.0 7 | # 8 | # Unless required by applicable law or agreed to in writing, software 9 | # distributed under the License is distributed on an "AS IS" BASIS, 10 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | # See the License for the specific language governing permissions and 12 | # limitations under the License. 13 | # 14 | # .gitignore - patterns of files that should not be added to source control 15 | 16 | ### JetBrains template 17 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion 18 | 19 | *.iml 20 | 21 | ## Directory-based project format: 22 | .idea/ 23 | # if you remove the above rule, at least ignore the following: 24 | 25 | # User-specific stuff: 26 | # .idea/workspace.xml 27 | # .idea/tasks.xml 28 | # .idea/dictionaries 29 | 30 | # Sensitive or high-churn files: 31 | # .idea/dataSources.ids 32 | # .idea/dataSources.xml 33 | # .idea/sqlDataSources.xml 34 | # .idea/dynamic.xml 35 | # .idea/uiDesigner.xml 36 | 37 | # Gradle: 38 | # .idea/gradle.xml 39 | # .idea/libraries 40 | 41 | # Mongo Explorer plugin: 42 | # .idea/mongoSettings.xml 43 | 44 | ## File-based project format: 45 | ## Plugin-specific files: 46 | 47 | # IntelliJ 48 | # mpeltonen/sbt-idea plugin 49 | # JIRA plugin 50 | # Crashlytics plugin (for Android Studio and IntelliJ) 51 | ### Maven template 52 | target/ 53 | 54 | .DS_Store 55 | velocity.log 56 | -------------------------------------------------------------------------------- /src/main/kotlin/com/nordicapis/kotlin_spark_sample/LoginController.kt: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2015 Nordic APIs AB 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | 15 | LoginController.kt - The controller class that provides the logic for the login endpoint 16 | */ 17 | 18 | package com.nordicapis.kotlin_spark_sample 19 | 20 | import spark.Request 21 | import spark.Response 22 | 23 | public class LoginController : Controllable() 24 | { 25 | // If this isn't overidden, even with this trivial response, it won't be routed. So, this is the minimum 26 | // implementation to get the page to show up. Usually, a model will be created and returned in addition to the 27 | // default response (which, because continueProcessing is true, will cause the router to continue processing). 28 | public override fun get(request: Request, response: Response): ControllerResult = ControllerResult() 29 | 30 | public override fun post(request: Request, response: Response): ControllerResult 31 | { 32 | var session = request.session() // Create session 33 | 34 | // Save the username in the session, so that it can be used in the authorize endpoint (e.g., for consent) 35 | session.attribute("username", request.queryParams("username")) 36 | 37 | // Redirect back to the authorize endpoint now that "login" has been performed 38 | response.redirect("/authorize") 39 | 40 | return ControllerResult(continueProcessing = false) 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/kotlin/com/nordicapis/kotlin_spark_sample/AuthorizeController.kt: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2015 Nordic APIs AB 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | 15 | AuthorizeController.kt - The controller class that provides the logic for the authorize endpoint 16 | */ 17 | 18 | package com.nordicapis.kotlin_spark_sample 19 | 20 | import org.slf4j.LoggerFactory 21 | import spark.Request 22 | import spark.Response 23 | import kotlin.reflect.jvm.java 24 | 25 | public class AuthorizeController : Controllable() 26 | { 27 | private val _logger = LoggerFactory.getLogger(javaClass()) 28 | 29 | public override fun before(request: Request, response: Response): Boolean 30 | { 31 | _logger.trace("before on Authorize controller invoked") 32 | 33 | if (request.session(false) == null) 34 | { 35 | _logger.debug("No session exists. Redirecting to login") 36 | 37 | response.redirect("/login") 38 | 39 | // Return false to abort any further processing 40 | return false 41 | } 42 | 43 | _logger.debug("Session exists") 44 | 45 | return true 46 | } 47 | 48 | public override fun get(request: Request, response: Response): ControllerResult = ControllerResult(model = mapOf( 49 | "user" to request.session(false).attribute("username"), 50 | "data" to mapOf( 51 | "e1" to "e1 value", 52 | "e2" to "e2 value", 53 | "e3" to "e3 value"))) 54 | } 55 | -------------------------------------------------------------------------------- /src/main/kotlin/com/nordicapis/kotlin_spark_sample/Controllable.kt: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2015 Nordic APIs AB 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | 15 | Controllable.kt - The interface of all controllers (i.e., all request handler logic classes) 16 | */ 17 | 18 | package com.nordicapis.kotlin_spark_sample 19 | 20 | import spark.Request 21 | import spark.Response 22 | 23 | SuppressWarnings("unused") 24 | abstract class Controllable 25 | { 26 | public open fun before(request: Request, response: Response): Boolean = true 27 | 28 | public open fun get(request: Request, response: Response): ControllerResult = ControllerResult() 29 | 30 | public open fun post(request: Request, response: Response): ControllerResult = ControllerResult() 31 | 32 | public open fun put(request: Request, response: Response): ControllerResult = ControllerResult() 33 | 34 | public open fun delete(request: Request, response: Response): ControllerResult = ControllerResult() 35 | 36 | public open fun patch(request: Request, response: Response): ControllerResult = ControllerResult() 37 | 38 | public open fun head(request: Request, response: Response): ControllerResult = ControllerResult() 39 | 40 | public open fun trace(request: Request, response: Response): ControllerResult = ControllerResult() 41 | 42 | public open fun connect(request: Request, response: Response): ControllerResult = ControllerResult() 43 | 44 | public open fun options(request: Request): ControllerResult = ControllerResult() 45 | 46 | public open fun after(request: Request, response: Response) { } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/kotlin/com/nordicapis/kotlin_spark_sample/Application.kt: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2015 Nordic APIs AB 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | 15 | Application.kt - The main class and functions used to expose an API application using Spark 16 | */ 17 | 18 | package com.nordicapis.kotlin_spark_sample 19 | 20 | import org.picocontainer.DefaultPicoContainer 21 | import org.picocontainer.MutablePicoContainer 22 | import spark.servlet.SparkApplication 23 | import kotlin.reflect.KClass 24 | import kotlin.reflect.jvm.java 25 | 26 | public class Application( 27 | var composer: Composable = Noncomposer(), 28 | var appContainer: MutablePicoContainer = DefaultPicoContainer(), 29 | var routes: () -> List>) : SparkApplication 30 | { 31 | private var router = Router() 32 | 33 | init 34 | { 35 | composer.composeApplication(appContainer) 36 | } 37 | 38 | override fun init() { } 39 | 40 | fun host() 41 | { 42 | var routes = routes.invoke() 43 | 44 | for (routeData in routes) 45 | { 46 | val (path, controllerClass, template) = routeData 47 | 48 | router.routeTo(path, appContainer, controllerClass, composer, template) 49 | } 50 | } 51 | 52 | data class RouteData(val path: String, val controllerClass: Class, val template: String? = null) 53 | } 54 | 55 | fun api(composer: Composable, routes: () -> List>) 56 | { 57 | Application(composer = composer, routes = routes).host() 58 | } 59 | 60 | fun path(path: String, to: KClass, renderWith: String? = null) : Application.RouteData 61 | { 62 | return Application.RouteData(path, to.java, renderWith) 63 | } 64 | 65 | fun route(vararg values: T): List = listOf(*values) 66 | -------------------------------------------------------------------------------- /src/main/kotlin/com/nordicapis/kotlin_spark_sample/Router.kt: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2015 Nordic APIs AB 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | 15 | Router.kt - The class that routes all controllers and handles templating 16 | */ 17 | 18 | package com.nordicapis.kotlin_spark_sample 19 | 20 | import org.picocontainer.DefaultPicoContainer 21 | import org.picocontainer.PicoContainer 22 | import spark.* 23 | import spark.Spark.halt 24 | import spark.template.velocity.VelocityTemplateEngine 25 | import java.util.* 26 | import kotlin.reflect.jvm.java 27 | 28 | class Router constructor() : SparkBase() 29 | { 30 | public fun routeTo(path: String, container: PicoContainer, controllerClass: Class, 31 | composer: Composable, template: String? = null) 32 | { 33 | for (classMethod in controllerClass.getDeclaredMethods()) 34 | { 35 | val methodName = classMethod.getName() 36 | 37 | if (methodName == "before" || methodName == "after") 38 | { 39 | continue // We don't want to route after or before using Spark, so skip these. 40 | } 41 | 42 | // See if the controller class' method is overriding one of Controllable's 43 | for (interfaceMethod in javaClass().getMethods()) 44 | { 45 | if (methodName == interfaceMethod.getName() && // method names match? 46 | classMethod.getReturnType() == interfaceMethod.getReturnType() && // method return the same type? 47 | Arrays.deepEquals(classMethod.getParameterTypes(), interfaceMethod.getParameterTypes())) // Params match? 48 | { 49 | if (template == null || template.isBlank()) 50 | { 51 | addRoute(methodName, path, container, controllerClass, composer) 52 | } 53 | else 54 | { 55 | addTemplatizedRoute(methodName, template, path, container, controllerClass, composer) 56 | } 57 | 58 | break 59 | } 60 | } 61 | } 62 | } 63 | 64 | private fun addTemplatizedRoute(httpMethod: String, template: String, path: String, 65 | container: PicoContainer, controllerClass: Class, composer: Composable) 66 | { 67 | val r = fun (request: Request, response: Response): ModelAndView 68 | { 69 | var model = router(request, response, container, controllerClass, composer) 70 | 71 | return ModelAndView(model, template) 72 | } 73 | 74 | SparkBase.addRoute(httpMethod, TemplateViewRouteImpl.create(path, r, VelocityTemplateEngine())) 75 | } 76 | 77 | private fun addRoute(httpMethod: String, path: String, container: PicoContainer, 78 | controllerClass: Class, composer: Composable) 79 | { 80 | val r = fun (request: Request, response: Response): Any 81 | { 82 | router(request, response, container, controllerClass, composer) 83 | 84 | return response.body() 85 | } 86 | 87 | SparkBase.addRoute(httpMethod, SparkBase.wrap(path, r)) 88 | } 89 | 90 | private fun router(request: Request, response: Response, container: PicoContainer, 91 | controllerClass: Class, composer: Composable) : Map 92 | { 93 | val requestContainer = DefaultPicoContainer(container) 94 | var model : Map = emptyMap() 95 | 96 | composer.composeRequest(requestContainer) 97 | 98 | try 99 | { 100 | val controller = requestContainer.getComponent(controllerClass) 101 | 102 | if (controller.before(request, response)) 103 | { 104 | // Fire the controller's method depending on the HTTP method of the request 105 | val httpMethod = request.requestMethod().toLowerCase() 106 | val method = controllerClass.getMethod(httpMethod, javaClass(), javaClass()) 107 | val result = method.invoke(controller, request, response) 108 | 109 | if (result is ControllerResult && result.continueProcessing) 110 | { 111 | controller.after(request, response) 112 | 113 | model = result.model 114 | } 115 | } 116 | } 117 | catch (e: Exception) 118 | { 119 | halt(500, "Server Error") 120 | } 121 | 122 | return model 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 17 | 18 | 20 | 4.0.0 21 | 22 | com.nordicapis 23 | kotlin_spark_sample 24 | 1.0 25 | jar 26 | 27 | Nordic APIs Kotlin / Spark Sample 28 | http://nordicapis.com/building-apis-on-the-jvm-using-kotlin-and-spark-part-1/ 29 | 30 | 31 | UTF-8 32 | 0.12.1218 33 | 34 | 35 | 36 | ${project.basedir}/src/main/kotlin 37 | 38 | 39 | 40 | kotlin-maven-plugin 41 | org.jetbrains.kotlin 42 | ${kotlin.version} 43 | 44 | 45 | 46 | 47 | compile 48 | compile 49 | 50 | compile 51 | 52 | 53 | 54 | test-compile 55 | test-compile 56 | 57 | test-compile 58 | 59 | 60 | 61 | 62 | 63 | 64 | org.apache.maven.plugins 65 | maven-assembly-plugin 66 | 67 | 68 | 69 | attached 70 | 71 | package 72 | 73 | 74 | jar-with-dependencies 75 | 76 | 77 | 78 | com.nordicapis.kotlin_spark_sample.Kotlin_spark_samplePackage 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | org.jetbrains.kotlin 91 | kotlin-stdlib 92 | ${kotlin.version} 93 | 94 | 95 | org.jetbrains.kotlin 96 | kotlin-reflect 97 | ${kotlin.version} 98 | 99 | 100 | com.sparkjava 101 | spark-core 102 | 2.2 103 | 104 | 105 | org.slf4j 106 | slf4j-simple 107 | 108 | 109 | 110 | 111 | org.apache.httpcomponents 112 | httpclient 113 | 4.2 114 | 115 | 116 | org.picocontainer 117 | picocontainer 118 | 2.14.3 119 | 120 | 121 | com.sparkjava 122 | spark-template-velocity 123 | 2.0.0 124 | 125 | 126 | org.slf4j 127 | slf4j-log4j12 128 | 1.7.10 129 | 130 | 131 | 132 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | --------------------------------------------------------------------------------