1 5 package org.exoplatform.portlet.faces; 6 7 import java.io.IOException ; 8 import javax.faces.FactoryFinder; 9 import javax.faces.application.*; 10 import javax.faces.context.FacesContext; 11 import javax.faces.context.FacesContextFactory; 12 import javax.faces.lifecycle.Lifecycle; 13 import javax.faces.lifecycle.LifecycleFactory; 14 import javax.portlet.*; 15 import org.apache.commons.logging.Log; 16 import org.exoplatform.services.log.LogUtil; 17 import org.exoplatform.commons.utils.ExceptionUtil; 18 import org.exoplatform.portlet.faces.application.FacesPortletViewHandler; 19 import org.exoplatform.portlet.faces.application.PortletFacesData; 20 21 26 public class ExoFacesPortlet extends GenericPortlet { 27 28 private static final String COMPONENT_TAG_STACK_ATTR = "javax.faces.webapp.COMPONENT_TAG_STACK"; 29 private static final String GLOBAL_ID_VIEW = "javax.faces.webapp.GLOBAL_ID_VIEW"; 30 private static final String CURRENT_VIEW_ROOT = "javax.faces.webapp.CURRENT_VIEW_ROOT"; 31 32 private Lifecycle facesLifecycle_ ; 33 34 public void init(PortletConfig config) throws PortletException { 35 super.init(config); 36 try { 37 XHTMLRendererConfiguration.confiure() ; 38 XHTMLMPRendererConfiguration.confiure() ; 39 LifecycleFactory lfactory = (LifecycleFactory) FactoryFinder.getFactory( FactoryFinder.LIFECYCLE_FACTORY ); 40 facesLifecycle_ = lfactory.getLifecycle( LifecycleFactory.DEFAULT_LIFECYCLE ); 41 ApplicationFactory factory = 42 (ApplicationFactory) FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY); 43 Application app = factory.getApplication(); 44 ViewHandler handler = new FacesPortletViewHandler(app.getStateManager()); 45 app.setViewHandler( handler ); 46 } catch (Exception ex) { 47 getLog().error("Error: ", ex); 48 } 49 } 50 51 public void render (RenderRequest request, RenderResponse response) throws PortletException, java.io.IOException { 52 Object stack = request.getAttribute(COMPONENT_TAG_STACK_ATTR ); 54 Object currentViewRoot = request.getAttribute(CURRENT_VIEW_ROOT ); 55 Object globalIdView = request.getAttribute(GLOBAL_ID_VIEW); 56 request.removeAttribute(CURRENT_VIEW_ROOT ); 58 request.removeAttribute(COMPONENT_TAG_STACK_ATTR ); 59 request.removeAttribute(GLOBAL_ID_VIEW); 60 WindowState state = request.getWindowState(); 62 if (!state.equals(WindowState.MINIMIZED)) { 63 response.setContentType("text/html") ; 64 processFacesLifeCycle(request, response) ; 65 } 66 request.setAttribute(COMPONENT_TAG_STACK_ATTR, stack); 68 request.setAttribute(CURRENT_VIEW_ROOT , currentViewRoot); 69 request.setAttribute(GLOBAL_ID_VIEW, globalIdView); 70 } 72 73 public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException { 74 processFacesLifeCycle(request, response) ; 75 } 76 77 protected void processFacesLifeCycle(PortletRequest request, PortletResponse response) { 78 String lastViewId = null ; 79 FacesContext facesContext = null; 80 try { 81 PortletMode mode = request.getPortletMode() ; 82 PortletConfig config = getPortletConfig(); 83 FacesContextFactory factory = 84 (FacesContextFactory) FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY ); 85 facesContext = factory.getFacesContext( config, request, response, facesLifecycle_ ); 86 PortletFacesData data = PortletFacesData.getPortletFacesData(facesContext) ; 87 lastViewId = request.getParameter(".view-id") ; 88 if (lastViewId != null) { 89 int idx = lastViewId.indexOf("?") ; 90 if(idx > 0) lastViewId = lastViewId.substring(0, idx); 91 } else { 92 lastViewId = data.getLastView(mode) ; 93 } 94 request.setAttribute("javax.servlet.include.path_info", lastViewId) ; 95 96 facesLifecycle_.execute(facesContext); 97 facesLifecycle_.render(facesContext); 98 data.setLastView(mode, facesContext.getViewRoot().getViewId()) ; 99 } catch (Throwable t) { 100 t = ExceptionUtil.getRootCause(t) ; 101 logError(t, lastViewId) ; 102 } finally { 103 facesContext.release(); 104 } 105 } 106 107 private void logError(Throwable t, String lastViewId) { 108 StringBuffer b = new StringBuffer () ; 109 b.append("portlet is " + getPortletConfig().getPortletName()).append("\n") ; 110 b.append("Last View Id is " + lastViewId).append("\n") ; 111 b.append("ExoFacesPortlet Error: ") ; 112 getLog().error(b.toString(), t) ; 113 } 114 115 private Log getLog() { return LogUtil.getLog("org.exoplatform.portlet.faces.faces"); } 116 } | Popular Tags |