|
| 1 | +package helloworld; |
| 2 | + |
| 3 | +import com.amazonaws.services.lambda.runtime.Context; |
| 4 | +import com.amazonaws.services.lambda.runtime.RequestHandler; |
| 5 | +import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent; |
| 6 | +import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent; |
| 7 | +import org.apache.logging.log4j.LogManager; |
| 8 | +import org.apache.logging.log4j.Logger; |
| 9 | +import software.amazon.lambda.powertools.idempotency.Idempotency; |
| 10 | +import software.amazon.lambda.powertools.idempotency.IdempotencyConfig; |
| 11 | +import software.amazon.lambda.powertools.idempotency.Idempotent; |
| 12 | +import software.amazon.lambda.powertools.idempotency.persistence.DynamoDBPersistenceStore; |
| 13 | +import software.amazon.lambda.powertools.utilities.JsonConfig; |
| 14 | + |
| 15 | +import java.io.BufferedReader; |
| 16 | +import java.io.IOException; |
| 17 | +import java.io.InputStreamReader; |
| 18 | +import java.net.URL; |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.stream.Collectors; |
| 22 | + |
| 23 | +public class AppIdempotency implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> { |
| 24 | + private final static Logger LOG = LogManager.getLogger(); |
| 25 | + |
| 26 | + public AppIdempotency() { |
| 27 | + // we need to initialize idempotency configuration before the handleRequest method is called |
| 28 | + Idempotency.config().withConfig( |
| 29 | + IdempotencyConfig.builder() |
| 30 | + .withEventKeyJMESPath("powertools_json(body).address") |
| 31 | + .withUseLocalCache() |
| 32 | + .build()) |
| 33 | + .withPersistenceStore( |
| 34 | + DynamoDBPersistenceStore.builder() |
| 35 | + .withTableName("idempotency_table") |
| 36 | + .build() |
| 37 | + ).configure(); |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | + /** |
| 42 | + * Try with: |
| 43 | + * <pre> |
| 44 | + * curl -X POST https://[REST-API-ID].execute-api.[REGION].amazonaws.com/Prod/helloidem/ -H "Content-Type: application/json" -d '{"address": "https://checkip.amazonaws.com"}' |
| 45 | + * </pre> |
| 46 | + * @param input |
| 47 | + * @param context |
| 48 | + * @return |
| 49 | + */ |
| 50 | + @Idempotent |
| 51 | + public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEvent input, final Context context) { |
| 52 | + Map<String, String> headers = new HashMap<>(); |
| 53 | + |
| 54 | + headers.put("Content-Type", "application/json"); |
| 55 | + headers.put("Access-Control-Allow-Origin", "*"); |
| 56 | + headers.put("Access-Control-Allow-Methods", "GET, OPTIONS"); |
| 57 | + headers.put("Access-Control-Allow-Headers", "*"); |
| 58 | + |
| 59 | + APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent() |
| 60 | + .withHeaders(headers); |
| 61 | + try { |
| 62 | + String address = JsonConfig.get().getObjectMapper().readTree(input.getBody()).get("address").asText(); |
| 63 | + final String pageContents = this.getPageContents(address); |
| 64 | + String output = String.format("{ \"message\": \"hello world\", \"location\": \"%s\" }", pageContents); |
| 65 | + |
| 66 | + LOG.debug("ip is {}", pageContents); |
| 67 | + return response |
| 68 | + .withStatusCode(200) |
| 69 | + .withBody(output); |
| 70 | + |
| 71 | + } catch (IOException e) { |
| 72 | + return response |
| 73 | + .withBody("{}") |
| 74 | + .withStatusCode(500); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // we could actually also put the @Idempotent annotation here |
| 79 | + private String getPageContents(String address) throws IOException { |
| 80 | + URL url = new URL(address); |
| 81 | + try (BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()))) { |
| 82 | + return br.lines().collect(Collectors.joining(System.lineSeparator())); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments