Introduce CoWebFilter

This commit introduces the CoWebFilter, an abstract WebFilter
implementation that allows for coroutine usage.

Closes gh-29672
This commit is contained in:
Arjen Poutsma 2023-01-16 11:00:33 +01:00
parent c0e38a2f1e
commit 3a585f8c29
3 changed files with 123 additions and 0 deletions

View File

@ -60,6 +60,7 @@ dependencies {
optional("org.apache.groovy:groovy")
optional("org.jetbrains.kotlin:kotlin-reflect")
optional("org.jetbrains.kotlin:kotlin-stdlib")
optional("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")
optional("org.jetbrains.kotlinx:kotlinx-serialization-cbor")
optional("org.jetbrains.kotlinx:kotlinx-serialization-json")
optional("org.jetbrains.kotlinx:kotlinx-serialization-protobuf")

View File

@ -0,0 +1,66 @@
/*
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.server
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.reactor.awaitSingleOrNull
import kotlinx.coroutines.reactor.mono
import reactor.core.publisher.Mono
/**
* Kotlin-specific implementation of the [WebFilter] interface that allows for
* using co-routines.
*
* @author Arjen Poutsma
* @since 6.0.5
*/
abstract class CoWebFilter : WebFilter {
final override fun filter(exchange: ServerWebExchange, chain: WebFilterChain): Mono<Void> {
return mono(Dispatchers.Unconfined) {
filter(exchange, object : CoWebFilterChain {
override suspend fun filter(exchange: ServerWebExchange) {
return chain.filter(exchange).cast(Unit.javaClass).awaitSingleOrNull() ?: Unit
}
})}.then()
}
/**
* Process the Web request and (optionally) delegate to the next
* `WebFilter` through the given [WebFilterChain].
* @param exchange the current server exchange
* @param chain provides a way to delegate to the next filter
*/
protected abstract suspend fun filter(exchange: ServerWebExchange, chain: CoWebFilterChain)
}
/**
* Kotlin-specific adaption of [WebFilterChain] that allows for co-routines.
*
* @author Arjen Poutsma
* @since 6.0.5
*/
interface CoWebFilterChain {
/**
* Delegate to the next [WebFilter] in the chain.
* @param exchange the current server exchange
*/
suspend fun filter(exchange: ServerWebExchange)
}

View File

@ -0,0 +1,56 @@
/*
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.server
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.mockito.BDDMockito
import org.mockito.Mockito
import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest
import org.springframework.web.testfixture.server.MockServerWebExchange
import reactor.core.publisher.Mono
import reactor.test.StepVerifier
/**
* @author Arjen Poutsma
*/
class CoWebFilterTests {
@Test
fun filter() {
val exchange = MockServerWebExchange.from(MockServerHttpRequest.get("https://example.com"))
val chain = Mockito.mock(WebFilterChain::class.java)
BDDMockito.given(chain.filter(exchange)).willReturn(Mono.empty())
val filter = MyCoWebFilter()
val result = filter.filter(exchange, chain)
StepVerifier.create(result)
.verifyComplete()
assertThat(exchange.attributes["foo"]).isEqualTo("bar")
}
}
private class MyCoWebFilter : CoWebFilter() {
override suspend fun filter(exchange: ServerWebExchange, chain: CoWebFilterChain) {
exchange.attributes["foo"] = "bar"
chain.filter(exchange)
}
}