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

Commit

Permalink
feat: Fixes filter to handle only beagle requests (#98)
Browse files Browse the repository at this point in the history
* Fixes filter to handle only beagle requests

Signed-off-by: Hernand Azevedo <[email protected]>

* Fixes filter to handle only beagle requests

Signed-off-by: Hernand Azevedo <[email protected]>

* Fixes filter to handle only beagle requests

Signed-off-by: Hernand Azevedo <[email protected]>

Signed-off-by: Hernand Azevedo <[email protected]>
  • Loading branch information
hernandazevedozup authored Sep 26, 2022
1 parent d444a92 commit 60cd067
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 24 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,34 @@ 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()
} else {
chain?.doFilter(request, response)
}
}

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
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ fun testFilterIsNoOp(filter: Filter, request: ServletRequest?, response: Servlet
filter.doFilter(request, response, chain)
if (request != null) verify { request wasNot Called }
if (response != null) verify { response wasNot Called }
if (chain != null) verify { chain wasNot Called }
if (chain != null) verify { chain.doFilter(request, response) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,19 @@ internal class BeaglePlatformFilterTest {

@Test
fun `doFilter when request is null`() =
this.testPlatformFilterIsNoOp(null, mockk(), mockk())
this.testPlatformFilterIsNoOp(null, mockk(), mockk(relaxUnitFun = true))

@Test
fun `doFilter when request is not HTTPServletRequest`() =
this.testPlatformFilterIsNoOp(mockk(), mockk<HttpServletResponse>(), mockk())
this.testPlatformFilterIsNoOp(mockk(), mockk<HttpServletResponse>(), mockk(relaxUnitFun = true))

@Test
fun `doFilter when response is null`() =
this.testPlatformFilterIsNoOp(mockk(), null, mockk())
this.testPlatformFilterIsNoOp(mockk(), null, mockk(relaxUnitFun = true))

@Test
fun `doFilter when response is not HTTPServletResponse`() =
this.testPlatformFilterIsNoOp(mockk<HttpServletRequest>(), mockk(), mockk())
this.testPlatformFilterIsNoOp(mockk<HttpServletRequest>(), mockk(), mockk(relaxUnitFun = true))

@Test
fun `doFilter when chain is null`() =
Expand Down

0 comments on commit 60cd067

Please sign in to comment.