From b31295246cab9b4bc7fabd72ac76b1ea09e738ad Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Sun, 1 Jun 2014 09:47:04 +0100 Subject: [PATCH] Make WebRequestTraceFilter once per request --- .../actuate/trace/WebRequestTraceFilter.java | 42 +++++++------------ 1 file changed, 14 insertions(+), 28 deletions(-) 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 f64c07eac9..a622507d9f 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 @@ -25,10 +25,7 @@ import java.util.Map; 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 javax.servlet.http.HttpServletResponse; @@ -38,6 +35,7 @@ import org.springframework.boot.autoconfigure.web.ErrorAttributes; import org.springframework.core.Ordered; import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.ServletRequestAttributes; +import org.springframework.web.filter.OncePerRequestFilter; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; @@ -47,16 +45,16 @@ import com.fasterxml.jackson.databind.ObjectMapper; * * @author Dave Syer */ -public class WebRequestTraceFilter implements Filter, Ordered { +public class WebRequestTraceFilter extends OncePerRequestFilter implements Ordered { private final Log logger = LogFactory.getLog(WebRequestTraceFilter.class); private boolean dumpRequests = false; - private final TraceRepository traceRepository; - private int order = Integer.MAX_VALUE; + private final TraceRepository traceRepository; + private final ObjectMapper objectMapper = new ObjectMapper(); private ErrorAttributes errorAttributes; @@ -69,10 +67,11 @@ public class WebRequestTraceFilter implements Filter, Ordered { } /** - * @param order the order to set + * Debugging feature. If enabled, and trace logging is enabled then web request + * headers will be logged. */ - public void setOrder(int order) { - this.order = order; + public void setDumpRequests(boolean dumpRequests) { + this.dumpRequests = dumpRequests; } @Override @@ -80,19 +79,14 @@ public class WebRequestTraceFilter implements Filter, Ordered { return this.order; } - /** - * Debugging feature. If enabled, and trace logging is enabled then web request - * headers will be logged. - */ - public void setDumpRequests(boolean dumpRequests) { - this.dumpRequests = dumpRequests; + public void setOrder(int order) { + this.order = order; } @Override - public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) - throws IOException, ServletException { - HttpServletRequest request = (HttpServletRequest) req; - HttpServletResponse response = (HttpServletResponse) res; + protected void doFilterInternal(HttpServletRequest request, + HttpServletResponse response, FilterChain filterChain) + throws ServletException, IOException { Map trace = getTrace(request); if (this.logger.isTraceEnabled()) { @@ -113,7 +107,7 @@ public class WebRequestTraceFilter implements Filter, Ordered { } try { - chain.doFilter(request, response); + filterChain.doFilter(request, response); } finally { enhanceTrace(trace, response); @@ -168,14 +162,6 @@ public class WebRequestTraceFilter implements Filter, Ordered { return trace; } - @Override - public void init(FilterConfig filterConfig) throws ServletException { - } - - @Override - public void destroy() { - } - public void setErrorAttributes(ErrorAttributes errorAttributes) { this.errorAttributes = errorAttributes; }