Skip to content

Commit 314e3c6

Browse files
authored
Merge pull request #11 from zeroae/f/json-output
Add support for JSON Output format
2 parents 6842724 + aafafc6 commit 314e3c6

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ repositories {
1313

1414
dependencies {
1515
implementation 'uk.ac.gate:gate-core:8.6.1'
16+
implementation 'uk.ac.gate.plugins:format-json:8.7'
1617
implementation 'com.amazonaws:aws-lambda-java-core:1.2.1'
1718
implementation 'com.amazonaws:aws-lambda-java-events:2.2.9'
1819

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@
66
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
77

88
import gate.*;
9+
import gate.corpora.export.GATEJsonExporter;
910
import gate.creole.ExecutionException;
1011
import gate.creole.ResourceInstantiationException;
1112
import gate.util.GateException;
1213
import gate.util.persistence.PersistenceManager;
1314
import org.apache.log4j.LogManager;
1415
import org.apache.log4j.Logger;
1516

17+
import java.io.ByteArrayOutputStream;
1618
import java.io.File;
19+
import java.io.IOException;
1720
import java.net.URL;
1821
import java.util.HashMap;
1922
import java.util.Objects;
@@ -34,8 +37,10 @@ public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatew
3437

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

3842
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, final Context context) {
43+
final String responseType = input.getHeaders().getOrDefault("Accept", "application/xml");
3944
final APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent()
4045
.withHeaders(new HashMap<>());
4146
response.getHeaders().put("Content-Type", "text/plain");
@@ -46,9 +51,9 @@ public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent in
4651
corpus.add(doc);
4752
try {
4853
application.execute();
49-
response.getHeaders().put("Content-Type", "application/xml");
50-
return response.withBody(doc.toXml()).withStatusCode(200);
51-
} catch (ExecutionException e) {
54+
response.getHeaders().put("Content-Type", responseType);
55+
return response.withBody(encode(doc, responseType)).withStatusCode(200);
56+
} catch (ExecutionException | IOException e) {
5257
logger.error(e);
5358
return response.withBody(e.getMessage()).withStatusCode(500);
5459
} finally {
@@ -61,6 +66,21 @@ public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent in
6166
}
6267
}
6368

69+
/**
70+
* @param doc an instance of gate.Document
71+
* @param responseType One of the supported response types
72+
*/
73+
private String encode(Document doc, String responseType) throws IOException {
74+
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
75+
if (responseType.equals("application/json")) {
76+
gateJsonExporter.export(doc, baos);
77+
return baos.toString();
78+
}
79+
else {
80+
return doc.toXml();
81+
}
82+
}
83+
6484
private static CorpusController loadApplication() {
6585
try {
6686
final String gappResourcePah = System.getenv("GATE_APP_NAME") + "/application.xgapp";

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
44
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
5-
import org.junit.Before;
5+
import com.fasterxml.jackson.core.JsonFactory;
6+
import com.fasterxml.jackson.core.JsonParser;
67
import org.junit.BeforeClass;
78
import org.junit.Test;
89

@@ -57,4 +58,25 @@ public void testMissingContentType() throws Exception {
5758
final APIGatewayProxyResponseEvent result = app.handleRequest(input, context);
5859
assertEquals(result.getStatusCode().intValue(), 200);
5960
}
61+
62+
@Test
63+
public void testApplicationJsonResponse() throws Exception {
64+
final HashMap<String, String> inputHeaders = new HashMap<>();
65+
inputHeaders.put("Accept", "application/json");
66+
APIGatewayProxyRequestEvent input = new APIGatewayProxyRequestEvent()
67+
.withHttpMethod("POST")
68+
.withHeaders(Collections.unmodifiableMap(inputHeaders))
69+
.withBody("My name is Lambda Function and I approve this test.");
70+
final TestContext context = new TestContext();
71+
final APIGatewayProxyResponseEvent result = app.handleRequest(input, context);
72+
assertEquals(result.getStatusCode().intValue(), 200);
73+
74+
// Ensure we get back application/json back
75+
assertEquals(result.getHeaders().get("Content-Type"), "application/json");
76+
final JsonFactory factory = new JsonFactory();
77+
final JsonParser parser = factory.createParser(result.getBody());
78+
while (!parser.isClosed()) {
79+
parser.nextToken();
80+
}
81+
}
6082
}

0 commit comments

Comments
 (0)