|
| 1 | +package com.baeldung.lambda.apigateway; |
| 2 | + |
| 3 | +import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; |
| 4 | +import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder; |
| 5 | +import com.amazonaws.services.dynamodbv2.document.*; |
| 6 | +import com.amazonaws.services.dynamodbv2.document.spec.PutItemSpec; |
| 7 | +import com.amazonaws.services.lambda.runtime.Context; |
| 8 | +import com.amazonaws.services.lambda.runtime.RequestStreamHandler; |
| 9 | +import com.baeldung.lambda.apigateway.model.Person; |
| 10 | +import org.json.simple.JSONObject; |
| 11 | +import org.json.simple.parser.JSONParser; |
| 12 | +import org.json.simple.parser.ParseException; |
| 13 | + |
| 14 | +import java.io.*; |
| 15 | + |
| 16 | +public class APIDemoHandler implements RequestStreamHandler { |
| 17 | + |
| 18 | + private JSONParser parser = new JSONParser(); |
| 19 | + private static final String DYNAMODB_TABLE_NAME = System.getenv("TABLE_NAME"); |
| 20 | + |
| 21 | + @Override |
| 22 | + public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException { |
| 23 | + |
| 24 | + BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); |
| 25 | + JSONObject responseJson = new JSONObject(); |
| 26 | + |
| 27 | + AmazonDynamoDB client = AmazonDynamoDBClientBuilder.defaultClient(); |
| 28 | + DynamoDB dynamoDb = new DynamoDB(client); |
| 29 | + |
| 30 | + try { |
| 31 | + JSONObject event = (JSONObject) parser.parse(reader); |
| 32 | + |
| 33 | + if (event.get("body") != null) { |
| 34 | + |
| 35 | + Person person = new Person((String) event.get("body")); |
| 36 | + |
| 37 | + dynamoDb.getTable(DYNAMODB_TABLE_NAME) |
| 38 | + .putItem(new PutItemSpec().withItem(new Item().withNumber("id", person.getId()) |
| 39 | + .withString("firstName", person.getFirstName()) |
| 40 | + .withString("lastName", person.getLastName()).withNumber("age", person.getAge()) |
| 41 | + .withString("address", person.getAddress()))); |
| 42 | + } |
| 43 | + |
| 44 | + JSONObject responseBody = new JSONObject(); |
| 45 | + responseBody.put("message", "New item created"); |
| 46 | + |
| 47 | + JSONObject headerJson = new JSONObject(); |
| 48 | + headerJson.put("x-custom-header", "my custom header value"); |
| 49 | + |
| 50 | + responseJson.put("statusCode", 200); |
| 51 | + responseJson.put("headers", headerJson); |
| 52 | + responseJson.put("body", responseBody.toString()); |
| 53 | + |
| 54 | + } catch (ParseException pex) { |
| 55 | + responseJson.put("statusCode", 400); |
| 56 | + responseJson.put("exception", pex); |
| 57 | + } |
| 58 | + |
| 59 | + OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8"); |
| 60 | + writer.write(responseJson.toString()); |
| 61 | + writer.close(); |
| 62 | + } |
| 63 | + |
| 64 | + public void handleGetByPathParam(InputStream inputStream, OutputStream outputStream, Context context) |
| 65 | + throws IOException { |
| 66 | + |
| 67 | + BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); |
| 68 | + JSONObject responseJson = new JSONObject(); |
| 69 | + |
| 70 | + AmazonDynamoDB client = AmazonDynamoDBClientBuilder.defaultClient(); |
| 71 | + DynamoDB dynamoDb = new DynamoDB(client); |
| 72 | + |
| 73 | + Item result = null; |
| 74 | + try { |
| 75 | + JSONObject event = (JSONObject) parser.parse(reader); |
| 76 | + JSONObject responseBody = new JSONObject(); |
| 77 | + |
| 78 | + if (event.get("pathParameters") != null) { |
| 79 | + |
| 80 | + JSONObject pps = (JSONObject) event.get("pathParameters"); |
| 81 | + if (pps.get("id") != null) { |
| 82 | + |
| 83 | + int id = Integer.parseInt((String) pps.get("id")); |
| 84 | + result = dynamoDb.getTable(DYNAMODB_TABLE_NAME).getItem("id", id); |
| 85 | + } |
| 86 | + |
| 87 | + } |
| 88 | + if (result != null) { |
| 89 | + |
| 90 | + Person person = new Person(result.toJSON()); |
| 91 | + responseBody.put("Person", person); |
| 92 | + responseJson.put("statusCode", 200); |
| 93 | + } else { |
| 94 | + |
| 95 | + responseBody.put("message", "No item found"); |
| 96 | + responseJson.put("statusCode", 404); |
| 97 | + } |
| 98 | + |
| 99 | + JSONObject headerJson = new JSONObject(); |
| 100 | + headerJson.put("x-custom-header", "my custom header value"); |
| 101 | + |
| 102 | + responseJson.put("headers", headerJson); |
| 103 | + responseJson.put("body", responseBody.toString()); |
| 104 | + |
| 105 | + } catch (ParseException pex) { |
| 106 | + responseJson.put("statusCode", 400); |
| 107 | + responseJson.put("exception", pex); |
| 108 | + } |
| 109 | + |
| 110 | + OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8"); |
| 111 | + writer.write(responseJson.toString()); |
| 112 | + writer.close(); |
| 113 | + } |
| 114 | + |
| 115 | + public void handleGetByQueryParam(InputStream inputStream, OutputStream outputStream, Context context) |
| 116 | + throws IOException { |
| 117 | + |
| 118 | + BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); |
| 119 | + JSONObject responseJson = new JSONObject(); |
| 120 | + |
| 121 | + AmazonDynamoDB client = AmazonDynamoDBClientBuilder.defaultClient(); |
| 122 | + DynamoDB dynamoDb = new DynamoDB(client); |
| 123 | + |
| 124 | + Item result = null; |
| 125 | + try { |
| 126 | + JSONObject event = (JSONObject) parser.parse(reader); |
| 127 | + JSONObject responseBody = new JSONObject(); |
| 128 | + |
| 129 | + if (event.get("queryStringParameters") != null) { |
| 130 | + |
| 131 | + JSONObject qps = (JSONObject) event.get("queryStringParameters"); |
| 132 | + if (qps.get("id") != null) { |
| 133 | + |
| 134 | + int id = Integer.parseInt((String) qps.get("id")); |
| 135 | + result = dynamoDb.getTable(DYNAMODB_TABLE_NAME).getItem("id", id); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + if (result != null) { |
| 140 | + |
| 141 | + Person person = new Person(result.toJSON()); |
| 142 | + responseBody.put("Person", person); |
| 143 | + responseJson.put("statusCode", 200); |
| 144 | + } else { |
| 145 | + |
| 146 | + responseBody.put("message", "No item found"); |
| 147 | + responseJson.put("statusCode", 404); |
| 148 | + } |
| 149 | + |
| 150 | + JSONObject headerJson = new JSONObject(); |
| 151 | + headerJson.put("x-custom-header", "my custom header value"); |
| 152 | + |
| 153 | + responseJson.put("headers", headerJson); |
| 154 | + responseJson.put("body", responseBody.toString()); |
| 155 | + |
| 156 | + } catch (ParseException pex) { |
| 157 | + responseJson.put("statusCode", 400); |
| 158 | + responseJson.put("exception", pex); |
| 159 | + } |
| 160 | + |
| 161 | + OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8"); |
| 162 | + writer.write(responseJson.toString()); |
| 163 | + writer.close(); |
| 164 | + } |
| 165 | + |
| 166 | +} |
0 commit comments