Rohan Urkude
Rohan Urkude

Reputation: 43

AWS Lambda Java Integration

I want to integrate my Saml SSO application with AWS Lambda, but unfortunately my Saml code takes its input as can be seen below in the code. I need to send HttpServletRequest and HttpServletResponse as input to my java handler. So it requires request and response as input but my lambda handler takes only input as JSON or java POJO, and I am confused as how to proceed.

    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
    //validation 
    //output
  return authentication;
    }

Upvotes: 1

Views: 1090

Answers (1)

Usman Mutawakil
Usman Mutawakil

Reputation: 5259

The AWS team has created a serverless wrapper that exposes request and response objects. This should allow you to do what you need.In their handler you implement a new interface and their underlying functionality returns the request and response to you as AwsProxyRequest and AwsProxyResponse which should be children of HttpServletRequest and HttpServletResponse.

Code

public class StreamLambdaHandler implements RequestStreamHandler {
    private SpringLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
    private Logger log = LoggerFactory.getLogger(StreamLambdaHandler.class);

    @Override
    public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context)
            throws IOException {
        if (handler == null) {
            try {
                handler = SpringLambdaContainerHandler.getAwsProxyHandler(PetStoreSpringAppConfig.class);
            } catch (ContainerInitializationException e) {
                log.error("Cannot initialize Spring container", e);
                outputStream.close();
                throw new RuntimeException(e);
            }
        }

        AwsProxyRequest request = LambdaContainerHandler.getObjectMapper().readValue(inputStream, AwsProxyRequest.class);

        AwsProxyResponse resp = handler.proxy(request, context);

        LambdaContainerHandler.getObjectMapper().writeValue(outputStream, resp);
        // just in case it wasn't closed by the mapper
        outputStream.close();
    }
}

Source -> https://github.com/awslabs/aws-serverless-java-container

Upvotes: 2

Related Questions