webtier@glassfish.java.net

Re: [webtier] JSF/JSF2 Seemingly random NullPointerExceptionsinrequest.get/setAttribute() using Filters

From: Lincoln Baxter, III <lincolnbaxter_at_gmail.com>
Date: Wed, 08 Apr 2009 21:48:20 -0400

On Mon, 2009-04-06 at 17:01 -0700, Ryan Lubke wrote:

> As a first step, yes, I would recommend releasing the
> FacesContext prior to forwarding.


Ok, so I tried this, and it didn't quite work, but... I think I have it
narrowed down to a JSF issue... by performing all Faces work after other
direct accesses to the Request object, the nullpointers seem to be
avoided.

I did not try comparing request objects, or throwing the exception as
you suggested

Here is my FacesContext construction logic:


/*
 * PrettyFaces is an OpenSource JSF library to create bookmarkable URLs.
 *
 * Copyright (C) 2008 - Lincoln Baxter, III <lincoln_at_ocpsoft.com>
 *
 * This program is free software: you can redistribute it and/or modify
it under
 * the terms of the GNU Lesser General Public License as published by
the Free Software
 * Foundation, either version 3 of the License, or (at your option) any
later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but
WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public
License along with
 * this program. If not, see the file COPYING.LESSER or visit the GNU
website at
 * <http://www.gnu.org/licenses/>.
 */
package com.ocpsoft.pretty.util;

import javax.faces.FactoryFinder;
import javax.faces.context.FacesContext;
import javax.faces.context.FacesContextFactory;
import javax.faces.lifecycle.Lifecycle;
import javax.faces.lifecycle.LifecycleFactory;
import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class FacesContextBuilder
{
    public FacesContext getFacesContext(final ServletRequest request,
final ServletResponse response)
    {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        if (facesContext != null)
        {
            return facesContext;
        }

        FacesContextFactory contextFactory = (FacesContextFactory)
FactoryFinder
                .getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
        LifecycleFactory lifecycleFactory = (LifecycleFactory)
FactoryFinder
                .getFactory(FactoryFinder.LIFECYCLE_FACTORY);
        Lifecycle lifecycle =
lifecycleFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);

        ServletContext servletContext = ((HttpServletRequest)
request).getSession().getServletContext();
        facesContext = contextFactory.getFacesContext(servletContext,
request, response, lifecycle);

InnerFacesContext.setFacesContextAsCurrentInstance(facesContext);

        return facesContext;
    }

    private abstract static class InnerFacesContext extends FacesContext
    {
        protected static void setFacesContextAsCurrentInstance(final
FacesContext facesContext)
        {
            FacesContext.setCurrentInstance(facesContext);
        }
    }

}