Package org.jboss.portletbridge.lifecycle

Source Code of org.jboss.portletbridge.lifecycle.PortalPhaseListener

/******************************************************************************
* 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.richfaces.RichFacesHelper;
import org.jboss.portletbridge.application.PortletStateHolder;
import org.jboss.portletbridge.application.PortletWindowState;
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);

    PortletWindowState windowState = null;
    PortletBridgeContext bridgeContext = (PortletBridgeContext) context
        .getExternalContext().getRequestMap().get(
            PortletBridgeContext.REQUEST_PARAMETER_NAME);
    if (null != bridgeContext) {
      windowState = bridgeContext.getWindowState();
    }

    if (phaseId.equals(PhaseId.RESTORE_VIEW)) {
      if (Bridge.PortletPhase.RenderPhase.equals(portletPhase)) {
        context.renderResponse();
      }

    } else if (phaseId.equals(PhaseId.RENDER_RESPONSE)) {
      // save Seam conversation Id.
      if (null != windowState) {
        windowState.saveSeamConversationId(context);
      }

    }

  }

  /*
   * (non-Javadoc)
   *
   * @see
   * javax.faces.event.PhaseListener#beforePhase(javax.faces.event.PhaseEvent)
   */
  @SuppressWarnings("unchecked")
  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.

      PortletWindowState windowState = null;
      PortletBridgeContext bridgeContext = (PortletBridgeContext) context
          .getExternalContext().getRequestMap().get(
              PortletBridgeContext.REQUEST_PARAMETER_NAME);
      if (null != bridgeContext) {
        windowState = bridgeContext.getWindowState();
      }

      if (null != ajaxContext && null != windowState) {
        Map commonAjaxParameters = ajaxContext.getCurrentInstance(
            context).getCommonAjaxParameters();

        PortalActionURL pal = windowState.getPortalActionURL();
        if (null != pal) {
          pal.setParameter(AbstractExternalContext.VIEW_ID_PARAMETER,
              context.getViewRoot().getViewId());
        }
        commonAjaxParameters.put(PortletStateHolder.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;
  }

}
TOP

Related Classes of org.jboss.portletbridge.lifecycle.PortalPhaseListener

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.