From 0d62b0cb3ccb1c1902e2df4b7a2a39880fe811dc Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Thu, 14 Sep 2017 12:34:17 +0200 Subject: [PATCH] Move configuration of TraceEndpoint See gh-10263 --- .../trace/TraceEndpointProperties.java | 52 ++++++ .../TraceWebFilterAutoConfiguration.java | 14 +- .../TraceWebFilterAutoConfigurationTests.java | 7 +- .../boot/actuate/trace/Include.java | 131 +++++++++++++++ .../boot/actuate/trace/TraceProperties.java | 151 ------------------ .../actuate/trace/WebRequestTraceFilter.java | 21 ++- .../trace/WebRequestTraceFilterTests.java | 60 +++---- 7 files changed, 239 insertions(+), 197 deletions(-) create mode 100644 spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/trace/TraceEndpointProperties.java create mode 100644 spring-boot-actuator/src/main/java/org/springframework/boot/actuate/trace/Include.java delete mode 100644 spring-boot-actuator/src/main/java/org/springframework/boot/actuate/trace/TraceProperties.java diff --git a/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/trace/TraceEndpointProperties.java b/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/trace/TraceEndpointProperties.java new file mode 100644 index 0000000000..a3138a1c69 --- /dev/null +++ b/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/trace/TraceEndpointProperties.java @@ -0,0 +1,52 @@ +/* + * Copyright 2012-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.actuate.autoconfigure.trace; + +import java.util.HashSet; +import java.util.Set; + +import org.springframework.boot.actuate.trace.Include; +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * Configuration properties for tracing. + * + * @author Wallace Wadge + * @author Phillip Webb + * @author Venil Noronha + * @author Madhura Bhave + * @author Stephane Nicoll + * @since 1.3.0 + */ +@ConfigurationProperties(prefix = "management.trace") +public class TraceEndpointProperties { + + /** + * Items to be included in the trace. Defaults to request/response headers (including + * cookies) and errors. + */ + private Set include = new HashSet<>(Include.defaultIncludes()); + + public Set getInclude() { + return this.include; + } + + public void setInclude(Set include) { + this.include = include; + } + +} diff --git a/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/trace/TraceWebFilterAutoConfiguration.java b/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/trace/TraceWebFilterAutoConfiguration.java index a9fa4c5d6d..b7674f9c80 100644 --- a/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/trace/TraceWebFilterAutoConfiguration.java +++ b/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/trace/TraceWebFilterAutoConfiguration.java @@ -19,9 +19,7 @@ package org.springframework.boot.actuate.autoconfigure.trace; import javax.servlet.Servlet; import javax.servlet.ServletRegistration; -import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.ObjectProvider; -import org.springframework.boot.actuate.trace.TraceProperties; import org.springframework.boot.actuate.trace.TraceRepository; import org.springframework.boot.actuate.trace.WebRequestTraceFilter; import org.springframework.boot.autoconfigure.AutoConfigureAfter; @@ -46,28 +44,28 @@ import org.springframework.web.servlet.DispatcherServlet; @ConditionalOnClass({ Servlet.class, DispatcherServlet.class, ServletRegistration.class }) @AutoConfigureAfter(TraceRepositoryAutoConfiguration.class) @ConditionalOnProperty(prefix = "management.trace.filter", name = "enabled", matchIfMissing = true) -@EnableConfigurationProperties(TraceProperties.class) +@EnableConfigurationProperties(TraceEndpointProperties.class) public class TraceWebFilterAutoConfiguration { private final TraceRepository traceRepository; - private final TraceProperties traceProperties; + private final TraceEndpointProperties endpointProperties; private final ErrorAttributes errorAttributes; public TraceWebFilterAutoConfiguration(TraceRepository traceRepository, - TraceProperties traceProperties, + TraceEndpointProperties endpointProperties, ObjectProvider errorAttributes) { this.traceRepository = traceRepository; - this.traceProperties = traceProperties; + this.endpointProperties = endpointProperties; this.errorAttributes = errorAttributes.getIfAvailable(); } @Bean @ConditionalOnMissingBean - public WebRequestTraceFilter webRequestLoggingFilter(BeanFactory beanFactory) { + public WebRequestTraceFilter webRequestLoggingFilter() { WebRequestTraceFilter filter = new WebRequestTraceFilter(this.traceRepository, - this.traceProperties); + this.endpointProperties.getInclude()); if (this.errorAttributes != null) { filter.setErrorAttributes(this.errorAttributes); } diff --git a/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/trace/TraceWebFilterAutoConfigurationTests.java b/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/trace/TraceWebFilterAutoConfigurationTests.java index a5cc306c58..2bf5bc39bb 100644 --- a/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/trace/TraceWebFilterAutoConfigurationTests.java +++ b/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/trace/TraceWebFilterAutoConfigurationTests.java @@ -21,7 +21,6 @@ import java.util.Map; import org.junit.After; import org.junit.Test; -import org.springframework.boot.actuate.trace.TraceProperties; import org.springframework.boot.actuate.trace.TraceRepository; import org.springframework.boot.actuate.trace.WebRequestTraceFilter; import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; @@ -91,7 +90,7 @@ public class TraceWebFilterAutoConfigurationTests { @Bean public TestWebRequestTraceFilter testWebRequestTraceFilter( - TraceRepository repository, TraceProperties properties) { + TraceRepository repository, TraceEndpointProperties properties) { return new TestWebRequestTraceFilter(repository, properties); } @@ -100,8 +99,8 @@ public class TraceWebFilterAutoConfigurationTests { static class TestWebRequestTraceFilter extends WebRequestTraceFilter { TestWebRequestTraceFilter(TraceRepository repository, - TraceProperties properties) { - super(repository, properties); + TraceEndpointProperties properties) { + super(repository, properties.getInclude()); } @Override diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/trace/Include.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/trace/Include.java new file mode 100644 index 0000000000..29f3f096dc --- /dev/null +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/trace/Include.java @@ -0,0 +1,131 @@ +/* + * Copyright 2012-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.actuate.trace; + +import java.util.Collections; +import java.util.LinkedHashSet; +import java.util.Set; + +/** + * Include options for tracing. + * + * @author Wallace Wadge + * @since 2.0.0 + */ +public enum Include { + + /** + * Include request headers. + */ + REQUEST_HEADERS, + + /** + * Include response headers. + */ + RESPONSE_HEADERS, + + /** + * Include "Cookie" in request and "Set-Cookie" in response headers. + */ + COOKIES, + + /** + * Include authorization header (if any). + */ + AUTHORIZATION_HEADER, + + /** + * Include errors (if any). + */ + ERRORS, + + /** + * Include path info. + */ + PATH_INFO, + + /** + * Include the translated path. + */ + PATH_TRANSLATED, + + /** + * Include the context path. + */ + CONTEXT_PATH, + + /** + * Include the user principal. + */ + USER_PRINCIPAL, + + /** + * Include the parameters. + */ + PARAMETERS, + + /** + * Include the query string. + */ + QUERY_STRING, + + /** + * Include the authentication type. + */ + AUTH_TYPE, + + /** + * Include the remote address. + */ + REMOTE_ADDRESS, + + /** + * Include the session ID. + */ + SESSION_ID, + + /** + * Include the remote user. + */ + REMOTE_USER, + + /** + * Include the time taken to service the request in milliseconds. + */ + TIME_TAKEN; + + private static final Set DEFAULT_INCLUDES; + + static { + Set defaultIncludes = new LinkedHashSet<>(); + defaultIncludes.add(Include.REQUEST_HEADERS); + defaultIncludes.add(Include.RESPONSE_HEADERS); + defaultIncludes.add(Include.COOKIES); + defaultIncludes.add(Include.ERRORS); + defaultIncludes.add(Include.TIME_TAKEN); + DEFAULT_INCLUDES = Collections.unmodifiableSet(defaultIncludes); + } + + /** + * Return the default {@link Include}. + * @return the default include. + */ + public static Set defaultIncludes() { + return DEFAULT_INCLUDES; + } + +} diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/trace/TraceProperties.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/trace/TraceProperties.java deleted file mode 100644 index a74def4238..0000000000 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/trace/TraceProperties.java +++ /dev/null @@ -1,151 +0,0 @@ -/* - * Copyright 2012-2017 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.boot.actuate.trace; - -import java.util.Collections; -import java.util.HashSet; -import java.util.LinkedHashSet; -import java.util.Set; - -import org.springframework.boot.context.properties.ConfigurationProperties; - -/** - * Configuration properties for tracing. - * - * @author Wallace Wadge - * @author Phillip Webb - * @author Venil Noronha - * @author Madhura Bhave - * @since 1.3.0 - */ -@ConfigurationProperties(prefix = "management.trace") -public class TraceProperties { - - private static final Set DEFAULT_INCLUDES; - - static { - Set defaultIncludes = new LinkedHashSet<>(); - defaultIncludes.add(Include.REQUEST_HEADERS); - defaultIncludes.add(Include.RESPONSE_HEADERS); - defaultIncludes.add(Include.COOKIES); - defaultIncludes.add(Include.ERRORS); - defaultIncludes.add(Include.TIME_TAKEN); - DEFAULT_INCLUDES = Collections.unmodifiableSet(defaultIncludes); - } - - /** - * Items to be included in the trace. Defaults to request/response headers (including - * cookies) and errors. - */ - private Set include = new HashSet<>(DEFAULT_INCLUDES); - - public Set getInclude() { - return this.include; - } - - public void setInclude(Set include) { - this.include = include; - } - - /** - * Include options for tracing. - */ - public enum Include { - - /** - * Include request headers. - */ - REQUEST_HEADERS, - - /** - * Include response headers. - */ - RESPONSE_HEADERS, - - /** - * Include "Cookie" in request and "Set-Cookie" in response headers. - */ - COOKIES, - - /** - * Include authorization header (if any). - */ - AUTHORIZATION_HEADER, - - /** - * Include errors (if any). - */ - ERRORS, - - /** - * Include path info. - */ - PATH_INFO, - - /** - * Include the translated path. - */ - PATH_TRANSLATED, - - /** - * Include the context path. - */ - CONTEXT_PATH, - - /** - * Include the user principal. - */ - USER_PRINCIPAL, - - /** - * Include the parameters. - */ - PARAMETERS, - - /** - * Include the query string. - */ - QUERY_STRING, - - /** - * Include the authentication type. - */ - AUTH_TYPE, - - /** - * Include the remote address. - */ - REMOTE_ADDRESS, - - /** - * Include the session ID. - */ - SESSION_ID, - - /** - * Include the remote user. - */ - REMOTE_USER, - - /** - * Include the time taken to service the request in milliseconds. - */ - TIME_TAKEN - - } - -} diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/trace/WebRequestTraceFilter.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/trace/WebRequestTraceFilter.java index 06a7e633c6..5c2075a2e6 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/trace/WebRequestTraceFilter.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/trace/WebRequestTraceFilter.java @@ -38,7 +38,6 @@ import javax.servlet.http.HttpSession; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.springframework.boot.actuate.trace.TraceProperties.Include; import org.springframework.boot.web.servlet.error.ErrorAttributes; import org.springframework.core.Ordered; import org.springframework.http.HttpStatus; @@ -68,16 +67,26 @@ public class WebRequestTraceFilter extends OncePerRequestFilter implements Order private ErrorAttributes errorAttributes; - private final TraceProperties properties; + private final Set includes; /** * Create a new {@link WebRequestTraceFilter} instance. * @param repository the trace repository - * @param properties the trace properties + * @param includes the {@link Include} to apply */ - public WebRequestTraceFilter(TraceRepository repository, TraceProperties properties) { + public WebRequestTraceFilter(TraceRepository repository, Set includes) { this.repository = repository; - this.properties = properties; + this.includes = includes; + } + + /** + * Create a new {@link WebRequestTraceFilter} instance with the default + * {@link Include} to apply. + * @param repository the trace repository + * @see Include#defaultIncludes() + */ + public WebRequestTraceFilter(TraceRepository repository) { + this(repository, Include.defaultIncludes()); } /** @@ -247,7 +256,7 @@ public class WebRequestTraceFilter extends OncePerRequestFilter implements Order } private boolean isIncluded(Include include) { - return this.properties.getInclude().contains(include); + return this.includes.contains(include); } public void setErrorAttributes(ErrorAttributes errorAttributes) { diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/trace/WebRequestTraceFilterTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/trace/WebRequestTraceFilterTests.java index a6fbd41a78..2eeb30e3d3 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/trace/WebRequestTraceFilterTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/trace/WebRequestTraceFilterTests.java @@ -28,7 +28,6 @@ import javax.servlet.ServletException; import org.junit.Test; -import org.springframework.boot.actuate.trace.TraceProperties.Include; import org.springframework.boot.web.servlet.error.DefaultErrorAttributes; import org.springframework.mock.web.MockFilterChain; import org.springframework.mock.web.MockHttpServletRequest; @@ -55,17 +54,13 @@ public class WebRequestTraceFilterTests { private final InMemoryTraceRepository repository = new InMemoryTraceRepository(); - private TraceProperties properties = new TraceProperties(); - - private WebRequestTraceFilter filter = new WebRequestTraceFilter(this.repository, - this.properties); - @Test @SuppressWarnings("unchecked") public void filterAddsTraceWithDefaultIncludes() { + WebRequestTraceFilter filter = new WebRequestTraceFilter(this.repository); MockHttpServletRequest request = spy(new MockHttpServletRequest("GET", "/foo")); request.addHeader("Accept", "application/json"); - Map trace = this.filter.getTrace(request); + Map trace = filter.getTrace(request); assertThat(trace.get("method")).isEqualTo("GET"); assertThat(trace.get("path")).isEqualTo("/foo"); Map map = (Map) trace.get("headers"); @@ -76,7 +71,8 @@ public class WebRequestTraceFilterTests { @Test @SuppressWarnings({ "rawtypes", "unchecked" }) public void filterAddsTraceWithCustomIncludes() throws IOException, ServletException { - this.properties.setInclude(EnumSet.allOf(Include.class)); + WebRequestTraceFilter filter = new WebRequestTraceFilter(this.repository, + EnumSet.allOf(Include.class)); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); request.addHeader("Accept", "application/json"); request.addHeader("Cookie", "testCookie=testValue;"); @@ -95,7 +91,7 @@ public class WebRequestTraceFilterTests { MockHttpServletResponse response = new MockHttpServletResponse(); response.addHeader("Content-Type", "application/json"); response.addHeader("Set-Cookie", "a=b"); - this.filter.doFilterInternal(request, response, (req, resp) -> { + filter.doFilterInternal(request, response, (req, resp) -> { BufferedReader bufferedReader = req.getReader(); while (bufferedReader.readLine() != null) { // read the contents as normal (forces cache to fill up) @@ -126,11 +122,12 @@ public class WebRequestTraceFilterTests { @SuppressWarnings({ "unchecked" }) public void filterDoesNotAddResponseHeadersWithoutResponseHeadersInclude() throws ServletException, IOException { - this.properties.setInclude(Collections.singleton(Include.REQUEST_HEADERS)); + WebRequestTraceFilter filter = new WebRequestTraceFilter(this.repository, + Collections.singleton(Include.REQUEST_HEADERS)); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); MockHttpServletResponse response = new MockHttpServletResponse(); response.addHeader("Content-Type", "application/json"); - this.filter.doFilterInternal(request, response, (req, resp) -> { + filter.doFilterInternal(request, response, (req, resp) -> { }); Map info = this.repository.findAll().iterator().next().getInfo(); Map headers = (Map) info.get("headers"); @@ -141,11 +138,12 @@ public class WebRequestTraceFilterTests { @SuppressWarnings({ "unchecked" }) public void filterDoesNotAddRequestCookiesWithCookiesExclude() throws ServletException, IOException { - this.properties.setInclude(Collections.singleton(Include.REQUEST_HEADERS)); + WebRequestTraceFilter filter = new WebRequestTraceFilter(this.repository, + Collections.singleton(Include.REQUEST_HEADERS)); MockHttpServletRequest request = spy(new MockHttpServletRequest("GET", "/foo")); request.addHeader("Accept", "application/json"); request.addHeader("Cookie", "testCookie=testValue;"); - Map map = (Map) this.filter.getTrace(request) + Map map = (Map) filter.getTrace(request) .get("headers"); assertThat(map.get("request").toString()).isEqualTo("{Accept=application/json}"); } @@ -154,10 +152,11 @@ public class WebRequestTraceFilterTests { @SuppressWarnings({ "unchecked" }) public void filterDoesNotAddAuthorizationHeaderWithoutAuthorizationHeaderInclude() throws ServletException, IOException { + WebRequestTraceFilter filter = new WebRequestTraceFilter(this.repository); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); request.addHeader("Authorization", "my-auth-header"); MockHttpServletResponse response = new MockHttpServletResponse(); - this.filter.doFilterInternal(request, response, (req, resp) -> { + filter.doFilterInternal(request, response, (req, resp) -> { }); Map info = this.repository.findAll().iterator().next().getInfo(); Map headers = (Map) info.get("headers"); @@ -168,12 +167,12 @@ public class WebRequestTraceFilterTests { @SuppressWarnings({ "unchecked" }) public void filterAddsAuthorizationHeaderWhenAuthorizationHeaderIncluded() throws ServletException, IOException { - this.properties.setInclude( + WebRequestTraceFilter filter = new WebRequestTraceFilter(this.repository, EnumSet.of(Include.REQUEST_HEADERS, Include.AUTHORIZATION_HEADER)); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); request.addHeader("Authorization", "my-auth-header"); MockHttpServletResponse response = new MockHttpServletResponse(); - this.filter.doFilterInternal(request, response, (req, resp) -> { + filter.doFilterInternal(request, response, (req, resp) -> { }); Map info = this.repository.findAll().iterator().next().getInfo(); Map headers = (Map) info.get("headers"); @@ -185,13 +184,14 @@ public class WebRequestTraceFilterTests { @SuppressWarnings({ "unchecked" }) public void filterDoesNotAddResponseCookiesWithCookiesExclude() throws ServletException, IOException { - this.properties.setInclude(Collections.singleton(Include.RESPONSE_HEADERS)); + WebRequestTraceFilter filter = new WebRequestTraceFilter(this.repository, + Collections.singleton(Include.RESPONSE_HEADERS)); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); MockHttpServletResponse response = new MockHttpServletResponse(); response.addHeader("Content-Type", "application/json"); response.addHeader("Set-Cookie", "testCookie=testValue;"); - Map trace = this.filter.getTrace(request); - this.filter.enhanceTrace(trace, response); + Map trace = filter.getTrace(request); + filter.enhanceTrace(trace, response); Map map = (Map) trace.get("headers"); assertThat(map.get("response").toString()) .isEqualTo("{Content-Type=application/json, status=200}"); @@ -199,12 +199,13 @@ public class WebRequestTraceFilterTests { @Test public void filterHasResponseStatus() { + WebRequestTraceFilter filter = new WebRequestTraceFilter(this.repository); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); MockHttpServletResponse response = new MockHttpServletResponse(); response.setStatus(404); response.addHeader("Content-Type", "application/json"); - Map trace = this.filter.getTrace(request); - this.filter.enhanceTrace(trace, response); + Map trace = filter.getTrace(request); + filter.enhanceTrace(trace, response); @SuppressWarnings("unchecked") Map map = (Map) ((Map) trace .get("headers")).get("response"); @@ -213,10 +214,11 @@ public class WebRequestTraceFilterTests { @Test public void filterAddsTimeTaken() throws Exception { + WebRequestTraceFilter filter = new WebRequestTraceFilter(this.repository); MockHttpServletRequest request = spy(new MockHttpServletRequest("GET", "/foo")); MockHttpServletResponse response = new MockHttpServletResponse(); MockFilterChain chain = new MockFilterChain(); - this.filter.doFilter(request, response, chain); + filter.doFilter(request, response, chain); String timeTaken = (String) this.repository.findAll().iterator().next().getInfo() .get("timeTaken"); assertThat(timeTaken).isNotNull(); @@ -224,15 +226,16 @@ public class WebRequestTraceFilterTests { @Test public void filterHasError() { - this.filter.setErrorAttributes(new DefaultErrorAttributes()); + WebRequestTraceFilter filter = new WebRequestTraceFilter(this.repository); + filter.setErrorAttributes(new DefaultErrorAttributes()); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); MockHttpServletResponse response = new MockHttpServletResponse(); response.setStatus(500); request.setAttribute("javax.servlet.error.exception", new IllegalStateException("Foo")); response.addHeader("Content-Type", "application/json"); - Map trace = this.filter.getTrace(request); - this.filter.enhanceTrace(trace, response); + Map trace = filter.getTrace(request); + filter.enhanceTrace(trace, response); @SuppressWarnings("unchecked") Map map = (Map) trace.get("error"); System.err.println(map); @@ -243,11 +246,12 @@ public class WebRequestTraceFilterTests { @SuppressWarnings("unchecked") public void filterHas500ResponseStatusWhenExceptionIsThrown() throws ServletException, IOException { + WebRequestTraceFilter filter = new WebRequestTraceFilter(this.repository); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); MockHttpServletResponse response = new MockHttpServletResponse(); try { - this.filter.doFilterInternal(request, response, (req, resp) -> { + filter.doFilterInternal(request, response, (req, resp) -> { throw new RuntimeException(); }); fail("Exception was swallowed"); @@ -264,7 +268,7 @@ public class WebRequestTraceFilterTests { @Test @SuppressWarnings("unchecked") public void postProcessRequestHeaders() throws Exception { - this.filter = new WebRequestTraceFilter(this.repository, this.properties) { + WebRequestTraceFilter filter = new WebRequestTraceFilter(this.repository) { @Override protected void postProcessRequestHeaders(Map headers) { @@ -275,7 +279,7 @@ public class WebRequestTraceFilterTests { MockHttpServletRequest request = spy(new MockHttpServletRequest("GET", "/foo")); request.addHeader("Accept", "application/json"); request.addHeader("Test", "spring"); - Map map = (Map) this.filter.getTrace(request) + Map map = (Map) filter.getTrace(request) .get("headers"); assertThat(map.get("request").toString()).isEqualTo("{Accept=application/json}"); }