Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewExpiredException handled by error-page, still in log

JSF-applications can throw ViewExpiredExceptions when a session is expired. This event will be quite common when having guests on your system. Therefor the application will handle this event without any trouble for the guest, as follows:

<error-page>
    <exception-type>javax.faces.application.ViewExpiredException</exception-type>
    <location>/expired</location>
</error-page>

When a guest tries to send a request on an expired session, he will be redirected to /expired. Since I don't consider this Exception as a problem worth mentioning, I would like to prevent writing the stacktrace to the log of my applicationserver.

How can I accomplish this?

like image 222
Menno Avatar asked Apr 20 '13 10:04

Menno


1 Answers

There are basically 2 options which each boil down to the same solution: catch, suppress and navigate to the error page all by yourself using either a servlet filter or a JSF exception handler. This way the exception won't reach the servletcontainer which would then handle and log it automatically.

Provided that the error page does really the job for you (this namely won't work with JSF ajax requests, unless you've a custom JSF ExceptionHandler), then a servlet filter mapped on URL pattern matching JSF requests which does the following in its doFilter() method should suffice:

try {
    chain.doFilter(request, response);
} catch (ServletException e) {
    if (e.getRootCause() instanceof ViewExpiredException) {
        request.getRequestDispatcher("/expired").forward(request, response);
    } else {
        throw e;
    }
}

If you would like to cover JSF ajax requests as well, then you can't go around a JSF exception handler. Head to the following related answers to see some concrete examples:

  • Why use a JSF ExceptionHandlerFactory instead of <error-page> redirection?
  • Check if session exists JSF
like image 90
BalusC Avatar answered Nov 11 '22 02:11

BalusC