/* Ċ Ġ* ĠL am bl in Ċ Ġ* ĠCopyright Ġ 2 0 1 8 ĠBor isl av ĠShe ker ov Ċ Ġ* ĠLicensed Ġunder ĠApache Ġ 2 . 0 : Ġhttps :// github . com / Bor isl av She ker ov / lam bl in / blob / master / LICENSE Ċ Ġ*/ Ċ Ċ package Ġcom . lam bl in . core . handler Ċ Ċ import Ġcom . amazonaws . services . lambda . runtime . events . API Gateway Proxy Request Event Ċ import Ġcom . amazonaws . services . lambda . runtime . events . API Gateway Proxy Response Event Ċ import Ġcom . lam bl in . core . Controller Registry Ċ import Ġcom . lam bl in . core . Endpoint Invoker Ċ import Ġcom . lam bl in . core . extract . Composite Param Value Injector Ċ import Ġcom . lam bl in . core . json Mapper Registry Ċ import Ġcom . lam bl in . core . model . Handler Method Ċ import Ġcom . lam bl in . core . model . Handler Method Comparator Ċ import Ġcom . lam bl in . core . model . HttpMethod Ċ import Ġcom . lam bl in . core . model . HttpResponse Ċ import Ġcom . lam bl in . core . model . StatusCode Ċ import Ġcom . lam bl in . core . security . Default Endpoint Authorization Checker Ċ import Ġcom . lam bl in . core . security . Endpoint Authorization Checker Ċ import Ġorg . slf 4 j . LoggerFactory Ċ Ċ private Ġval ĠLOGGER Ġ= ĠLoggerFactory . getLogger ( RequestHandler :: class . java ) Ċ Ċ internal Ġclass ĠRequest Handler ( ĊĠĠĠ Ġprivate Ġval Ġendpoint Invoker : ĠEndpoint Invoker , ĊĠĠĠ Ġprivate Ġval Ġendpoint Authorization Checker : ĠEndpoint Authorization Checker ) Ġ{ ĊĊĠĠĠ Ġcompanion Ġobject Ġ{ ĊĠĠĠĠĠĠĠ Ġinternal Ġfun Ġinstance ( controller Registry : ĠController Registry ) Ġ= ĠRequest Handler ( ĊĠĠĠĠĠĠĠĠĠĠĠ ĠEndpoint Invoker ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠComposite Param Value Injector . instance (), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġcontroller Registry ), ĊĠĠĠĠĠĠĠĠĠĠĠ ĠDefault Endpoint Authorization Checker ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ/** ĊĠĠĠĠ Ġ* ĠAt tempts Ġto Ġhandle Ġan Ġ[ API Gateway Proxy Request Event ] Ġby Ġfirst Ġlooking Ġfor ĊĠĠĠĠ Ġ* Ġa Ġsuitable Ġ[ Handler Method ] Ġand Ġthen Ġdeleg ating Ġthe Ġexecution Ġto Ġit . ĊĠĠĠĠ Ġ*/ ĊĠĠĠ Ġfun Ġhandle ( ĊĠĠĠĠĠĠĠ Ġrequest : ĠAPI Gateway Proxy Request Event , ĊĠĠĠĠĠĠĠ Ġhttp Method To Handlers : ĠMap < HttpMethod , ĠSet < Handler Method >> ): ĠAPI Gateway Proxy Response Event Ġ{ ĊĊĠĠĠĠĠĠĠ Ġreturn Ġtry Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠLOGGER . debug (" Handling Ġrequest Ġto Ġ[ {} ]", Ġrequest . path ) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġhandler Request ( request , Ġhttp Method To Handlers ) ĊĠĠĠĠĠĠĠ Ġ} Ġcatch Ġ( e : ĠRuntimeException ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠLOGGER . error (" Error Ġhandling Ġrequest ", Ġe ) ĊĊĠĠĠĠĠĠĠĠĠĠĠ ĠAPI Gateway Proxy Response Event () ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. with StatusCode ( StatusCode . API _ ERROR . code ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. with Headers ( map Of (" Content - Type " Ġto Ġ" application / json ")) ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġhandler Request ( ĊĠĠĠĠĠĠĠ Ġrequest : ĠAPI Gateway Proxy Request Event , ĊĠĠĠĠĠĠĠ Ġhttp Method To Handlers : ĠMap < HttpMethod , ĠSet < Handler Method >> ): ĠAPI Gateway Proxy Response Event Ġ{ ĊĊĠĠĠĠĠĠĠ Ġval Ġhandlers For HttpMethod Ġ= Ġhttp Method To Handlers [ HttpMethod . valueOf ( request . http Method )] ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ?: Ġreturn ĠAPI Gateway Proxy Response Event (). with StatusCode ( StatusCode . NOT _ FOUND . code ) ĊĊĠĠĠĠĠĠĠ Ġval Ġrequest Handler Method Ġ= Ġhandlers For HttpMethod . as Sequence () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. sorted With ( Handler Method Comparator ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. find Ġ{ Ġit . matches ( request . path , Ġrequest . getAll QueryParams ()) Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ?: Ġreturn ĠAPI Gateway Proxy Response Event (). with StatusCode ( StatusCode . NOT _ FOUND . code ) ĊĊĠĠĠĠĠĠĠ ĠLOGGER . debug ( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ" Handler Ġmethod Ġ[ {}] Ġin Ġ[ {}] Ġselected ", ĊĠĠĠĠĠĠĠĠĠĠĠ Ġrequest Handler Method . http Method . name , ĊĠĠĠĠĠĠĠĠĠĠĠ Ġrequest Handler Method . controller Class . simple Name ) ĊĊĠĠĠĠĠĠĠ Ġval Ġrequest Authorized Ġ= Ġrequest Handler Method . access Control ?. let Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġendpoint Authorization Checker . is Request Authorized ( request , Ġit ) ĊĠĠĠĠĠĠĠ Ġ} Ġ?: Ġtrue ĊĊĠĠĠĠĠĠĠ Ġreturn Ġif Ġ( request Authorized ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġresponse Ġ= Ġendpoint Invoker . invoke ( request Handler Method , Ġrequest ) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġcreate Api Gateway Response Event ( response ) ĊĠĠĠĠĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġcreate Api Gateway Response Event ( HttpResponse . un authorized ()) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġcreate Api Gateway Response Event ( ĊĠĠĠĠĠĠĠ Ġresponse : ĠHttpResponse < *> ĊĠĠĠ Ġ): ĠAPI Gateway Proxy Response Event Ġ= ĊĊĠĠĠĠĠĠĠ Ġif Ġ( should Serialize ResponseBody Json ( response . statusCode )) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠAPI Gateway Proxy Response Event (). apply Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġwith StatusCode ( response . statusCode . code ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġwith Headers ( response . headers Ġ+ Ġmap Of (" Content - Type " Ġto Ġ" application / json ")) ĊĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġresponse . body ?. let Ġ{ Ġwith Body ( json Mapper Registry . json Mapper . writeValue AsString ( response . body )) Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠAPI Gateway Proxy Response Event (). apply Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġwith StatusCode ( response . statusCode . code ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġwith Headers ( response . headers ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġshould Serialize ResponseBody Json ( statusCode : ĠStatusCode ) Ġ= ĠstatusCode Ġ=== ĠStatusCode . OK ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ|| ĠstatusCode Ġ=== ĠStatusCode . CREATED ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ|| ĠstatusCode Ġ=== ĠStatusCode . ACCEPT ED Ċ Ċ } Ċ Ċ // ĠRetrieves Ġthe Ġmap Ġof Ġall Ġquery Ġparameters Ġ( single - key Ġ+ Ġmulti - key ) Ċ fun ĠAPI Gateway Proxy Request Event . getAll QueryParams () Ġ= ĊĠĠĠ Ġ( queryString Parameters Ġ?: Ġmap Of ()) Ġ+ Ġ( multi Value QueryString Parameters Ġ?: Ġmap Of ())