Skip to content
This repository has been archived by the owner on Jun 1, 2024. It is now read-only.

Commit

Permalink
Fixes filter to handle only beagle requests
Browse files Browse the repository at this point in the history
Signed-off-by: Hernand Azevedo <[email protected]>
  • Loading branch information
hernandazevedozup committed Sep 26, 2022
1 parent d444a92 commit 9b7da7b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,22 @@ class BeaglePlatformFilter(private val objectMapper: ObjectMapper) : OncePerRequ

@Suppress("UNCHECKED_CAST")
private fun treatResponse(wrappedResponse: MutableHttpResponse<*>, currentPlatform: String?) {
wrappedResponse.body.ifPresent {
if (it !is NettySystemFileCustomizableResponseType && it !is String) {
val jsonTree = this.objectMapper.readTree(
this.objectMapper.writeValueAsString(it)
)
BeaglePlatformUtil.treatBeaglePlatform(
currentPlatform,
jsonTree
)
(wrappedResponse as MutableHttpResponse<String>).body(this.objectMapper.writeValueAsString(jsonTree))
if (currentPlatform != null) {
wrappedResponse.body.ifPresent {
if (it !is NettySystemFileCustomizableResponseType && it !is String) {
val jsonTree = this.objectMapper.readTree(
this.objectMapper.writeValueAsString(it)
)
BeaglePlatformUtil.treatBeaglePlatform(
currentPlatform,
jsonTree
)
(wrappedResponse as MutableHttpResponse<String>).body(
this.objectMapper.writeValueAsString(
jsonTree
)
)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,29 @@ import javax.servlet.http.HttpServletResponse

class BeaglePlatformFilter(private val objectMapper: ObjectMapper) : Filter {

override fun doFilter(request: ServletRequest?, response: ServletResponse?, chain: FilterChain?) {
if (chain != null && request is HttpServletRequest && response is HttpServletResponse) {
request.setAttribute(
BeaglePlatformUtil.BEAGLE_PLATFORM_HEADER,
request.getHeader(BeaglePlatformUtil.BEAGLE_PLATFORM_HEADER)
)
val responseWrapper = ContentCachingResponseWrapper(response)
override fun doFilter(
request: ServletRequest?,
response: ServletResponse?,
chain: FilterChain?
) {
val httpServletResponse = response as? HttpServletResponse
val httpServletRequest = request as? HttpServletRequest
val platformHeader = httpServletRequest?.getHeader(BeaglePlatformUtil.BEAGLE_PLATFORM_HEADER)
val isServletRequestResponse = request is HttpServletRequest && response is HttpServletResponse

if (chain != null && isServletRequestResponse && platformHeader != null) {
request.setAttribute(BeaglePlatformUtil.BEAGLE_PLATFORM_HEADER, platformHeader)
val responseWrapper = ContentCachingResponseWrapper(httpServletResponse)
chain.doFilter(request, responseWrapper)
treatResponse(responseWrapper, request.getHeader(BeaglePlatformUtil.BEAGLE_PLATFORM_HEADER))
treatResponse(responseWrapper, platformHeader)
responseWrapper.copyBodyToResponse()
}
}

private fun treatResponse(responseWrapper: ContentCachingResponseWrapper, currentPlatform: String?) {
private fun treatResponse(
responseWrapper: ContentCachingResponseWrapper,
currentPlatform: String?
) {
if (responseWrapper.contentType == MediaType.APPLICATION_JSON_VALUE) {
val jsonData = this.objectMapper.readTree(responseWrapper.contentAsByteArray).also {
BeaglePlatformUtil.treatBeaglePlatform(currentPlatform, it)
Expand Down

0 comments on commit 9b7da7b

Please sign in to comment.