forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request quarkusio#18222 from geoand/quarkusio#18201
Introduce support for Kotlin Flow as return type in RESTEasy Reactive
- Loading branch information
Showing
9 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...c/main/kotlin/org/jboss/resteasy/reactive/server/runtime/kotlin/FlowToPublisherHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.jboss.resteasy.reactive.server.runtime.kotlin | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.reactive.asPublisher | ||
import org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext | ||
import org.jboss.resteasy.reactive.server.spi.ServerRestHandler | ||
|
||
class FlowToPublisherHandler : ServerRestHandler { | ||
|
||
override fun handle(requestContext: ResteasyReactiveRequestContext?) { | ||
val result = requestContext!!.result | ||
if (result is Flow<*>) { | ||
requestContext.result = (result as Flow<Any>) // cast needed for extension function | ||
.asPublisher() | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
.../src/main/java/org/jboss/resteasy/reactive/server/model/FixedHandlersChainCustomizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org.jboss.resteasy.reactive.server.model; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import org.jboss.resteasy.reactive.common.model.ResourceClass; | ||
import org.jboss.resteasy.reactive.server.spi.ServerRestHandler; | ||
|
||
public class FixedHandlersChainCustomizer implements HandlerChainCustomizer { | ||
|
||
private List<ServerRestHandler> handlers; | ||
private Phase phase; | ||
|
||
public FixedHandlersChainCustomizer(List<ServerRestHandler> handlers, Phase phase) { | ||
this.handlers = handlers; | ||
this.phase = phase; | ||
} | ||
|
||
public FixedHandlersChainCustomizer() { | ||
} | ||
|
||
@Override | ||
public List<ServerRestHandler> handlers(Phase phase, ResourceClass resourceClass, | ||
ServerResourceMethod serverResourceMethod) { | ||
if (this.phase == phase) { | ||
return handlers; | ||
} | ||
return Collections.emptyList(); | ||
} | ||
|
||
public List<ServerRestHandler> getHandlers() { | ||
return handlers; | ||
} | ||
|
||
public void setHandlers(List<ServerRestHandler> handlers) { | ||
this.handlers = handlers; | ||
} | ||
|
||
public Phase getPhase() { | ||
return phase; | ||
} | ||
|
||
public void setPhase(Phase phase) { | ||
this.phase = phase; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...ve-kotlin/standard/src/main/kotlin/io/quarkus/it/resteasy/reactive/kotlin/FlowResource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package io.quarkus.it.resteasy.reactive.kotlin | ||
|
||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.flow.flow | ||
import org.jboss.resteasy.reactive.RestSseElementType | ||
import javax.ws.rs.GET | ||
import javax.ws.rs.Path | ||
import javax.ws.rs.Produces | ||
import javax.ws.rs.core.MediaType | ||
|
||
@Path("flow") | ||
class FlowResource { | ||
|
||
@GET | ||
@Path("str") | ||
@Produces(MediaType.SERVER_SENT_EVENTS) | ||
fun sseStrings() = flow { | ||
emit("Hello") | ||
emit("From") | ||
emit("Kotlin") | ||
emit("Flow") | ||
} | ||
|
||
@GET | ||
@Path("json") | ||
@Produces(MediaType.SERVER_SENT_EVENTS) | ||
@RestSseElementType(MediaType.APPLICATION_JSON) | ||
fun sseJson() = flow { | ||
emit(Country("Barbados", "Bridgetown")) | ||
delay(1000) | ||
emit(Country("Mauritius", "Port Louis")) | ||
delay(1000) | ||
emit(Country("Fiji", "Suva")) | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...gration-tests/resteasy-reactive-kotlin/standard/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...otlin/standard/src/test/kotlin/io/quarkus/it/resteasy/reactive/kotlin/FlowResourceTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package io.quarkus.it.resteasy.reactive.kotlin | ||
|
||
import io.quarkus.test.common.http.TestHTTPResource | ||
import io.quarkus.test.junit.QuarkusTest | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Test | ||
import java.util.* | ||
import java.util.concurrent.CompletableFuture | ||
import java.util.concurrent.TimeUnit | ||
import javax.ws.rs.client.ClientBuilder | ||
import javax.ws.rs.client.WebTarget | ||
import javax.ws.rs.sse.SseEventSource | ||
|
||
@QuarkusTest | ||
class FlowResourceTest { | ||
|
||
@TestHTTPResource("/flow") | ||
var flowPath: String? = null | ||
|
||
@Test | ||
fun testSeeStrings() { | ||
testSse("str", 5) { | ||
assertThat(it).containsExactly("Hello", "From", "Kotlin", "Flow") | ||
} | ||
} | ||
|
||
@Test | ||
fun testSeeJson() { | ||
testSse("json", 10) { | ||
assertThat(it).containsExactly( | ||
"{\"name\":\"Barbados\",\"capital\":\"Bridgetown\"}", | ||
"{\"name\":\"Mauritius\",\"capital\":\"Port Louis\"}", | ||
"{\"name\":\"Fiji\",\"capital\":\"Suva\"}") | ||
} | ||
} | ||
|
||
private fun testSse(path: String, timeout: Long, assertion: (List<String>) -> Unit) { | ||
val client = ClientBuilder.newBuilder().build() | ||
val target: WebTarget = client.target("$flowPath/$path") | ||
SseEventSource.target(target).reconnectingEvery(Int.MAX_VALUE.toLong(), TimeUnit.SECONDS) | ||
.build().use { eventSource -> | ||
val res = CompletableFuture<List<String>>() | ||
val collect = Collections.synchronizedList(ArrayList<String>()) | ||
eventSource.register({ inboundSseEvent -> collect.add(inboundSseEvent.readData()) }, { throwable -> res.completeExceptionally(throwable) }) { res.complete(collect) } | ||
eventSource.open() | ||
val list = res.get(timeout, TimeUnit.SECONDS) | ||
assertion(list) | ||
} | ||
} | ||
} |