Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public class LambdaHandler implements RequestHandler<AwsProxyRequest, AwsProxyRe
}
```

## Security context
# Security context
The `aws-serverless-java-container-core` contains a default implementation of the `SecurityContextWriter` that supports API Gateway's proxy integration. The generated security context uses the API Gateway `$context` object to establish the request security context. The context looks for the following values in order and returns the first matched type:

1. Cognito My User Pools
Expand All @@ -98,7 +98,7 @@ The String values for these are exposed as static variables in the `AwsProxySecu
2. `AUTH_SCHEME_CUSTOM`
3. `AUTH_SCHEME_IAM`

## Supporting other event types
# Supporting other event types
The `RequestReader` and `ResponseWriter` interfaces in the core package can be used to support event types and generate different responses. For example, ff you have configured mapping templates in
API Gateway to create a custom event body or response you can create your own implementation of the `RequestReader` and `ResponseWriter` to handle these.

Expand All @@ -115,23 +115,26 @@ JerseyLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler =
jaxRsApplication);
```

## Jersey Servlet request
The `aws-serverless-java-container-jersey` includes a Jersey factory class to produce `HttpServletRequest` objects for your methods. First, you will need to register the factory with your Jersey application.
# Jersey Servlet injection
The `aws-serverless-java-container-jersey` includes Jersey factory classes to produce `HttpServletRequest` and `ServletContext` objects for your methods. First, you will need to register the factory with your Jersey application.

```java
ResourceConfig app = new ResourceConfig()
.packages("com.amazonaws.serverless.proxy.test.jersey")
.register(new AbstractBinder() {
@Override
protected void configure() {
bindFactory(JerseyAwsProxyServletRequestFactory.class)
bindFactory(AwsProxyServletRequestFactory.class)
.to(HttpServletRequest.class)
.in(RequestScoped.class);
bindFactory(AwsProxyServletContextFactory.class)
.to(ServletContext.class)
.in(RequestScoped.class);
}
});
```

Once the factory is registered, you can receive `HttpServletRequest` objects in your methods.
Once the factory is registered, you can receive `HttpServletRequest` and `ServletContext` objects in your methods using the `@Context` annotation.

```java
@Path("/my-servlet") @GET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,25 +91,29 @@ public boolean containsHeader(String s) {

@Override
public String encodeURL(String s) {
return URLEncoder.encode(s);
// We do not support session tracking using the URL right now, we do not encode urls
return s;
}


@Override
public String encodeRedirectURL(String s) {
return URLEncoder.encode(s);
// Return the URL without changing it, we do not support session tracking using URLs
return s;
}


@Override
@Deprecated
public String encodeUrl(String s) {
return URLEncoder.encode(s);
return this.encodeURL(s);
}


@Override
@Deprecated
public String encodeRedirectUrl(String s) {
return URLEncoder.encode(s);
return this.encodeRedirectURL(s);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,12 @@ protected Class<? extends AwsProxyRequest> getRequestClass() {
// Methods - Package
//-------------------------------------------------------------

static AwsProxyRequest getCurrentRequest() {
public static AwsProxyRequest getCurrentRequest() {
return currentRequest;
}


static Context getCurrentLambdaContext() {
public static Context getCurrentLambdaContext() {
return currentLambdaContext;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance
* with the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package com.amazonaws.serverless.proxy.jersey.factory;


import org.glassfish.hk2.api.Factory;

import javax.servlet.ServletContext;


/**
* Implementation of Jersey's <code>Factory</code> object for <code>ServletContext</code> objects. This can be used
* by Jersey to generate a Servlet context given an <code>AwsProxyRequest</code> event.
*
* <pre>
* <code>
* ResourceConfig app = new ResourceConfig().packages("my.app.package")
* .register(new AbstractBinder() {
* {@literal @}Override
* protected void configure() {
* bindFactory(AwsProxyServletContextFactory.class)
* .to(ServletContext.class)
* .in(RequestScoped.class);
* }
* });
* </code>
* </pre>
*/
public class AwsProxyServletContextFactory implements Factory<ServletContext> {
@Override
public ServletContext provide() {
return AwsProxyServletRequestFactory.getRequest().getServletContext();
}


@Override
public void dispose(ServletContext servletContext) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package com.amazonaws.serverless.proxy.jersey;
package com.amazonaws.serverless.proxy.jersey.factory;


import com.amazonaws.serverless.exceptions.InvalidRequestEventException;
import com.amazonaws.serverless.proxy.internal.AwsProxySecurityContextWriter;
import com.amazonaws.serverless.proxy.internal.servlet.AwsProxyHttpServletRequest;
import com.amazonaws.serverless.proxy.internal.servlet.AwsProxyHttpServletRequestReader;
import com.amazonaws.serverless.proxy.jersey.JerseyAwsProxyRequestReader;

import org.glassfish.hk2.api.Factory;

import javax.servlet.http.HttpServletRequest;
Expand All @@ -29,30 +33,42 @@
* .register(new AbstractBinder() {
* {@literal @}Override
* protected void configure() {
* bindFactory(JerseyAwsProxyServletRequestFactory.class)
* bindFactory(AwsProxyServletRequestFactory.class)
* .to(HttpServletRequest.class)
* .in(RequestScoped.class);
* }
* });
* </code>
* </pre>
*/
public class JerseyAwsProxyServletRequestFactory
public class AwsProxyServletRequestFactory
implements Factory<HttpServletRequest> {

private static AwsProxyHttpServletRequestReader requestReader = new AwsProxyHttpServletRequestReader();

//-------------------------------------------------------------
// Implementation - Factory
//-------------------------------------------------------------

@Override
public HttpServletRequest provide() {
return new AwsProxyHttpServletRequest(JerseyAwsProxyRequestReader.getCurrentRequest(),
JerseyAwsProxyRequestReader.getCurrentLambdaContext(),
AwsProxySecurityContextWriter.getCurrentContext());
return getRequest();
}


@Override
public void dispose(HttpServletRequest httpServletRequest) {
}

public static HttpServletRequest getRequest() {
try {
return requestReader.readRequest(JerseyAwsProxyRequestReader.getCurrentRequest(),
AwsProxySecurityContextWriter.getCurrentContext(),
JerseyAwsProxyRequestReader.getCurrentLambdaContext());
} catch (InvalidRequestEventException e) {
e.printStackTrace();
return null;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.amazonaws.serverless.proxy.test.jersey.model.MapResponseModel;
import com.amazonaws.serverless.proxy.test.jersey.model.SingleValueModel;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*;
import javax.ws.rs.container.ContainerRequestContext;
Expand Down Expand Up @@ -57,6 +58,15 @@ public MapResponseModel echoServletHeaders(@Context HttpServletRequest context)
return headers;
}

@Path("/servlet-context") @GET
@Produces(MediaType.APPLICATION_JSON)
public SingleValueModel echoContextInformation(@Context ServletContext context) {
SingleValueModel singleValueModel = new SingleValueModel();
singleValueModel.setValue(context.getServerInfo());

return singleValueModel;
}

@Path("/query-string") @GET
@Produces(MediaType.APPLICATION_JSON)
public MapResponseModel echoQueryString(@Context UriInfo context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

import com.amazonaws.serverless.proxy.internal.model.AwsProxyRequest;
import com.amazonaws.serverless.proxy.internal.model.AwsProxyResponse;
import com.amazonaws.serverless.proxy.jersey.JerseyAwsProxyServletRequestFactory;
import com.amazonaws.serverless.proxy.internal.servlet.AwsServletContext;
import com.amazonaws.serverless.proxy.jersey.factory.AwsProxyServletContextFactory;
import com.amazonaws.serverless.proxy.jersey.factory.AwsProxyServletRequestFactory;
import com.amazonaws.serverless.proxy.jersey.JerseyLambdaContainerHandler;
import com.amazonaws.serverless.proxy.test.jersey.model.MapResponseModel;
import com.amazonaws.serverless.proxy.test.jersey.model.SingleValueModel;
Expand All @@ -31,6 +33,7 @@
import org.glassfish.jersey.server.ResourceConfig;
import org.junit.Test;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import java.io.IOException;
Expand All @@ -52,9 +55,12 @@ public class JerseyAwsProxyTest {
.register(new AbstractBinder() {
@Override
protected void configure() {
bindFactory(JerseyAwsProxyServletRequestFactory.class)
bindFactory(AwsProxyServletRequestFactory.class)
.to(HttpServletRequest.class)
.in(RequestScoped.class);
bindFactory(AwsProxyServletContextFactory.class)
.to(ServletContext.class)
.in(RequestScoped.class);
}
});
private static JerseyLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler = JerseyLambdaContainerHandler.getAwsProxyHandler(app);
Expand Down Expand Up @@ -89,6 +95,16 @@ public void headers_servletRequest_echo() {
validateMapResponseModel(output);
}

@Test
public void context_serverInfo_correctContext() {
AwsProxyRequest request = new AwsProxyRequestBuilder("/echo/servlet-context", "GET").build();
AwsProxyResponse output = handler.proxy(request, lambdaContext);
assertEquals(200, output.getStatusCode());
assertEquals("application/json", output.getHeaders().get("Content-Type"));

validateSingleValueModel(output, AwsServletContext.SERVER_INFO);
}

@Test
public void queryString_uriInfo_echo() {
AwsProxyRequest request = new AwsProxyRequestBuilder("/echo/query-string", "GET")
Expand Down