com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent Java Examples

The following examples show how to use com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent. 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 vote down vote up
@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 vote down vote up
@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: AbstractMicronautLambdaRuntime.java    From micronaut-aws with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param handlerResponse Handler response object
 * @return If the handlerResponseType and the responseType are identical just returns the supplied object. However,
 * if the response type is of type {@link APIGatewayProxyResponseEvent} it attempts to serialized the handler response
 * as a JSON String and set it in the response body. If the object cannot be serialized, a 400 response is returned
 */
@Nullable
protected ResponseType createResponse(HandlerResponseType handlerResponse) {
    if (handlerResponseType == responseType) {
        logn(LogLevel.TRACE, "HandlerResponseType and ResponseType are identical");
        return (ResponseType) handlerResponse;

    } else if (responseType == APIGatewayProxyResponseEvent.class) {
        logn(LogLevel.TRACE, "response type is APIGatewayProxyResponseEvent");
        String json = serializeAsJsonString(handlerResponse);
        if (json != null) {
            return (ResponseType) respond(HttpStatus.OK, json, MediaType.APPLICATION_JSON);
        }
        return (ResponseType) respond(HttpStatus.BAD_REQUEST,
                "Could not serialize " + handlerResponse.toString() + " as json",
                MediaType.TEXT_PLAIN);
    }
    return null;
}
 
Example #4
Source File: SpringBootApiGatewayRequestHandler.java    From spring-cloud-function with Apache License 2.0 6 votes vote down vote up
@Override
protected APIGatewayProxyResponseEvent convertOutput(Object output) {
	if (functionReturnsMessage(output)) {
		Message<?> message = (Message<?>) output;
		return new APIGatewayProxyResponseEvent()
				.withStatusCode((Integer) message.getHeaders()
						.getOrDefault("statuscode", HttpStatus.OK.value()))
				.withHeaders(toResponseHeaders(message.getHeaders()))
				.withBody(serializeBody(message.getPayload()));
	}
	else {
		return new APIGatewayProxyResponseEvent()
				.withStatusCode(HttpStatus.OK.value())
				.withBody(serializeBody(output));

	}
}
 
Example #5
Source File: SpringBootApiGatewayRequestHandlerTests.java    From spring-cloud-function with Apache License 2.0 6 votes vote down vote up
@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 #6
Source File: SpringBootApiGatewayRequestHandlerTests.java    From spring-cloud-function with Apache License 2.0 6 votes vote down vote up
@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 #7
Source File: AbstractMicronautLambdaRuntime.java    From micronaut-aws with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param status HTTP Status of the response
 * @param body Body of the response
 * @param contentType HTTP Header Content-Type value
 * @return a {@link APIGatewayProxyResponseEvent} populated with the supplied status, body and content type
 */
protected APIGatewayProxyResponseEvent respond(HttpStatus status, String body, String contentType) {
    APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent();
    Map<String, String> headers = new HashMap<>();
    headers.put(HttpHeaders.CONTENT_TYPE, contentType);
    response.setHeaders(headers);
    response.setBody(body);
    response.setStatusCode(status.getCode());
    logn(LogLevel.TRACE, "response: " + status.getCode() + " content type: " + headers.get(HttpHeaders.CONTENT_TYPE) + " message " + body);
    return response;
}
 
Example #8
Source File: HelloHandler.java    From Cloud-Native-Applications-in-Java with MIT License 5 votes vote down vote up
@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 #9
Source File: HelloHandlerTest.java    From Cloud-Native-Applications-in-Java with MIT License 5 votes vote down vote up
@Test
public void handleEmptyRequest() {
  input.withPathParamters(Collections.emptyMap());
  APIGatewayProxyResponseEvent res = handler.handleRequest(input, null);
  assertNotNull(res);
  assertEquals("Hello World!", res.getBody());
}
 
Example #10
Source File: SpringBootApiGatewayRequestHandler.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@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 #11
Source File: SpringBootApiGatewayRequestHandlerTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@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 #12
Source File: SpringBootApiGatewayRequestHandlerTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@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 #13
Source File: SpringBootApiGatewayRequestHandlerTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@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 #14
Source File: SpringBootApiGatewayRequestHandlerTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@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 #15
Source File: HelloHandlerTest.java    From Cloud-Native-Applications-in-Java with MIT License 4 votes vote down vote up
@Test
public void handleRequest() {
  APIGatewayProxyResponseEvent res = handler.handleRequest(input, null);
  assertNotNull(res);
  assertEquals("Hello Universe!", res.getBody());
}