diff options
author | antoine.vinot <antoine.vinot@sonarsource.com> | 2023-12-12 09:07:34 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2023-12-19 20:02:55 +0000 |
commit | 6a2f77f0e34e09ddd7202e92e685948229570f3f (patch) | |
tree | 676d6a86b5a26dfc936359ee6f6c53b75425a691 /server/sonar-webserver/src | |
parent | 7fe24812e4dccac7aebff9b2c5109727a76af56c (diff) | |
download | sonarqube-6a2f77f0e34e09ddd7202e92e685948229570f3f.tar.gz sonarqube-6a2f77f0e34e09ddd7202e92e685948229570f3f.zip |
SONAR-21227 Configure a new Logger file for deprecated API usages
Diffstat (limited to 'server/sonar-webserver/src')
2 files changed, 145 insertions, 0 deletions
diff --git a/server/sonar-webserver/src/it/java/org/sonar/server/platform/web/EndpointPathFilterTest.java b/server/sonar-webserver/src/it/java/org/sonar/server/platform/web/EndpointPathFilterTest.java new file mode 100644 index 00000000000..f838137cd56 --- /dev/null +++ b/server/sonar-webserver/src/it/java/org/sonar/server/platform/web/EndpointPathFilterTest.java @@ -0,0 +1,89 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.platform.web; + +import java.io.IOException; +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.MDC; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class EndpointPathFilterTest { + + private static final String ENDPOINT_PATH = "/api/system/status"; + private static final String ENTRYPOINT_MDC_KEY = "ENTRYPOINT"; + + private final HttpServletRequest servletRequest = mock(HttpServletRequest.class); + private final HttpServletResponse servletResponse = mock(HttpServletResponse.class); + private final FilterChain filterChain = mock(FilterChain.class); + private final EndpointPathFilter endpointPathFilter = new EndpointPathFilter(); + + @Before + public void setUp() { + when(servletRequest.getRequestURI()).thenReturn(ENDPOINT_PATH); + } + + @Test + public void doFilter_shouldPutEndpointToMDCAndRemoveItAfterChainExecution() throws ServletException, IOException { + doAnswer(invocation -> assertThat(MDC.get("ENTRYPOINT")).isEqualTo(ENDPOINT_PATH)) + .when(filterChain) + .doFilter(servletRequest, servletResponse); + + endpointPathFilter.doFilter(servletRequest, servletResponse, filterChain); + + assertThat(MDC.get(ENTRYPOINT_MDC_KEY)).isNull(); + } + + @Test + public void doFilter_whenChainFails_shouldPutInMDCAndRemoveItAfter() throws IOException, ServletException { + RuntimeException exception = new RuntimeException("Simulating chain failing"); + doAnswer(invocation -> { + assertThat(MDC.get(ENTRYPOINT_MDC_KEY)).isEqualTo(ENDPOINT_PATH); + throw exception; + }) + .when(filterChain) + .doFilter(servletRequest, servletResponse); + + assertThatThrownBy(() -> endpointPathFilter.doFilter(servletRequest, servletResponse, filterChain)).isEqualTo(exception); + assertThat(MDC.get(ENTRYPOINT_MDC_KEY)).isNull(); + } + + @Test + public void doFilter_whenNotHttpServletRequest_shouldAddEmptyPath() throws ServletException, IOException { + doAnswer(invocation -> assertThat(MDC.get("ENTRYPOINT")).isEqualTo("-")) + .when(filterChain) + .doFilter(servletRequest, servletResponse); + + endpointPathFilter.doFilter(mock(ServletRequest.class), servletResponse, filterChain); + + assertThat(MDC.get(ENTRYPOINT_MDC_KEY)).isNull(); + } + +} diff --git a/server/sonar-webserver/src/main/java/org/sonar/server/platform/web/EndpointPathFilter.java b/server/sonar-webserver/src/main/java/org/sonar/server/platform/web/EndpointPathFilter.java new file mode 100644 index 00000000000..c60921b8ea9 --- /dev/null +++ b/server/sonar-webserver/src/main/java/org/sonar/server/platform/web/EndpointPathFilter.java @@ -0,0 +1,56 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.platform.web; + +import java.io.IOException; +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import org.sonar.server.platform.web.logging.EntrypointMDCStorage; + +public class EndpointPathFilter implements Filter { + + @Override + public void init(FilterConfig filterConfig) throws ServletException { + // nothing to do + } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + String endpointPath = null; + if (request instanceof HttpServletRequest httpRequest) { + endpointPath = httpRequest.getRequestURI(); + } + + try (EntrypointMDCStorage entrypointMDCStorage = new EntrypointMDCStorage(endpointPath)) { + chain.doFilter(request, response); + } + + } + + @Override + public void destroy() { + // nothing to do + } +} |