From 0370c7b2c15ff398fa8286241c5bb3029cceb2c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Sodr=C3=A9?= Date: Fri, 12 Mar 2021 23:19:41 -0500 Subject: [PATCH 1/2] Parse `multi-value` query when it is passed as a single-value comma delimited string. --- .../complete/http/test-annotations-query.http | 38 +++++++++++++++++++ src/main/java/co/zeroae/gate/App.java | 8 +++- src/test/java/co/zeroae/gate/AppTest.java | 12 +++++- 3 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 examples/complete/http/test-annotations-query.http diff --git a/examples/complete/http/test-annotations-query.http b/examples/complete/http/test-annotations-query.http new file mode 100644 index 0000000..9a5b094 --- /dev/null +++ b/examples/complete/http/test-annotations-query.http @@ -0,0 +1,38 @@ +### POST Single AnnotationRequest +POST {{endpoint}}/{{name}}?annotations=:Token +Content-Type: application/fastinfoset +Accept: application/json + +< ./hello-world.finf +> {% +client.test("Request executed successfully", function() { + client.assert(response.body.entities.Token.length === 3, "Expected 3 tokens."); +}); +%} + + +### POST MultiValue AnnotationRequest +POST {{endpoint}}/{{name}}?annotations=:Token&annotations=:Sentence +Content-Type: application/fastinfoset +Accept: application/json + +< ./hello-world.finf +> {% +client.test("Request executed successfully", function() { + client.assert(response.body.entities.Token.length === 3, "Expected 3 tokens."); + client.assert(response.body.entities.Sentence.length === 1, "Expected 1 sentence."); +}); +%} + +### POST multi-valued comma separated annotations query +POST {{endpoint}}/{{name}}?annotations=:Token, :Sentence +Content-Type: application/fastinfoset +Accept: application/json + +< ./hello-world.finf +> {% +client.test("Request executed successfully", function() { + client.assert(response.body.entities.Token.length === 3, "Expected 3 tokens."); + client.assert(response.body.entities.Sentence.length === 1, "Expected 1 sentence."); +}); +%} diff --git a/src/main/java/co/zeroae/gate/App.java b/src/main/java/co/zeroae/gate/App.java index ea9b958..3f0831e 100644 --- a/src/main/java/co/zeroae/gate/App.java +++ b/src/main/java/co/zeroae/gate/App.java @@ -23,6 +23,7 @@ import java.net.URL; import java.net.URLStreamHandler; import java.util.*; +import java.util.function.Supplier; /** * This class implements a GATE application using AWS Lambda. @@ -139,7 +140,12 @@ public APIGatewayProxyResponseEvent handleExecute(APIGatewayProxyRequestEvent in AWSXRay.beginSubsegment("Gate Export"); AWSXRay.getCurrentSubsegment().putMetadata("Content-Type", response.getHeaders().get("Content-Type")); - final List annotationSelector = mQueryStringParams.get("annotations"); + final List annotationSelector = ((Supplier>) () -> { + String singleValued = queryStringParams.get("annotations"); + if (singleValued == null) + return mQueryStringParams.get("annotations"); + return Arrays.asList(singleValued.split("\\s*,\\s*")); + }).get(); try { return export(exporter, doc, annotationSelector, response).withStatusCode(200); diff --git a/src/test/java/co/zeroae/gate/AppTest.java b/src/test/java/co/zeroae/gate/AppTest.java index bf02cd4..115e18e 100644 --- a/src/test/java/co/zeroae/gate/AppTest.java +++ b/src/test/java/co/zeroae/gate/AppTest.java @@ -105,11 +105,21 @@ public void testAnnotationSelector() throws Exception { // Now we downselect to only one field. input.withMultiValueQueryStringParameters(new HashMap<>()) .getMultiValueQueryStringParameters() - .put("annotations", Collections.singletonList(":Token")); + .put("annotations", Collections.singletonList(":Token")); result = app.handleRequest(input, context); doc = Utils.xmlToDocument(new StringReader(result.getBody())); assertEquals(1, doc.getAnnotations().getAllTypes().size()); + + // Do the same but URL Encode the annotations query + input.withMultiValueQueryStringParameters(null) + .withQueryStringParameters(new HashMap<>()) + .getQueryStringParameters() + .put("annotations", ":Token, :Sentence"); + result = app.handleRequest(input, context); + doc = Utils.xmlToDocument(new StringReader(result.getBody())); + assertEquals(2, doc.getAnnotations().getAllTypes().size()); + } @Test From 3ee1db2220233c47f281a0beda00f48637ba5df3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Sodr=C3=A9?= Date: Fri, 12 Mar 2021 23:23:53 -0500 Subject: [PATCH 2/2] Update src/test/java/co/zeroae/gate/AppTest.java --- src/test/java/co/zeroae/gate/AppTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/co/zeroae/gate/AppTest.java b/src/test/java/co/zeroae/gate/AppTest.java index 115e18e..b390882 100644 --- a/src/test/java/co/zeroae/gate/AppTest.java +++ b/src/test/java/co/zeroae/gate/AppTest.java @@ -111,7 +111,6 @@ public void testAnnotationSelector() throws Exception { doc = Utils.xmlToDocument(new StringReader(result.getBody())); assertEquals(1, doc.getAnnotations().getAllTypes().size()); - // Do the same but URL Encode the annotations query input.withMultiValueQueryStringParameters(null) .withQueryStringParameters(new HashMap<>()) .getQueryStringParameters()