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
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ repositories {

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

Expand Down
26 changes: 23 additions & 3 deletions src/main/java/co/zeroae/gate/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;

import gate.*;
import gate.corpora.export.GATEJsonExporter;
import gate.creole.ExecutionException;
import gate.creole.ResourceInstantiationException;
import gate.util.GateException;
import gate.util.persistence.PersistenceManager;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Objects;
Expand All @@ -34,8 +37,10 @@ public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatew

private static final Logger logger = LogManager.getLogger(App.class);
private static final CorpusController application = loadApplication();
private static final GATEJsonExporter gateJsonExporter = new GATEJsonExporter();

public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, final Context context) {
final String responseType = input.getHeaders().getOrDefault("Accept", "application/xml");
final APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent()
.withHeaders(new HashMap<>());
response.getHeaders().put("Content-Type", "text/plain");
Expand All @@ -46,9 +51,9 @@ public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent in
corpus.add(doc);
try {
application.execute();
response.getHeaders().put("Content-Type", "application/xml");
return response.withBody(doc.toXml()).withStatusCode(200);
} catch (ExecutionException e) {
response.getHeaders().put("Content-Type", responseType);
return response.withBody(encode(doc, responseType)).withStatusCode(200);
} catch (ExecutionException | IOException e) {
logger.error(e);
return response.withBody(e.getMessage()).withStatusCode(500);
} finally {
Expand All @@ -61,6 +66,21 @@ public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent in
}
}

/**
* @param doc an instance of gate.Document
* @param responseType One of the supported response types
*/
private String encode(Document doc, String responseType) throws IOException {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (responseType.equals("application/json")) {
gateJsonExporter.export(doc, baos);
return baos.toString();
}
else {
return doc.toXml();
}
}

private static CorpusController loadApplication() {
try {
final String gappResourcePah = System.getenv("GATE_APP_NAME") + "/application.xgapp";
Expand Down
24 changes: 23 additions & 1 deletion src/test/java/co/zeroae/gate/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
import org.junit.Before;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import org.junit.BeforeClass;
import org.junit.Test;

Expand Down Expand Up @@ -57,4 +58,25 @@ public void testMissingContentType() throws Exception {
final APIGatewayProxyResponseEvent result = app.handleRequest(input, context);
assertEquals(result.getStatusCode().intValue(), 200);
}

@Test
public void testApplicationJsonResponse() throws Exception {
final HashMap<String, String> inputHeaders = new HashMap<>();
inputHeaders.put("Accept", "application/json");
APIGatewayProxyRequestEvent input = new APIGatewayProxyRequestEvent()
.withHttpMethod("POST")
.withHeaders(Collections.unmodifiableMap(inputHeaders))
.withBody("My name is Lambda Function and I approve this test.");
final TestContext context = new TestContext();
final APIGatewayProxyResponseEvent result = app.handleRequest(input, context);
assertEquals(result.getStatusCode().intValue(), 200);

// Ensure we get back application/json back
assertEquals(result.getHeaders().get("Content-Type"), "application/json");
final JsonFactory factory = new JsonFactory();
final JsonParser parser = factory.createParser(result.getBody());
while (!parser.isClosed()) {
parser.nextToken();
}
}
}