Skip to content

Commit 86e7ba3

Browse files
committed
Use APIGatewayProxyRequestEvent
1 parent 470a056 commit 86e7ba3

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ repositories {
1212
}
1313

1414
dependencies {
15-
implementation 'com.amazonaws:aws-lambda-java-core:1.2.1'
1615
implementation 'uk.ac.gate:gate-core:8.6.1'
16+
implementation 'com.amazonaws:aws-lambda-java-core:1.2.1'
17+
implementation 'com.amazonaws:aws-lambda-java-events:2.2.9'
1718

1819
runtimeOnly 'com.amazonaws:aws-lambda-java-log4j2:1.2.0'
1920

src/main/java/co/zeroae/gate/App.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import com.amazonaws.services.lambda.runtime.Context;
44
import com.amazonaws.services.lambda.runtime.RequestHandler;
5+
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
6+
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
7+
58
import gate.*;
69
import gate.creole.ExecutionException;
710
import gate.creole.ResourceInstantiationException;
@@ -21,7 +24,7 @@
2124
* It loads the application from the .gapp file defined by the GATE_APP_FILE environment variable.
2225
* For every lambda invocation, it runs the application and outputs the result in GateXML format.
2326
*/
24-
public class App implements RequestHandler< Map<String, ?>, GatewayResponse> {
27+
public class App implements RequestHandler<APIGatewayProxyRequestEvent, GatewayResponse> {
2528
static {
2629
try {
2730
Gate.init();
@@ -33,18 +36,16 @@ public class App implements RequestHandler< Map<String, ?>, GatewayResponse> {
3336
private static final Logger logger = LogManager.getLogger(App.class);
3437
private static final CorpusController application = loadApplication();
3538

36-
public GatewayResponse handleRequest(final Map<String, ?> input, final Context context) {
39+
public GatewayResponse handleRequest(APIGatewayProxyRequestEvent input, final Context context) {
3740
final Map<String, String> headers = new HashMap<>();
3841
headers.put("Content-Type", "text/plain");
3942

40-
@SuppressWarnings("unchecked")
41-
final Map<String, String> inputHeaders = (Map<String, String>) input.get("headers");
42-
if (!inputHeaders.get("Content-Type").equalsIgnoreCase("text/plain")) {
43+
if (!input.getHeaders().get("Content-Type").equalsIgnoreCase("text/plain")) {
4344
return new GatewayResponse("We only support text/plain input.", headers, 400);
4445
}
4546

4647
try {
47-
final Document doc = Factory.newDocument(input.get("body").toString());
48+
final Document doc = Factory.newDocument(input.getBody());
4849
final Corpus corpus = application.getCorpus();
4950
corpus.add(doc);
5051
try {

src/test/java/co/zeroae/gate/AppTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package co.zeroae.gate;
22

3+
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
34
import org.junit.Test;
45

56
import java.util.Collections;
@@ -18,15 +19,16 @@ public void successfulResponse() throws Exception {
1819
// Create the Input
1920
final HashMap<String, String> input_headers = new HashMap<>();
2021
input_headers.put("Content-Type", "text/plain");
21-
final HashMap<String, Object> input = new HashMap<>();
22-
input.put("headers", Collections.unmodifiableMap(input_headers));
23-
input.put("body", "This is a test. My name is LambdaTestFunction, and I just watched the T.V. show Wanda Vision.");
22+
APIGatewayProxyRequestEvent input = new APIGatewayProxyRequestEvent()
23+
.withHttpMethod("POST")
24+
.withHeaders(Collections.unmodifiableMap(input_headers))
25+
.withBody("This is a test. My name is LambdaTestFunction, and I just watched the T.V. show Wanda Vision.");
2426

2527
// Context
2628
final TestContext context = new TestContext();
2729

2830
// Invoke the App
29-
final GatewayResponse result = app.handleRequest(Collections.unmodifiableMap(input), context);
31+
final GatewayResponse result = app.handleRequest(input, context);
3032

3133
// Assert Results
3234
assertEquals(result.getStatusCode(), 200);

0 commit comments

Comments
 (0)