JSF / PrimeFaces / AJAX / WEbFilter Requests and Viewing Expected Exceptions

I am starting a new JavaEE7 project that requires PrimeFaces 5.0 components. The JSF implementation is Mojarra 2.2.0.

After installing the initial project dependencies, I went to handle user sessions and handled the viewExpiredException correctly.

First step, I added below code to web.xml, so after session expires, user is redirected to corresponding page. Fine.

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

      

After that I added PrimeFaces and tested the AJAX components after the session ended. Does not work! Then I added the code below to faces-config.xml, so after the AJAX call, when the session is disconnected, users are redirected to the appropriate page. Fine.

<factory>
    <exception-handler-factory>
        org.primefaces.application.exceptionhandler.PrimeExceptionHandlerFactory
    </exception-handler-factory>
</factory>

      

Then I went to implement a WebFilter to enable authorization like this:

@WebFilter("/*")
public class AuthorizationFilter implements Filter {

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws ServletException, IOException {
    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) res;

    HttpSession session = request.getSession(false);

    User user = (session != null) ? (User) session.getAttribute("user") : null;
    String loginURL = request.getContextPath() + "/login.xhtml";

    boolean loginRequest = request.getRequestURI().startsWith(loginURL);
    boolean resourceRequest = request.getRequestURI().startsWith(request.getContextPath() + ResourceHandler.RESOURCE_IDENTIFIER);

    if (user != null || loginRequest || resourceRequest) {
        chain.doFilter(request, response);
    } else {
        response.sendRedirect(loginURL);
    }
}

      

After that my gracefully handled viewExpiredException went away (that's ok, I'll handle the session in the filter), but the AJAX requests after the session expired are no longer processed. Does this mean when I implement a WebFilter I have to handle all requests in it (also detect AJAX calls and redirect accordingly) and I can drop the web.xml and faces-config.xml configuration for exceptions?

TIA, D00de.

+3


source to share





All Articles