Polishing 0df050ec32
This commit is contained in:
@@ -161,10 +161,10 @@ public class FunctionInvoker implements RequestStreamHandler {
|
|||||||
byte[] payload = StreamUtils.copyToByteArray(input);
|
byte[] payload = StreamUtils.copyToByteArray(input);
|
||||||
|
|
||||||
if (logger.isInfoEnabled()) {
|
if (logger.isInfoEnabled()) {
|
||||||
logger.info("===> Incoming JSON for ApiGateway Event: " + new String(payload));
|
logger.info("Incoming JSON for ApiGateway Event: " + new String(payload));
|
||||||
}
|
}
|
||||||
|
|
||||||
Message message = null;
|
MessageBuilder messageBuilder = null;
|
||||||
Object request = this.mapper.readValue(payload, Object.class);
|
Object request = this.mapper.readValue(payload, Object.class);
|
||||||
Type inputType = FunctionTypeUtils.getInputType(function.getFunctionType(), 0);
|
Type inputType = FunctionTypeUtils.getInputType(function.getFunctionType(), 0);
|
||||||
if (FunctionTypeUtils.isMessage(inputType)) {
|
if (FunctionTypeUtils.isMessage(inputType)) {
|
||||||
@@ -178,39 +178,29 @@ public class FunctionInvoker implements RequestStreamHandler {
|
|||||||
Assert.isTrue(inputType instanceof Class && KinesisEvent.class.isAssignableFrom((Class<?>) inputType) || mapInputType,
|
Assert.isTrue(inputType instanceof Class && KinesisEvent.class.isAssignableFrom((Class<?>) inputType) || mapInputType,
|
||||||
"Only KinesisEvent or Map type is supported as input type for functions that accept with Kinesis Event");
|
"Only KinesisEvent or Map type is supported as input type for functions that accept with Kinesis Event");
|
||||||
Object event = mapInputType ? requestMap : this.mapper.convertValue(requestMap, KinesisEvent.class);
|
Object event = mapInputType ? requestMap : this.mapper.convertValue(requestMap, KinesisEvent.class);
|
||||||
message = MessageBuilder.withPayload(event).setHeader("aws-context", context).build();
|
messageBuilder = MessageBuilder.withPayload(event);
|
||||||
}
|
}
|
||||||
else if (requestMap.containsKey("httpMethod")) {
|
else if (requestMap.containsKey("httpMethod")) { // API Gateway
|
||||||
logger.info("Incoming request is API Gateway");
|
logger.info("Incoming request is API Gateway");
|
||||||
if (inputType.getTypeName().endsWith(APIGatewayProxyRequestEvent.class.getSimpleName())) {
|
if (inputType.getTypeName().endsWith(APIGatewayProxyRequestEvent.class.getSimpleName())) {
|
||||||
APIGatewayProxyRequestEvent gatewayEvent = this.mapper.convertValue(requestMap, APIGatewayProxyRequestEvent.class);
|
APIGatewayProxyRequestEvent gatewayEvent = this.mapper.convertValue(requestMap, APIGatewayProxyRequestEvent.class);
|
||||||
message = MessageBuilder.withPayload(gatewayEvent).setHeader("aws-context", context).build();
|
messageBuilder = MessageBuilder.withPayload(gatewayEvent);
|
||||||
}
|
}
|
||||||
else if (mapInputType) {
|
else if (mapInputType) {
|
||||||
message = MessageBuilder.withPayload(requestMap)
|
messageBuilder = MessageBuilder.withPayload(requestMap)
|
||||||
.setHeader("httpMethod", requestMap.get("httpMethod"))
|
.setHeader("httpMethod", requestMap.get("httpMethod"));
|
||||||
.setHeader("aws-context", context)
|
|
||||||
.build();
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Object body = requestMap.remove("body");
|
Object body = requestMap.remove("body");
|
||||||
if (body instanceof String) {
|
body = body instanceof String ? ("\"" + body + "\"").getBytes(StandardCharsets.UTF_8) : mapper.writeValueAsBytes(body);
|
||||||
body = ("\"" + body + "\"").getBytes(StandardCharsets.UTF_8);
|
messageBuilder = MessageBuilder.withPayload(body)
|
||||||
}
|
.copyHeaders(requestMap);
|
||||||
else { // assume array or map
|
|
||||||
body = mapper.writeValueAsBytes(body);
|
|
||||||
}
|
|
||||||
|
|
||||||
message = MessageBuilder.withPayload(body)
|
|
||||||
.copyHeaders(requestMap)
|
|
||||||
.setHeader("aws-context", context)
|
|
||||||
.build();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (message == null) {
|
if (messageBuilder == null) {
|
||||||
message = MessageBuilder.withPayload(payload).setHeader("aws-context", context).build();
|
messageBuilder = MessageBuilder.withPayload(payload);
|
||||||
}
|
}
|
||||||
return message;
|
return messageBuilder.setHeader("aws-context", context).build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -379,6 +379,7 @@ public class FunctionInvokerTests {
|
|||||||
assertThat(result.get("body")).isEqualTo("hello");
|
assertThat(result.get("body")).isEqualTo("hello");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("rawtypes")
|
||||||
@Test
|
@Test
|
||||||
public void testApiGatewayEventAsMap() throws Exception {
|
public void testApiGatewayEventAsMap() throws Exception {
|
||||||
System.setProperty("MAIN_CLASS", ApiGatewayConfiguration.class.getName());
|
System.setProperty("MAIN_CLASS", ApiGatewayConfiguration.class.getName());
|
||||||
|
|||||||
@@ -71,9 +71,6 @@ public abstract class JsonMapper {
|
|||||||
}
|
}
|
||||||
result = ((String) value).getBytes(StandardCharsets.UTF_8);
|
result = ((String) value).getBytes(StandardCharsets.UTF_8);
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
logger.warn("Object does not represent a valid JSON. Object is: " + value);
|
|
||||||
}
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user