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

feat: Fixes filter to handle only beagle requests #98

Merged
merged 3 commits into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,32 @@ 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 isServletRequestResponse = request is HttpServletRequest && response is HttpServletResponse
val httpServletResponse = response as? HttpServletResponse
val httpServletRequest = request as? HttpServletRequest
val platformHeader =
if (isServletRequestResponse)
httpServletRequest?.getHeader(BeaglePlatformUtil.BEAGLE_PLATFORM_HEADER)
else null

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()
}
hernandazevedozup marked this conversation as resolved.
Show resolved Hide resolved
}

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