Question

Due to a bug in Mojarra 2.2.0 I'd like to use javax.faces-2.2.6.jar with Glassfish 4. It does work if I replace the existing /opt/glassfish4/glassfish/modules/javax.faces.jar but that would affect all apps in all domains.

I tried putting it at least in Glassfish's modules/endorsed/ directory but that had no effect.

Is it possible to replace the JSF implementation using Maven's pom.xml for just one application?

Was it helpful?

Solution

You can, of course, simply place the new javax.faces dependency within the pom.xml above your glassfish dependency to build your app. But this will not prevent someone from deploying to a non-customized glassfish install.

To prevent the application from running at all, you're going to have to do a library version check within a Filter and throw a ServletException if it doesn't match the expected version. This won't prevent a deployment, but this is the next most intrusive option since it all requests will respond with a ServletException.

For example:

import java.io.IOException;

import javax.faces.application.ResourceHandler;
import javax.faces.context.FacesContext;
import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;

@WebFilter(value = "/*", dispatcherTypes = { DispatcherType.REQUEST, DispatcherType.FORWARD })
public class RelativePathFacesFilter implements Filter {

    private static String EXPECTED_FACES_VERSION = "2.2.6";

    private String facesVersion;
    private boolean correctVersion;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        facesVersion = FacesContext.getCurrentInstance().getClass().getPackage().getImplementationVersion();
        correctVersion = EXPECTED_FACES_VERSION.equals(facesVersion);
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        if (!correctVersion) {
            throw new ServletException("Expected Faces version=" + EXPECTED_FACES_VERSION + ", but instead got " + facesVersion);
        }

        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top