Search in sources :

Example 1 with PortletPhase

use of javax.portlet.faces.Bridge.PortletPhase in project liferay-faces-bridge-impl by liferay.

the class ManagedBeanScopePhaseListener method afterPhase.

@Override
public void afterPhase(PhaseEvent phaseEvent) {
    if (phaseEvent.getPhaseId() == PhaseId.RENDER_RESPONSE) {
        FacesContext facesContext = phaseEvent.getFacesContext();
        PortletPhase portletRequestPhase = BridgeUtil.getPortletRequestPhase(facesContext);
        if ((portletRequestPhase == Bridge.PortletPhase.RENDER_PHASE) || (portletRequestPhase == Bridge.PortletPhase.RESOURCE_PHASE)) {
            // Remove any managed-beans in request scope. According to the JSF 2.0 JavaDocs for {@link
            // ExternalContext.getRequestMap}, before a managed-bean is removed from the map, any public no-argument
            // void return methods annotated with javax.annotation.PreDestroy must be called first. Note that the
            // bridge {@link RequestAttributeMap.remove(Object)} method will ensure that any @PreDestroy method(s)
            // are called. The JavaDocs also state that this should only be the case for objects that are actually
            // managed-beans.
            ExternalContext externalContext = facesContext.getExternalContext();
            Map<String, Object> requestScope = externalContext.getRequestMap();
            List<String> managedBeanKeysToRemove = new ArrayList<String>();
            Set<Map.Entry<String, Object>> mapEntries = requestScope.entrySet();
            String appConfigAttrName = ApplicationConfig.class.getName();
            Map<String, Object> applicationMap = externalContext.getApplicationMap();
            ApplicationConfig applicationConfig = (ApplicationConfig) applicationMap.get(appConfigAttrName);
            PortletContext portletContext = (PortletContext) externalContext.getContext();
            BeanManagerFactory beanManagerFactory = (BeanManagerFactory) BridgeFactoryFinder.getFactory(portletContext, BeanManagerFactory.class);
            BeanManager beanManager = beanManagerFactory.getBeanManager(applicationConfig.getFacesConfig());
            for (Map.Entry<String, Object> mapEntry : mapEntries) {
                String potentialManagedBeanName = mapEntry.getKey();
                Object potentialManagedBeanValue = mapEntry.getValue();
                // simply pass the attribute name.
                if (beanManager.isManagedBean(potentialManagedBeanName, potentialManagedBeanValue)) {
                    managedBeanKeysToRemove.add(potentialManagedBeanName);
                }
            }
            for (String managedBeanKey : managedBeanKeysToRemove) {
                requestScope.remove(managedBeanKey);
            }
        }
    }
}
Also used : FacesContext(javax.faces.context.FacesContext) PortletPhase(javax.portlet.faces.Bridge.PortletPhase) ArrayList(java.util.ArrayList) ApplicationConfig(com.liferay.faces.util.config.ApplicationConfig) ExternalContext(javax.faces.context.ExternalContext) BeanManagerFactory(com.liferay.faces.bridge.bean.BeanManagerFactory) PortletContext(javax.portlet.PortletContext) BeanManager(com.liferay.faces.bridge.bean.BeanManager) Map(java.util.Map)

Example 2 with PortletPhase

use of javax.portlet.faces.Bridge.PortletPhase in project liferay-faces-bridge-impl by liferay.

the class BridgeRequestAttributeListener method attributeAdded.

/**
 * This method is called after an attribute is added to the ServletRequest. Note that this should only get called
 * for remote WSRP portlets. For more info, see: http://issues.liferay.com/browse/FACES-146
 */
@Override
public void attributeAdded(ServletRequestAttributeEvent servletRequestAttributeEvent) {
    // NOTE: We only care about phases prior to the HEADER_PHASE because we're concerned here about managed beans
    // that get added to the request scope when the BridgeRequestScope begins. We're trying to provide those managed
    // beans with an opportunity to prepare for an unexpected invocation of their methods annotated with
    // @PreDestroy.
    ServletRequest servletRequest = servletRequestAttributeEvent.getServletRequest();
    PortletPhase phase = (PortletPhase) servletRequest.getAttribute(Bridge.PORTLET_LIFECYCLE_PHASE);
    // HEADER_PHASE, then
    if ((phase != null) && (phase != PortletPhase.HEADER_PHASE)) {
        // If the attribute being added is not excluded, then invoke all methods on the attribute value (class
        // instance) that are annotated with the BridgeRequestScopeAttributeAdded annotation.
        String attributeName = servletRequestAttributeEvent.getName();
        FacesContext facesContext = FacesContext.getCurrentInstance();
        BridgeConfig bridgeConfig = RequestMapUtil.getBridgeConfig(facesContext);
        Set<String> excludedRequestScopeAttributes = bridgeConfig.getExcludedRequestAttributes();
        if (!excludedRequestScopeAttributes.contains(attributeName)) {
            Object attributeValue = servletRequestAttributeEvent.getValue();
            logger.trace("Attribute added name=[{0}] value=[{1}]", attributeName, attributeValue);
            if (attributeValue != null) {
                Method[] methods = attributeValue.getClass().getMethods();
                if (methods != null) {
                    for (Method method : methods) {
                        if (method != null) {
                            if (method.isAnnotationPresent(BridgeRequestScopeAttributeAdded.class)) {
                                try {
                                    method.invoke(attributeValue);
                                } catch (Exception e) {
                                    logger.error(e);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : ServletRequest(javax.servlet.ServletRequest) FacesContext(javax.faces.context.FacesContext) PortletPhase(javax.portlet.faces.Bridge.PortletPhase) BridgeConfig(javax.portlet.faces.BridgeConfig) Method(java.lang.reflect.Method)

Example 3 with PortletPhase

use of javax.portlet.faces.Bridge.PortletPhase in project liferay-faces-bridge-impl by liferay.

the class ViewHandlerCompatImpl method getRedirectURL.

@Override
public String getRedirectURL(FacesContext facesContext, String viewId, Map<String, List<String>> parameters, boolean includeViewParams) {
    PortletPhase portletRequestPhase = BridgeUtil.getPortletRequestPhase(facesContext);
    ExternalContext externalContext = facesContext.getExternalContext();
    // Determine whether or not it is necessary to work-around the patch applied to Mojarra in JAVASERVERFACES-3023.
    final Product MOJARRA = ProductFactory.getProductInstance(externalContext, Product.Name.MOJARRA);
    boolean workaroundMojarra = (MOJARRA.isDetected()) && ((portletRequestPhase == Bridge.PortletPhase.ACTION_PHASE) || (portletRequestPhase == Bridge.PortletPhase.EVENT_PHASE));
    Map<String, Object> requestMap = externalContext.getRequestMap();
    if (workaroundMojarra) {
        requestMap.put(BridgeExt.RESPONSE_CHARACTER_ENCODING, "UTF-8");
    }
    String redirectURL = super.getRedirectURL(facesContext, viewId, parameters, includeViewParams);
    if (workaroundMojarra) {
        requestMap.remove(BridgeExt.RESPONSE_CHARACTER_ENCODING);
    }
    return redirectURL;
}
Also used : PortletPhase(javax.portlet.faces.Bridge.PortletPhase) ExternalContext(javax.faces.context.ExternalContext) Product(com.liferay.faces.util.product.Product)

Example 4 with PortletPhase

use of javax.portlet.faces.Bridge.PortletPhase in project liferay-faces-bridge-impl by liferay.

the class BridgeURLActionImpl method toBaseURL.

@Override
public BaseURL toBaseURL(FacesContext facesContext) throws MalformedURLException {
    BaseURL baseURL;
    // If this is executing during the ACTION_PHASE of the portlet lifecycle, then
    PortletPhase portletRequestPhase = BridgeUtil.getPortletRequestPhase(facesContext);
    if (portletRequestPhase == Bridge.PortletPhase.ACTION_PHASE) {
        // Since ActionResponse is not a MimeResponse, there is no way to create a RenderURL in a standard way.
        baseURL = new BaseURLBridgeURIAdapterImpl(bridgeURI);
    } else // Otherwise,
    {
        // Otherwise, if the URI starts with a "#" character, or it's an absolute URL that is external to
        // this portlet, then simply return the URI as required by the Bridge Spec.
        String uri = bridgeURI.toString();
        if (uri.startsWith("#") || (bridgeURI.isAbsolute() && bridgeURI.isExternal(contextPath))) {
            // TCK: encodeActionURLPoundCharTest
            baseURL = new BaseURLBridgeURIAdapterImpl(bridgeURI);
        } else // then return an absolute path (to the path in the URI) as required by the Bridge Spec.
        if (directLink || bridgeURI.isExternal(contextPath)) {
            ExternalContext externalContext = facesContext.getExternalContext();
            PortletRequest portletRequest = (PortletRequest) externalContext.getRequest();
            baseURL = new BaseURLDirectImpl(bridgeURI, portletRequest.getScheme(), portletRequest.getServerName(), portletRequest.getServerPort());
        } else // Otherwise,
        {
            // Determine whether or not the portlet mode is to be changed by examining the
            // "javax.portlet.faces.PortletMode" parameter.
            boolean modeChanged = false;
            String portletMode = getParameter(Bridge.PORTLET_MODE_PARAMETER);
            if ((portletMode != null) && (portletMode.length() > 0)) {
                modeChanged = true;
            }
            // "portlet:resource".
            if (bridgeURI.isPortletScheme()) {
                Bridge.PortletPhase urlPortletPhase = bridgeURI.getPortletPhase();
                if (urlPortletPhase == Bridge.PortletPhase.ACTION_PHASE) {
                    baseURL = createActionURL(facesContext, modeChanged);
                } else if (urlPortletPhase == Bridge.PortletPhase.RENDER_PHASE) {
                    baseURL = createRenderURL(facesContext, modeChanged);
                } else {
                    baseURL = createResourceURL(facesContext, modeChanged);
                }
            } else {
                if (portletRequestPhase == Bridge.PortletPhase.EVENT_PHASE) {
                    baseURL = new BaseURLBridgeURIAdapterImpl(bridgeURI);
                } else if (bookmarkable || (redirect && (portletRequestPhase == PortletPhase.RESOURCE_PHASE))) {
                    baseURL = createRenderURL(facesContext, modeChanged);
                } else {
                    baseURL = createActionURL(facesContext, modeChanged);
                }
            }
            // render parameters from the current PortletRequest to the BaseURL.
            if (selfReferencing) {
                ExternalContext externalContext = facesContext.getExternalContext();
                PortletRequest portletRequest = (PortletRequest) externalContext.getRequest();
                copyRenderParameters(portletRequest, baseURL, externalContext.encodeNamespace(""), UINamingContainer.getSeparatorChar(facesContext));
            }
            // PortletURL.
            if (baseURL instanceof PortletURL) {
                PortletURL portletURL = (PortletURL) baseURL;
                ExternalContext externalContext = facesContext.getExternalContext();
                PortletRequest portletRequest = (PortletRequest) externalContext.getRequest();
                PortletURLHelper.setPortletMode(portletURL, portletMode, portletRequest);
                // According to the Bridge Spec, the "javax.portlet.faces.PortletMode"" parameter must not be
                // "carried forward to the generated reference." According to a clarification in the Portlet 3.0
                // JavaDoc for BaseURL#setProperty(String,String), setting the parameter to null will remove it.
                portletURL.setParameter(Bridge.PORTLET_MODE_PARAMETER, (String) null);
                String windowState = getParameter(Bridge.PORTLET_WINDOWSTATE_PARAMETER);
                PortletURLHelper.setWindowState(portletURL, windowState, portletRequest);
                // According to the Bridge Spec, the "javax.portlet.faces.WindowState" parameter must not be
                // "carried forward to the generated reference." According to a clarification in the Portlet 3.0
                // JavaDoc for BaseURL#setProperty(String,String), setting the parameter to null will remove it.
                portletURL.setParameter(Bridge.PORTLET_WINDOWSTATE_PARAMETER, (String) null);
            }
            // Apply the security.
            String secure = getParameter(Bridge.PORTLET_SECURE_PARAMETER);
            PortletURLHelper.setSecure(baseURL, secure);
            // According to the Bridge Spec, the "javax.portlet.faces.Secure" parameter must not be "carried
            // forward to the generated reference." According to a clarification in the Portlet 3.0 JavaDoc for
            // BaseURL#setProperty(String,String), setting the parameter to null will remove it.
            baseURL.setParameter(Bridge.PORTLET_SECURE_PARAMETER, (String) null);
        }
    }
    return baseURL;
}
Also used : PortletPhase(javax.portlet.faces.Bridge.PortletPhase) PortletRequest(javax.portlet.PortletRequest) ExternalContext(javax.faces.context.ExternalContext) BaseURL(javax.portlet.BaseURL) PortletURL(javax.portlet.PortletURL)

Example 5 with PortletPhase

use of javax.portlet.faces.Bridge.PortletPhase in project liferay-faces-bridge-impl by liferay.

the class BridgeRequestScopeImpl method restoreState.

@Override
@SuppressWarnings("unchecked")
public void restoreState(FacesContext facesContext) {
    logger.debug("restoreState(facesContext)");
    boolean restoreNonExcludedRequestAttributes = ((beganInPhase == Bridge.PortletPhase.ACTION_PHASE) || (beganInPhase == Bridge.PortletPhase.EVENT_PHASE) || (beganInPhase == Bridge.PortletPhase.RESOURCE_PHASE));
    PortletPhase portletRequestPhase = BridgeUtil.getPortletRequestPhase(facesContext);
    if (portletRequestPhase == Bridge.PortletPhase.HEADER_PHASE) {
        ExternalContext externalContext = facesContext.getExternalContext();
        PortletRequest portletRequest = (PortletRequest) externalContext.getRequest();
        if (!portletMode.equals(portletRequest.getPortletMode())) {
            if (!portletModeChanged) {
                // TCK (requestNoScopeOnModeChangeTest) - In this test, a navigation-rule fires in the
                // ACTION_PHASE of the portlet lifecycle that contains a portlet mode change from VIEW to EDIT.
                // Since the BridgeRequestScope instance created in the ACTION_PHASE is not maintained, "this" will
                // be a new instance and the mode change that took place in the ACTION_PHASE will not be known. In
                // this case, the BridgeRequestScope was not maintained from the ACTION_PHASE to the RENDER_PHASE
                // but a navigation-rule. Detecting a mode change is possible though by checking the request to see
                // if it differs from VIEW mode (the default).
                portletModeChanged = true;
            }
            restoreNonExcludedRequestAttributes = false;
        }
    }
    if (((beganInPhase == Bridge.PortletPhase.ACTION_PHASE) || (beganInPhase == Bridge.PortletPhase.EVENT_PHASE) || (beganInPhase == Bridge.PortletPhase.RESOURCE_PHASE))) {
        // Restore the view root that may have been saved during the action/event/render phase of the portlet
        // lifecycle.
        UIViewRoot uiViewRoot = (UIViewRoot) getAttribute(BRIDGE_REQ_SCOPE_ATTR_FACES_VIEW_ROOT);
        if (uiViewRoot != null) {
            facesContext.setViewRoot(uiViewRoot);
            logger.debug("Restored viewId=[{0}] uiViewRoot=[{1}]", uiViewRoot.getViewId(), uiViewRoot);
        } else {
            logger.debug("Did not restore uiViewRoot");
        }
        if (!redirectOcurred) {
            // Restore the faces messages that may have been saved during the action/event/render phase of the
            // portlet lifecycle.
            List<FacesMessageWrapper> facesMessages = (List<FacesMessageWrapper>) getAttribute(BRIDGE_REQ_SCOPE_ATTR_FACES_MESSAGES);
            boolean restoredFacesMessages = false;
            if (facesMessages != null) {
                for (FacesMessageWrapper facesMessageWrapper : facesMessages) {
                    String clientId = facesMessageWrapper.getClientId();
                    FacesMessage facesMessage = facesMessageWrapper.getFacesMessage();
                    facesContext.addMessage(clientId, facesMessage);
                    logger.trace("Restored facesMessage=[{0}]", facesMessage.getSummary());
                    restoredFacesMessages = true;
                }
            }
            if (restoredFacesMessages) {
                logger.debug("Restored facesMessages");
            } else {
                logger.debug("Did not restore any facesMessages");
            }
            // NOTE: PROPOSE-FOR-BRIDGE3-API: https://issues.apache.org/jira/browse/PORTLETBRIDGE-203 Restore the
            // FacesContext attributes that may have been saved during the ACTION_PHASE of the portlet lifecycle.
            restoreJSF2FacesContextAttributes(facesContext);
        }
    }
    if (restoreNonExcludedRequestAttributes) {
        // Restore the non-excluded request attributes.
        List<RequestAttribute> savedRequestAttributes = (List<RequestAttribute>) getAttribute(BRIDGE_REQ_SCOPE_ATTR_REQUEST_ATTRIBUTES);
        boolean restoredNonExcludedRequestAttributes = false;
        if (savedRequestAttributes != null) {
            ExternalContext externalContext = facesContext.getExternalContext();
            Map<String, Object> currentRequestAttributes = externalContext.getRequestMap();
            // If a redirect did not occur, then restore the non-excluded request attributes.
            if (!redirectOcurred) {
                for (RequestAttribute requestAttribute : savedRequestAttributes) {
                    String name = requestAttribute.getName();
                    Object value = requestAttribute.getValue();
                    logger.trace("Restoring non-excluded request attribute name=[{0}] value=[{1}]", name, value);
                    currentRequestAttributes.put(name, value);
                    restoredNonExcludedRequestAttributes = true;
                }
            }
        }
        if (restoredNonExcludedRequestAttributes) {
            logger.debug("Restored non-excluded request attributes");
        } else {
            logger.debug("Did not restore any non-excluded request attributes");
        }
    }
    // If running in the EVENT_PHASE or HEADER_PHASE, then the client window must be restored.
    if ((portletRequestPhase == Bridge.PortletPhase.EVENT_PHASE) || (portletRequestPhase == Bridge.PortletPhase.HEADER_PHASE)) {
        // PROPOSE-FOR-BRIDGE3-API
        restoreClientWindow(facesContext.getExternalContext());
    }
    // If running in the HEADER_PHASE, then the Flash scope must be restored.
    if (portletRequestPhase == Bridge.PortletPhase.HEADER_PHASE) {
        // NOTE: PROPOSED-FOR-BRIDGE3-API: https://issues.apache.org/jira/browse/PORTLETBRIDGE-201
        // Restore the flash scope.
        ExternalContext externalContext = facesContext.getExternalContext();
        restoreFlashState(externalContext);
    }
    // If running in the HEADER_PHASE, then the incongruity context must be restored.
    if (((beganInPhase == Bridge.PortletPhase.ACTION_PHASE) || (beganInPhase == Bridge.PortletPhase.EVENT_PHASE)) && (portletRequestPhase == Bridge.PortletPhase.HEADER_PHASE)) {
        List<IncongruityAttribute> savedIncongruityAttributes = (List<IncongruityAttribute>) getAttribute(BRIDGE_REQ_SCOPE_ATTR_INCONGRUITY_CONTEXT_ATTRIBUTES);
        if (savedIncongruityAttributes != null) {
            ExternalContext externalContext = facesContext.getExternalContext();
            IncongruityContext incongruityContext = (IncongruityContext) externalContext.getRequestMap().get(IncongruityContext.class.getName());
            Map<String, Object> incongruityContextAttributes = incongruityContext.getAttributes();
            for (IncongruityAttribute incongruityAttribute : savedIncongruityAttributes) {
                String key = incongruityAttribute.getName();
                Object value = incongruityAttribute.getValue();
                incongruityContextAttributes.put(key, value);
            }
        }
    }
}
Also used : PortletPhase(javax.portlet.faces.Bridge.PortletPhase) FacesMessageWrapper(com.liferay.faces.bridge.util.internal.FacesMessageWrapper) IncongruityContext(com.liferay.faces.bridge.context.internal.IncongruityContext) PortletRequest(javax.portlet.PortletRequest) ExternalContext(javax.faces.context.ExternalContext) ArrayList(java.util.ArrayList) List(java.util.List) UIViewRoot(javax.faces.component.UIViewRoot) FacesMessage(javax.faces.application.FacesMessage)

Aggregations

PortletPhase (javax.portlet.faces.Bridge.PortletPhase)5 ExternalContext (javax.faces.context.ExternalContext)4 ArrayList (java.util.ArrayList)2 FacesContext (javax.faces.context.FacesContext)2 PortletRequest (javax.portlet.PortletRequest)2 BeanManager (com.liferay.faces.bridge.bean.BeanManager)1 BeanManagerFactory (com.liferay.faces.bridge.bean.BeanManagerFactory)1 IncongruityContext (com.liferay.faces.bridge.context.internal.IncongruityContext)1 FacesMessageWrapper (com.liferay.faces.bridge.util.internal.FacesMessageWrapper)1 ApplicationConfig (com.liferay.faces.util.config.ApplicationConfig)1 Product (com.liferay.faces.util.product.Product)1 Method (java.lang.reflect.Method)1 List (java.util.List)1 Map (java.util.Map)1 FacesMessage (javax.faces.application.FacesMessage)1 UIViewRoot (javax.faces.component.UIViewRoot)1 BaseURL (javax.portlet.BaseURL)1 PortletContext (javax.portlet.PortletContext)1 PortletURL (javax.portlet.PortletURL)1 BridgeConfig (javax.portlet.faces.BridgeConfig)1