0

I use Apache MyFaces 2 on WebSphere Application Server 8. I want to implement a custom ExceptionHandler that handles ViewExpiredException.

I use the code which BalusC posted . The Factory is called at the right time but I get a NullPointerException when handleNavigation is called here:

public class ViewExpiredExceptionHandler extends ExceptionHandlerWrapper {

    private ExceptionHandler wrapped;

    public ViewExpiredExceptionHandler(ExceptionHandler wrapped) {
        this.wrapped = wrapped;
    }


    @Override
    public void handle() throws FacesException {
        FacesContext facesContext = FacesContext.getCurrentInstance();

        for (Iterator<ExceptionQueuedEvent> iter = getUnhandledExceptionQueuedEvents()
                .iterator(); iter.hasNext();) {
            Throwable exception = iter.next().getContext().getException();

            if (exception instanceof ViewExpiredException) {
                facesContext
                        .getApplication()
                        .getNavigationHandler()
                        .handleNavigation(facesContext, null,
                                "/content/home?faces-redirect=true&expired=true");
                facesContext.renderResponse();
                iter.remove();
            }
        }

        getWrapped().handle();
    }

    @Override
    public ExceptionHandler getWrapped() {
        return wrapped;
    }

}

The Exception is:

com.ibm.ws.webcontainer.servlet.ServletWrapper service SRVE0068E: An exception was thrown by one of the service methods of the servlet [Faces Servlet] in application [My_App]. Exception created : [java.lang.NullPointerException
    at org.apache.myfaces.application.NavigationHandlerImpl.getNavigationCase(NavigationHandlerImpl.java:203)
    at org.apache.myfaces.application.NavigationHandlerImpl.handleNavigation(NavigationHandlerImpl.java:77)
    at myapp.ViewExpiredExceptionHandler.handle(ViewExpiredExceptionHandler.java:33)
    at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:191)
    at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:189)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1147)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:722)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:449)
    at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:178)
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.invokeTarget(WebAppFilterChain.java:125)
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:92)
    at myapp.auth.RequestFilter.doFilter(RequestFilter.java:35)
    at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:192)
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:89)
    at com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:919)
    at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1016)
    at com.ibm.ws.webcontainer.servlet.CacheServletWrapper.handleRequest(CacheServletWrapper.java:87)
    at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:886)
    at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1655)
    at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:195)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:452)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:511)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:305)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:276)
    at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDiscriminators(NewConnectionInitialReadCallback.java:214)
    at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:113)
    at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)
    at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
    at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
    at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
    at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
    at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
    at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
    at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1650)
]

EDIT: First of all /content is a directory in my WebContent. It is not the context path. I changed my code and used external redirect like this:

if (exception instanceof ViewExpiredException) {
        String loc = facesContext.getExternalContext()
            .getRequestServletPath();
        try {
            facesContext.getExternalContext().redirect(loc);
            //facesContext.getExternalContext().redirect("/content/home.xhtml");
            //facesContext.getExternalContext().redirect("/");
        } catch (IOException e) {
            e.printStackTrace();
        }
}

But still get the same NullPointerException on facesContext.getExternalContext().redirect. But the ExternalContext is not null. I got the RequestServletPath.

So whats wrong with my code here?

Best regards - veote

Community
  • 1
  • 1
veote
  • 1,400
  • 9
  • 33
  • 62

2 Answers2

2

The "/content/home?faces-redirect=true&expired=true" is apparently not recognized as a valid navigation case. It's impossible to tell what's wrong in there as details about your environment are unclear. Is /content the context path or not? If so, then it should be omitted. Is there really a /home.xhtml file? Etcetera.

If you still can't figure it out, then it's probably easier to just send a redirect as you seem to be after a redirect anyway.

externalContext.redirect("/content/home.xhtml?expired=true");

Note that this will still result in a 404 if the URL is invalid, but it'll be probably easier to figure out for you.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
1

I get a similar set of errors. Following the above approach, my handle navigation

nav.handleNavigation(fc, null, "/error");

Returns a NPE like above (same line numbers even).

If I modify to just do a re-direct, I get:

java.lang.NullPointerException
at org.apache.myfaces.context.servlet.PartialViewContextImpl.getPartialResponseWriter(PartialViewContextImpl.java:303)
at org.primefaces.context.PrimePartialViewContext.getPartialResponseWriter(PrimePartialViewContext.java:45)
at org.apache.myfaces.context.servlet.ServletExternalContextImpl.redirect(ServletExternalContextImpl.java:452)
at my.package.CustomExceptionHandler.handle(CustomExceptionHandler.java:134)
at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:191)
at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:189)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1235)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:758)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:441)

In both cases I believe the cause is that the getViewRoot() is null which is apparently not expected by 'getNavigationCase' or getPartialResponseWriter.

I was able to fix using the example at this blog post (http://ovaraksin.blogspot.com/2010/10/jsf-ajax-redirect-after-session-timeout.html). Seems that Mojarra and PrimeFaces (I am using PrimeFaces 3.5 with MyFaces) have issues with the standard re-direct in the case of this error. The fix is in the case that

facescontext.getResponseWriter() == null && facescontext.getRenderKit() == null

and it's a partial request and an ajax request, then use the RenderKit to create a ResponseWriter and set it on the facescontext.

The externalContext.redirect() then works.

Edit: more specifically, in the blog post, the SecurityPhaseListener has a doRedirect(FacesContext fc, String redirectPage) method which includes a section called:

 // fix for renderer kit (Mojarra's and PrimeFaces's ajax redirect)

I lifted that section into a redirect response method that I call from the block of if (exception instanceof ViewExpiredException)

EDIT OK, final solution. With help from a sliver of code from http://jira.icesoft.org/browse/ICE-4251 I was able to implement this using the handlenavigation. Here is the call I added from the JIRA issue which gets executed in the handle()

checkViewRoot(facesContext,"/error.jsf");

... Here is the method

    public static void checkViewRoot( FacesContext ctx, String viewId) {
    if (ctx.getViewRoot() == null) {
        UIViewRoot viewRoot = ctx.getApplication().getViewHandler().createView(ctx, viewId);
        if (viewRoot != null) {
            ctx.setViewRoot(viewRoot);
        }
    }
}

... And after that, handlenavigation is called in the handle() method.

    facesContext
            .getApplication()
            .getNavigationHandler()
            .handleNavigation(facesContext, null,
                    (redirectPage != null ? redirectPage : ""));
    facesContext.renderResponse();

Note that in my experience thus far, viewId must be an existing page and different than the one to which you need to re-direct.

Nice thing is the facesmessage I set with the viewexpiredexception message now displays on the new page which I was having all kinds of trouble with before with the redirect.

peater
  • 1,233
  • 15
  • 20