BAEL-1821 (#4487)
* Moved Lambda examples to separate module Implementation of API Gateway example * Format fixes * Format fixes * Minor fixes * Minor fixes * Minor fixes
This commit is contained in:
parent
65221919f7
commit
bde11efe2c
99
aws-lambda/pom.xml
Normal file
99
aws-lambda/pom.xml
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>parent-modules</artifactId>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>aws-lambda</artifactId>
|
||||||
|
<version>0.1.0-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
<name>aws-lambda</name>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.amazonaws</groupId>
|
||||||
|
<artifactId>aws-java-sdk-dynamodb</artifactId>
|
||||||
|
<version>1.11.241</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.amazonaws</groupId>
|
||||||
|
<artifactId>aws-java-sdk-core</artifactId>
|
||||||
|
<version>1.11.241</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.amazonaws</groupId>
|
||||||
|
<artifactId>aws-lambda-java-core</artifactId>
|
||||||
|
<version>${aws-lambda-java-core.version}</version>
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<artifactId>commons-logging</artifactId>
|
||||||
|
<groupId>commons-logging</groupId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.amazonaws</groupId>
|
||||||
|
<artifactId>aws-lambda-java-events</artifactId>
|
||||||
|
<version>${aws-lambda-java-events.version}</version>
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<artifactId>commons-logging</artifactId>
|
||||||
|
<groupId>commons-logging</groupId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.code.gson</groupId>
|
||||||
|
<artifactId>gson</artifactId>
|
||||||
|
<version>${gson.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>commons-io</groupId>
|
||||||
|
<artifactId>commons-io</artifactId>
|
||||||
|
<version>${commons-io.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.googlecode.json-simple</groupId>
|
||||||
|
<artifactId>json-simple</artifactId>
|
||||||
|
<version>${json-simple.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-shade-plugin</artifactId>
|
||||||
|
<version>${maven-shade-plugin.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<createDependencyReducedPom>false</createDependencyReducedPom>
|
||||||
|
</configuration>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<phase>package</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>shade</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
<properties>
|
||||||
|
<json-simple.version>1.1.1</json-simple.version>
|
||||||
|
<org.json.version>20180130</org.json.version>
|
||||||
|
<commons-io.version>2.5</commons-io.version>
|
||||||
|
<aws-lambda-java-events.version>1.3.0</aws-lambda-java-events.version>
|
||||||
|
<aws-lambda-java-core.version>1.2.0</aws-lambda-java-core.version>
|
||||||
|
<gson.version>2.8.2</gson.version>
|
||||||
|
<aws-java-sdk-core.version>1.11.241</aws-java-sdk-core.version>
|
||||||
|
<maven-shade-plugin.version>3.0.0</maven-shade-plugin.version>
|
||||||
|
<maven-dependency-plugin.version>2.10</maven-dependency-plugin.version>
|
||||||
|
</properties>
|
||||||
|
</project>
|
@ -0,0 +1,166 @@
|
|||||||
|
package com.baeldung.lambda.apigateway;
|
||||||
|
|
||||||
|
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
|
||||||
|
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
|
||||||
|
import com.amazonaws.services.dynamodbv2.document.*;
|
||||||
|
import com.amazonaws.services.dynamodbv2.document.spec.PutItemSpec;
|
||||||
|
import com.amazonaws.services.lambda.runtime.Context;
|
||||||
|
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
|
||||||
|
import com.baeldung.lambda.apigateway.model.Person;
|
||||||
|
import org.json.simple.JSONObject;
|
||||||
|
import org.json.simple.parser.JSONParser;
|
||||||
|
import org.json.simple.parser.ParseException;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
|
public class APIDemoHandler implements RequestStreamHandler {
|
||||||
|
|
||||||
|
private JSONParser parser = new JSONParser();
|
||||||
|
private static final String DYNAMODB_TABLE_NAME = System.getenv("TABLE_NAME");
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
|
||||||
|
|
||||||
|
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
|
||||||
|
JSONObject responseJson = new JSONObject();
|
||||||
|
|
||||||
|
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.defaultClient();
|
||||||
|
DynamoDB dynamoDb = new DynamoDB(client);
|
||||||
|
|
||||||
|
try {
|
||||||
|
JSONObject event = (JSONObject) parser.parse(reader);
|
||||||
|
|
||||||
|
if (event.get("body") != null) {
|
||||||
|
|
||||||
|
Person person = new Person((String) event.get("body"));
|
||||||
|
|
||||||
|
dynamoDb.getTable(DYNAMODB_TABLE_NAME)
|
||||||
|
.putItem(new PutItemSpec().withItem(new Item().withNumber("id", person.getId())
|
||||||
|
.withString("firstName", person.getFirstName())
|
||||||
|
.withString("lastName", person.getLastName()).withNumber("age", person.getAge())
|
||||||
|
.withString("address", person.getAddress())));
|
||||||
|
}
|
||||||
|
|
||||||
|
JSONObject responseBody = new JSONObject();
|
||||||
|
responseBody.put("message", "New item created");
|
||||||
|
|
||||||
|
JSONObject headerJson = new JSONObject();
|
||||||
|
headerJson.put("x-custom-header", "my custom header value");
|
||||||
|
|
||||||
|
responseJson.put("statusCode", 200);
|
||||||
|
responseJson.put("headers", headerJson);
|
||||||
|
responseJson.put("body", responseBody.toString());
|
||||||
|
|
||||||
|
} catch (ParseException pex) {
|
||||||
|
responseJson.put("statusCode", 400);
|
||||||
|
responseJson.put("exception", pex);
|
||||||
|
}
|
||||||
|
|
||||||
|
OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8");
|
||||||
|
writer.write(responseJson.toString());
|
||||||
|
writer.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void handleGetByPathParam(InputStream inputStream, OutputStream outputStream, Context context)
|
||||||
|
throws IOException {
|
||||||
|
|
||||||
|
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
|
||||||
|
JSONObject responseJson = new JSONObject();
|
||||||
|
|
||||||
|
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.defaultClient();
|
||||||
|
DynamoDB dynamoDb = new DynamoDB(client);
|
||||||
|
|
||||||
|
Item result = null;
|
||||||
|
try {
|
||||||
|
JSONObject event = (JSONObject) parser.parse(reader);
|
||||||
|
JSONObject responseBody = new JSONObject();
|
||||||
|
|
||||||
|
if (event.get("pathParameters") != null) {
|
||||||
|
|
||||||
|
JSONObject pps = (JSONObject) event.get("pathParameters");
|
||||||
|
if (pps.get("id") != null) {
|
||||||
|
|
||||||
|
int id = Integer.parseInt((String) pps.get("id"));
|
||||||
|
result = dynamoDb.getTable(DYNAMODB_TABLE_NAME).getItem("id", id);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
if (result != null) {
|
||||||
|
|
||||||
|
Person person = new Person(result.toJSON());
|
||||||
|
responseBody.put("Person", person);
|
||||||
|
responseJson.put("statusCode", 200);
|
||||||
|
} else {
|
||||||
|
|
||||||
|
responseBody.put("message", "No item found");
|
||||||
|
responseJson.put("statusCode", 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
JSONObject headerJson = new JSONObject();
|
||||||
|
headerJson.put("x-custom-header", "my custom header value");
|
||||||
|
|
||||||
|
responseJson.put("headers", headerJson);
|
||||||
|
responseJson.put("body", responseBody.toString());
|
||||||
|
|
||||||
|
} catch (ParseException pex) {
|
||||||
|
responseJson.put("statusCode", 400);
|
||||||
|
responseJson.put("exception", pex);
|
||||||
|
}
|
||||||
|
|
||||||
|
OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8");
|
||||||
|
writer.write(responseJson.toString());
|
||||||
|
writer.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void handleGetByQueryParam(InputStream inputStream, OutputStream outputStream, Context context)
|
||||||
|
throws IOException {
|
||||||
|
|
||||||
|
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
|
||||||
|
JSONObject responseJson = new JSONObject();
|
||||||
|
|
||||||
|
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.defaultClient();
|
||||||
|
DynamoDB dynamoDb = new DynamoDB(client);
|
||||||
|
|
||||||
|
Item result = null;
|
||||||
|
try {
|
||||||
|
JSONObject event = (JSONObject) parser.parse(reader);
|
||||||
|
JSONObject responseBody = new JSONObject();
|
||||||
|
|
||||||
|
if (event.get("queryStringParameters") != null) {
|
||||||
|
|
||||||
|
JSONObject qps = (JSONObject) event.get("queryStringParameters");
|
||||||
|
if (qps.get("id") != null) {
|
||||||
|
|
||||||
|
int id = Integer.parseInt((String) qps.get("id"));
|
||||||
|
result = dynamoDb.getTable(DYNAMODB_TABLE_NAME).getItem("id", id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result != null) {
|
||||||
|
|
||||||
|
Person person = new Person(result.toJSON());
|
||||||
|
responseBody.put("Person", person);
|
||||||
|
responseJson.put("statusCode", 200);
|
||||||
|
} else {
|
||||||
|
|
||||||
|
responseBody.put("message", "No item found");
|
||||||
|
responseJson.put("statusCode", 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
JSONObject headerJson = new JSONObject();
|
||||||
|
headerJson.put("x-custom-header", "my custom header value");
|
||||||
|
|
||||||
|
responseJson.put("headers", headerJson);
|
||||||
|
responseJson.put("body", responseBody.toString());
|
||||||
|
|
||||||
|
} catch (ParseException pex) {
|
||||||
|
responseJson.put("statusCode", 400);
|
||||||
|
responseJson.put("exception", pex);
|
||||||
|
}
|
||||||
|
|
||||||
|
OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8");
|
||||||
|
writer.write(responseJson.toString());
|
||||||
|
writer.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,68 @@
|
|||||||
|
package com.baeldung.lambda.apigateway.model;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.GsonBuilder;
|
||||||
|
|
||||||
|
public class Person {
|
||||||
|
|
||||||
|
private int id;
|
||||||
|
private String firstName;
|
||||||
|
private String lastName;
|
||||||
|
private int age;
|
||||||
|
private String address;
|
||||||
|
|
||||||
|
public Person(String json) {
|
||||||
|
Gson gson = new Gson();
|
||||||
|
Person request = gson.fromJson(json, Person.class);
|
||||||
|
this.id = request.getId();
|
||||||
|
this.firstName = request.getFirstName();
|
||||||
|
this.lastName = request.getLastName();
|
||||||
|
this.age = request.getAge();
|
||||||
|
this.address = request.getAddress();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
final Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||||
|
return gson.toJson(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFirstName() {
|
||||||
|
return firstName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFirstName(String firstName) {
|
||||||
|
this.firstName = firstName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLastName() {
|
||||||
|
return lastName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLastName(String lastName) {
|
||||||
|
this.lastName = lastName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getAge() {
|
||||||
|
return age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAge(int age) {
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAddress() {
|
||||||
|
return address;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAddress(String address) {
|
||||||
|
this.address = address;
|
||||||
|
}
|
||||||
|
}
|
1
pom.xml
1
pom.xml
@ -18,6 +18,7 @@
|
|||||||
<module>atomix</module>
|
<module>atomix</module>
|
||||||
<module>apache-cayenne</module>
|
<module>apache-cayenne</module>
|
||||||
<module>aws</module>
|
<module>aws</module>
|
||||||
|
<module>aws-lambda</module>
|
||||||
<module>akka-streams</module>
|
<module>akka-streams</module>
|
||||||
<module>algorithms</module>
|
<module>algorithms</module>
|
||||||
<module>annotations</module>
|
<module>annotations</module>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user