Make WebRequestTraceFilter once per request

pull/1016/head
Dave Syer 11 years ago
parent c719ab7aee
commit b31295246c

@ -25,10 +25,7 @@ import java.util.Map;
import javax.servlet.Filter; import javax.servlet.Filter;
import javax.servlet.FilterChain; import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
@ -38,6 +35,7 @@ import org.springframework.boot.autoconfigure.web.ErrorAttributes;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.filter.OncePerRequestFilter;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
@ -47,16 +45,16 @@ import com.fasterxml.jackson.databind.ObjectMapper;
* *
* @author Dave Syer * @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 final Log logger = LogFactory.getLog(WebRequestTraceFilter.class);
private boolean dumpRequests = false; private boolean dumpRequests = false;
private final TraceRepository traceRepository;
private int order = Integer.MAX_VALUE; private int order = Integer.MAX_VALUE;
private final TraceRepository traceRepository;
private final ObjectMapper objectMapper = new ObjectMapper(); private final ObjectMapper objectMapper = new ObjectMapper();
private ErrorAttributes errorAttributes; 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) { public void setDumpRequests(boolean dumpRequests) {
this.order = order; this.dumpRequests = dumpRequests;
} }
@Override @Override
@ -80,19 +79,14 @@ public class WebRequestTraceFilter implements Filter, Ordered {
return this.order; return this.order;
} }
/** public void setOrder(int order) {
* Debugging feature. If enabled, and trace logging is enabled then web request this.order = order;
* headers will be logged.
*/
public void setDumpRequests(boolean dumpRequests) {
this.dumpRequests = dumpRequests;
} }
@Override @Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) protected void doFilterInternal(HttpServletRequest request,
throws IOException, ServletException { HttpServletResponse response, FilterChain filterChain)
HttpServletRequest request = (HttpServletRequest) req; throws ServletException, IOException {
HttpServletResponse response = (HttpServletResponse) res;
Map<String, Object> trace = getTrace(request); Map<String, Object> trace = getTrace(request);
if (this.logger.isTraceEnabled()) { if (this.logger.isTraceEnabled()) {
@ -113,7 +107,7 @@ public class WebRequestTraceFilter implements Filter, Ordered {
} }
try { try {
chain.doFilter(request, response); filterChain.doFilter(request, response);
} }
finally { finally {
enhanceTrace(trace, response); enhanceTrace(trace, response);
@ -168,14 +162,6 @@ public class WebRequestTraceFilter implements Filter, Ordered {
return trace; return trace;
} }
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void destroy() {
}
public void setErrorAttributes(ErrorAttributes errorAttributes) { public void setErrorAttributes(ErrorAttributes errorAttributes) {
this.errorAttributes = errorAttributes; this.errorAttributes = errorAttributes;
} }

Loading…
Cancel
Save