Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ repositories {
}

dependencies {
implementation 'com.amazonaws:aws-lambda-java-core:1.2.1'
implementation 'uk.ac.gate:gate-core:8.6.1'
implementation 'com.amazonaws:aws-lambda-java-core:1.2.1'
implementation 'com.amazonaws:aws-lambda-java-events:2.2.9'

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

Expand Down
29 changes: 15 additions & 14 deletions src/main/java/co/zeroae/gate/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;

import gate.*;
import gate.creole.ExecutionException;
import gate.creole.ResourceInstantiationException;
Expand All @@ -13,15 +16,14 @@
import java.io.File;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

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

public GatewayResponse handleRequest(final Map<String, ?> input, final Context context) {
final Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "text/plain");
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, final Context context) {
final APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent()
.withHeaders(new HashMap<>());
response.getHeaders().put("Content-Type", "text/plain");

@SuppressWarnings("unchecked")
final Map<String, String> inputHeaders = (Map<String, String>) input.get("headers");
if (!inputHeaders.get("Content-Type").equalsIgnoreCase("text/plain")) {
return new GatewayResponse("We only support text/plain input.", headers, 400);
if (!input.getHeaders().get("Content-Type").equalsIgnoreCase("text/plain")) {
return response.withBody("We only support text/plain input.").withStatusCode(400);
}

try {
final Document doc = Factory.newDocument(input.get("body").toString());
final Document doc = Factory.newDocument(input.getBody());
final Corpus corpus = application.getCorpus();
corpus.add(doc);
try {
application.execute();
headers.put("Content-Type", "application/xml");
return new GatewayResponse(doc.toXml(), headers, 200);
response.getHeaders().put("Content-Type", "application/xml");
return response.withBody(doc.toXml()).withStatusCode(200);
} catch (ExecutionException e) {
logger.error(e);
return new GatewayResponse(e.getMessage(), headers, 500);
return response.withBody(e.getMessage()).withStatusCode(500);
} finally {
corpus.clear();
Factory.deleteResource(doc);
}
} catch (ResourceInstantiationException e) {
logger.warn(e);
return new GatewayResponse(e.getMessage(), headers, 400);
return response.withBody(e.getMessage()).withStatusCode(400);
}
}

Expand Down
33 changes: 0 additions & 33 deletions src/main/java/co/zeroae/gate/GatewayResponse.java

This file was deleted.

17 changes: 10 additions & 7 deletions src/test/java/co/zeroae/gate/AppTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package co.zeroae.gate;

import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
import org.junit.Test;

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

// Create the Input
final HashMap<String, String> input_headers = new HashMap<>();
input_headers.put("Content-Type", "text/plain");
final HashMap<String, Object> input = new HashMap<>();
input.put("headers", Collections.unmodifiableMap(input_headers));
input.put("body", "This is a test. My name is LambdaTestFunction, and I just watched the T.V. show Wanda Vision.");
APIGatewayProxyRequestEvent input = new APIGatewayProxyRequestEvent()
.withHttpMethod("POST")
.withHeaders(Collections.unmodifiableMap(input_headers))
.withBody("This is a test. My name is LambdaTestFunction, and I just watched the T.V. show Wanda Vision.");

// Context
final TestContext context = new TestContext();

// Invoke the App
final GatewayResponse result = app.handleRequest(Collections.unmodifiableMap(input), context);
final APIGatewayProxyResponseEvent result = app.handleRequest(input, context);

// Assert Results
assertEquals(result.getStatusCode(), 200);
assertEquals(result.getStatusCode().intValue(), 200);
assertEquals(result.getHeaders().get("Content-Type"), "application/xml");
String content = result.getBody();
final String content = result.getBody();
assertNotNull(content);
assertTrue(content.contains("GateDocument version=\"3\""));
}
Expand Down