/
TemplateRoutingHttpHandler.kt
39 lines (33 loc) · 1.6 KB
/
TemplateRoutingHttpHandler.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package org.http4k.routing
import org.http4k.core.Filter
import org.http4k.core.HttpHandler
import org.http4k.core.Method
import org.http4k.core.Request
import org.http4k.core.Response
import org.http4k.core.UriTemplate
import org.http4k.core.then
internal data class TemplateRoutingHttpHandler(
private val method: Method?,
private val template: UriTemplate,
private val httpHandler: HttpHandler,
private val notFoundHandler: HttpHandler = routeNotFoundHandler,
private val methodNotAllowedHandler: HttpHandler = routeMethodNotAllowedHandler) : RoutingHttpHandler {
override fun match(request: Request): RouterMatch =
if (template.matches(request.uri.path)) {
when (method) {
null, request.method -> RouterMatch.MatchingHandler { RoutedResponse(httpHandler(RoutedRequest(it, template)), template) }
else -> RouterMatch.MethodNotMatched
}
} else RouterMatch.Unmatched
override fun invoke(request: Request): Response = when (val matchResult = match(request)) {
is RouterMatch.MatchingHandler -> matchResult(request)
is RouterMatch.MethodNotMatched -> methodNotAllowedHandler(request)
is RouterMatch.Unmatched -> notFoundHandler(request)
}
override fun withFilter(new: Filter): RoutingHttpHandler =
copy(httpHandler = new.then(httpHandler),
notFoundHandler = new.then(notFoundHandler),
methodNotAllowedHandler = new.then(methodNotAllowedHandler)
)
override fun withBasePath(new: String): RoutingHttpHandler = copy(template = UriTemplate.from("$new/$template"))
}