Skip to content

Commit f1e78dd

Browse files
authored
Merge pull request #8 from zeroae/chore/use-aws-events
Use was-lambda-java-events library
2 parents 470a056 + a1ab471 commit f1e78dd

File tree

4 files changed

+27
-55
lines changed

4 files changed

+27
-55
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: 15 additions & 14 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;
@@ -13,15 +16,14 @@
1316
import java.io.File;
1417
import java.net.URL;
1518
import java.util.HashMap;
16-
import java.util.Map;
1719
import java.util.Objects;
1820

1921
/**
2022
* This class implements a GATE application using AWS Lambda.
2123
* It loads the application from the .gapp file defined by the GATE_APP_FILE environment variable.
2224
* For every lambda invocation, it runs the application and outputs the result in GateXML format.
2325
*/
24-
public class App implements RequestHandler< Map<String, ?>, GatewayResponse> {
26+
public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
2527
static {
2628
try {
2729
Gate.init();
@@ -33,34 +35,33 @@ public class App implements RequestHandler< Map<String, ?>, GatewayResponse> {
3335
private static final Logger logger = LogManager.getLogger(App.class);
3436
private static final CorpusController application = loadApplication();
3537

36-
public GatewayResponse handleRequest(final Map<String, ?> input, final Context context) {
37-
final Map<String, String> headers = new HashMap<>();
38-
headers.put("Content-Type", "text/plain");
38+
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, final Context context) {
39+
final APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent()
40+
.withHeaders(new HashMap<>());
41+
response.getHeaders().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-
return new GatewayResponse("We only support text/plain input.", headers, 400);
43+
if (!input.getHeaders().get("Content-Type").equalsIgnoreCase("text/plain")) {
44+
return response.withBody("We only support text/plain input.").withStatusCode(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 {
5152
application.execute();
52-
headers.put("Content-Type", "application/xml");
53-
return new GatewayResponse(doc.toXml(), headers, 200);
53+
response.getHeaders().put("Content-Type", "application/xml");
54+
return response.withBody(doc.toXml()).withStatusCode(200);
5455
} catch (ExecutionException e) {
5556
logger.error(e);
56-
return new GatewayResponse(e.getMessage(), headers, 500);
57+
return response.withBody(e.getMessage()).withStatusCode(500);
5758
} finally {
5859
corpus.clear();
5960
Factory.deleteResource(doc);
6061
}
6162
} catch (ResourceInstantiationException e) {
6263
logger.warn(e);
63-
return new GatewayResponse(e.getMessage(), headers, 400);
64+
return response.withBody(e.getMessage()).withStatusCode(400);
6465
}
6566
}
6667

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

Lines changed: 0 additions & 33 deletions
This file was deleted.

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

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

3+
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
4+
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
35
import org.junit.Test;
46

57
import java.util.Collections;
@@ -13,25 +15,26 @@ public class AppTest {
1315
@Test
1416
public void successfulResponse() throws Exception {
1517
App app = withEnvironmentVariable("GATE_APP_NAME", "annie")
16-
.execute(() -> new App());
18+
.execute(App::new);
1719

1820
// Create the Input
1921
final HashMap<String, String> input_headers = new HashMap<>();
2022
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.");
23+
APIGatewayProxyRequestEvent input = new APIGatewayProxyRequestEvent()
24+
.withHttpMethod("POST")
25+
.withHeaders(Collections.unmodifiableMap(input_headers))
26+
.withBody("This is a test. My name is LambdaTestFunction, and I just watched the T.V. show Wanda Vision.");
2427

2528
// Context
2629
final TestContext context = new TestContext();
2730

2831
// Invoke the App
29-
final GatewayResponse result = app.handleRequest(Collections.unmodifiableMap(input), context);
32+
final APIGatewayProxyResponseEvent result = app.handleRequest(input, context);
3033

3134
// Assert Results
32-
assertEquals(result.getStatusCode(), 200);
35+
assertEquals(result.getStatusCode().intValue(), 200);
3336
assertEquals(result.getHeaders().get("Content-Type"), "application/xml");
34-
String content = result.getBody();
37+
final String content = result.getBody();
3538
assertNotNull(content);
3639
assertTrue(content.contains("GateDocument version=\"3\""));
3740
}

0 commit comments

Comments
 (0)