Browse Source

Polish Kotlin Web functional DSL

Issue: SPR-15065
pull/1288/merge
Sebastien Deleuze 9 years ago
parent
commit
be45de0571
  1. 62
      spring-web-reactive/src/main/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensions.kt
  2. 30
      spring-web-reactive/src/test/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensionsTests.kt

62
spring-web-reactive/src/main/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensions.kt

@ -13,12 +13,12 @@ import reactor.core.publisher.Mono @@ -13,12 +13,12 @@ import reactor.core.publisher.Mono
* fun route(request: ServerRequest) = route(request) {
* accept(TEXT_HTML).apply {
* (GET("/user/") or GET("/users/")) { findAllView() }
* GET("/user/{login}") { findViewById() }
* GET("/user/{login}", ::findViewById)
* }
* accept(APPLICATION_JSON).apply {
* (GET("/api/user/") or GET("/api/users/")) { findAll() }
* POST("/api/user/") { create() }
* POST("/api/user/{login}") { findOne() }
* POST("/api/user/", ::create)
* POST("/api/user/{login}", ::findOne)
* }
* }
* ```
@ -35,62 +35,62 @@ class RouterDsl { @@ -35,62 +35,62 @@ class RouterDsl {
val children = mutableListOf<RouterDsl>()
val routes = mutableListOf<RouterFunction<ServerResponse>>()
operator fun RequestPredicate.invoke(f: () -> HandlerFunction<ServerResponse>) {
routes += RouterFunctions.route(this, f())
}
infix fun RequestPredicate.and(other: RequestPredicate): RequestPredicate = this.and(other)
infix fun RequestPredicate.or(other: RequestPredicate): RequestPredicate = this.or(other)
operator fun RequestPredicate.not(): RequestPredicate = this.negate()
fun GET(pattern: String, f: () -> HandlerFunction<ServerResponse>) {
routes += RouterFunctions.route(RequestPredicates.GET(pattern), f())
operator fun RequestPredicate.invoke(f: (ServerRequest) -> Mono<ServerResponse>) {
routes += RouterFunctions.route(this, HandlerFunction { f(it) })
}
fun GET(pattern: String, f: (ServerRequest) -> Mono<ServerResponse>) {
routes += RouterFunctions.route(RequestPredicates.GET(pattern), HandlerFunction { f(it) } )
}
fun HEAD(pattern: String, f: () -> HandlerFunction<ServerResponse>) {
routes += RouterFunctions.route(RequestPredicates.HEAD(pattern), f())
fun HEAD(pattern: String, f: (ServerRequest) -> Mono<ServerResponse>) {
routes += RouterFunctions.route(RequestPredicates.HEAD(pattern), HandlerFunction { f(it) })
}
fun POST(pattern: String, f: () -> HandlerFunction<ServerResponse>) {
routes += RouterFunctions.route(RequestPredicates.POST(pattern), f())
fun POST(pattern: String, f: (ServerRequest) -> Mono<ServerResponse>) {
routes += RouterFunctions.route(RequestPredicates.POST(pattern), HandlerFunction { f(it) })
}
fun PUT(pattern: String, f: () -> HandlerFunction<ServerResponse>) {
routes += RouterFunctions.route(RequestPredicates.PUT(pattern), f())
fun PUT(pattern: String, f: (ServerRequest) -> Mono<ServerResponse>) {
routes += RouterFunctions.route(RequestPredicates.PUT(pattern), HandlerFunction { f(it) })
}
fun PATCH(pattern: String, f: () -> HandlerFunction<ServerResponse>) {
routes += RouterFunctions.route(RequestPredicates.PATCH(pattern), f())
fun PATCH(pattern: String, f: (ServerRequest) -> Mono<ServerResponse>) {
routes += RouterFunctions.route(RequestPredicates.PATCH(pattern), HandlerFunction { f(it) })
}
fun DELETE(pattern: String, f: () -> HandlerFunction<ServerResponse>) {
routes += RouterFunctions.route(RequestPredicates.DELETE(pattern), f())
fun DELETE(pattern: String, f: (ServerRequest) -> Mono<ServerResponse>) {
routes += RouterFunctions.route(RequestPredicates.DELETE(pattern), HandlerFunction { f(it) })
}
fun OPTIONS(pattern: String, f: () -> HandlerFunction<ServerResponse>) {
routes += RouterFunctions.route(RequestPredicates.OPTIONS(pattern), f())
fun OPTIONS(pattern: String, f: (ServerRequest) -> Mono<ServerResponse>) {
routes += RouterFunctions.route(RequestPredicates.OPTIONS(pattern), HandlerFunction { f(it) })
}
fun accept(vararg mediaType: MediaType, f: () -> HandlerFunction<ServerResponse>) {
routes += RouterFunctions.route(RequestPredicates.accept(*mediaType), f())
fun accept(mediaType: MediaType, f: (ServerRequest) -> Mono<ServerResponse>) {
routes += RouterFunctions.route(RequestPredicates.accept(mediaType), HandlerFunction { f(it) })
}
fun contentType(vararg mediaType: MediaType, f: () -> HandlerFunction<ServerResponse>) {
routes += RouterFunctions.route(RequestPredicates.contentType(*mediaType), f())
fun contentType(mediaType: MediaType, f: (ServerRequest) -> Mono<ServerResponse>) {
routes += RouterFunctions.route(RequestPredicates.contentType(mediaType), HandlerFunction { f(it) })
}
fun headers(headerPredicate: (ServerRequest.Headers)->Boolean, f: () -> HandlerFunction<ServerResponse>) {
routes += RouterFunctions.route(RequestPredicates.headers(headerPredicate), f())
fun headers(headerPredicate: (ServerRequest.Headers)->Boolean, f: (ServerRequest) -> Mono<ServerResponse>) {
routes += RouterFunctions.route(RequestPredicates.headers(headerPredicate), HandlerFunction { f(it) })
}
fun method(httpMethod: HttpMethod, f: () -> HandlerFunction<ServerResponse>) {
routes += RouterFunctions.route(RequestPredicates.method(httpMethod), f())
fun method(httpMethod: HttpMethod, f: (ServerRequest) -> Mono<ServerResponse>) {
routes += RouterFunctions.route(RequestPredicates.method(httpMethod), HandlerFunction { f(it) })
}
fun path(pattern: String, f: () -> HandlerFunction<ServerResponse>) {
routes += RouterFunctions.route(RequestPredicates.path(pattern), f())
fun path(pattern: String, f: (ServerRequest) -> Mono<ServerResponse>) {
routes += RouterFunctions.route(RequestPredicates.path(pattern), HandlerFunction { f(it) })
}
fun resources(path: String, location: Resource) {

30
spring-web-reactive/src/test/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensionsTests.kt

@ -92,23 +92,22 @@ class RouterFunctionExtensionsTests { @@ -92,23 +92,22 @@ class RouterFunctionExtensionsTests {
.verifyComplete()
}
class FooController : RouterFunction<ServerResponse> {
override fun route(request: ServerRequest) = route(request) {
(GET("/foo/") or GET("/foos/")) { handle() }
override fun route(req: ServerRequest) = route(req) {
(GET("/foo/") or GET("/foos/")) { handle(req) }
accept(APPLICATION_JSON).apply {
POST("/api/foo/") { handle() }
PUT("/api/foo/") { handle() }
DELETE("/api/foo/") { handle() }
POST("/api/foo/", ::handle)
PUT("/api/foo/", ::handle)
DELETE("/api/foo/", ::handle)
}
accept(APPLICATION_ATOM_XML) { handle() }
contentType(APPLICATION_OCTET_STREAM) { handle() }
method(HttpMethod.PATCH) { handle() }
accept(APPLICATION_ATOM_XML, ::handle)
contentType(APPLICATION_OCTET_STREAM) { handle(req) }
method(HttpMethod.PATCH) { handle(req) }
headers({ it.accept().contains(APPLICATION_JSON) }).apply {
GET("/api/foo/") { handle() }
GET("/api/foo/", ::handle)
}
headers({ it.header("bar").isNotEmpty() }) { handle() }
headers({ it.header("bar").isNotEmpty() }, ::handle)
resources("/org/springframework/web/reactive/function/**",
ClassPathResource("/org/springframework/web/reactive/function/response.txt"))
resources {
@ -119,11 +118,10 @@ class RouterFunctionExtensionsTests { @@ -119,11 +118,10 @@ class RouterFunctionExtensionsTests {
Mono.empty()
}
}
path("/baz") { handle() }
path("/baz") { handle(req) }
}
fun handle() = HandlerFunction { ok().build() }
}
}
private fun handle(req: ServerRequest) = ok().build()

Loading…
Cancel
Save