in src/main/java/com/amazonaws/handler/UpdateOrderHandler.java [54:145]
public void handleRequest(InputStream input, OutputStream output,
Context context) throws IOException {
final JsonNode event;
try {
event = objectMapper.readTree(input);
} catch (JsonMappingException e) {
writeInvalidJsonInStreamResponse(objectMapper, output, e.getMessage());
return;
}
if (event == null) {
writeInvalidJsonInStreamResponse(objectMapper, output, "event was null");
return;
}
final JsonNode pathParameterMap = event.findValue("pathParameters");
final String orderId = Optional.ofNullable(pathParameterMap)
.map(mapNode -> mapNode.get("order_id"))
.map(JsonNode::asText)
.orElse(null);
if (isNullOrEmpty(orderId)) {
objectMapper.writeValue(output,
new GatewayResponse<>(
objectMapper.writeValueAsString(ORDER_ID_WAS_NOT_SET),
APPLICATION_JSON, SC_BAD_REQUEST));
return;
}
JsonNode updateOrderRequestBody = event.findValue("body");
if (updateOrderRequestBody == null) {
objectMapper.writeValue(output,
new GatewayResponse<>(
objectMapper.writeValueAsString(
new ErrorMessage("Body was null",
SC_BAD_REQUEST)),
APPLICATION_JSON, SC_BAD_REQUEST));
return;
}
final UpdateOrderRequest request;
try {
request = objectMapper.readValue(
updateOrderRequestBody.asText(), UpdateOrderRequest.class);
} catch (JsonParseException | JsonMappingException e) {
objectMapper.writeValue(output,
new GatewayResponse<>(
objectMapper.writeValueAsString(
new ErrorMessage("Invalid JSON in body: "
+ e.getMessage(), SC_BAD_REQUEST)),
APPLICATION_JSON, SC_BAD_REQUEST));
return;
}
if (request == null) {
objectMapper.writeValue(output,
new GatewayResponse<>(
objectMapper.writeValueAsString(REQUEST_WAS_NULL_ERROR),
APPLICATION_JSON, SC_BAD_REQUEST));
return;
}
try {
Order updatedOrder = orderDao.updateOrder(
Order.builder().orderId(orderId)
.customerId(request.getCustomerId())
.version(request.getVersion())
.preTaxAmount(request.getPreTaxAmount())
.postTaxAmount(request.getPostTaxAmount())
.build());
objectMapper.writeValue(output, new GatewayResponse<>(
objectMapper.writeValueAsString(updatedOrder),
APPLICATION_JSON, SC_OK));
} catch (UnableToUpdateException e) {
objectMapper.writeValue(output, new GatewayResponse<>(
objectMapper.writeValueAsString(
new ErrorMessage(e.getMessage(), SC_CONFLICT)),
APPLICATION_JSON, SC_CONFLICT));
} catch (TableDoesNotExistException e) {
objectMapper.writeValue(output, new GatewayResponse<>(
objectMapper.writeValueAsString(
new ErrorMessage(e.getMessage(), SC_BAD_REQUEST)),
APPLICATION_JSON, SC_BAD_REQUEST));
} catch (IllegalArgumentException e) {
objectMapper.writeValue(output, new GatewayResponse<>(
objectMapper.writeValueAsString(
new ErrorMessage(e.getMessage(), SC_BAD_REQUEST)),
APPLICATION_JSON, SC_BAD_REQUEST));
} catch (IllegalStateException e) {
objectMapper.writeValue(output, new GatewayResponse<>(
objectMapper.writeValueAsString(
new ErrorMessage(e.getMessage(), SC_INTERNAL_SERVER_ERROR)),
APPLICATION_JSON, SC_INTERNAL_SERVER_ERROR));
}
}