|
17 | 17 | import com.amazonaws.services.lambda.runtime.Context;
|
18 | 18 | import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
|
19 | 19 | import com.fasterxml.jackson.databind.ObjectMapper;
|
| 20 | + |
20 | 21 | import java.io.IOException;
|
21 | 22 | import java.io.InputStream;
|
22 | 23 | import java.io.OutputStream;
|
23 |
| -import java.util.Map; |
| 24 | +import java.nio.charset.StandardCharsets; |
| 25 | + |
| 26 | +import org.apache.logging.log4j.LogManager; |
| 27 | +import org.apache.logging.log4j.Logger; |
24 | 28 | import software.amazon.lambda.powertools.logging.Logging;
|
25 | 29 | import software.amazon.lambda.powertools.metrics.Metrics;
|
26 | 30 |
|
| 31 | +import java.io.InputStreamReader; |
| 32 | +import java.io.BufferedReader; |
| 33 | +import java.io.BufferedWriter; |
| 34 | +import java.io.OutputStreamWriter; |
| 35 | +import java.io.PrintWriter; |
| 36 | + |
27 | 37 | public class AppStream implements RequestStreamHandler {
|
28 | 38 | private static final ObjectMapper mapper = new ObjectMapper();
|
| 39 | + private final static Logger log = LogManager.getLogger(AppStream.class); |
29 | 40 |
|
30 | 41 | @Override
|
31 | 42 | @Logging(logEvent = true)
|
32 | 43 | @Metrics(namespace = "ServerlessAirline", service = "payment", captureColdStart = true)
|
33 |
| - public void handleRequest(InputStream input, OutputStream output, Context context) throws IOException { |
34 |
| - Map map = mapper.readValue(input, Map.class); |
| 44 | + // RequestStreamHandler can be used instead of RequestHandler for cases when you'd like to deserialize request body or serialize response body yourself, instead of allowing that to happen automatically |
| 45 | + // Note that you still need to return a proper JSON for API Gateway to handle |
| 46 | + // See Lambda Response format for examples: https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html |
| 47 | + public void handleRequest(InputStream input, OutputStream output, Context context) { |
| 48 | + try (BufferedReader reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8)); |
| 49 | + PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(output, StandardCharsets.UTF_8)))) { |
35 | 50 |
|
36 |
| - System.out.println(map.size()); |
| 51 | + log.info("Received: " + mapper.writerWithDefaultPrettyPrinter().writeValueAsString(mapper.readTree(reader))); |
| 52 | + |
| 53 | + writer.write("{\"body\": \"" + System.currentTimeMillis() + "\"} "); |
| 54 | + } catch (IOException e) { |
| 55 | + log.error("Something has gone wrong: ", e); |
| 56 | + } |
37 | 57 | }
|
38 | 58 | }
|
| 59 | + |
0 commit comments