Skip to content

Instantly share code, notes, and snippets.

@banterCZ
Created February 7, 2013 11:42
Show Gist options
  • Save banterCZ/4730442 to your computer and use it in GitHub Desktop.
Save banterCZ/4730442 to your computer and use it in GitHub Desktop.
When you ask for url matching JSF servlet mapping, you get the FileNotFoundException and http code 500. This filter fixes it.
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* This filter interprets {@link FileNotFoundException} as HTTP status code <code>404</code>.
*
* @author banterCZ
*/
public class FileNotFoundFilter implements Filter {
/**
* {@inheritDoc}
*/
@Override
public void init(FilterConfig filterConfig) throws ServletException {
//no code
}
/**
* Interprets {@link FileNotFoundException} as HTTP status code <code>404</code>
*
* {@inheritDoc}
*/
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
if (!(request instanceof HttpServletRequest) || !(response instanceof HttpServletResponse)) {
throw new ServletException("FileNotFoundFilter just supports HTTP requests");
}
doFilter((HttpServletRequest) request, (HttpServletResponse) response, filterChain);
}
protected void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws IOException, ServletException {
try {
filterChain.doFilter(request, response);
} catch (FileNotFoundException e) {
response.sendError(HttpServletResponse.SC_NOT_FOUND, request.getRequestURI());
}
}
/**
* {@inheritDoc}
*/
@Override
public void destroy() {
//no code
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment