Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package com.yammer.dropwizard.servlets;
 
 import javax.servlet.*;
 
 import static com.yammer.dropwizard.util.Servlets.getFullUrl;

A servlet filter which adds the request method and URI to the thread name processing the request for the duration of the request.
public class ThreadNameFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfigthrows ServletException { /* unused */ }
    @Override
    public void destroy() { /* unused */ }
    @Override
    public void doFilter(ServletRequest request,
                         ServletResponse response,
                         FilterChain chainthrows IOExceptionServletException {
        final HttpServletRequest req = (HttpServletRequestrequest;
        final Thread current = Thread.currentThread();
        final String oldName = current.getName();
        try {
            current.setName(formatName(reqoldName));
            chain.doFilter(requestresponse);
        } finally {
            current.setName(oldName);
        }
    }
    private static String formatName(HttpServletRequest reqString oldName) {
        return oldName + " - " + req.getMethod() + ' ' + getFullUrl(req);
    }
New to GrepCode? Check out our FAQ X