@@ -26,11 +26,46 @@ public abstract class SlackApiLambdaHandler implements RequestHandler<ApiGateway
2626 private final App app ;
2727
2828 public SlackApiLambdaHandler (App app ) {
29- this .app = app ;
3029 this .requestParser = new SlackRequestParser (app .config ());
30+ this .app = app ;
31+ app .start ();
32+ }
33+
34+ /**
35+ * Returns true if the given incoming request is an internal warmup request.
36+ * You can use your own logic for distinguishing requests from Slack from your own internal warmup trigger.
37+ */
38+ protected abstract boolean isWarmupRequest (ApiGatewayRequest awsReq );
39+
40+ @ Override
41+ public ApiGatewayResponse handleRequest (ApiGatewayRequest input , Context context ) {
42+ if (isWarmupRequest (input )) {
43+ // This one is always an internal request
44+ // (It's not possible to create this request body via API Gateway)
45+ log .info ("Successfully responded to a warmup request ({})" , input );
46+ return null ;
47+ }
48+ Request <?> req = toSlackRequest (input );
49+ try {
50+ if (req == null ) {
51+ return ApiGatewayResponse .builder ().statusCode (400 ).rawBody ("Invalid Request" ).build ();
52+ }
53+ return toApiGatewayResponse (app .run (req ));
54+ } catch (Exception e ) {
55+ log .error ("Failed to respond to a request (request: {}, error: {})" , input .getBody (), e .getMessage (), e );
56+ // As this response body can be exposed, it should not have detailed information.
57+ return ApiGatewayResponse .builder ().statusCode (500 ).rawBody ("Internal Server Error" ).build ();
58+ }
3159 }
3260
33- protected App getApp () {
61+ // -------------------------------------------
62+ // Extensible internal methods
63+ // -------------------------------------------
64+
65+ /**
66+ * Returns the underlying {@link App} instance in this handler.
67+ */
68+ protected App app () {
3469 return app ;
3570 }
3671
@@ -57,29 +92,9 @@ protected ApiGatewayResponse toApiGatewayResponse(Response slackResp) {
5792 .build ();
5893 }
5994
60- protected abstract boolean isWarmupRequest (ApiGatewayRequest awsReq );
61-
62- @ Override
63- public ApiGatewayResponse handleRequest (ApiGatewayRequest input , Context context ) {
64- if (isWarmupRequest (input )) {
65- // This one is always an internal request
66- // (It's not possible to create this request body via API Gateway)
67- app .start ();
68- log .info ("Successfully responded to a warmup request ({})" , input );
69- return null ;
70- }
71- Request <?> req = toSlackRequest (input );
72- try {
73- if (req == null ) {
74- return ApiGatewayResponse .builder ().statusCode (400 ).rawBody ("Invalid Request" ).build ();
75- }
76- return toApiGatewayResponse (app .run (req ));
77- } catch (Exception e ) {
78- log .error ("Failed to respond to a request (request: {}, error: {})" , input .getBody (), e .getMessage (), e );
79- // As this response body can be exposed, it should not have detailed information.
80- return ApiGatewayResponse .builder ().statusCode (500 ).rawBody ("Internal Server Error" ).build ();
81- }
82- }
95+ // -------------------------------------------
96+ // Private utility methods
97+ // -------------------------------------------
8398
8499 private static Map <String , String > toStringToStringMap (Map <String , List <String >> stringToStringListMap ) {
85100 Map <String , String > headers = new HashMap <>();
0 commit comments