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.
Give extension a way to run code before exception mapping
- Loading branch information
Showing
7 changed files
with
213 additions
and
1 deletion.
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
99 changes: 99 additions & 0 deletions
99
...src/test/java/io/quarkus/resteasy/reactive/server/test/PreExceptionMapperHandlerTest.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,99 @@ | ||
package io.quarkus.resteasy.reactive.server.test; | ||
|
||
import static io.restassured.RestAssured.get; | ||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import java.util.function.Consumer; | ||
import java.util.function.Supplier; | ||
|
||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.core.Response; | ||
|
||
import org.jboss.resteasy.reactive.server.ServerExceptionMapper; | ||
import org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext; | ||
import org.jboss.resteasy.reactive.server.spi.ResteasyReactiveContainerRequestContext; | ||
import org.jboss.resteasy.reactive.server.spi.ServerRestHandler; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.builder.BuildChainBuilder; | ||
import io.quarkus.builder.BuildContext; | ||
import io.quarkus.builder.BuildStep; | ||
import io.quarkus.resteasy.reactive.server.spi.PreExceptionMapperHandlerBuildItem; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.smallrye.mutiny.Uni; | ||
|
||
public class PreExceptionMapperHandlerTest { | ||
|
||
@RegisterExtension | ||
static QuarkusUnitTest test = new QuarkusUnitTest() | ||
.setArchiveProducer(new Supplier<>() { | ||
@Override | ||
public JavaArchive get() { | ||
return ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(Resource.class, Mappers.class, DummyPreExceptionMapperHandler.class); | ||
} | ||
}).addBuildChainCustomizer(new Consumer<>() { | ||
@Override | ||
public void accept(BuildChainBuilder buildChainBuilder) { | ||
buildChainBuilder.addBuildStep(new BuildStep() { | ||
@Override | ||
public void execute(BuildContext context) { | ||
context.produce( | ||
new PreExceptionMapperHandlerBuildItem(new DummyPreExceptionMapperHandler())); | ||
} | ||
}).produces(PreExceptionMapperHandlerBuildItem.class).build(); | ||
} | ||
}); | ||
|
||
@Test | ||
public void test() { | ||
get("/test") | ||
.then() | ||
.statusCode(999) | ||
.header("foo", "bar"); | ||
|
||
get("/test/uni") | ||
.then() | ||
.statusCode(999) | ||
.header("foo", "bar"); | ||
} | ||
|
||
@Path("test") | ||
public static class Resource { | ||
|
||
@GET | ||
public String get() { | ||
throw new RuntimeException("dummy"); | ||
} | ||
|
||
@Path("uni") | ||
@GET | ||
public Uni<String> uniGet() { | ||
return Uni.createFrom().item(() -> { | ||
throw new RuntimeException("dummy"); | ||
}); | ||
} | ||
} | ||
|
||
public static class Mappers { | ||
|
||
@ServerExceptionMapper(RuntimeException.class) | ||
Response handle(ResteasyReactiveContainerRequestContext requestContext) { | ||
return Response.status(999).header("foo", requestContext.getProperty("foo")).build(); | ||
} | ||
|
||
} | ||
|
||
public static class DummyPreExceptionMapperHandler implements ServerRestHandler { | ||
|
||
@Override | ||
public void handle(ResteasyReactiveRequestContext requestContext) throws Exception { | ||
assertThat(requestContext.getThrowable()).isInstanceOf(RuntimeException.class); | ||
requestContext.setProperty("foo", "bar"); | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...main/java/io/quarkus/resteasy/reactive/server/spi/PreExceptionMapperHandlerBuildItem.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,42 @@ | ||
package io.quarkus.resteasy.reactive.server.spi; | ||
|
||
import jakarta.ws.rs.Priorities; | ||
|
||
import org.jboss.resteasy.reactive.server.spi.ServerRestHandler; | ||
|
||
import io.quarkus.builder.item.MultiBuildItem; | ||
|
||
/** | ||
* A build item that allows extension to define a {@link ServerRestHandler} that runs write before | ||
* RESTEasy Reactive attempt to do exception mapping according to the JAX-RS spec. | ||
* This is only meant to be used in very advanced use cases. | ||
*/ | ||
public final class PreExceptionMapperHandlerBuildItem extends MultiBuildItem | ||
implements Comparable<PreExceptionMapperHandlerBuildItem> { | ||
|
||
private final ServerRestHandler handler; | ||
private final int priority; | ||
|
||
public PreExceptionMapperHandlerBuildItem(ServerRestHandler handler, int priority) { | ||
this.handler = handler; | ||
this.priority = priority; | ||
} | ||
|
||
public PreExceptionMapperHandlerBuildItem(ServerRestHandler handler) { | ||
this.handler = handler; | ||
this.priority = Priorities.USER; | ||
} | ||
|
||
@Override | ||
public int compareTo(PreExceptionMapperHandlerBuildItem o) { | ||
return Integer.compare(priority, o.priority); | ||
} | ||
|
||
public ServerRestHandler getHandler() { | ||
return handler; | ||
} | ||
|
||
public int getPriority() { | ||
return priority; | ||
} | ||
} |
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
34 changes: 34 additions & 0 deletions
34
...e/src/main/java/org/jboss/resteasy/reactive/server/model/DelegatingServerRestHandler.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,34 @@ | ||
package org.jboss.resteasy.reactive.server.model; | ||
|
||
import java.util.List; | ||
|
||
import org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext; | ||
import org.jboss.resteasy.reactive.server.spi.ServerRestHandler; | ||
|
||
public class DelegatingServerRestHandler implements ServerRestHandler { | ||
|
||
private List<ServerRestHandler> delegates; | ||
|
||
public DelegatingServerRestHandler(List<ServerRestHandler> delegates) { | ||
this.delegates = delegates; | ||
} | ||
|
||
// for bytecode recording | ||
public DelegatingServerRestHandler() { | ||
} | ||
|
||
public List<ServerRestHandler> getDelegates() { | ||
return delegates; | ||
} | ||
|
||
public void setDelegates(List<ServerRestHandler> delegates) { | ||
this.delegates = delegates; | ||
} | ||
|
||
@Override | ||
public void handle(ResteasyReactiveRequestContext requestContext) throws Exception { | ||
for (int i = 0; i < delegates.size(); i++) { | ||
delegates.get(0).handle(requestContext); | ||
} | ||
} | ||
} |