1

Omnifaces 2.0, Primefaces 5, wildFly 8.2

After session timeout if I try to make an ajax call with registered FullAjaxExceptonHandler in browser will be shown xml response and not the rendered error page.

The similiar error comes if PrimeExceptionHandler is used. If both excepton handlers are removed the similiar incorrect output in browser and exception comes. See EDIT browser output.

EDIT 2 If the rows in web.xml are changed to "JSP" Login

        <form-login-page>/login/login.jsp</form-login-page>
        <form-error-page>/login/login.jsp</form-error-page>

then AjaxExceptionHandler is not called.

login.xhtml is very similiar to https://stackoverflow.com/a/2207147/2023524 . Only with RequestScope and stateless view.

output in browser:

<partial-response id="j_id1"><changes><update id="javax.faces.ViewRoot"><html xmlns="http://www.w3.org/1999/xhtml">
            text of ErrorHandlerViewExpired.xhtml ...

</html></update><update id="j_id1:javax.faces.ViewState:0">stateless</update></changes></partial-response>

web.xml:

<login-config>
    <auth-method>FORM</auth-method>
    <realm-name>app</realm-name>
    <form-login-config>
        <form-login-page>/login/login.xhtml</form-login-page>
        <form-error-page>/login/login.xhtml</form-error-page>
    </form-login-config>
</login-config>
<error-page>
    <error-code>500</error-code>
    <location>/errors/errorHandlerWeb.xhtml?faces-redirect=true</location>
</error-page>
<error-page>
    <exception-type>javax.faces.application.ViewExpiredException</exception-type>
    <location>/errors/errorHandlerViewExpired.xhtml?faces-redirect=true</location>
</error-page>
<error-page>
    <exception-type>java.lang.Exception</exception-type>
    <location>/errors/errorHandlerProg.xhtml</location>
</error-page>

faces-config.xml:

<factory>
   <exception-handler-factory>
      org.omnifaces.exceptionhandler.FullAjaxExceptionHandlerFactory
   </exception-handler-factory>
</factory>

Console:

FullAjaxExceptionHandler: An exception occurred during processing JSF ajax request. Error page '/errors/errorHandlerViewExpired.xhtml?faces-redirect=true' will be shown.: javax.faces.application.ViewExpiredException: viewId:/login/login.xhtml - Ansicht /login/login.xhtml konnte nicht wiederhergestellt werden.
    at com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:212) [jsf-impl-2.2.11.jar:2.2.11]
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) [jsf-impl-2.2.11.jar:2.2.11]

EDIT

Output in browser without some ajaxExceptionHandler:

<partial-response id="j_id1">
  <redirect url="/app/pageWithAjax.jsf"/>
    <error>
    <error-name>class javax.faces.application.ViewExpiredException</error-name>
     <error-message>viewId:/login/login.xhtml - Ansicht /login/login.xhtml konnte nicht wiederhergestellt werden.
     </error-message></error></partial-response>
Community
  • 1
  • 1
Tony
  • 2,266
  • 4
  • 33
  • 54
  • Is this a restricted page with login? How are you authenticating users? Spring Security? Regardless, it's the authentication framework who's incorrectly re-executing the JSF ajax request instead of creating a new GET request. – BalusC Mar 04 '16 at 14:36
  • The page is restricted with "FORM" authentication. The users are authenticated over JSF Login backing bean. (Standard Java EE)Login view is "login/login.xhtml". Then it should be `WildFly` that makes trouble. Please also see update – Tony Mar 04 '16 at 14:50
  • I have re-read the question and got a bit confused on the exact use case. Do I understand this right: 1) You've a restricted JSF page open in browser. 2) Session gets expired in background. 3) You fire an ajax request on that restricted JSF page. 4) You see with contents of `errorHandlerViewExpired.xhtml` plain in browser. Is this sequence correct? – BalusC Mar 04 '16 at 20:43
  • Yes, you are right. I open restricted page `/app/pagesWithAjax.xhtml`. The page is open in browser and after session timeout I click on `p:dataTable` with ajax event on row. Then in browser I see . – Tony Mar 07 '16 at 08:38
  • This is indeed not the expected behavior. I will later try to reproduce. – BalusC Mar 07 '16 at 08:39
  • Someone else reported the same problem today and I have reproduced it on WildFly 10.0.0 and fixed it for OmniFaces 2.3-SNAPSHOT. Can you please try it as well? http://stackoverflow.com/q/35841356 – BalusC Mar 07 '16 at 12:34

1 Answers1

0

The right solution with Omnifaces is described here:

https://stackoverflow.com/a/35843836/2023524


Here is a solution for one concrete situation, only if user loged in with Java EE loging and want to redirect to error page and not to login page.

import java.io.IOException;

import javax.faces.FacesException;
import javax.faces.application.ViewExpiredException;
import javax.faces.context.ExceptionHandler;
import javax.faces.context.FacesContext;

import org.omnifaces.exceptionhandler.FullAjaxExceptionHandler;
import org.omnifaces.util.Faces;

public class CustomExceptionViewExpiredHandler extends FullAjaxExceptionHandler{

    public CustomExceptionViewExpiredHandler(ExceptionHandler wrapped) {
        super(wrapped);
    }

    @Override
    public void handle() throws FacesException {
            boolean b = handleViewExpiredException(Faces.getContext());
            if (!b)
                return;
            super.handle();

    }

    private boolean handleViewExpiredException (FacesContext context){
        if (context == null || !context.getPartialViewContext().isAjaxRequest()) {
            return false; // Not an ajax request.
        }

        //Test user Principal, java authentication
        if (Faces.getSession() == null || Faces.getRequest().getUserPrincipal() == null){
            try {
                String errorPageLocation = findErrorPageLocation(context, new SomeSessionExpiredException());

                Faces.redirect(Faces.getRequestContextPath() + errorPageLocation);
                Faces.responseComplete();
                return false;
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }
}
Community
  • 1
  • 1
Tony
  • 2,266
  • 4
  • 33
  • 54