I am running into a problem implementing JsfPortletView
Line here is failing with class cast.
Code:
facesContext = contextFactory.getFacesContext(portletContext, request, response, lifecycle);
I think this is occurring because
contextFactory is a portlet context factory because of jsf-portlet integration library.
request and response are not portlet compatible and hence class cast.
So either we've got to figure out a way to continue to use servlet context factory here or somehow convert servlet request and response into portlet request and response.
That's my 2cents... Any ideas?
Here is the code so far..
Code:
/*
* Created on Apr 22, 2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package org.springframework.web.servlet.view;
import java.util.Map;
import javax.faces.FactoryFinder;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.context.FacesContextFactory;
import javax.faces.lifecycle.Lifecycle;
import javax.faces.lifecycle.LifecycleFactory;
import javax.portlet.PortletContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.portlet.context.PortletApplicationContext;
/**
* @author jztb88
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class JsfPortletView extends InternalResourceView {
/**
* Render the internal resource given the specified model.
* This includes setting the model as request attributes.
*/
protected void renderMergedOutputModel(
Map model, HttpServletRequest request, HttpServletResponse response) throws Exception {
// Expose the model object as request attributes.
exposeModelAsRequestAttributes(model, request);
// Expose helpers as request attributes, if any.
exposeHelpers(request);
FacesContext facesContext = getFacesContext(request, response);
// you could just render the page here but since we are still in filter, it might be
// a better idea to forward to the actual page
// facesContext.getApplication().getViewHandler().renderView(facesContext, facesContext.getViewRoot() );
// Forward to the resource (typically a JSP).
// Note: The JSP is supposed to determine the content type itself.
RequestDispatcher rd = request.getRequestDispatcher(facesContext.getViewRoot().getViewId() );
if (rd == null) {
throw new ServletException(
"Could not get RequestDispatcher for [" + getUrl() + "]: check that this file exists within your WAR");
}
// If already included or response already committed, perform include, else forward.
if (useInclude(request, response)) {
rd.include(request, response);
if (logger.isDebugEnabled()) {
logger.debug("Included resource [" + getUrl() + "] in InternalResourceView '" + getBeanName() + "'");
}
}
else {
rd.forward(request, response);
if (logger.isDebugEnabled()) {
logger.debug("Forwarded to resource [" + getUrl() + "] in InternalResourceView '" + getBeanName() + "'");
}
}
}
// You need an inner class to be able to call FacesContext.setCurrentInstance
// since it's a protected method
private abstract static class InnerFacesContext extends FacesContext
{
protected static void setFacesContextAsCurrentInstance(FacesContext facesContext) {
FacesContext.setCurrentInstance(facesContext);
}
}
private FacesContext getFacesContext(HttpServletRequest request, HttpServletResponse response) throws Exception {
// Try to get it first
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);
PortletContext portletContext = ((PortletApplicationContext) getApplicationContext()).getPortletContext();
// Doesn't set this instance as the current instance of FacesContext.getCurrentInstance
facesContext = contextFactory.getFacesContext(portletContext, request, response, lifecycle);
// Set using our inner class
InnerFacesContext.setFacesContextAsCurrentInstance(facesContext);
// Determine the path for the request dispatcher.
String dispatcherPath = prepareForRendering(request, response);
// set a new viewRoot, otherwise context.getViewRoot returns null
UIViewRoot view = facesContext.getApplication().getViewHandler().createView(facesContext, dispatcherPath);
facesContext.setViewRoot(view);
return facesContext;
}
}