Improve parity between Java and Kotlin router DSL
This commit adds following functions to the Kotlin DSL: add, filter, before, after and onError. Closes gh-23524
This commit is contained in:
@@ -60,7 +60,8 @@ fun router(routes: (RouterFunctionDsl.() -> Unit)) = RouterFunctionDsl(routes).b
|
||||
*/
|
||||
class RouterFunctionDsl(private val init: (RouterFunctionDsl.() -> Unit)) {
|
||||
|
||||
private val builder = RouterFunctions.route()
|
||||
@PublishedApi
|
||||
internal val builder = RouterFunctions.route()
|
||||
|
||||
/**
|
||||
* Return a composed request predicate that tests against both this predicate AND
|
||||
@@ -504,6 +505,74 @@ class RouterFunctionDsl(private val init: (RouterFunctionDsl.() -> Unit)) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge externally defined router functions into this one.
|
||||
* @param routerFunction the router function to be added
|
||||
* @since 5.2
|
||||
*/
|
||||
fun add(routerFunction: RouterFunction<ServerResponse>) {
|
||||
builder.add(routerFunction)
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters all routes created by this router with the given filter function. Filter
|
||||
* functions are typically used to address cross-cutting concerns, such as logging,
|
||||
* security, etc.
|
||||
* @param filterFunction the function to filter all routes built by this router
|
||||
* @since 5.2
|
||||
*/
|
||||
fun filter(filterFunction: (ServerRequest, (ServerRequest) -> ServerResponse) -> ServerResponse) {
|
||||
builder.filter { request, next ->
|
||||
filterFunction(request) {
|
||||
next.handle(request)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the request object for all routes created by this builder with the given request
|
||||
* processing function. Filters are typically used to address cross-cutting concerns, such
|
||||
* as logging, security, etc.
|
||||
* @param requestProcessor a function that transforms the request
|
||||
* @since 5.2
|
||||
*/
|
||||
fun before(requestProcessor: (ServerRequest) -> ServerRequest) {
|
||||
builder.before(requestProcessor)
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the response object for all routes created by this builder with the given response
|
||||
* processing function. Filters are typically used to address cross-cutting concerns, such
|
||||
* as logging, security, etc.
|
||||
* @param responseProcessor a function that transforms the response
|
||||
* @since 5.2
|
||||
*/
|
||||
fun after(responseProcessor: (ServerRequest, ServerResponse) -> ServerResponse) {
|
||||
builder.after(responseProcessor)
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters all exceptions that match the predicate by applying the given response provider
|
||||
* function.
|
||||
* @param predicate the type of exception to filter
|
||||
* @param responseProvider a function that creates a response
|
||||
* @since 5.2
|
||||
*/
|
||||
fun onError(predicate: (Throwable) -> Boolean, responseProvider: (Throwable, ServerRequest) -> ServerResponse) {
|
||||
builder.onError(predicate, responseProvider)
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters all exceptions that match the predicate by applying the given response provider
|
||||
* function.
|
||||
* @param E the type of exception to filter
|
||||
* @param responseProvider a function that creates a response
|
||||
* @since 5.2
|
||||
*/
|
||||
inline fun <reified E : Throwable> onError(noinline responseProvider: (Throwable, ServerRequest) -> ServerResponse) {
|
||||
builder.onError({it is E}, responseProvider)
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a composed routing function created from all the registered routes.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user