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
session primefaces jsf-2.2 glassfish-4 java-ee-7


source to share


No one has answered this question yet

Check out similar questions:

41
Session timeout and handling ViewExpiredException in JSF / PrimeFaces ajax request
ten
Automatically perform client-side actions at the end of the session
4
Handle ViewExpiredExceptions From AJAX Requests With Primefaces 5.1
3
How to deal with session timeouts in AJAX requests?
3
Session expires in flask in ajax context
2
Prides ajaxExceptionHandler not working in liferay 7
0
Websphere Portal JSF 2 session timeout with button on click causes view Expired Exception
0
Multiple JSF Users Throw ViewExpiredException
0
Primefaces PrimeExceptionHandlerELResolver in Spring Boot
0
Redirect to ViewExpiredException in AJAX request in JSF 2



All Articles
Loading...
X
Show
Funny
Dev
Pics