From af16acdd1d54abc22c2b1daeaef0107c50682360 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Fri, 14 Apr 2017 15:00:23 +0200 Subject: [PATCH] SONAR-9138 Improve logs displaying servlet filter patterns at server startup --- .../platform/web/MasterServletFilter.java | 2 +- .../platform/web/MasterServletFilterTest.java | 61 ++++++++++++++++++- .../java/org/sonar/api/web/ServletFilter.java | 18 ++++++ .../org/sonar/api/web/ServletFilterTest.java | 13 ++++ 4 files changed, 91 insertions(+), 3 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/web/MasterServletFilter.java b/server/sonar-server/src/main/java/org/sonar/server/platform/web/MasterServletFilter.java index 9679d4e2c1b..af90a3a96ce 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/web/MasterServletFilter.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/web/MasterServletFilter.java @@ -68,7 +68,7 @@ public class MasterServletFilter implements Filter { List filterList = Lists.newArrayList(); for (ServletFilter extension : filterExtensions) { try { - Loggers.get(MasterServletFilter.class).info(String.format("Initializing servlet filter %s [pattern=%s]", extension, extension.doGetPattern())); + Loggers.get(MasterServletFilter.class).info(String.format("Initializing servlet filter %s [pattern=%s]", extension, extension.doGetPattern().label())); extension.init(config); filterList.add(extension); } catch (Exception e) { diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/web/MasterServletFilterTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/web/MasterServletFilterTest.java index 59a01dcb095..4282814c38a 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/platform/web/MasterServletFilterTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/web/MasterServletFilterTest.java @@ -32,7 +32,10 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.sonar.api.utils.log.LogTester; +import org.sonar.api.utils.log.LoggerLevel; import org.sonar.api.web.ServletFilter; +import org.sonar.api.web.ServletFilter.UrlPattern; import static java.util.Arrays.asList; import static java.util.Collections.singletonList; @@ -48,6 +51,9 @@ public class MasterServletFilterTest { @Rule public ExpectedException expectedException = ExpectedException.none(); + @Rule + public LogTester logTester = new LogTester(); + @Before public void resetSingleton() { MasterServletFilter.INSTANCE = null; @@ -55,7 +61,7 @@ public class MasterServletFilterTest { @Test public void should_init_and_destroy_filters() throws Exception { - ServletFilter filter = mock(ServletFilter.class); + ServletFilter filter = createMockFilter(); FilterConfig config = mock(FilterConfig.class); MasterServletFilter master = new MasterServletFilter(); master.init(config, singletonList(filter)); @@ -81,7 +87,7 @@ public class MasterServletFilterTest { expectedException.expect(IllegalStateException.class); expectedException.expectMessage("foo"); - ServletFilter filter = mock(ServletFilter.class); + ServletFilter filter = createMockFilter(); doThrow(new IllegalStateException("foo")).when(filter).init(any(FilterConfig.class)); FilterConfig config = mock(FilterConfig.class); @@ -122,6 +128,23 @@ public class MasterServletFilterTest { assertThat(filter2.count).isEqualTo(2); } + @Test + public void display_servlet_filter_patterns_in_INFO_log() throws Exception { + ServletFilter filter = new PatternFilter(UrlPattern.builder().includes("/api/issues").excludes("/batch/projects").build()); + FilterConfig config = mock(FilterConfig.class); + MasterServletFilter master = new MasterServletFilter(); + + master.init(config, singletonList(filter)); + + assertThat(logTester.logs(LoggerLevel.INFO)).containsOnly("Initializing servlet filter PatternFilter [pattern=UrlPattern{inclusions=[/api/issues], exclusions=[/batch/projects]}]"); + } + + private static ServletFilter createMockFilter() { + ServletFilter filter = mock(ServletFilter.class); + when(filter.doGetPattern()).thenReturn(UrlPattern.builder().build()); + return filter; + } + private static final class TrueFilter extends ServletFilter { private static int globalCount = 0; private int count = 0; @@ -142,4 +165,38 @@ public class MasterServletFilterTest { } } + private static class PatternFilter extends ServletFilter { + + private final UrlPattern urlPattern; + + PatternFilter(UrlPattern urlPattern) { + this.urlPattern = urlPattern; + } + + @Override + public UrlPattern doGetPattern() { + return urlPattern; + } + + @Override + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { + // Nothing to do + } + + @Override + public void init(FilterConfig filterConfig) throws ServletException { + // Nothing to do + } + + @Override + public void destroy() { + // Nothing to do + } + + @Override + public String toString() { + return "PatternFilter"; + } + } + } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/web/ServletFilter.java b/sonar-plugin-api/src/main/java/org/sonar/api/web/ServletFilter.java index 927cbb6a33c..c71ec53ee43 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/web/ServletFilter.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/web/ServletFilter.java @@ -102,6 +102,24 @@ public abstract class ServletFilter implements Filter { throw new IllegalStateException("this method is deprecated and should not be used anymore"); } + public String label() { + return "UrlPattern{" + + "inclusions=[" + convertPatternsToString(inclusions) + "]" + + ", exclusions=[" + convertPatternsToString(exclusions) + "]" + + '}'; + } + + private static String convertPatternsToString(List input) { + StringBuilder output = new StringBuilder(); + if (input.isEmpty()) { + return ""; + } + if (input.size() == 1) { + return output.append(input.get(0)).toString(); + } + return output.append(input.get(0)).append(", ...").toString(); + } + /** * Defines only a single inclusion pattern. This is a shortcut for {@code builder().includes(inclusionPattern).build()}. */ diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/web/ServletFilterTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/web/ServletFilterTest.java index 21367e83ec4..503a0732b88 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/web/ServletFilterTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/web/ServletFilterTest.java @@ -228,6 +228,19 @@ public class ServletFilterTest { "/mstile*"); } + @Test + public void test_label() throws Exception { + assertThat(ServletFilter.UrlPattern.builder().build().label()).isEqualTo("UrlPattern{inclusions=[], exclusions=[]}"); + assertThat(ServletFilter.UrlPattern.builder() + .includes("/foo/*") + .excludes("/foo/login") + .build().label()).isEqualTo("UrlPattern{inclusions=[/foo/*], exclusions=[/foo/login]}"); + assertThat(ServletFilter.UrlPattern.builder() + .includes("/foo/*", "/foo/lo*") + .excludes("/foo/login", "/foo/logout", "/foo/list") + .build().label()).isEqualTo("UrlPattern{inclusions=[/foo/*, ...], exclusions=[/foo/login, ...]}"); + } + private static class FakeFilter extends ServletFilter { @Override public UrlPattern doGetPattern() { -- 2.39.5