From 1e3c680dcc2fda1032d920c00d13c30ef594b774 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Tue, 15 Nov 2016 15:52:48 +0100 Subject: [PATCH] SONAR-8332 add request ID in request under key ID --- .../web/requestid/RequestIdFilter.java | 4 +++- .../web/requestid/RequestIdFilterTest.java | 24 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/web/requestid/RequestIdFilter.java b/server/sonar-server/src/main/java/org/sonar/server/platform/web/requestid/RequestIdFilter.java index 01c5f2b3691..cc2ee2841f7 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/web/requestid/RequestIdFilter.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/web/requestid/RequestIdFilter.java @@ -59,7 +59,9 @@ public class RequestIdFilter implements Filter { if (requestIdGenerator == null) { chain.doFilter(request, response); } else { - try (RequestIdMDCStorage mdcStorage = new RequestIdMDCStorage(requestIdGenerator.generate())) { + String requestId = requestIdGenerator.generate(); + try (RequestIdMDCStorage mdcStorage = new RequestIdMDCStorage(requestId)) { + request.setAttribute("ID", requestId); chain.doFilter(request, response); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/web/requestid/RequestIdFilterTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/web/requestid/RequestIdFilterTest.java index 4ece09fd04b..2bd5c6bfd84 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/platform/web/requestid/RequestIdFilterTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/web/requestid/RequestIdFilterTest.java @@ -32,8 +32,11 @@ import org.sonar.server.platform.Platform; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.fail; +import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; public class RequestIdFilterTest { @@ -86,6 +89,16 @@ public class RequestIdFilterTest { } } + @Test + public void filter_adds_requestId_to_request_passed_on_to_chain() throws IOException, ServletException { + String requestId = "request id"; + when(requestIdGenerator.generate()).thenReturn(requestId); + + underTest.doFilter(servletRequest, servletResponse, filterChain); + + verify(servletRequest).setAttribute("ID", requestId); + } + @Test public void filter_does_not_fail_when_there_is_no_RequestIdGenerator_in_container() throws IOException, ServletException { Platform platform = mock(Platform.class); @@ -94,4 +107,15 @@ public class RequestIdFilterTest { underTest.doFilter(servletRequest, servletResponse, filterChain); } + + @Test + public void filter_does_not_add_requestId_to_request_passed_on_to_chain_when_there_is_no_RequestIdGenerator_in_container() throws IOException, ServletException { + Platform platform = mock(Platform.class); + when(platform.getContainer()).thenReturn(new ComponentContainer()); + RequestIdFilter underTest = new RequestIdFilter(platform); + + underTest.doFilter(servletRequest, servletResponse, filterChain); + + verify(servletRequest, times(0)).setAttribute(anyString(), anyString()); + } } -- 2.39.5