Skip to content

Commit ebcaa14

Browse files
committed
Added APIGatewayController
1 parent 8bc292b commit ebcaa14

4 files changed

Lines changed: 53 additions & 10 deletions

File tree

pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@
2323
<artifactId>s3-event-notifications</artifactId>
2424
<version>${aws-java-sdk2-version}</version>
2525
</dependency>
26+
<dependency>
27+
<groupId>org.apache.httpcomponents.client5</groupId>
28+
<artifactId>httpclient5</artifactId>
29+
<version>5.5</version>
30+
</dependency>
31+
<dependency>
32+
<groupId>com.amazonaws.serverless</groupId>
33+
<artifactId>aws-serverless-java-container-core</artifactId>
34+
<version>2.1.4</version>
35+
</dependency>
2636
<dependency>
2737
<groupId>software.amazon.awssdk</groupId>
2838
<artifactId>s3</artifactId>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package za.co.prescient;
2+
3+
import com.amazonaws.serverless.proxy.model.Headers;
4+
import com.amazonaws.services.lambda.runtime.Context;
5+
import com.amazonaws.services.lambda.runtime.RequestHandler;
6+
import com.amazonaws.serverless.proxy.model.AwsProxyRequest;
7+
import com.amazonaws.serverless.proxy.model.AwsProxyResponse;
8+
import jakarta.ws.rs.core.HttpHeaders;
9+
import org.apache.hc.core5.http.HttpStatus;
10+
11+
import java.util.Map;
12+
13+
/**
14+
* The purpose of this handler is to kick off a process from and S3 trigger
15+
*/
16+
public class ApiGatewayController implements RequestHandler<AwsProxyRequest, AwsProxyResponse> {
17+
18+
@Override
19+
public AwsProxyResponse handleRequest(AwsProxyRequest input, Context context) {
20+
Map<String, String> requestParameters = input.getQueryStringParameters();
21+
String endpoint = input.getResource();
22+
try {
23+
System.out.println(input.getHttpMethod() + " method called on endpoint " + endpoint);
24+
return buildTextResponse(HttpStatus.SC_OK, "OK");
25+
} catch (Exception e) {
26+
return buildTextResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Error: " + e.getMessage());
27+
}
28+
}
29+
30+
public static AwsProxyResponse buildTextResponse(int code, String body) {
31+
Headers headers = new Headers();
32+
headers.add(HttpHeaders.CONTENT_TYPE, "text/html");
33+
return new AwsProxyResponse(code, headers, body);
34+
}
35+
}

src/main/java/za/co/prescient/S3Controller.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@
22

33
import com.amazonaws.services.lambda.runtime.Context;
44
import com.amazonaws.services.lambda.runtime.RequestHandler;
5-
import software.amazon.awssdk.eventnotifications.s3.model.S3EventNotification;
5+
import com.amazonaws.services.lambda.runtime.events.S3Event;
66

77
/**
88
* The purpose of this handler is to kick off a process from and S3 trigger
99
*/
10-
public class S3Controller implements RequestHandler<String, Void> {
10+
public class S3Controller implements RequestHandler<S3Event, Void> {
1111

1212
@Override
13-
public Void handleRequest(String jsonS3Event, Context context) {
14-
System.out.println("Message received: " + jsonS3Event);
15-
S3EventNotification s3EventNotification = S3EventNotification.fromJson(jsonS3Event);
16-
Utils.process(s3EventNotification);
13+
public Void handleRequest(S3Event s3event, Context context) {
14+
System.out.println("Message received: " + s3event);
15+
Utils.process(s3event.getRecords().get(0));
1716
return null;
1817
}
1918
}

src/main/java/za/co/prescient/Utils.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
import com.amazonaws.services.lambda.runtime.events.SNSEvent;
44
import com.amazonaws.services.lambda.runtime.events.SQSEvent;
5-
import software.amazon.awssdk.eventnotifications.s3.model.S3EventNotification;
6-
import software.amazon.awssdk.eventnotifications.s3.model.S3EventNotificationRecord;
5+
import com.amazonaws.services.lambda.runtime.events.models.s3.S3EventNotification;
76

87
public class Utils {
98

@@ -21,12 +20,12 @@ public static void process(SQSEvent.SQSMessage sqsMessage) {
2120
}
2221

2322
public static void process(S3EventNotification s3EventNotification) {
24-
for (S3EventNotificationRecord record : s3EventNotification.getRecords()) {
23+
for (S3EventNotification.S3EventNotificationRecord record : s3EventNotification.getRecords()) {
2524
process(record);
2625
}
2726
}
2827

29-
public static void process(S3EventNotificationRecord record) {
28+
public static void process(S3EventNotification.S3EventNotificationRecord record) {
3029
String bucket = record.getS3().getBucket().getName();
3130
String key = record.getS3().getObject().getKey();
3231
System.out.println("Processing: " + bucket + " : " + key);

0 commit comments

Comments
 (0)