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.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<String, Object> 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;
}

Loading…
Cancel
Save