com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent Java Examples
The following examples show how to use
com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FunctionConfiguration.java From blog-tutorials with MIT License | 8 votes |
@Bean public Function<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> processXmlOrder() { return value -> { try { ObjectMapper objectMapper = new XmlMapper(); Order order = objectMapper.readValue(value.getBody(), Order.class); logger.info("Successfully deserialized XML order '{}'", order); // ... processing Order order.setProcessed(true); APIGatewayProxyResponseEvent responseEvent = new APIGatewayProxyResponseEvent(); responseEvent.setStatusCode(201); responseEvent.setHeaders(Map.of("Content-Type", "application/xml")); responseEvent.setBody(objectMapper.writeValueAsString(order)); return responseEvent; } catch (IOException e) { e.printStackTrace(); return new APIGatewayProxyResponseEvent().withStatusCode(500); } }; }
Example #2
Source File: SimpleHttpHandler.java From blog-tutorials with MIT License | 7 votes |
@Override public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) { System.out.println(input.getBody()); APIGatewayProxyResponseEvent responseEvent = new APIGatewayProxyResponseEvent(); responseEvent.setBody("{\"name\": \"duke\"}"); responseEvent.setStatusCode(200); Map<String, String> headers = new HashMap(); headers.put("X-Custom-Header", "Hello World from Serverless!"); responseEvent.setHeaders(headers); return responseEvent; }
Example #3
Source File: SpringBootApiGatewayRequestHandlerTests.java From spring-cloud-function with Apache License 2.0 | 6 votes |
@Test public void functionMessageBean() { this.handler = new SpringBootApiGatewayRequestHandler( FunctionMessageConfig.class); APIGatewayProxyRequestEvent request = new APIGatewayProxyRequestEvent(); request.setBody("{\"value\":\"foo\"}"); Object output = this.handler.handleRequest(request, null); assertThat(output).isInstanceOf(APIGatewayProxyResponseEvent.class); assertThat(((APIGatewayProxyResponseEvent) output).getStatusCode()) .isEqualTo(200); assertThat(((APIGatewayProxyResponseEvent) output).getHeaders().get("spring")) .isEqualTo("cloud"); assertThat(((APIGatewayProxyResponseEvent) output).getBody()) .isEqualTo("{\"value\":\"FOO\"}"); }
Example #4
Source File: SpringBootApiGatewayRequestHandlerTests.java From spring-cloud-function with Apache License 2.0 | 6 votes |
@Test public void functionBean() { System.setProperty("function.name", "function"); this.handler = new SpringBootApiGatewayRequestHandler(FunctionConfig.class); APIGatewayProxyRequestEvent request = new APIGatewayProxyRequestEvent(); request.setBody("{\"value\":\"foo\"}"); Object output = this.handler.handleRequest(request, null); assertThat(output).isInstanceOf(APIGatewayProxyResponseEvent.class); assertThat(((APIGatewayProxyResponseEvent) output).getStatusCode()) .isEqualTo(200); assertThat(((APIGatewayProxyResponseEvent) output).getBody()) .isEqualTo("{\"value\":\"FOO\"}"); APIGatewayProxyRequestEvent bodyEncryptedRequest = new APIGatewayProxyRequestEvent(); bodyEncryptedRequest.setBody( Base64.getEncoder().encodeToString("{\"value\":\"foo\"}".getBytes())); bodyEncryptedRequest.setIsBase64Encoded(true); output = this.handler.handleRequest(bodyEncryptedRequest, null); assertThat(output).isInstanceOf(APIGatewayProxyResponseEvent.class); assertThat(((APIGatewayProxyResponseEvent) output).getStatusCode()) .isEqualTo(200); assertThat(((APIGatewayProxyResponseEvent) output).getBody()) .isEqualTo("{\"value\":\"FOO\"}"); }
Example #5
Source File: SpringBootApiGatewayRequestHandlerTests.java From spring-cloud-function with Apache License 2.0 | 5 votes |
@Test public void functionMessageBeanWithEmptyResponse() { this.handler = new SpringBootApiGatewayRequestHandler( FunctionMessageConsumerConfig.class); APIGatewayProxyRequestEvent request = new APIGatewayProxyRequestEvent(); Object output = this.handler.handleRequest(request, null); assertThat(output).isInstanceOf(APIGatewayProxyResponseEvent.class); assertThat(((APIGatewayProxyResponseEvent) output).getStatusCode()) .isEqualTo(200); assertThat(((APIGatewayProxyResponseEvent) output).getBody()).isNull(); }
Example #6
Source File: AbstractMicronautLambdaRuntime.java From micronaut-aws with Apache License 2.0 | 5 votes |
/** * * @param request Request obtained from the Runtime API * @return if the request and the handler type are the same, just return the request, if the request is of type {@link APIGatewayProxyRequestEvent} it attempts to build an object of type HandlerRequestType with the body of the request, else returns {@code null} * @throws JsonProcessingException if underlying request body contains invalid content * @throws JsonMappingException if the request body JSON structure does not match structure * expected for result type (or has other mismatch issues) */ @Nullable protected HandlerRequestType createHandlerRequest(RequestType request) throws JsonProcessingException, JsonMappingException { if (requestType == handlerRequestType) { return (HandlerRequestType) request; } else if (request instanceof APIGatewayProxyRequestEvent) { logn(LogLevel.TRACE, "request of type APIGatewayProxyRequestEvent"); String content = ((APIGatewayProxyRequestEvent) request).getBody(); return valueFromContent(content, handlerRequestType); } logn(LogLevel.TRACE, "createHandlerRequest return null"); return null; }
Example #7
Source File: SpringBootApiGatewayRequestHandlerTests.java From spring-cloud-function with Apache License 2.0 | 5 votes |
@Test public void functionMessageBeanWithRequestParameters() { this.handler = new SpringBootApiGatewayRequestHandler( FunctionMessageEchoReqParametersConfig.class); APIGatewayProxyRequestEvent request = new APIGatewayProxyRequestEvent(); request.setPathParameters(Collections.singletonMap("path", "pathValue")); request.setQueryStringParameters(Collections.singletonMap("query", "queryValue")); request.setHeaders(Collections.singletonMap("test-header", "headerValue")); request.setHttpMethod("GET"); Object output = this.handler.handleRequest(request, null); assertThat(output).isInstanceOf(APIGatewayProxyResponseEvent.class); assertThat(((APIGatewayProxyResponseEvent) output).getStatusCode()) .isEqualTo(200); assertThat(((APIGatewayProxyResponseEvent) output).getHeaders().get("path")) .isEqualTo("pathValue"); assertThat(((APIGatewayProxyResponseEvent) output).getHeaders().get("query")) .isEqualTo("queryValue"); assertThat( ((APIGatewayProxyResponseEvent) output).getHeaders().get("test-header")) .isEqualTo("headerValue"); assertThat(((APIGatewayProxyResponseEvent) output).getHeaders().get("httpMethod")) .isEqualTo("GET"); assertThat(((APIGatewayProxyResponseEvent) output).getBody()) .isEqualTo("{\"value\":\"body\"}"); }
Example #8
Source File: SpringBootApiGatewayRequestHandlerTests.java From spring-cloud-function with Apache License 2.0 | 5 votes |
@Test public void consumerBean() { System.setProperty("function.name", "consumer"); this.handler = new SpringBootApiGatewayRequestHandler(FunctionConfig.class); APIGatewayProxyRequestEvent request = new APIGatewayProxyRequestEvent(); request.setBody("\"strVal\":\"test for consumer\""); Object output = this.handler.handleRequest(request, null); assertThat(output).isInstanceOf(APIGatewayProxyResponseEvent.class); assertThat(((APIGatewayProxyResponseEvent) output).getStatusCode()) .isEqualTo(200); }
Example #9
Source File: SpringBootApiGatewayRequestHandlerTests.java From spring-cloud-function with Apache License 2.0 | 5 votes |
@Test public void supplierBean() { System.setProperty("function.name", "supplier"); this.handler = new SpringBootApiGatewayRequestHandler(FunctionConfig.class); APIGatewayProxyRequestEvent request = new APIGatewayProxyRequestEvent(); Object output = this.handler.handleRequest(request, null); assertThat(output).isInstanceOf(APIGatewayProxyResponseEvent.class); assertThat(((APIGatewayProxyResponseEvent) output).getStatusCode()) .isEqualTo(200); assertThat(((APIGatewayProxyResponseEvent) output).getBody()) .isEqualTo("\"hello!\""); }
Example #10
Source File: FunctionInvoker.java From spring-cloud-function with Apache License 2.0 | 5 votes |
@SuppressWarnings({ "unchecked", "rawtypes" }) @Override public void handleRequest(InputStream input, OutputStream output, Context context) throws IOException { Message requestMessage = this.generateMessage(input, context); Message<byte[]> responseMessage = (Message<byte[]>) this.function.apply(requestMessage); byte[] responseBytes = responseMessage.getPayload(); if (requestMessage.getHeaders().containsKey("httpMethod") || requestMessage.getPayload() instanceof APIGatewayProxyRequestEvent) { // API Gateway Map<String, Object> response = new HashMap<String, Object>(); response.put("isBase64Encoded", false); MessageHeaders headers = responseMessage.getHeaders(); int statusCode = headers.containsKey("statusCode") ? (int) headers.get("statusCode") : 200; response.put("statusCode", statusCode); if (isKinesis(requestMessage)) { HttpStatus httpStatus = HttpStatus.valueOf(statusCode); response.put("statusDescription", httpStatus.toString()); } String body = new String(responseMessage.getPayload(), StandardCharsets.UTF_8).replaceAll("\"", ""); response.put("body", body); Map<String, String> responseHeaders = new HashMap<>(); headers.keySet().forEach(key -> responseHeaders.put(key, headers.get(key).toString())); response.put("headers", responseHeaders); responseBytes = mapper.writeValueAsBytes(response); } StreamUtils.copy(responseBytes, output); }
Example #11
Source File: SpringBootApiGatewayRequestHandler.java From spring-cloud-function with Apache License 2.0 | 5 votes |
@Override public Object handleRequest(APIGatewayProxyRequestEvent event, Context context) { Object response = super.handleRequest(event, context); if (returnsOutput()) { return response; } else { return new APIGatewayProxyResponseEvent() .withStatusCode(HttpStatus.OK.value()); } }
Example #12
Source File: SpringBootApiGatewayRequestHandler.java From spring-cloud-function with Apache License 2.0 | 5 votes |
private MessageHeaders getHeaders(APIGatewayProxyRequestEvent event) { Map<String, Object> headers = new HashMap<>(); if (event.getHeaders() != null) { headers.putAll(event.getHeaders()); } if (event.getQueryStringParameters() != null) { headers.putAll(event.getQueryStringParameters()); } if (event.getPathParameters() != null) { headers.putAll(event.getPathParameters()); } headers.put("httpMethod", event.getHttpMethod()); headers.put("request", event); return new MessageHeaders(headers); }
Example #13
Source File: SpringBootApiGatewayRequestHandler.java From spring-cloud-function with Apache License 2.0 | 5 votes |
private Object deserializeBody(APIGatewayProxyRequestEvent event) { try { return this.mapper.readValue( (event.getIsBase64Encoded() != null && event.getIsBase64Encoded()) ? new String(Base64.decodeBase64(event.getBody())) : event.getBody(), getInputType()); } catch (Exception e) { throw new IllegalStateException("Cannot convert event", e); } }
Example #14
Source File: SpringBootApiGatewayRequestHandler.java From spring-cloud-function with Apache License 2.0 | 5 votes |
@Override protected Object convertEvent(APIGatewayProxyRequestEvent event) { Object deserializedBody = event.getBody() != null ? deserializeBody(event) : Optional.empty(); return functionAcceptsMessage() ? new GenericMessage<>(deserializedBody, getHeaders(event)) : deserializedBody; }
Example #15
Source File: HelloHandler.java From Cloud-Native-Applications-in-Java with MIT License | 5 votes |
@Override public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent request, Context context) { String who = "World"; if ( request.getPathParameters() != null ) { String name = request.getPathParameters().get("name"); if ( name != null && !"".equals(name.trim()) ) { who = name; } } return new APIGatewayProxyResponseEvent().withStatusCode(HttpURLConnection.HTTP_OK).withBody(String.format("Hello %s!", who)); }
Example #16
Source File: FunctionInvokerTests.java From spring-cloud-function with Apache License 2.0 | 4 votes |
@Bean public Function<APIGatewayProxyRequestEvent, String> inputApiEvent() { return v -> { return v.getBody(); }; }
Example #17
Source File: FunctionInvokerTests.java From spring-cloud-function with Apache License 2.0 | 4 votes |
@Bean public Function<Message<APIGatewayProxyRequestEvent>, String> inputApiEventAsMessage() { return v -> { return v.getPayload().getBody(); }; }
Example #18
Source File: HelloHandlerTest.java From Cloud-Native-Applications-in-Java with MIT License | 4 votes |
@Before public void setUp() throws Exception { handler = new HelloHandler(); Map<String, String> pathParams = new HashMap<>(); pathParams.put("name", "Universe"); input = new APIGatewayProxyRequestEvent().withPath("/hello").withPathParamters(pathParams); }