Skip to content

Commit

Permalink
Take conditional annotation into account for Request and Response fil…
Browse files Browse the repository at this point in the history
…ters

Fixes: #29115
  • Loading branch information
geoand committed Nov 8, 2022
1 parent bf907e9 commit 0b512a3
Show file tree
Hide file tree
Showing 6 changed files with 260 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public class BuildTimeEnabledProcessor {
private static final DotName UNLESS_BUILD_PROPERTY_CONTAINER = DotName
.createSimple(UnlessBuildProperty.List.class.getName());

public static final Set<DotName> BUILD_TIME_ENABLED_BEAN_ANNOTATIONS = Set.of(IF_BUILD_PROFILE, UNLESS_BUILD_PROFILE,
IF_BUILD_PROPERTY, IF_BUILD_PROPERTY_CONTAINER, UNLESS_BUILD_PROPERTY, UNLESS_BUILD_PROPERTY_CONTAINER);

@BuildStep
void ifBuildProfile(CombinedIndexBuildItem index, BuildProducer<BuildTimeConditionBuildItem> producer) {
Collection<AnnotationInstance> annotationInstances = index.getIndex().getAnnotations(IF_BUILD_PROFILE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import io.quarkus.arc.ArcUndeclaredThrowableException;
import io.quarkus.arc.Unremovable;
import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
import io.quarkus.arc.deployment.BuildTimeEnabledProcessor;
import io.quarkus.arc.deployment.GeneratedBeanBuildItem;
import io.quarkus.arc.deployment.GeneratedBeanGizmoAdaptor;
import io.quarkus.arc.processor.DotNames;
Expand Down Expand Up @@ -310,7 +311,16 @@ public void handleCustomAnnotatedMethods(
}

List<FilterGeneration.GeneratedFilter> generatedFilters = FilterGeneration.generate(index,
Set.of(HTTP_SERVER_REQUEST, HTTP_SERVER_RESPONSE, ROUTING_CONTEXT), Set.of(Unremovable.class.getName()));
Set.of(HTTP_SERVER_REQUEST, HTTP_SERVER_RESPONSE, ROUTING_CONTEXT), Set.of(Unremovable.class.getName()),
(methodInfo -> {
List<AnnotationInstance> classAnnotations = methodInfo.declaringClass().declaredAnnotations();
for (AnnotationInstance classAnnotation : classAnnotations) {
if (BuildTimeEnabledProcessor.BUILD_TIME_ENABLED_BEAN_ANNOTATIONS.contains(classAnnotation.name())) {
return true;
}
}
return false;
}));
for (var generated : generatedFilters) {
for (var i : generated.getGeneratedClasses()) {
generatedBean.produce(new GeneratedBeanBuildItem(i.getName(), i.getData()));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package io.quarkus.resteasy.reactive.server.test.customproviders;

import static io.restassured.RestAssured.*;
import static org.assertj.core.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertFalse;

import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Priorities;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;

import org.hamcrest.Matchers;
import org.jboss.resteasy.reactive.server.ServerRequestFilter;
import org.jboss.resteasy.reactive.server.ServerResponseFilter;
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.arc.profile.IfBuildProfile;
import io.quarkus.arc.properties.IfBuildProperty;
import io.quarkus.test.QuarkusUnitTest;
import io.restassured.http.Header;
import io.smallrye.mutiny.Uni;

public class ConditionalBeanFiltersTest {

@RegisterExtension
static QuarkusUnitTest test = new QuarkusUnitTest()
.setArchiveProducer(new Supplier<>() {
@Override
public JavaArchive get() {
return ShrinkWrap.create(JavaArchive.class)
.addClasses(WontBeEnabledFilter.class, WillBeEnabledFilter.class, AlwaysEnabledFilter.class,
TestResource.class);
}
});

@Test
public void testExpectedFilters() {
List<String> responseFiltersValues = get("/test/filters")
.then().statusCode(200)
.body(Matchers.is("void-on,response-on,uni-on,always"))
.extract()
.headers()
.getList("response-filters")
.stream()
.map(Header::getValue)
.collect(Collectors.toList());
assertThat(responseFiltersValues).containsOnly("always", "void-on", "uni-on");
}

@Path("test")
public static class TestResource {

@Path("filters")
@GET
public String filters(HttpHeaders headers) {
return String.join(",", headers.getRequestHeader("request-filters"));
}
}

@IfBuildProperty(name = "notexistingproperty", stringValue = "true")
public static class WontBeEnabledFilter {

@ServerRequestFilter(priority = Priorities.USER + 1)
public void voidRequestFilter(ContainerRequestContext requestContext) {
requestContext.getHeaders().add("request-filters", "void-off");
}

@ServerRequestFilter(priority = Priorities.USER + 2)
public Response responseTypeRequestFilter(ContainerRequestContext requestContext) {
requestContext.getHeaders().add("request-filters", "response-off");
return null;
}

@ServerRequestFilter(priority = Priorities.USER + 3)
public Uni<Void> uniRequestFilter(ContainerRequestContext requestContext) {
requestContext.getHeaders().add("request-filters", "uni-off");
return Uni.createFrom().nullItem();
}

// if any of the following were to be executed, they would fail, thrown an exception and result in an HTTP 500

@ServerResponseFilter
public void voidResponseFilter(ContainerResponseContext ctx) {
assertFalse(true);
}

@ServerResponseFilter
public Uni<Void> uniResponseFilter(ContainerResponseContext ctx) {
assertFalse(true);
return Uni.createFrom().nullItem();
}
}

@IfBuildProfile("test")
public static class WillBeEnabledFilter {

@ServerRequestFilter(priority = Priorities.USER + 4)
public void voidRequestFilter(ContainerRequestContext requestContext) {
requestContext.getHeaders().add("request-filters", "void-on");
}

@ServerRequestFilter(priority = Priorities.USER + 5)
public Optional<Response> responseTypeRequestFilter(ContainerRequestContext requestContext) {
requestContext.getHeaders().add("request-filters", "response-on");
return Optional.empty();
}

@ServerRequestFilter(priority = Priorities.USER + 6)
public Uni<Response> uniRequestFilter(ContainerRequestContext requestContext) {
requestContext.getHeaders().add("request-filters", "uni-on");
return Uni.createFrom().nullItem();
}

@ServerResponseFilter(priority = Priorities.USER + 4)
public void voidResponseFilter(ContainerResponseContext ctx) {
ctx.getHeaders().add("response-filters", "void-on");
}

@ServerResponseFilter(priority = Priorities.USER + 6)
public Uni<Void> uniResponseFilter(ContainerResponseContext ctx) {
ctx.getHeaders().add("response-filters", "uni-on");
return Uni.createFrom().nullItem();
}
}

public static class AlwaysEnabledFilter {

@ServerRequestFilter(priority = Priorities.USER + 100)
public void alwaysRequestFilter(ContainerRequestContext requestContext) {
requestContext.getHeaders().add("request-filters", "always");
}

@ServerResponseFilter(priority = Priorities.USER + 100)
public void voidResponseFilter(ContainerResponseContext ctx) {
ctx.getHeaders().add("response-filters", "always");
}
}
}
Loading

0 comments on commit 0b512a3

Please sign in to comment.