Polish
This commit is contained in:
@@ -1,19 +1,21 @@
|
|||||||
package org.springframework.cloud.function.adapter.aws;
|
package org.springframework.cloud.function.adapter.aws;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
|
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
|
||||||
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
|
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.cloud.function.context.catalog.FunctionInspector;
|
import org.springframework.cloud.function.context.catalog.FunctionInspector;
|
||||||
import org.springframework.messaging.Message;
|
import org.springframework.messaging.Message;
|
||||||
import org.springframework.messaging.MessageHeaders;
|
import org.springframework.messaging.MessageHeaders;
|
||||||
import org.springframework.messaging.support.GenericMessage;
|
import org.springframework.messaging.support.GenericMessage;
|
||||||
|
|
||||||
import java.util.HashMap;
|
public class SpringBootApiGatewayRequestHandler extends
|
||||||
import java.util.Map;
|
SpringBootRequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
|
||||||
|
|
||||||
public class SpringBootApiGatewayRequestHandler extends SpringBootRequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ObjectMapper mapper;
|
private ObjectMapper mapper;
|
||||||
@@ -29,11 +31,13 @@ public class SpringBootApiGatewayRequestHandler extends SpringBootRequestHandler
|
|||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
protected Object convertEvent(APIGatewayProxyRequestEvent event) {
|
protected Object convertEvent(APIGatewayProxyRequestEvent event) {
|
||||||
Object body = deserializeBody(event.getBody());
|
Object body = deserializeBody(event.getBody());
|
||||||
if (functionAcceptsMessage()) {
|
if (functionAcceptsMessage()) {
|
||||||
return new GenericMessage<>(body, getHeaders(event));
|
return new GenericMessage<>(body, getHeaders(event));
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
return body;
|
return body;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -45,7 +49,8 @@ public class SpringBootApiGatewayRequestHandler extends SpringBootRequestHandler
|
|||||||
private Object deserializeBody(String json) {
|
private Object deserializeBody(String json) {
|
||||||
try {
|
try {
|
||||||
return mapper.readValue(json, getInputType());
|
return mapper.readValue(json, getInputType());
|
||||||
} catch (Exception e) {
|
}
|
||||||
|
catch (Exception e) {
|
||||||
throw new IllegalStateException("Cannot convert event", e);
|
throw new IllegalStateException("Cannot convert event", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -59,19 +64,19 @@ public class SpringBootApiGatewayRequestHandler extends SpringBootRequestHandler
|
|||||||
return new MessageHeaders(headers);
|
return new MessageHeaders(headers);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
protected APIGatewayProxyResponseEvent convertOutput(Object output) {
|
protected APIGatewayProxyResponseEvent convertOutput(Object output) {
|
||||||
if (functionReturnsMessage(output)) {
|
if (functionReturnsMessage(output)) {
|
||||||
Message message = (Message) output;
|
Message<?> message = (Message<?>) output;
|
||||||
return new APIGatewayProxyResponseEvent()
|
return new APIGatewayProxyResponseEvent().withStatusCode(
|
||||||
.withStatusCode((Integer) message.getHeaders().getOrDefault("statusCode", 200))
|
(Integer) message.getHeaders().getOrDefault("statusCode", 200))
|
||||||
.withHeaders(toResponseHeaders(message.getHeaders()))
|
.withHeaders(toResponseHeaders(message.getHeaders()))
|
||||||
.withBody(serializeBody(message.getPayload()));
|
.withBody(serializeBody(message.getPayload()));
|
||||||
} else {
|
}
|
||||||
return new APIGatewayProxyResponseEvent()
|
else {
|
||||||
.withStatusCode(200)
|
return new APIGatewayProxyResponseEvent().withStatusCode(200)
|
||||||
.withBody(serializeBody(output));
|
.withBody(serializeBody(output));
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -81,14 +86,16 @@ public class SpringBootApiGatewayRequestHandler extends SpringBootRequestHandler
|
|||||||
|
|
||||||
private Map<String, String> toResponseHeaders(MessageHeaders messageHeaders) {
|
private Map<String, String> toResponseHeaders(MessageHeaders messageHeaders) {
|
||||||
Map<String, String> responseHeaders = new HashMap<>();
|
Map<String, String> responseHeaders = new HashMap<>();
|
||||||
messageHeaders.forEach((key, value) -> responseHeaders.put(key, value.toString()));
|
messageHeaders
|
||||||
|
.forEach((key, value) -> responseHeaders.put(key, value.toString()));
|
||||||
return responseHeaders;
|
return responseHeaders;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String serializeBody(Object body) {
|
private String serializeBody(Object body) {
|
||||||
try {
|
try {
|
||||||
return mapper.writeValueAsString(body);
|
return mapper.writeValueAsString(body);
|
||||||
} catch (JsonProcessingException e) {
|
}
|
||||||
|
catch (JsonProcessingException e) {
|
||||||
throw new IllegalStateException("Cannot convert output", e);
|
throw new IllegalStateException("Cannot convert output", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,7 +28,8 @@ import reactor.core.publisher.Flux;
|
|||||||
/**
|
/**
|
||||||
* @author Mark Fisher
|
* @author Mark Fisher
|
||||||
*/
|
*/
|
||||||
public class SpringBootRequestHandler<E, O> extends SpringFunctionInitializer implements RequestHandler<E, Object> {
|
public class SpringBootRequestHandler<E, O> extends SpringFunctionInitializer
|
||||||
|
implements RequestHandler<E, Object> {
|
||||||
|
|
||||||
public SpringBootRequestHandler(Class<?> configurationClass) {
|
public SpringBootRequestHandler(Class<?> configurationClass) {
|
||||||
super(configurationClass);
|
super(configurationClass);
|
||||||
@@ -51,7 +52,7 @@ public class SpringBootRequestHandler<E, O> extends SpringFunctionInitializer im
|
|||||||
for (Object value : output.toIterable()) {
|
for (Object value : output.toIterable()) {
|
||||||
result.add(convertOutput(value));
|
result.add(convertOutput(value));
|
||||||
}
|
}
|
||||||
if (isSingleValue(input) && result.size()==1) {
|
if (isSingleValue(input) && result.size() == 1) {
|
||||||
return result.get(0);
|
return result.get(0);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
@@ -72,8 +73,9 @@ public class SpringBootRequestHandler<E, O> extends SpringFunctionInitializer im
|
|||||||
return event;
|
return event;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected O convertOutput(Object output) {
|
@SuppressWarnings("unchecked")
|
||||||
return (O) output;
|
protected O convertOutput(Object output) {
|
||||||
}
|
return (O) output;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user