diff --git a/aws-serverless-java-container-jersey/src/main/java/com/amazonaws/serverless/proxy/jersey/JerseyLambdaContainerHandler.java b/aws-serverless-java-container-jersey/src/main/java/com/amazonaws/serverless/proxy/jersey/JerseyLambdaContainerHandler.java index 54a4ac0c8..576d2b66c 100644 --- a/aws-serverless-java-container-jersey/src/main/java/com/amazonaws/serverless/proxy/jersey/JerseyLambdaContainerHandler.java +++ b/aws-serverless-java-container-jersey/src/main/java/com/amazonaws/serverless/proxy/jersey/JerseyLambdaContainerHandler.java @@ -137,9 +137,21 @@ public JerseyLambdaContainerHandler(Class requestTypeClass, ((ResourceConfig)jaxRsApplication).register(new AbstractBinder() { @Override protected void configure() { - bindFactory(AwsProxyServletContextSupplier.class).to(ServletContext.class).in(RequestScoped.class); - bindFactory(AwsProxyServletRequestSupplier.class).to(HttpServletRequest.class).in(RequestScoped.class); - bindFactory(AwsProxyServletResponseSupplier.class).to(HttpServletResponse.class).in(RequestScoped.class); + bindFactory(AwsProxyServletContextSupplier.class) + .proxy(true) + .proxyForSameScope(true) + .to(ServletContext.class) + .in(RequestScoped.class); + bindFactory(AwsProxyServletRequestSupplier.class) + .proxy(true) + .proxyForSameScope(true) + .to(HttpServletRequest.class) + .in(RequestScoped.class); + bindFactory(AwsProxyServletResponseSupplier.class) + .proxy(true) + .proxyForSameScope(true) + .to(HttpServletResponse.class) + .in(RequestScoped.class); } }); } diff --git a/aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/jersey/EchoJerseyResource.java b/aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/jersey/EchoJerseyResource.java index 1b25bd924..4381e8d08 100644 --- a/aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/jersey/EchoJerseyResource.java +++ b/aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/jersey/EchoJerseyResource.java @@ -13,6 +13,7 @@ package com.amazonaws.serverless.proxy.jersey; import com.amazonaws.serverless.proxy.RequestReader; +import com.amazonaws.serverless.proxy.jersey.providers.ServletRequestFilter; import com.amazonaws.serverless.proxy.model.ApiGatewayRequestContext; import com.amazonaws.serverless.proxy.jersey.model.MapResponseModel; import com.amazonaws.serverless.proxy.jersey.model.SingleValueModel; @@ -54,6 +55,18 @@ public SingleValueModel echoDecodedParam(@QueryParam("param") String param) { return model; } + @Path("/filter-attribute") @GET + @Produces(MediaType.APPLICATION_JSON) + public SingleValueModel returnFilterAttribute(@Context HttpServletRequest req) { + SingleValueModel model = new SingleValueModel(); + if (req.getAttribute(ServletRequestFilter.FILTER_ATTRIBUTE_NAME) == null) { + model.setValue(""); + } else { + model.setValue(req.getAttribute(ServletRequestFilter.FILTER_ATTRIBUTE_NAME).toString()); + } + return model; + } + @Path("/list-query-string") @GET @Produces(MediaType.APPLICATION_JSON) public SingleValueModel echoQueryStringLength(@QueryParam("list") List param) { diff --git a/aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/jersey/JerseyAwsProxyTest.java b/aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/jersey/JerseyAwsProxyTest.java index 3187797f2..7d459359a 100644 --- a/aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/jersey/JerseyAwsProxyTest.java +++ b/aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/jersey/JerseyAwsProxyTest.java @@ -14,6 +14,7 @@ import com.amazonaws.serverless.proxy.internal.LambdaContainerHandler; +import com.amazonaws.serverless.proxy.jersey.providers.ServletRequestFilter; import com.amazonaws.serverless.proxy.model.AwsProxyRequest; import com.amazonaws.serverless.proxy.model.AwsProxyResponse; import com.amazonaws.serverless.proxy.internal.servlet.AwsServletContext; @@ -52,6 +53,7 @@ public class JerseyAwsProxyTest { private static ObjectMapper objectMapper = new ObjectMapper(); private static ResourceConfig app = new ResourceConfig().packages("com.amazonaws.serverless.proxy.jersey") .register(LoggingFeature.class) + .register(ServletRequestFilter.class) .property(LoggingFeature.LOGGING_FEATURE_VERBOSITY_SERVER, LoggingFeature.Verbosity.PAYLOAD_ANY); private static JerseyLambdaContainerHandler handler = JerseyLambdaContainerHandler.getAwsProxyHandler(app); @@ -122,6 +124,19 @@ public void requestScheme_valid_expectHttps() { validateSingleValueModel(output, "https"); } + @Test + public void requestFilter_injectsServletRequest_expectCustomAttribute() { + AwsProxyRequest request = new AwsProxyRequestBuilder("/echo/filter-attribute", "GET") + .json() + .build(); + + AwsProxyResponse output = handler.proxy(request, lambdaContext); + assertEquals(200, output.getStatusCode()); + assertEquals("application/json", output.getMultiValueHeaders().getFirst("Content-Type")); + + validateSingleValueModel(output, ServletRequestFilter.FILTER_ATTRIBUTE_VALUE); + } + @Test public void authorizer_securityContext_customPrincipalSuccess() { AwsProxyRequest request = new AwsProxyRequestBuilder("/echo/authorizer-principal", "GET") diff --git a/aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/jersey/providers/ServletRequestFilter.java b/aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/jersey/providers/ServletRequestFilter.java new file mode 100644 index 000000000..70d65a64c --- /dev/null +++ b/aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/jersey/providers/ServletRequestFilter.java @@ -0,0 +1,22 @@ +package com.amazonaws.serverless.proxy.jersey.providers; + + +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.container.ContainerRequestContext; +import javax.ws.rs.container.ContainerRequestFilter; +import javax.ws.rs.core.Context; + +import java.io.IOException; + + +public class ServletRequestFilter implements ContainerRequestFilter { + public static final String FILTER_ATTRIBUTE_NAME = "ServletFilter"; + public static final String FILTER_ATTRIBUTE_VALUE = "done"; + + @Context HttpServletRequest request; + + public void filter(ContainerRequestContext ctx) throws IOException { + request.setAttribute(FILTER_ATTRIBUTE_NAME, FILTER_ATTRIBUTE_VALUE); + } + +} \ No newline at end of file