Skip to content

Commit 463295d

Browse files
authored
Merge pull request #29 from awslabs/cognito-user-pool
Updated README with samples
2 parents 8c08a8a + 09c848f commit 463295d

File tree

3 files changed

+81
-2
lines changed

3 files changed

+81
-2
lines changed

README.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ To include the library in your Maven project, add the desired implementation to
1818
The simplest way to run your application serverlessly is to configure [API Gateway](https://aws.amazon.com/api-gateway/) to use the
1919
[`AWS_PROXY`](http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-set-up-simple-proxy.html#api-gateway-set-up-lambda-proxy-integration-on-proxy-resource) integration type and
2020
configure your desired `LambdaContainerHandler` implementation to use `AwsProxyRequest`/`AwsProxyResponse` readers and writers. Both Spark and Jersey implementations provide static helper methods that
21-
pre-configure this for you.
21+
pre-configure this for you.
22+
23+
When using a Cognito User Pool authorizer, use the Lambda `RequestStreamHandler` instead of the POJO-based `RequestHandler` handler. An example of this is included at the bottom of this file. The POJO handler does not support Jackson annotations required for the `CognitoAuthorizerClaims` class.
2224

2325
### Jersey support
2426
The library expects to receive a valid [JAX-RS](https://jax-rs-spec.java.net) application object. For the Jersey implementation this is the `ResourceConfig` object.
@@ -153,3 +155,36 @@ handler.onStartup(c -> {
153155
// servlet name mappings are disabled and will throw an exception
154156
});
155157
```
158+
159+
# Using the Lambda Stream handler
160+
By default, Lambda does not use Jackson annotations when marshalling and unmarhsalling JSON. This can cause issues when receiving requests that include the claims object from a Cognito User Pool authorizer. To support these type of requests, use Lambda's `RequestStreamHandler` interface instead of the POJO-based `RequestHandler`. This allows you to use a custom version of Jackson with support for annotations.
161+
162+
This library uses Jackson annotations in the `com.amazonaws.serverless.proxy.internal.model.CognitoAuthorizerClaims` object. The example below shows how to do this with a `SpringLambdaContainerHandler`, you can use the same methodology with all of the other implementations.
163+
164+
```java
165+
public class StreamLambdaHandler implements RequestStreamHandler {
166+
private SpringLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
167+
private static ObjectMapper mapper = new ObjectMapper();
168+
169+
@Override
170+
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context)
171+
throws IOException {
172+
if (handler == null) {
173+
try {
174+
handler = SpringLambdaContainerHandler.getAwsProxyHandler(PetStoreSpringAppConfig.class);
175+
} catch (ContainerInitializationException e) {
176+
e.printStackTrace();
177+
outputStream.close();
178+
}
179+
}
180+
181+
AwsProxyRequest request = mapper.readValue(inputStream, AwsProxyRequest.class);
182+
183+
AwsProxyResponse resp = handler.proxy(request, context);
184+
185+
mapper.writeValue(outputStream, resp);
186+
// just in case it wasn't closed by the mapper
187+
outputStream.close();
188+
}
189+
}
190+
```

aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/model/ApiGatewayAuthorizerContext.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public class ApiGatewayAuthorizerContext {
3939
private String principalId;
4040
private CognitoAuthorizerClaims claims;
4141

42-
4342
//-------------------------------------------------------------
4443
// Methods - Public
4544
//-------------------------------------------------------------
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.amazonaws.serverless.sample.spring;
2+
3+
4+
import com.amazonaws.serverless.exceptions.ContainerInitializationException;
5+
import com.amazonaws.serverless.proxy.internal.model.AwsProxyRequest;
6+
import com.amazonaws.serverless.proxy.internal.model.AwsProxyResponse;
7+
import com.amazonaws.serverless.proxy.spring.SpringLambdaContainerHandler;
8+
import com.amazonaws.services.lambda.runtime.Context;
9+
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
10+
11+
import com.fasterxml.jackson.databind.ObjectMapper;
12+
13+
import java.io.IOException;
14+
import java.io.InputStream;
15+
import java.io.OutputStream;
16+
17+
18+
/**
19+
* Created by bulianis on 5/2/17.
20+
*/
21+
public class StreamLambdaHandler implements RequestStreamHandler {
22+
private SpringLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
23+
private static ObjectMapper mapper = new ObjectMapper();
24+
25+
@Override
26+
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context)
27+
throws IOException {
28+
if (handler == null) {
29+
try {
30+
handler = SpringLambdaContainerHandler.getAwsProxyHandler(PetStoreSpringAppConfig.class);
31+
} catch (ContainerInitializationException e) {
32+
e.printStackTrace();
33+
outputStream.close();
34+
}
35+
}
36+
37+
AwsProxyRequest request = mapper.readValue(inputStream, AwsProxyRequest.class);
38+
39+
AwsProxyResponse resp = handler.proxy(request, context);
40+
41+
mapper.writeValue(outputStream, resp);
42+
// just in case it wasn't closed by the mapper
43+
outputStream.close();
44+
}
45+
}

0 commit comments

Comments
 (0)