/******************************************************************************
* JBoss, a division of Red Hat *
* Copyright 2006, Red Hat Middleware, LLC, and individual *
* contributors as indicated by the @authors tag. See the *
* copyright.txt in the distribution for a full listing of *
* individual contributors. *
* *
* This 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 2.1 of *
* the License, or (at your option) any later version. *
* *
* This software 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 software; if not, write to the Free *
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
******************************************************************************/
package org.jboss.portletbridge.lifecycle;
import org.jboss.portletbridge.AjaxPortletBridge;
import org.jboss.portletbridge.BridgeRequestScope;
import org.jboss.portletbridge.RequestScopeManager;
import org.jboss.portletbridge.richfaces.RichFacesHelper;
import org.jboss.portletbridge.context.AbstractExternalContext;
import org.jboss.portletbridge.context.PortalActionURL;
import org.jboss.portletbridge.context.PortletBridgeContext;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import javax.portlet.faces.Bridge;
import java.util.Map;
import java.util.HashMap;
/**
* @author asmirnov
*/
public class PortalPhaseListener implements PhaseListener {
/**
*
*/
private static final long serialVersionUID = -4023885603543145666L;
private RichFacesHelper ajaxContext;
public PortalPhaseListener() {
try {
ajaxContext = new RichFacesHelper();
} catch (NoClassDefFoundError e) {
// Do nothing, no Richfaces classes available.
ajaxContext = null;
}
}
/*
* (non-Javadoc)
*
* @see
* javax.faces.event.PhaseListener#afterPhase(javax.faces.event.PhaseEvent)
*/
public void afterPhase(PhaseEvent event) {
PhaseId phaseId = event.getPhaseId();
FacesContext context = event.getFacesContext();
Object portletPhase = context.getExternalContext().getRequestMap().get(
Bridge.PORTLET_LIFECYCLE_PHASE);
if (phaseId.equals(PhaseId.RESTORE_VIEW)) {
if (Bridge.PortletPhase.RENDER_PHASE.equals(portletPhase)) {
context.renderResponse();
}
} else if (phaseId.equals(PhaseId.PROCESS_VALIDATIONS)) {
if (Bridge.PortletPhase.EVENT_PHASE.equals(portletPhase)) {
context.responseComplete();
}
}
}
/*
* (non-Javadoc)
*
* @see
* javax.faces.event.PhaseListener#beforePhase(javax.faces.event.PhaseEvent)
*/
public void beforePhase(PhaseEvent event) {
PhaseId phaseId = event.getPhaseId();
FacesContext context = event.getFacesContext();
Object portletPhase = context.getExternalContext().getRequestMap().get(
Bridge.PORTLET_LIFECYCLE_PHASE);
if (null == portletPhase && phaseId.equals(PhaseId.RENDER_RESPONSE)) {
// For a servlet phase, put all portletbridge-related parameters
// back to ajaxContext.
BridgeRequestScope windowState = null;
PortletBridgeContext bridgeContext = (PortletBridgeContext) context
.getExternalContext().getRequestMap().get(
PortletBridgeContext.REQUEST_PARAMETER_NAME);
if (null != bridgeContext) {
windowState = bridgeContext.getRequestScope();
}
if (null != ajaxContext && null != windowState) {
Map<String,Object> commonAjaxParameters = ajaxContext.getCurrentInstance(
context).getCommonAjaxParameters();
commonAjaxParameters.put(RequestScopeManager.STATE_ID_PARAMETER,
bridgeContext.getStateId().toString());
commonAjaxParameters.put(
AjaxPortletBridge.AJAX_NAMESPACE_PARAMETER, windowState
.getNamespace());
}
}
}
/*
* (non-Javadoc)
*
* @see javax.faces.event.PhaseListener#getPhaseId()
*/
public PhaseId getPhaseId() {
// This listener process all phases.
return PhaseId.ANY_PHASE;
}
}