Skip to content

Commit

Permalink
Take @ConstrainedTo into account for interceptors
Browse files Browse the repository at this point in the history
Without this change interceptors meant to be
used on the client were being used on the
server as well
  • Loading branch information
geoand authored and gsmet committed Oct 24, 2023
1 parent d5602cb commit 8207192
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.Set;
import java.util.function.Function;

import jakarta.ws.rs.RuntimeType;
import jakarta.ws.rs.container.ContainerRequestFilter;
import jakarta.ws.rs.container.ContainerResponseFilter;

Expand All @@ -24,6 +25,7 @@
import org.jboss.resteasy.reactive.common.model.ResourceInterceptor;
import org.jboss.resteasy.reactive.common.model.ResourceInterceptors;
import org.jboss.resteasy.reactive.common.processor.NameBindingUtil;
import org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames;
import org.jboss.resteasy.reactive.spi.BeanFactory;

/**
Expand Down Expand Up @@ -221,6 +223,18 @@ private static <T> ResourceInterceptor<T> handleDiscoveredInterceptor(
interceptorContainer.addNameRequestInterceptor(interceptor);
}
}

RuntimeType runtimeType = null;
if (keepProviderResult == ApplicationScanningResult.KeepProviderResult.SERVER_ONLY) {
runtimeType = RuntimeType.SERVER;
}
AnnotationInstance constrainedToInstance = filterClass
.declaredAnnotation(ResteasyReactiveDotNames.CONSTRAINED_TO);
if (constrainedToInstance != null) {
runtimeType = RuntimeType.valueOf(constrainedToInstance.value().asEnum());
}
interceptor.setRuntimeType(runtimeType);

return interceptor;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Set;

import jakarta.ws.rs.Priorities;
import jakarta.ws.rs.RuntimeType;

import org.jboss.resteasy.reactive.spi.BeanFactory;

Expand All @@ -25,7 +26,9 @@ public class ResourceInterceptor<T>

private String className;

public transient Map<String, Object> metadata; // by using 'public transient' we ensure that this field will not be populated at runtime
public transient Map<String, Object> metadata; // by using 'public transient' we ensure that this field will not be populated at runtime \

private RuntimeType runtimeType;

public void setFactory(BeanFactory<T> factory) {
this.factory = factory;
Expand Down Expand Up @@ -84,6 +87,14 @@ public void setWithFormRead(boolean withFormRead) {
this.withFormRead = withFormRead;
}

public RuntimeType getRuntimeType() {
return runtimeType;
}

public void setRuntimeType(RuntimeType runtimeType) {
this.runtimeType = runtimeType;
}

// spec says that writer interceptors are sorted in ascending order
@Override
public int compareTo(ResourceInterceptor<T> o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.TreeMap;
import java.util.function.Consumer;

import jakarta.ws.rs.RuntimeType;
import jakarta.ws.rs.container.ContainerRequestFilter;
import jakarta.ws.rs.container.ContainerResponseFilter;
import jakarta.ws.rs.container.DynamicFeature;
Expand Down Expand Up @@ -152,6 +153,9 @@ private <T> LinkedHashMap<ResourceInterceptor<T>, T> createInterceptorInstances(
List<BeanFactory.BeanInstance<T>> responseBeanInstances = new ArrayList<>(interceptors.size());
Collections.sort(interceptors);
for (ResourceInterceptor<T> interceptor : interceptors) {
if (RuntimeType.CLIENT.equals(interceptor.getRuntimeType())) {
continue;
}
BeanFactory.BeanInstance<T> beanInstance = interceptor.getFactory().createInstance();
responseBeanInstances.add(beanInstance);
T containerResponseFilter = beanInstance.getInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import java.io.IOException;
import java.util.Date;

import jakarta.ws.rs.ConstrainedTo;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.RuntimeType;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
Expand All @@ -28,7 +30,7 @@ public class CustomHeadersAndWriterInterceptorTest {
@RegisterExtension
static ResteasyReactiveUnitTest runner = new ResteasyReactiveUnitTest()
.withApplicationRoot((jar) -> jar
.addClasses(TestResource.class, DummyWriterInterceptor.class));
.addClasses(TestResource.class, DummyWriterInterceptor.class, FailingWriterInterceptor.class));

@Test
void testResponseHeaders() {
Expand Down Expand Up @@ -61,4 +63,14 @@ public void aroundWriteTo(WriterInterceptorContext context) throws IOException,
}
}

@Provider
@ConstrainedTo(RuntimeType.CLIENT)
public static class FailingWriterInterceptor implements WriterInterceptor {

@Override
public void aroundWriteTo(WriterInterceptorContext context) throws WebApplicationException {
throw new RuntimeException("this is never called");
}
}

}

0 comments on commit 8207192

Please sign in to comment.