-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 674a430
Showing
23 changed files
with
868 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
target | ||
.idea | ||
java.dynamodb.iml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# README # | ||
|
||
In order to run this app, simply upload it to your AWS Lambda and fill your DynamoDB with preferred data | ||
|
||
### What is this repository for? ### | ||
|
||
This is an example repo for java programmers that want to work with AWS-Lambda and DynamoDB | ||
|
||
By given brand_id and language, the app will return a merge between the given language tags and the common tags | ||
|
||
explore the unitests and the appended json scheme to understand more | ||
|
||
### How do I get set up? ### | ||
|
||
using AWS Lambda and DynamoDB | ||
serverless.yml -> adjust the sys-env-vars to your system |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<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"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.serverless</groupId> | ||
<artifactId>java.dynamodb</artifactId> | ||
<packaging>jar</packaging> | ||
<version>1.29.03</version> | ||
<name>Tags Handler</name> | ||
|
||
<properties> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<repositories> | ||
<repository> | ||
<id>dynamodb-local-oregon</id> | ||
<name>DynamoDB Local Release Repository</name> | ||
<url>https://s3-us-west-2.amazonaws.com/dynamodb-local/release</url> | ||
</repository> | ||
</repositories> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.amazonaws</groupId> | ||
<artifactId>aws-lambda-java-log4j</artifactId> | ||
<version>1.0.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.amazonaws</groupId> | ||
<artifactId>aws-lambda-java-core</artifactId> | ||
<version>1.1.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.amazonaws</groupId> | ||
<artifactId>aws-java-sdk-dynamodb</artifactId> | ||
<version>1.11.164</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-core</artifactId> | ||
<version>2.8.9</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-databind</artifactId> | ||
<version>2.8.9</version> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-annotations</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-annotations</artifactId> | ||
<version>2.8.9</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.code.gson</groupId> | ||
<artifactId>gson</artifactId> | ||
<version>2.8.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.inject</groupId> | ||
<artifactId>guice</artifactId> | ||
<version>4.1.0</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.12</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-core</artifactId> | ||
<version>2.7.17</version> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-shade-plugin</artifactId> | ||
<version>3.0.0</version> | ||
<configuration> | ||
<createDependencyReducedPom>false</createDependencyReducedPom> | ||
</configuration> | ||
<executions> | ||
<execution> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>shade</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Welcome to Serverless! | ||
# | ||
# This file is the main config file for your service. | ||
# It's very minimal at this point and uses default values. | ||
# You can always add more config options for more control. | ||
# We've included some commented out config examples here. | ||
# Just uncomment any of them to get that config option. | ||
# | ||
# For full config options, check the docs: | ||
# docs.serverless.com | ||
# | ||
# Happy Coding! | ||
|
||
service: java-dynamo | ||
|
||
# You can pin your service to only deploy with a specific Serverless version | ||
# Check out our docs for more details | ||
# frameworkVersion: "=X.X.X" | ||
|
||
provider: | ||
name: aws | ||
runtime: java8 | ||
profile: serverless-admin | ||
region: eu-central-1 | ||
|
||
environment: | ||
APP_LOG_LEVEL: ERROR | ||
DYNAMO_ACCESS_KEY: ACCESS_KEY | ||
DYNAMO_SECRET_KEY: SECRET_KEY | ||
APP_AWS_REGION: MY_REGION | ||
|
||
package: | ||
artifact: target/java-dynamo-1.29.03.jar | ||
|
||
functions: | ||
getTags: | ||
handler: com.serverless.TagsHandler | ||
events: | ||
- http: | ||
path: java-dynamo/tags/{brand_id}/{language} | ||
method: get | ||
cors: true | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.serverless; | ||
|
||
import com.amazonaws.services.lambda.runtime.Context; | ||
import com.amazonaws.services.lambda.runtime.RequestHandler; | ||
import com.google.gson.JsonObject; | ||
import com.google.inject.Guice; | ||
import com.google.inject.Injector; | ||
import com.serverless.api.ApiGatewayResponse; | ||
import com.serverless.api.Request; | ||
import com.serverless.bl.services.InputService; | ||
import com.serverless.bl.services.TagsService; | ||
import com.serverless.utils.TagsInjector; | ||
import com.serverless.utils.exceptions.DatasourceException; | ||
import com.serverless.utils.exceptions.InvalidInputException; | ||
import org.apache.log4j.Logger; | ||
|
||
import java.util.Map; | ||
|
||
import static com.serverless.api.ResponseStatus.BAD_REQUEST; | ||
import static com.serverless.api.ResponseStatus.SERVER_ERROR; | ||
|
||
public class TagsHandler implements RequestHandler<Map<String, Object>, ApiGatewayResponse> { | ||
|
||
private static final Injector INJECTOR = Guice.createInjector(new TagsInjector()); | ||
|
||
private static final Logger LOG = Logger.getLogger(TagsHandler.class); | ||
|
||
@Override | ||
public ApiGatewayResponse handleRequest(Map<String, Object> input, Context context) { | ||
try { | ||
Request request = INJECTOR.getInstance(InputService.class).getInputParams(input); | ||
|
||
JsonObject tags = getTagsService().getTags(request); | ||
|
||
LOG.info(":handleRequest: received=" + input + " result=" + tags); | ||
|
||
return ApiGatewayResponse.builder() | ||
.setObjectBody(tags) | ||
.build(); | ||
} catch (InvalidInputException e) { | ||
LOG.error(":handleRequest: " + e.getMessage(), e); | ||
return ApiGatewayResponse.builder() | ||
.setStatusCode(BAD_REQUEST.getStatus()) | ||
.setObjectBody(getErrMsg(e.getMessage())) | ||
.build(); | ||
} catch (DatasourceException e) { | ||
LOG.error(":handleRequest: " + e.getMessage(), e); | ||
return ApiGatewayResponse.builder() | ||
.setStatusCode(SERVER_ERROR.getStatus()) | ||
.setObjectBody(getErrMsg(e.getMessage())) | ||
.build(); | ||
} catch (Exception e) { | ||
String errMsg = "failed to retrieve tags content"; | ||
LOG.error(":handleRequest: " + errMsg, e); | ||
return ApiGatewayResponse.builder() | ||
.setStatusCode(SERVER_ERROR.getStatus()) | ||
.setObjectBody(getErrMsg(errMsg)) | ||
.build(); | ||
} | ||
} | ||
|
||
TagsService getTagsService() { | ||
return INJECTOR.getInstance(TagsService.class); | ||
} | ||
|
||
JsonObject getErrMsg(String errMsg) { | ||
JsonObject response = new JsonObject(); | ||
response.addProperty("errMsg", errMsg); | ||
return response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.serverless.api; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.google.gson.JsonObject; | ||
import org.apache.log4j.Logger; | ||
|
||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
import static com.serverless.api.ResponseStatus.*; | ||
|
||
@SuppressWarnings("unused") | ||
public class ApiGatewayResponse { | ||
|
||
private final int statusCode; | ||
private final String body; | ||
private final Map<String, String> headers; | ||
|
||
ApiGatewayResponse(int statusCode, JsonObject body, Map<String, String> headers) { | ||
this.statusCode = statusCode; | ||
this.body = body.toString(); | ||
this.headers = headers; | ||
} | ||
|
||
public int getStatusCode() { | ||
return statusCode; | ||
} | ||
|
||
public String getBody() { | ||
return body; | ||
} | ||
|
||
public Map<String, String> getHeaders() { | ||
return headers; | ||
} | ||
|
||
public static Builder builder() { | ||
return new Builder(); | ||
} | ||
|
||
public static class Builder { | ||
|
||
private static final Logger LOG = Logger.getLogger(ApiGatewayResponse.Builder.class); | ||
|
||
private static final ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
private int statusCode = OK.getStatus(); | ||
private final Map<String, String> headers = Collections.singletonMap("Content-Type", "application/json"); | ||
private JsonObject objectBody; | ||
|
||
public Builder setStatusCode(int statusCode) { | ||
this.statusCode = statusCode; | ||
return this; | ||
} | ||
|
||
public Builder setObjectBody(JsonObject objectBody) { | ||
this.objectBody = objectBody; | ||
return this; | ||
} | ||
|
||
public ApiGatewayResponse build() { | ||
return new ApiGatewayResponse(statusCode, objectBody, headers); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.serverless.api; | ||
|
||
public class Request { | ||
|
||
private final int brandId; | ||
private final String language; | ||
|
||
public Request(int brandId, String language) { | ||
this.brandId = brandId; | ||
this.language = language; | ||
} | ||
|
||
public int getBrandId() { | ||
return brandId; | ||
} | ||
|
||
public String getLanguage() { | ||
return language; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Request{" + | ||
"brandId=" + brandId + | ||
", language='" + language + '\'' + | ||
'}'; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
|
||
Request request = (Request) o; | ||
|
||
return brandId == request.brandId && language.equals(request.language); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int result = brandId; | ||
result = 31 * result + language.hashCode(); | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.serverless.api; | ||
|
||
public enum ResponseStatus { | ||
|
||
OK(200), | ||
BAD_REQUEST(400), | ||
SERVER_ERROR(500); | ||
|
||
private final int status; | ||
|
||
ResponseStatus(int status) { | ||
this.status = status; | ||
} | ||
|
||
public int getStatus() { | ||
return status; | ||
} | ||
} |
Oops, something went wrong.