[solved] Exception handling JSF2

Splash Forums PrettyFaces Users [solved] Exception handling JSF2

This topic contains 20 replies, has 4 voices, and was last updated by  Ulisses Constantini 6 years, 8 months ago.

Viewing 15 posts - 1 through 15 (of 21 total)
  • Author
    Posts
  • #17774

    Ulisses Constantini
    Participant

    Hello, i add a exception handler in my app (like http://weblogs.java.net/blog/edburns/archive/2009/09/03/dealing-gracefully-viewexpiredexception-jsf2), but after i use prettyfaces, when send to “viewExpired” page, throw a exception (null source exception).

    Someone had a similar problem?

    #20125

    Not sure I’ve ever heard of a situation like this.

    It would help if you post your stack trace, and also list the versions of libraries you are using. JSF, PrettyFaces, Server, etc…

    Thanks,

    Lincoln

    #20126

    Ulisses Constantini
    Participant

    Hello Lincoln.

    The stack trace is:

    exception:

    javax.servlet.ServletException: java.lang.NullPointerException

    com.ocpsoft.pretty.PrettyFilter.doFilter(PrettyFilter.java:118)

    root cause:

    java.lang.NullPointerException

    at com.sun.faces.lifecycle.RestoreViewPhase.notifyAfter(RestoreViewPhase.java:288)

    at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:110)

    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)

    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)

    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)

    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)

    at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:359)

    at org.springframework.security.intercept.web.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)

    at org.springframework.security.intercept.web.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)

    at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:371)

    at org.springframework.security.ui.SessionFixationProtectionFilter.doFilterHttp(SessionFixationProtectionFilter.java:67)

    at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)

    at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:371)

    at org.springframework.security.ui.ExceptionTranslationFilter.doFilterHttp(ExceptionTranslationFilter.java:101)

    at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)

    at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:371)

    at org.springframework.security.providers.anonymous.AnonymousProcessingFilter.doFilterHttp(AnonymousProcessingFilter.java:105)

    at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)

    at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:371)

    at org.springframework.security.ui.rememberme.RememberMeProcessingFilter.doFilterHttp(RememberMeProcessingFilter.java:116)

    at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)

    at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:371)

    at org.springframework.security.wrapper.SecurityContextHolderAwareRequestFilter.doFilterHttp(SecurityContextHolderAwareRequestFilter.java:91)

    at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)

    at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:371)

    at org.springframework.security.ui.basicauth.BasicProcessingFilter.doFilterHttp(BasicProcessingFilter.java:173)

    at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)

    at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:371)

    at org.springframework.security.ui.AbstractProcessingFilter.doFilterHttp(AbstractProcessingFilter.java:271)

    at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)

    at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:371)

    at org.springframework.security.ui.logout.LogoutFilter.doFilterHttp(LogoutFilter.java:89)

    at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)

    at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:371)

    at org.springframework.security.context.HttpSessionContextIntegrationFilter.doFilterHttp(HttpSessionContextIntegrationFilter.java:235)

    at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)

    at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:371)

    at org.springframework.security.util.FilterChainProxy.doFilter(FilterChainProxy.java:174)

    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:236)

    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)

    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:242)

    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)

    at com.ocpsoft.pretty.PrettyFilter.doFilter(PrettyFilter.java:112)

    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:242)

    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)

    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243)

    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:201)

    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:163)

    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:108)

    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:556)

    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)

    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:402)

    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:249)

    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:267)

    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:245)

    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:260)

    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)

    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)

    at java.lang.Thread.run(Thread.java:619)

    Maybe something with springsecurity?

    #20127

    Which version of JSF are you using?

    #20128

    Ulisses Constantini
    Participant

    JSF 2.0.3.

    Maybe something with springsecurity?

    #20129

    I just looked at the JSF RI 2.0.3 sources. The NPE occurs in these lines:


    286  private void notifyAfter(FacesContext context, Lifecycle lifecycle) {
    287    UIViewRoot viewRoot = context.getViewRoot();
    288    MethodExpression afterPhase = viewRoot.getAfterPhaseListener();

    Seems like ViewRoot is not set… Hmmm…

    #20130

    Very weird issue. Could you post a bit more of the context? For example the ExceptionHandler you are using. You could also try to temporarily remove the Spring Security stuff just to check whether there is a difference.

    Most helpful would be an minimal example app that reproduces this issue.

    I guess there has to be some debugging to be done… :-)

    #20131

    So I have two questions.

    1. Why is the ViewRoot null?

    2. Can we do something to ensure that it is not?

    I’m guessing that you are using DynaView functionality, yes? this is quite possibly due to the fact that DynaView relies on intercepting a view request that does not exist.

    #20132

    0swald
    Participant

    Had almost the same issue while using DynaView and exactly the same ExceptionHandler code. In my case Facescontext was null, not ViewRoot. One can get context from ExceptionQueuedEvent, but i’m not sure that’s correct. fc = event.getContext().getContext();

    Actually I’m still working on that DynaView functionality, and the biggest problem I have is that PrettyFaces or PrimeFaces or both throw NullPointerException when I try to redirect during RESTORE_VIEW phase :(

    #20133

    Please try the latest snapshot version 3.0.2-SNAPSHOT from the OcpSoft repository:

    http://ocpsoft.com/repository/com/ocpsoft/prettyfaces-jsf2/3.0.2-SNAPSHOT/prettyfaces-jsf2-3.0.2-20100830.182659-26.jar

    Let me know if the problem remains. If this works, it will be a big deal and very helpful to know. I’ve attempted to create a viewHandler if it is null on DynaView requests. It’s currently a big hack, but if this works the concept can be refined in future releases. (I still maintain that this is a JSF bug, but they have not acknowledged it yet.)

    Thanks very much,

    Lincoln

    #20134

    Getting the FacesContext from ExceptionQueuedEvent is the correct method of accessing FacesContext. Always get the context provided through the method signature or built-in objects if possible.

    FacesContext.getCurrentInstance() should be avoided if at all possible.

    Let me know if the latest 3.0.2-SNAPSHOT works :) Thanks.

    #20135

    0swald
    Participant

    Hi Lincoln. Here are complete test results using latest snapshot

    ExceptionHandler:

    @Override
    public void handle() throws FacesException {
    for (Iterator<ExceptionQueuedEvent> i = getUnhandledExceptionQueuedEvents().iterator(); i.hasNext();) {
    ExceptionQueuedEvent event = i.next();
    ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource();
    Throwable t = context.getException();
    if(t instanceof DynaViewException) {
    try {
    FacesContext fc = event.getContext().getContext();
    NavigationHandler nav = fc.getApplication().getNavigationHandler();
    nav.handleNavigation(fc, null, "/faces/pages/test.xhtml");
    fc.renderResponse();
    } finally {
    i.remove();
    }
    }
    }
    super.handle();
    }

    If I throw Exception from DynaView’s url-parsing method it will be root cause for subsequent ELException, so I catch all parsing exceptions and put my own in queue:

    FacesContext fc = FacesContext.getCurrentInstance();
    //ExceptionQueuedEventContext exceptionQueuedEventContext = new ExceptionQueuedEventContext(fc, e);
    ExceptionQueuedEventContext exceptionQueuedEventContext = new ExceptionQueuedEventContext(fc, e, null, PhaseId.RESTORE_VIEW);
    ExceptionQueuedEvent redirectEvent = new ExceptionQueuedEvent(exceptionQueuedEventContext);
    fc.getExceptionHandler().processEvent(redirectEvent);

    My handler catches these events and, no matter which phase I use for ExceptionQueuedEventContext, I get following stacktrace:

    java.lang.NullPointerException
    at com.ocpsoft.pretty.PrettyContext.getCurrentInstance(PrettyContext.java:68)
    at com.ocpsoft.pretty.faces.application.PrettyRedirector.redirect(PrettyRedirector.java:51)
    at com.ocpsoft.pretty.faces.application.PrettyNavigationHandler.handleNavigation(PrettyNavigationHandler.java:49)
    at com.mycompany.web.faces.CustomErrorExceptionHandler.handle(CustomErrorExceptionHandler.java:44)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:119)
    at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:110)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
    at com.ocpsoft.pretty.PrettyFilter.doFilter(PrettyFilter.java:113)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
    at org.apache.catalina.core.ApplicationDispatcher.doInvoke(ApplicationDispatcher.java:820)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:517)
    at org.apache.catalina.core.ApplicationDispatcher.doDispatch(ApplicationDispatcher.java:488)
    at org.apache.catalina.core.ApplicationDispatcher.dispatch(ApplicationDispatcher.java:379)
    at org.apache.catalina.core.ApplicationDispatcher.dispatch(ApplicationDispatcher.java:336)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:314)
    at com.ocpsoft.pretty.PrettyFilter.doFilter(PrettyFilter.java:87)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
    at com.mycompany.web.multipart.MultipartFilter.doFilter(MultipartFilter.java:73)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:277)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
    at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:325)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:226)
    at org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:229)
    at org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:334)
    at org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:814)
    at org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:743)
    at org.apache.jk.common.ChannelSocket$SocketConnection.runIt(ChannelSocket.java:936)
    at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:682)
    at java.lang.Thread.run(Thread.java:619)

    #20136

    Ok, I fixed that bug. Try again now with the new snapshot.

    #20137

    0swald
    Participant
    java.lang.NullPointerException
    at com.ocpsoft.pretty.PrettyContext.getCurrentInstance(PrettyContext.java:68)
    at com.ocpsoft.pretty.faces.application.PrettyRedirector.redirect(PrettyRedirector.java:51)
    at com.ocpsoft.pretty.faces.application.PrettyNavigationHandler.handleNavigation(PrettyNavigationHandler.java:49)
    at com.mycompany.web.faces.CustomErrorExceptionHandler.handle(CustomErrorExceptionHandler.java:44)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:119)
    at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:110)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
    ...
    ...

    Not sure it’s a bug. When using redirect() instead of nav handler like that

    fc.getExternalContext().redirect("/faces/pages/index.xhtml");
    fc.responseComplete();

    I also get Exception:

    java.lang.NullPointerException
    at com.sun.faces.context.flash.ELFlash.doLastPhaseActions(ELFlash.java:589)
    at com.sun.faces.context.ExternalContextImpl.redirect(ExternalContextImpl.java:555)
    at com.mycompany.web.faces.CustomErrorExceptionHandler.handle(CustomErrorExceptionHandler.java:47)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:119)
    at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:110)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
    ...
    ...

    At least I always have the possibility for my DynaView parser to return error page instead of redirecting, and even pass neccessary data for further error handling. That will work 100%.

    Pretty is a layer above JSF, so digging down to faces lifecycle internals clashes a bit with Pretty’s ideology :)

    #20138

    That looks like you are still using the same snapshot. Are you sure it updated?

    –Lincoln

Viewing 15 posts - 1 through 15 (of 21 total)

You must be logged in to reply to this topic.

Comments are closed.