@@ -25,8 +25,13 @@ abstract class RequestHandler : RequestHandler<APIGatewayProxyRequestEvent, APIG
25
25
private val serializationHandlerChain by lazy { SerializationHandlerChain (serializationHandlers()) }
26
26
private val deserializationHandlerChain by lazy { DeserializationHandlerChain (deserializationHandlers()) }
27
27
28
+ override fun handleRequest (input : APIGatewayProxyRequestEvent , context : Context ): APIGatewayProxyResponseEvent =
29
+ input
30
+ .apply { headers = headers.mapKeys { it.key.toLowerCase() } }
31
+ .let { router.filter.then(this ::handleRequest)(it) }
32
+
28
33
@Suppress(" UNCHECKED_CAST" )
29
- override fun handleRequest (input : APIGatewayProxyRequestEvent , context : Context ): APIGatewayProxyResponseEvent {
34
+ private fun handleRequest (input : APIGatewayProxyRequestEvent ): APIGatewayProxyResponseEvent {
30
35
log.debug(" handling request with method '${input.httpMethod} ' and path '${input.path} ' - Accept:${input.acceptHeader()} Content-Type:${input.contentType()} $input " )
31
36
val routes = router.routes as List <RouterFunction <Any , Any >>
32
37
val matchResults: List <RequestMatchResult > = routes.map { routerFunction: RouterFunction <Any , Any > ->
@@ -37,19 +42,12 @@ abstract class RequestHandler : RequestHandler<APIGatewayProxyRequestEvent, APIG
37
42
val matchedAcceptType = routerFunction.requestPredicate.matchedAcceptType(input.acceptedMediaTypes())
38
43
? : MediaType .parse(router.defaultContentType)
39
44
40
- // Phase 1: Deserialization
41
- // TODO: find a way to also invoke the filter chain on failed deserialization
42
45
val handler: HandlerFunction <Any , Any > = routerFunction.handler
43
- val requestBody = try {
44
- deserializeRequest(handler, input)
45
- } catch (e: Exception ) {
46
- return createResponse(matchedAcceptType, exceptionToResponseEntity(e))
47
- }
48
46
49
- // Phase 2: Content Handling
50
- val request = Request (input, requestBody, routerFunction.requestPredicate.pathPattern)
51
- return createResponse(matchedAcceptType, router.filter.then {
47
+ val response =
52
48
try {
49
+ val requestBody = deserializeRequest(handler, input)
50
+ val request = Request (input, requestBody, routerFunction.requestPredicate.pathPattern)
53
51
when {
54
52
missingPermissions(input, routerFunction) ->
55
53
ResponseEntity (403 , ApiError (" missing permissions" , " MISSING_PERMISSIONS" ))
@@ -58,7 +56,7 @@ abstract class RequestHandler : RequestHandler<APIGatewayProxyRequestEvent, APIG
58
56
} catch (e: Exception ) {
59
57
exceptionToResponseEntity(e, input)
60
58
}
61
- }(request) )
59
+ return createResponse(matchedAcceptType, response )
62
60
}
63
61
matchResult
64
62
}
@@ -106,22 +104,22 @@ abstract class RequestHandler : RequestHandler<APIGatewayProxyRequestEvent, APIG
106
104
when {
107
105
matchResults.any { it.matchPath && it.matchMethod && ! it.matchContentType } ->
108
106
ApiException (
109
- httpResponseStatus = 415 ,
110
- message = " Unsupported Media Type" ,
111
- code = " UNSUPPORTED_MEDIA_TYPE"
112
- )
107
+ httpResponseStatus = 415 ,
108
+ message = " Unsupported Media Type" ,
109
+ code = " UNSUPPORTED_MEDIA_TYPE"
110
+ )
113
111
matchResults.any { it.matchPath && it.matchMethod && ! it.matchAcceptType } ->
114
112
ApiException (
115
- httpResponseStatus = 406 ,
116
- message = " Not Acceptable" ,
117
- code = " NOT_ACCEPTABLE"
118
- )
113
+ httpResponseStatus = 406 ,
114
+ message = " Not Acceptable" ,
115
+ code = " NOT_ACCEPTABLE"
116
+ )
119
117
matchResults.any { it.matchPath && ! it.matchMethod } ->
120
118
ApiException (
121
119
httpResponseStatus = 405 ,
122
120
message = " Method Not Allowed" ,
123
121
code = " METHOD_NOT_ALLOWED"
124
- )
122
+ )
125
123
else -> ApiException (
126
124
httpResponseStatus = 404 ,
127
125
message = " Not found" ,
@@ -215,4 +213,4 @@ abstract class RequestHandler : RequestHandler<APIGatewayProxyRequestEvent, APIG
215
213
companion object {
216
214
val log: Logger = LoggerFactory .getLogger(RequestHandler ::class .java)
217
215
}
218
- }
216
+ }
0 commit comments