Commit 87ab75489f94f2dc2129b6fe3db4ca54c546671c

Authored by Alex Mukha
1 parent 4f38de5445
Exists in master

Added possibility of saving entries into dynamodb.

... ... @@ -8,11 +8,21 @@
8 8 <artifactId>heartbeat</artifactId>
9 9 <version>2.0-SNAPSHOT</version>
10 10  
  11 + <properties>
  12 + <aws.lambda.core.version>1.1.0</aws.lambda.core.version>
  13 + <aws.dynamodb.sdk.version>1.11.68</aws.dynamodb.sdk.version>
  14 + </properties>
  15 +
11 16 <dependencies>
12 17 <dependency>
13 18 <groupId>com.amazonaws</groupId>
14 19 <artifactId>aws-lambda-java-core</artifactId>
15   - <version>1.1.0</version>
  20 + <version>${aws.lambda.core.version}</version>
  21 + </dependency>
  22 + <dependency>
  23 + <groupId>com.amazonaws</groupId>
  24 + <artifactId>aws-java-sdk-dynamodb</artifactId>
  25 + <version>${aws.dynamodb.sdk.version}</version>
16 26 </dependency>
17 27 </dependencies>
18 28  
... ... @@ -37,6 +47,7 @@
37 47 <plugin>
38 48 <groupId>org.apache.maven.plugins</groupId>
39 49 <artifactId>maven-compiler-plugin</artifactId>
  50 + <version>3.5.1</version>
40 51 <configuration>
41 52 <source>1.8</source>
42 53 <target>1.8</target>
... ...
src/main/java/org/alfresco/heartbeat/handler/HeartbeatRequestHandler.java
1 1 package org.alfresco.heartbeat.handler;
2 2  
3 3  
  4 +import com.amazonaws.auth.profile.ProfileCredentialsProvider;
  5 +import com.amazonaws.regions.Region;
  6 +import com.amazonaws.regions.Regions;
  7 +import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
  8 +import com.amazonaws.services.dynamodbv2.document.DynamoDB;
  9 +import com.amazonaws.services.dynamodbv2.document.Item;
  10 +import com.amazonaws.services.dynamodbv2.document.PutItemOutcome;
  11 +import com.amazonaws.services.dynamodbv2.document.Table;
4 12 import com.amazonaws.services.lambda.runtime.RequestHandler;
5 13 import org.alfresco.heartbeat.HeartbeatRequest;
6 14 import org.alfresco.heartbeat.HeartbeatResponse;
... ... @@ -14,12 +22,45 @@ import com.amazonaws.services.lambda.runtime.Context;
14 22 */
15 23 public class HeartbeatRequestHandler implements RequestHandler<HeartbeatRequest, HeartbeatResponse>
16 24 {
  25 + private static final String TABLE_NAME = "heartbeat";
  26 + private static final String PRIMARY_KEY = "repositoryId";
  27 + private static final String REPOSITORY_VERSION_KEY = "repositoryVersion";
  28 +
17 29 @Override
18 30 public HeartbeatResponse handleRequest(HeartbeatRequest request, Context context)
19 31 {
20 32 context.getLogger().log("Received object: " + request);
  33 + checkRequest(request);
  34 + AmazonDynamoDBClient client = new AmazonDynamoDBClient();
  35 + client.setRegion(Region.getRegion(Regions.EU_WEST_1));
  36 + DynamoDB dynamoDB = new DynamoDB(client);
  37 + Table table = dynamoDB.getTable(TABLE_NAME);
  38 + context.getLogger().log("Table description " + table.getDescription());
  39 + Item item = new Item()
  40 + .withPrimaryKey(PRIMARY_KEY, request.getRepositoryId())
  41 + .withString(REPOSITORY_VERSION_KEY, request.getRepositoryVersion());
  42 + PutItemOutcome outcome = table.putItem(item);
  43 +// TODO incorporate outcome in the response
21 44 HeartbeatResponse response = new HeartbeatResponse();
22 45 response.setSuccess(true);
23 46 return response;
24 47 }
  48 +
  49 + /**
  50 + * @return <code>true</code> if the request has at least minimum set of parameters present
  51 + */
  52 + private boolean checkRequest(HeartbeatRequest request)
  53 + {
  54 + if (request.getRepositoryId() == null ||
  55 + request.getRepositoryId().isEmpty() ||
  56 + request.getRepositoryVersion() == null ||
  57 + request.getRepositoryVersion().isEmpty())
  58 + {
  59 + return false;
  60 + }
  61 + else
  62 + {
  63 + return true;
  64 + }
  65 + }
25 66 }
... ...