diff --git a/build.gradle b/build.gradle index f59dbab..0483dbb 100644 --- a/build.gradle +++ b/build.gradle @@ -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' diff --git a/src/main/java/co/zeroae/gate/App.java b/src/main/java/co/zeroae/gate/App.java index 62bf661..42aa9a7 100644 --- a/src/main/java/co/zeroae/gate/App.java +++ b/src/main/java/co/zeroae/gate/App.java @@ -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; @@ -13,7 +16,6 @@ import java.io.File; import java.net.URL; import java.util.HashMap; -import java.util.Map; import java.util.Objects; /** @@ -21,7 +23,7 @@ * 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, GatewayResponse> { +public class App implements RequestHandler { static { try { Gate.init(); @@ -33,34 +35,33 @@ public class App implements RequestHandler< Map, GatewayResponse> { private static final Logger logger = LogManager.getLogger(App.class); private static final CorpusController application = loadApplication(); - public GatewayResponse handleRequest(final Map input, final Context context) { - final Map 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 inputHeaders = (Map) 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); } } diff --git a/src/main/java/co/zeroae/gate/GatewayResponse.java b/src/main/java/co/zeroae/gate/GatewayResponse.java deleted file mode 100644 index bd81ac4..0000000 --- a/src/main/java/co/zeroae/gate/GatewayResponse.java +++ /dev/null @@ -1,33 +0,0 @@ -package co.zeroae.gate; - -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -/** - * POJO containing response object for API Gateway. - */ -public class GatewayResponse { - - private final String body; - private final Map headers; - private final int statusCode; - - public GatewayResponse(final String body, final Map headers, final int statusCode) { - this.statusCode = statusCode; - this.body = body; - this.headers = Collections.unmodifiableMap(new HashMap<>(headers)); - } - - public String getBody() { - return body; - } - - public Map getHeaders() { - return headers; - } - - public int getStatusCode() { - return statusCode; - } -} diff --git a/src/test/java/co/zeroae/gate/AppTest.java b/src/test/java/co/zeroae/gate/AppTest.java index 8a1ee25..eedf5d9 100644 --- a/src/test/java/co/zeroae/gate/AppTest.java +++ b/src/test/java/co/zeroae/gate/AppTest.java @@ -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; @@ -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 input_headers = new HashMap<>(); input_headers.put("Content-Type", "text/plain"); - final HashMap 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\"")); }