package Ġcom . nim bus framework . nim bus aws . wrappers . http Ċ Ċ 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 . nim bus framework . nim bus aws . cloudformation . generation . ab stractions . Class For Reflection Service Ċ import Ġcom . nim bus framework . nim bus core . annotations . function . HttpServer less Function Ċ import Ġcom . nim bus framework . nim bus aws . cloudformation . model . processing . File Builder Method Information Ċ import Ġcom . nim bus framework . nim bus aws . wrappers . Server less Function File Builder Ċ import Ġcom . nim bus framework . nim bus core . annotations . Nim bus Constants Ċ import Ġcom . nim bus framework . nim bus core . annotations . function . HttpException Ċ import Ġcom . nim bus framework . nim bus core . clients . Jack son Client Ċ import Ġcom . nim bus framework . nim bus core . event ab stractions . Http Event Ċ import Ġcom . nim bus framework . nim bus core . event ab stractions . HttpResponse Ċ import Ġjava . util . HashMap Ċ import Ġjavax . annotation . processing . Processing Environment Ċ import Ġjavax . lang . model . element . Element Ċ Ċ class ĠHttp Server less Function File Builder ( ĊĠĠĠ Ġprocessing Env : ĠProcessing Environment , ĊĠĠĠ Ġfile Builder Method Information : ĠFile Builder Method Information , ĊĠĠĠ Ġcompiling Element : ĠElement , ĊĠĠĠ Ġclass For Reflection Service : ĠClass For Reflection Service Ċ ) Ġ: ĠServer less Function File Builder ( ĊĠĠĠ Ġprocessing Env , ĊĠĠĠ Ġfile Builder Method Information , ĊĠĠĠ ĠHttp Server less Function :: class . java . simple Name , ĊĠĠĠ ĠHttp Event :: class . java , ĊĠĠĠ Ġcompiling Element , ĊĠĠĠ ĠAPI Gateway Proxy Request Event :: class . java , ĊĠĠĠ ĠAPI Gateway Proxy Response Event :: class . java , ĊĠĠĠ Ġclass For Reflection Service Ċ ) Ġ{ ĊĊĠĠĠ Ġoverride Ġfun Ġgenerate ClassName (): ĠString Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġ" HttpServer less Function ${ file Builder Method Information . className }${ file Builder Method Information . methodName }" ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġwrite Imports () Ġ{ ĊĠĠĠĠĠĠĠ Ġwrite () ĊĊĠĠĠĠĠĠĠ Ġwrite (" import Ġ${ Jack son Client :: class . qualified Name }; ") ĊĠĠĠĠĠĠĠ Ġwrite (" import Ġ${ HashMap :: class . qualified Name }; ") ĊĠĠĠĠĠĠĠ Ġwrite (" import Ġ${ HttpResponse :: class . qualified Name }; ") ĊĠĠĠĠĠĠĠ Ġwrite (" import Ġ${ RestApi Gateway Event Mapper :: class . qualified Name }; ") ĊĠĠĠĠĠĠĠ Ġwrite (" import Ġ${ HttpException :: class . qualified Name }; ") ĊĊĠĠĠĠĠĠĠ Ġwrite () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġwrite Function ( input Param : ĠParam , Ġevent Param : ĠParam ) Ġ{ ĊĠĠĠĠĠĠĠ Ġwrite ("${ Http Event :: class . simple Name } Ġevent Ġ= Ġ${ RestApi Gateway Event Mapper :: class . simple Name }. get Http Event ( input , ĠrequestId ); ") ĊĠĠĠĠĠĠĠ Ġif Ġ( input Param . exists ()) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġwrite ("${ input Param . canonical Name ()} Ġparsed Type Ġ= ĠJack son Client . readValue ( input . getBody (), Ġ${ input Param . simple Name ()}. class ); ") ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġval Ġcall Prefix Ġ= Ġif Ġ( void Method Return ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ"" ĊĠĠĠĠĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ"${ file Builder Method Information . returnType } Ġresult Ġ= Ġ" ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġval ĠmethodName Ġ= Ġfile Builder Method Information . methodName ĊĠĠĠĠĠĠĠ Ġwhen Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġinput Param . does NotExist () Ġ&& Ġevent Param . does NotExist () Ġ-> Ġwrite ("${ call Prefix } handler .$ methodName (); ") ĊĠĠĠĠĠĠĠĠĠĠĠ Ġinput Param . does NotExist () Ġ-> Ġwrite ("${ call Prefix } handler .$ methodName ( event ); ") ĊĠĠĠĠĠĠĠĠĠĠĠ Ġevent Param . does NotExist () Ġ-> Ġwrite ("${ call Prefix } handler .$ methodName ( parsed Type ); ") ĊĠĠĠĠĠĠĠĠĠĠĠ Ġinput Param . index Ġ== Ġ 0 Ġ-> Ġwrite ("${ call Prefix } handler .$ methodName ( parsed Type , Ġevent ); ") ĊĠĠĠĠĠĠĠĠĠĠĠ Ġelse Ġ-> Ġwrite ("${ call Prefix } handler .$ methodName ( event , Ġparsed Type ); ") ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġwrite (" API Gateway Proxy Response Event Ġresponse Event Ġ= Ġnew ĠAPI Gateway Proxy Response Event (). with StatusCode ( 2 0 0 ); ") ĊĊĠĠĠĠĠĠĠ Ġif Ġ( file Builder Method Information . returnType . toString () Ġ== ĠHttpResponse :: class . qualified Name ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġwrite (" response Event . set Body ( result . getBody ()); ") ĊĠĠĠĠĠĠĠĠĠĠĠ Ġwrite (" response Event . set Headers ( result . getHeaders ()); ") ĊĠĠĠĠĠĠĠĠĠĠĠ Ġwrite (" response Event . set StatusCode ( result . getStatusCode ()); ") ĊĠĠĠĠĠĠĠĠĠĠĠ Ġwrite (" response Event . setIs Base 6 4 Encoded ( result . is Base 6 4 Encoded ()); ") ĊĠĠĠĠĠĠĠ Ġ} Ġelse Ġif Ġ(! void Method Return ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġwrite (" String Ġresponse Body Ġ= ĠJack son Client . writeValue AsString ( result ); ") ĊĠĠĠĠĠĠĠĠĠĠĠ Ġwrite (" response Event . set Body ( response Body ); ") ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġadd Cors Header (" response Event ") ĊĠĠĠĠĠĠĠ Ġwrite (" return Ġresponse Event ; ") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġwrite Custom ExceptionHandler () Ġ{ ĊĠĠĠĠĠĠĠ Ġwrite (" } Ġcatch Ġ( HttpException Ġe ) Ġ{ ") ĊĠĠĠĠĠĠĠ Ġwrite (" API Gateway Proxy Response Event Ġerror Response Ġ= Ġnew ĠAPI Gateway Proxy Response Event () ") ĊĠĠĠĠĠĠĠ Ġwrite ("\ t . with StatusCode ( e . getStatusCode ()) ") ĊĠĠĠĠĠĠĠ Ġwrite ("\ t . with Body ( e . getMessage ()); ") ĊĠĠĠĠĠĠĠ Ġadd Cors Header (" error Response ") ĊĠĠĠĠĠĠĠ Ġwrite (" return Ġerror Response ; ") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġwrite Handle Error () Ġ{ ĊĠĠĠĠĠĠĠ Ġwrite (" e . printStackTrace (); ") ĊĠĠĠĠĠĠĠ Ġwrite (" API Gateway Proxy Response Event Ġerror Response Ġ= Ġnew ĠAPI Gateway Proxy Response Event (). with StatusCode ( 5 0 0 ); ") ĊĠĠĠĠĠĠĠ Ġadd Cors Header (" error Response ") ĊĠĠĠĠĠĠĠ Ġwrite (" return Ġerror Response ; ") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġadd Cors Header ( variable Name : ĠString ) Ġ{ ĊĠĠĠĠĠĠĠ Ġwrite (" if Ġ($ variable Name . getHeaders () Ġ== Ġnull ) Ġ{ ") ĊĠĠĠĠĠĠĠ Ġwrite ("$ variable Name . set Headers ( new ĠHashMap <> ()); ") ĊĠĠĠĠĠĠĠ Ġwrite (" }") ĊĠĠĠĠĠĠĠ Ġwrite (" if Ġ(!$ variable Name . getHeaders (). containsKey (\" Access - Control - Allow - Origin \" )) Ġ{ ") ĊĠĠĠĠĠĠĠ Ġwrite (" String Ġallowed Cors Origin Ġ= ĠSystem . getenv (\" ${ Nim bus Constants . allowed Origin Env Variable }\ "); ") ĊĠĠĠĠĠĠĠ Ġwrite (" if Ġ( allowed Cors Origin Ġ!= Ġnull Ġ&& Ġ! allowed Cors Origin . equals (\" \" )) Ġ{ ") ĊĠĠĠĠĠĠĠ Ġwrite ("$ variable Name . getHeaders (). put (\" Access - Control - Allow - Origin \", Ġallowed Cors Origin ); ") ĊĠĠĠĠĠĠĠ Ġwrite (" }") ĊĠĠĠĠĠĠĠ Ġwrite (" }") ĊĠĠĠ Ġ} Ċ } Ċ