22
33import com .amazonaws .services .lambda .runtime .Context ;
44import com .amazonaws .services .lambda .runtime .RequestHandler ;
5+ import com .amazonaws .services .lambda .runtime .events .APIGatewayProxyRequestEvent ;
6+ import com .amazonaws .services .lambda .runtime .events .APIGatewayProxyResponseEvent ;
7+
58import gate .*;
69import gate .creole .ExecutionException ;
710import gate .creole .ResourceInstantiationException ;
1316import java .io .File ;
1417import java .net .URL ;
1518import java .util .HashMap ;
16- import java .util .Map ;
1719import java .util .Objects ;
1820
1921/**
2022 * This class implements a GATE application using AWS Lambda.
2123 * It loads the application from the .gapp file defined by the GATE_APP_FILE environment variable.
2224 * For every lambda invocation, it runs the application and outputs the result in GateXML format.
2325 */
24- public class App implements RequestHandler < Map < String , ?>, GatewayResponse > {
26+ public class App implements RequestHandler <APIGatewayProxyRequestEvent , APIGatewayProxyResponseEvent > {
2527 static {
2628 try {
2729 Gate .init ();
@@ -33,34 +35,33 @@ public class App implements RequestHandler< Map<String, ?>, GatewayResponse> {
3335 private static final Logger logger = LogManager .getLogger (App .class );
3436 private static final CorpusController application = loadApplication ();
3537
36- public GatewayResponse handleRequest (final Map <String , ?> input , final Context context ) {
37- final Map <String , String > headers = new HashMap <>();
38- 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" );
3942
40- @ SuppressWarnings ("unchecked" )
41- final Map <String , String > inputHeaders = (Map <String , String >) input .get ("headers" );
42- if (!inputHeaders .get ("Content-Type" ).equalsIgnoreCase ("text/plain" )) {
43- return new GatewayResponse ("We only support text/plain input." , headers , 400 );
43+ if (!input .getHeaders ().get ("Content-Type" ).equalsIgnoreCase ("text/plain" )) {
44+ return response .withBody ("We only support text/plain input." ).withStatusCode (400 );
4445 }
4546
4647 try {
47- final Document doc = Factory .newDocument (input .get ( "body" ). toString ());
48+ final Document doc = Factory .newDocument (input .getBody ());
4849 final Corpus corpus = application .getCorpus ();
4950 corpus .add (doc );
5051 try {
5152 application .execute ();
52- headers .put ("Content-Type" , "application/xml" );
53- return new GatewayResponse (doc .toXml (), headers , 200 );
53+ response . getHeaders () .put ("Content-Type" , "application/xml" );
54+ return response . withBody (doc .toXml ()). withStatusCode ( 200 );
5455 } catch (ExecutionException e ) {
5556 logger .error (e );
56- return new GatewayResponse (e .getMessage (), headers , 500 );
57+ return response . withBody (e .getMessage ()). withStatusCode ( 500 );
5758 } finally {
5859 corpus .clear ();
5960 Factory .deleteResource (doc );
6061 }
6162 } catch (ResourceInstantiationException e ) {
6263 logger .warn (e );
63- return new GatewayResponse (e .getMessage (), headers , 400 );
64+ return response . withBody (e .getMessage ()). withStatusCode ( 400 );
6465 }
6566 }
6667
0 commit comments