-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Allow suspend functions as to be uses as custom filters
- Loading branch information
Showing
11 changed files
with
356 additions
and
48 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
.../deployment/src/main/java/io/quarkus/resteasy/reactive/common/deployment/KotlinUtils.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,23 @@ | ||
package io.quarkus.resteasy.reactive.common.deployment; | ||
|
||
import static io.quarkus.resteasy.reactive.common.deployment.QuarkusResteasyReactiveDotNames.CONTINUATION; | ||
|
||
import java.util.List; | ||
|
||
import org.jboss.jandex.MethodInfo; | ||
import org.jboss.jandex.Type; | ||
|
||
public class KotlinUtils { | ||
|
||
private KotlinUtils() { | ||
} | ||
|
||
public static boolean isSuspendMethod(MethodInfo methodInfo) { | ||
List<Type> parameters = methodInfo.parameters(); | ||
if (parameters.isEmpty()) { | ||
return false; | ||
} | ||
|
||
return CONTINUATION.equals(parameters.get(parameters.size() - 1).name()); | ||
} | ||
} |
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
38 changes: 38 additions & 0 deletions
38
...otlin/org/jboss/resteasy/reactive/server/runtime/kotlin/AbstractSuspendedRequestFilter.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,38 @@ | ||
package org.jboss.resteasy.reactive.server.runtime.kotlin | ||
|
||
import io.smallrye.mutiny.Uni | ||
import io.smallrye.mutiny.coroutines.asUni | ||
import kotlinx.coroutines.async | ||
import org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext | ||
import org.jboss.resteasy.reactive.server.spi.ResteasyReactiveContainerRequestContext | ||
import org.jboss.resteasy.reactive.server.spi.ResteasyReactiveContainerRequestFilter | ||
|
||
/** | ||
* Base class used by Quarkus to generate an implementation at build-time that calls | ||
* a {@code suspend} method annotated with {@code @ServerRequestFilter} | ||
*/ | ||
@Suppress("unused") | ||
abstract class AbstractSuspendedRequestFilter : ResteasyReactiveContainerRequestFilter { | ||
|
||
abstract suspend fun doFilter(containerRequestContext: ResteasyReactiveContainerRequestContext): Any | ||
|
||
abstract fun handleResult(containerRequestContext: ResteasyReactiveContainerRequestContext, uniResult: Uni<*>) | ||
|
||
|
||
private val originalTCCL: ClassLoader = Thread.currentThread().contextClassLoader | ||
|
||
override fun filter(containerRequestContext: ResteasyReactiveContainerRequestContext) { | ||
val (dispatcher,coroutineScope) = prepareExecution(containerRequestContext.serverRequestContext as ResteasyReactiveRequestContext) | ||
|
||
val uni = coroutineScope.async(context = dispatcher) { | ||
// ensure the proper CL is not lost in dev-mode | ||
Thread.currentThread().contextClassLoader = originalTCCL | ||
|
||
// the implementation gets the proper values from the context and invokes the user supplied method | ||
doFilter(containerRequestContext) | ||
}.asUni() | ||
|
||
// the implementation should call the appropriate FilterUtil method | ||
handleResult(containerRequestContext, uni) | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...tlin/org/jboss/resteasy/reactive/server/runtime/kotlin/AbstractSuspendedResponseFilter.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,36 @@ | ||
package org.jboss.resteasy.reactive.server.runtime.kotlin | ||
|
||
import kotlinx.coroutines.launch | ||
import org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext | ||
import org.jboss.resteasy.reactive.server.spi.ResteasyReactiveContainerRequestContext | ||
import org.jboss.resteasy.reactive.server.spi.ResteasyReactiveContainerResponseFilter | ||
import javax.ws.rs.container.ContainerResponseContext | ||
|
||
/** | ||
* Base class used by Quarkus to generate an implementation at build-time that calls | ||
* a {@code suspend} method annotated with {@code @ServerResponseFilter} | ||
*/ | ||
@Suppress("unused") | ||
abstract class AbstractSuspendedResponseFilter : ResteasyReactiveContainerResponseFilter { | ||
|
||
abstract suspend fun doFilter(requestContext: ResteasyReactiveContainerRequestContext, responseContext: ContainerResponseContext): Any | ||
|
||
private val originalTCCL: ClassLoader = Thread.currentThread().contextClassLoader | ||
|
||
override fun filter(requestContext: ResteasyReactiveContainerRequestContext, responseContext: ContainerResponseContext) { | ||
val (dispatcher,coroutineScope) = prepareExecution(requestContext.serverRequestContext as ResteasyReactiveRequestContext) | ||
|
||
|
||
requestContext.suspend() | ||
coroutineScope.launch(context = dispatcher) { | ||
// ensure the proper CL is not lost in dev-mode | ||
Thread.currentThread().contextClassLoader = originalTCCL | ||
try { | ||
doFilter(requestContext, responseContext) | ||
} catch (t: Throwable) { | ||
(requestContext.serverRequestContext as ResteasyReactiveRequestContext).handleException(t, true) | ||
} | ||
requestContext.resume() | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
.../runtime/src/main/kotlin/org/jboss/resteasy/reactive/server/runtime/kotlin/FilterUtils.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 io.vertx.core.Vertx | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext | ||
import javax.enterprise.inject.spi.CDI | ||
|
||
fun prepareExecution(requestContext: ResteasyReactiveRequestContext): Pair<CoroutineDispatcher, ApplicationCoroutineScope> { | ||
val requestScope = requestContext.captureCDIRequestScope() | ||
val dispatcher: CoroutineDispatcher = Vertx.currentContext()?.let {VertxDispatcher(it,requestScope)} | ||
?: throw IllegalStateException("No Vertx context found") | ||
|
||
val coroutineScope = CDI.current().select(ApplicationCoroutineScope::class.java) | ||
requestContext.suspend() | ||
|
||
return Pair(dispatcher, coroutineScope.get()) | ||
} |
Oops, something went wrong.