users@glassfish.java.net

GF3.1 NullPointerException in NavigationHandlerImpl

From: <forums_at_java.net>
Date: Mon, 4 Apr 2011 10:18:27 -0500 (CDT)

Hi,

I have used GF3.0.1 for many months and created a "public class LoginFilter
implements PhaseListener" to control timeout session, to redirect the user to
login page. All worked fine in this version. Now, I migrated to GF3.1 and
the code for LoginFilter doesn't work anymore. The wepapp arises this
exception:

<code>

java.lang.NullPointerException
    at
com.sun.faces.application.NavigationHandlerImpl.updateRenderTargets(NavigationHandlerImpl.java:240)
    at
com.sun.faces.application.NavigationHandlerImpl.handleNavigation(NavigationHandlerImpl.java:197)
    at util.LoginFilter.afterPhase(LoginFilter.java:42)
    at com.sun.faces.lifecycle.Phase.handleAfterPhase(Phase.java:189)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:107)
    at
com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:113)
    at
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)

...

</code>

The faces-config.xml has this config:

<code>

  <lifecycle>
    <phase-listener>util.LoginFilter</phase-listener>
  </lifecycle>
  <navigation-rule>
    <from-view-id>*</from-view-id>
    <navigation-case>
      <from-outcome>logout</from-outcome>
      <to-view-id>/logins.xhtml</to-view-id>
    </navigation-case>
  </navigation-rule>

</code>

The complete class is here:

<code>

package util;
import entity.Usuario;
import javax.faces.application.NavigationHandler;
import javax.faces.component.UIViewRoot;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
public class LoginFilter implements PhaseListener {
  private static final long serialVersionUID = 1L;
  @Override
  public void beforePhase(PhaseEvent event) {
    FacesContext fc = event.getFacesContext();
    ExternalContext ec = fc.getExternalContext();
    HttpSession session = (HttpSession) ec.getSession(false);
    if (session == null) {
      try {
        String url =
((ServletContext)ec.getContext()).getContextPath();
        ec.redirect(url + "/index.jsp");
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
  @Override
  public void afterPhase(PhaseEvent event) {
    FacesContext fc = event.getFacesContext();
    try {
      UIViewRoot view = fc.getViewRoot();
      if (view == null) {
        NavigationHandler nh =
fc.getApplication().getNavigationHandler();
        //nh.handleNavigation(fc, "*", "logout");
        if (nh != null)
          nh.handleNavigation(fc, null, "logout");
      }
      else {
        String currentPage = view.getViewId();
        boolean isLoginPage =
                currentPage.indexOf("login") > -1 ||
                currentPage.indexOf("teste") > -1 ||
                currentPage.indexOf("confirmacaoChave") > -1;
        HttpSession session = (HttpSession)
fc.getExternalContext().getSession(false);
        Usuario usuario = (Usuario) session.getAttribute("usuario");
        if (!isLoginPage && (usuario == null || usuario.getNome() ==
null)) {
          NavigationHandler nh =
fc.getApplication().getNavigationHandler();
          if (nh != null)
            nh.handleNavigation(fc, null, "logout");
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
      NavigationHandler nh = fc.getApplication().getNavigationHandler();
      if (nh != null)
        nh.handleNavigation(fc, null, "logout");
    }
  }
  @Override
  public PhaseId getPhaseId() {
    return PhaseId.RESTORE_VIEW;
  }
}
</code>

Also, this config worked fine in GF 3.0.1 to show a expired page for user,
and it doesn't work with GF 3.1:

<code>

  <error-page>
   
<exception-type>javax.enterprise.context.NonexistentConversationException</exception-type>
    <location>/expired.html</location>
  </error-page>
  <error-page>
   
<exception-type>org.jboss.weld.context.NonexistentConversationException</exception-type>
    <location>/expired.html</location>
  </error-page>
  <error-page>
   
<exception-type>javax.faces.application.ViewExpiredException</exception-type>
    <location>/expired.html</location>
  </error-page>

</code>


--
[Message sent by forum member 'edilmar']
View Post: http://forums.java.net/node/788365