Skip to content

Commit a1ab471

Browse files
committed
Replace GatewayResponse.java with APIGatewayProxyResponseEvent
1 parent 86e7ba3 commit a1ab471

File tree

3 files changed

+15
-47
lines changed

3 files changed

+15
-47
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@
1616
import java.io.File;
1717
import java.net.URL;
1818
import java.util.HashMap;
19-
import java.util.Map;
2019
import java.util.Objects;
2120

2221
/**
2322
* This class implements a GATE application using AWS Lambda.
2423
* It loads the application from the .gapp file defined by the GATE_APP_FILE environment variable.
2524
* For every lambda invocation, it runs the application and outputs the result in GateXML format.
2625
*/
27-
public class App implements RequestHandler<APIGatewayProxyRequestEvent, GatewayResponse> {
26+
public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
2827
static {
2928
try {
3029
Gate.init();
@@ -36,12 +35,13 @@ public class App implements RequestHandler<APIGatewayProxyRequestEvent, GatewayR
3635
private static final Logger logger = LogManager.getLogger(App.class);
3736
private static final CorpusController application = loadApplication();
3837

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

4343
if (!input.getHeaders().get("Content-Type").equalsIgnoreCase("text/plain")) {
44-
return new GatewayResponse("We only support text/plain input.", headers, 400);
44+
return response.withBody("We only support text/plain input.").withStatusCode(400);
4545
}
4646

4747
try {
@@ -50,18 +50,18 @@ public GatewayResponse handleRequest(APIGatewayProxyRequestEvent input, final Co
5050
corpus.add(doc);
5151
try {
5252
application.execute();
53-
headers.put("Content-Type", "application/xml");
54-
return new GatewayResponse(doc.toXml(), headers, 200);
53+
response.getHeaders().put("Content-Type", "application/xml");
54+
return response.withBody(doc.toXml()).withStatusCode(200);
5555
} catch (ExecutionException e) {
5656
logger.error(e);
57-
return new GatewayResponse(e.getMessage(), headers, 500);
57+
return response.withBody(e.getMessage()).withStatusCode(500);
5858
} finally {
5959
corpus.clear();
6060
Factory.deleteResource(doc);
6161
}
6262
} catch (ResourceInstantiationException e) {
6363
logger.warn(e);
64-
return new GatewayResponse(e.getMessage(), headers, 400);
64+
return response.withBody(e.getMessage()).withStatusCode(400);
6565
}
6666
}
6767

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

Lines changed: 0 additions & 33 deletions
This file was deleted.

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package co.zeroae.gate;
22

33
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
4+
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
45
import org.junit.Test;
56

67
import java.util.Collections;
@@ -14,7 +15,7 @@ public class AppTest {
1415
@Test
1516
public void successfulResponse() throws Exception {
1617
App app = withEnvironmentVariable("GATE_APP_NAME", "annie")
17-
.execute(() -> new App());
18+
.execute(App::new);
1819

1920
// Create the Input
2021
final HashMap<String, String> input_headers = new HashMap<>();
@@ -28,12 +29,12 @@ public void successfulResponse() throws Exception {
2829
final TestContext context = new TestContext();
2930

3031
// Invoke the App
31-
final GatewayResponse result = app.handleRequest(input, context);
32+
final APIGatewayProxyResponseEvent result = app.handleRequest(input, context);
3233

3334
// Assert Results
34-
assertEquals(result.getStatusCode(), 200);
35+
assertEquals(result.getStatusCode().intValue(), 200);
3536
assertEquals(result.getHeaders().get("Content-Type"), "application/xml");
36-
String content = result.getBody();
37+
final String content = result.getBody();
3738
assertNotNull(content);
3839
assertTrue(content.contains("GateDocument version=\"3\""));
3940
}

0 commit comments

Comments
 (0)