Skip to content

Commit

Permalink
Added proxiable property to ServletRequest, Context, and Response sup…
Browse files Browse the repository at this point in the history
…pliers to allow injection in a request scope. Addresses issue #211
  • Loading branch information
sapessi committed Dec 20, 2018
1 parent 2be72a5 commit 3d8b655
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,21 @@ public JerseyLambdaContainerHandler(Class<RequestType> 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);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> param) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<AwsProxyRequest, AwsProxyResponse> handler = JerseyLambdaContainerHandler.getAwsProxyHandler(app);

Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}

0 comments on commit 3d8b655

Please sign in to comment.