From 9b7da7bc4046e30a5362888f9149d7d9da60d0d7 Mon Sep 17 00:00:00 2001 From: Hernand Azevedo Date: Mon, 26 Sep 2022 14:49:49 -0300 Subject: [PATCH 1/3] Fixes filter to handle only beagle requests Signed-off-by: Hernand Azevedo --- .../micronaut/filter/BeaglePlatformFilter.kt | 26 +++++++++++------- .../spring/filter/BeaglePlatformFilter.kt | 27 ++++++++++++------- 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/starters/micronaut/src/main/kotlin/br/com/zup/beagle/micronaut/filter/BeaglePlatformFilter.kt b/starters/micronaut/src/main/kotlin/br/com/zup/beagle/micronaut/filter/BeaglePlatformFilter.kt index d86b7f5..88c1f2f 100644 --- a/starters/micronaut/src/main/kotlin/br/com/zup/beagle/micronaut/filter/BeaglePlatformFilter.kt +++ b/starters/micronaut/src/main/kotlin/br/com/zup/beagle/micronaut/filter/BeaglePlatformFilter.kt @@ -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).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).body( + this.objectMapper.writeValueAsString( + jsonTree + ) + ) + } } } } diff --git a/starters/spring/src/main/kotlin/br/com/zup/beagle/spring/filter/BeaglePlatformFilter.kt b/starters/spring/src/main/kotlin/br/com/zup/beagle/spring/filter/BeaglePlatformFilter.kt index 7857ced..8bb1af0 100644 --- a/starters/spring/src/main/kotlin/br/com/zup/beagle/spring/filter/BeaglePlatformFilter.kt +++ b/starters/spring/src/main/kotlin/br/com/zup/beagle/spring/filter/BeaglePlatformFilter.kt @@ -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) From 309b0560d5d9b95d28f994859cbc20e428ca7345 Mon Sep 17 00:00:00 2001 From: Hernand Azevedo Date: Mon, 26 Sep 2022 15:17:52 -0300 Subject: [PATCH 2/3] Fixes filter to handle only beagle requests Signed-off-by: Hernand Azevedo --- .../com/zup/beagle/spring/filter/BeaglePlatformFilter.kt | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/starters/spring/src/main/kotlin/br/com/zup/beagle/spring/filter/BeaglePlatformFilter.kt b/starters/spring/src/main/kotlin/br/com/zup/beagle/spring/filter/BeaglePlatformFilter.kt index 8bb1af0..cdac30f 100644 --- a/starters/spring/src/main/kotlin/br/com/zup/beagle/spring/filter/BeaglePlatformFilter.kt +++ b/starters/spring/src/main/kotlin/br/com/zup/beagle/spring/filter/BeaglePlatformFilter.kt @@ -34,13 +34,16 @@ class BeaglePlatformFilter(private val objectMapper: ObjectMapper) : Filter { response: ServletResponse?, chain: FilterChain? ) { + val isServletRequestResponse = request is HttpServletRequest && response is HttpServletResponse 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 + 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) + request?.setAttribute(BeaglePlatformUtil.BEAGLE_PLATFORM_HEADER, platformHeader) val responseWrapper = ContentCachingResponseWrapper(httpServletResponse) chain.doFilter(request, responseWrapper) treatResponse(responseWrapper, platformHeader) From 0cca475f07a1a1d25651a8bb4d69d3165302ee73 Mon Sep 17 00:00:00 2001 From: Hernand Azevedo Date: Mon, 26 Sep 2022 15:35:39 -0300 Subject: [PATCH 3/3] Fixes filter to handle only beagle requests Signed-off-by: Hernand Azevedo --- .../com/zup/beagle/spring/filter/BeaglePlatformFilter.kt | 2 ++ .../com/zup/beagle/spring/filter/BeagleFilterTestUtils.kt | 2 +- .../zup/beagle/spring/filter/BeaglePlatformFilterTest.kt | 8 ++++---- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/starters/spring/src/main/kotlin/br/com/zup/beagle/spring/filter/BeaglePlatformFilter.kt b/starters/spring/src/main/kotlin/br/com/zup/beagle/spring/filter/BeaglePlatformFilter.kt index cdac30f..fa69969 100644 --- a/starters/spring/src/main/kotlin/br/com/zup/beagle/spring/filter/BeaglePlatformFilter.kt +++ b/starters/spring/src/main/kotlin/br/com/zup/beagle/spring/filter/BeaglePlatformFilter.kt @@ -48,6 +48,8 @@ class BeaglePlatformFilter(private val objectMapper: ObjectMapper) : Filter { chain.doFilter(request, responseWrapper) treatResponse(responseWrapper, platformHeader) responseWrapper.copyBodyToResponse() + } else { + chain?.doFilter(request, response) } } diff --git a/starters/spring/src/test/kotlin/br/com/zup/beagle/spring/filter/BeagleFilterTestUtils.kt b/starters/spring/src/test/kotlin/br/com/zup/beagle/spring/filter/BeagleFilterTestUtils.kt index 79e79f4..90c4720 100644 --- a/starters/spring/src/test/kotlin/br/com/zup/beagle/spring/filter/BeagleFilterTestUtils.kt +++ b/starters/spring/src/test/kotlin/br/com/zup/beagle/spring/filter/BeagleFilterTestUtils.kt @@ -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) } } \ No newline at end of file diff --git a/starters/spring/src/test/kotlin/br/com/zup/beagle/spring/filter/BeaglePlatformFilterTest.kt b/starters/spring/src/test/kotlin/br/com/zup/beagle/spring/filter/BeaglePlatformFilterTest.kt index f949aa4..39ad625 100644 --- a/starters/spring/src/test/kotlin/br/com/zup/beagle/spring/filter/BeaglePlatformFilterTest.kt +++ b/starters/spring/src/test/kotlin/br/com/zup/beagle/spring/filter/BeaglePlatformFilterTest.kt @@ -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(), mockk()) + this.testPlatformFilterIsNoOp(mockk(), mockk(), 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(), mockk(), mockk()) + this.testPlatformFilterIsNoOp(mockk(), mockk(), mockk(relaxUnitFun = true)) @Test fun `doFilter when chain is null`() =