Search in sources :

Example 1 with ApplicationConfig

use of com.liferay.faces.util.config.ApplicationConfig in project liferay-faces-bridge-impl by liferay.

the class ResourceValidatorBridgeImpl method isSelfReferencing.

@Override
public boolean isSelfReferencing(FacesContext facesContext, String resourceId) {
    ExternalContext externalContext = facesContext.getExternalContext();
    final Product PLUTO = ProductFactory.getProductInstance(externalContext, Product.Name.PLUTO);
    // If the delegation chain indicates that the specified resource is not self-referencing, then
    boolean selfReferencing = super.isSelfReferencing(facesContext, resourceId);
    if (PLUTO.isDetected() && (!selfReferencing) && (resourceId != null)) {
        // Process the configured servlet entries in order to determine which ones are portlet invokers.
        Set<String> invokerServletNames = new HashSet<String>();
        Map<String, Object> applicationMap = externalContext.getApplicationMap();
        ApplicationConfig applicationConfig = (ApplicationConfig) applicationMap.get(ApplicationConfig.class.getName());
        WebConfig webConfig = applicationConfig.getWebConfig();
        List<ConfiguredServlet> configuredServlets = webConfig.getConfiguredServlets();
        for (ConfiguredServlet configuredServlet : configuredServlets) {
            String configuredServletClass = configuredServlet.getServletClass();
            if (isInvokerServletClass(configuredServletClass)) {
                invokerServletNames.add(configuredServlet.getServletName());
            }
        }
        // For each of the servlet-mapping entries:
        List<ConfiguredServletMapping> configuredServletMappings = webConfig.getConfiguredServletMappings();
        for (ConfiguredServletMapping configuredServletMapping : configuredServletMappings) {
            // Determine whether or not the current servlet-mapping is mapped to a portlet invoker servlet-class.
            if (invokerServletNames.contains(configuredServletMapping.getServletName())) {
                if (configuredServletMapping.isMatch(resourceId)) {
                    selfReferencing = true;
                    break;
                }
            }
        }
    }
    return selfReferencing;
}
Also used : Product(com.liferay.faces.util.product.Product) WebConfig(com.liferay.faces.util.config.WebConfig) ApplicationConfig(com.liferay.faces.util.config.ApplicationConfig) ExternalContext(javax.faces.context.ExternalContext) ConfiguredServlet(com.liferay.faces.util.config.ConfiguredServlet) ConfiguredServletMapping(com.liferay.faces.util.config.ConfiguredServletMapping) HashSet(java.util.HashSet)

Example 2 with ApplicationConfig

use of com.liferay.faces.util.config.ApplicationConfig 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 3 with ApplicationConfig

use of com.liferay.faces.util.config.ApplicationConfig in project liferay-faces-bridge-impl by liferay.

the class BridgePhaseResourceImpl method getConfiguredFacesServletMappings.

protected List<ConfiguredServletMapping> getConfiguredFacesServletMappings() {
    String appConfigAttrName = ApplicationConfig.class.getName();
    ExternalContext externalContext = facesContext.getExternalContext();
    Map<String, Object> applicationMap = externalContext.getApplicationMap();
    ApplicationConfig applicationConfig = (ApplicationConfig) applicationMap.get(appConfigAttrName);
    FacesConfig facesConfig = applicationConfig.getFacesConfig();
    return facesConfig.getConfiguredFacesServletMappings();
}
Also used : FacesConfig(com.liferay.faces.util.config.FacesConfig) ApplicationConfig(com.liferay.faces.util.config.ApplicationConfig) ExternalContext(javax.faces.context.ExternalContext)

Example 4 with ApplicationConfig

use of com.liferay.faces.util.config.ApplicationConfig in project liferay-faces-bridge-impl by liferay.

the class ExternalContextCompat_2_2_Impl method getApplicationContextPath.

/**
 * @see    ExternalContext#getApplicationContextPath()
 * @since  JSF 2.2
 */
@Override
public String getApplicationContextPath() {
    if (applicationContextPath == null) {
        String appConfigAttrName = ApplicationConfig.class.getName();
        ApplicationConfig applicationConfig = (ApplicationConfig) getApplicationMap().get(appConfigAttrName);
        applicationContextPath = applicationConfig.getContextPath();
    }
    return applicationContextPath;
}
Also used : ApplicationConfig(com.liferay.faces.util.config.ApplicationConfig)

Example 5 with ApplicationConfig

use of com.liferay.faces.util.config.ApplicationConfig in project liferay-faces-bridge-impl by liferay.

the class BridgeSessionListener method sessionDestroyed.

@Override
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
    if (firstInstance) {
        // Discover Factories
        BeanManagerFactory beanManagerFactory = null;
        BridgeRequestScopeManagerFactory bridgeRequestScopeManagerFactory = null;
        HttpSession httpSession = null;
        ServletContext servletContext = null;
        PortletContext portletContext = null;
        try {
            httpSession = httpSessionEvent.getSession();
            servletContext = httpSession.getServletContext();
            portletContext = new PortletContextAdapter(servletContext);
            beanManagerFactory = (BeanManagerFactory) BridgeFactoryFinder.getFactory(portletContext, BeanManagerFactory.class);
            bridgeRequestScopeManagerFactory = (BridgeRequestScopeManagerFactory) BridgeFactoryFinder.getFactory(portletContext, BridgeRequestScopeManagerFactory.class);
        } catch (Exception e) {
            String contextPath = "unknown";
            if (servletContext != null) {
                contextPath = servletContext.getContextPath();
            }
            logger.error("Unable to discover factories for contextPath=[{0}] possibly because the portlet never received a RenderRequest", contextPath);
        }
        if ((beanManagerFactory != null) && (bridgeRequestScopeManagerFactory != null)) {
            // Cleanup instances of BridgeRequestScope that are associated with the expiring session.
            BridgeRequestScopeManager bridgeRequestScopeManager = bridgeRequestScopeManagerFactory.getBridgeRequestScopeManager(portletContext);
            bridgeRequestScopeManager.removeBridgeRequestScopesBySession(httpSession);
            // For each session attribute:
            String appConfigAttrName = ApplicationConfig.class.getName();
            ApplicationConfig applicationConfig = (ApplicationConfig) servletContext.getAttribute(appConfigAttrName);
            BeanManager beanManager = beanManagerFactory.getBeanManager(applicationConfig.getFacesConfig());
            try {
                Enumeration<String> attributeNames = httpSession.getAttributeNames();
                while (attributeNames.hasMoreElements()) {
                    String attributeName = attributeNames.nextElement();
                    // is an attribute that was set using PortletSession.setAttribute(String, Object).
                    if ((attributeName != null) && attributeName.startsWith("javax.portlet.p.")) {
                        int pos = attributeName.indexOf("?");
                        if (pos > 0) {
                            Object attributeValue = httpSession.getAttribute(attributeName);
                            httpSession.removeAttribute(attributeName);
                            if (attributeValue != null) {
                                // one would get cleaned-up by Mojarra.
                                if (beanManager.isManagedBean(attributeName, attributeValue)) {
                                    PreDestroyInvokerFactory preDestroyInvokerFactory = (PreDestroyInvokerFactory) BridgeFactoryFinder.getFactory(portletContext, PreDestroyInvokerFactory.class);
                                    PreDestroyInvoker preDestroyInvoker = preDestroyInvokerFactory.getPreDestroyInvoker(servletContext);
                                    preDestroyInvoker.invokeAnnotatedMethods(attributeValue, true);
                                } else // Otherwise,
                                {
                                    // If the current session attribute is Mojarra-vendor-specific, then
                                    String attributeValueClassName = attributeValue.getClass().getName();
                                    if (attributeName.contains(MOJARRA_ACTIVE_VIEW_MAPS) || attributeValueClassName.contains(MOJARRA_PACKAGE_PREFIX)) {
                                        // Rename the namespaced attribute by stripping off the standard portlet
                                        // prefix. This will enable Mojarra's session expiration features to find
                                        // attributes that it is expecting.
                                        String nonPrefixedName = attributeName.substring(pos + 1);
                                        logger.debug("Renaming Mojarra session attributeName=[{0}] -> [{1}]", attributeName, nonPrefixedName);
                                        httpSession.setAttribute(nonPrefixedName, attributeValue);
                                        // If this is the attribute that contains all of the active view maps, then
                                        if (MOJARRA_ACTIVE_VIEW_MAPS.equals(nonPrefixedName)) {
                                            if (mojarraAbleToCleanUpViewScopedDataAccessor.get(portletContext)) {
                                                // Invoke the Mojarra
                                                // ViewScopeManager.sessionDestroyed(HttpSessionEvent) method in
                                                // order to cleanup the active view maps. Rather than waiting for
                                                // the servlet container to call the method during session
                                                // expiration, it is important to call it directly within this
                                                // while-loop for two reasons: 1) If the developer did not
                                                // explicitly specify the order of the Mojarra ConfigureListener and
                                                // this BridgeSessionListener in WEB-IN/web.xml descriptor
                                                // (FACES-1483) then there is no guarantee that the method would get
                                                // called. 2) In the case of multiple portlet instances, each
                                                // instance has its own namespaced attribute in the session.
                                                // Renaming each namespaced attribute to
                                                // "com.sun.faces.application.view.activeViewMaps" would only enable
                                                // Mojarra to cleanup the last one.
                                                HttpSessionListener viewScopeManager = (HttpSessionListener) servletContext.getAttribute(MOJARRA_VIEW_SCOPE_MANAGER);
                                                if (viewScopeManager != null) {
                                                    try {
                                                        logger.debug("Asking Mojarra ViewScopeManager to cleanup @ViewScoped managed-beans");
                                                        viewScopeManager.sessionDestroyed(httpSessionEvent);
                                                    } catch (Exception e) {
                                                        logger.error(e);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            } catch (IllegalStateException e) {
                logger.warn("Server does not permit cleanup of Mojarra managed-beans during session expiration");
            }
        }
    }
}
Also used : BridgeRequestScopeManagerFactory(com.liferay.faces.bridge.scope.internal.BridgeRequestScopeManagerFactory) PreDestroyInvoker(com.liferay.faces.bridge.bean.PreDestroyInvoker) HttpSessionListener(javax.servlet.http.HttpSessionListener) HttpSession(javax.servlet.http.HttpSession) PreDestroyInvokerFactory(com.liferay.faces.bridge.bean.PreDestroyInvokerFactory) ApplicationConfig(com.liferay.faces.util.config.ApplicationConfig) BeanManagerFactory(com.liferay.faces.bridge.bean.BeanManagerFactory) ServletContext(javax.servlet.ServletContext) PortletContext(javax.portlet.PortletContext) PortletContextAdapter(com.liferay.faces.bridge.context.internal.PortletContextAdapter) BeanManager(com.liferay.faces.bridge.bean.BeanManager) BridgeRequestScopeManager(com.liferay.faces.bridge.scope.internal.BridgeRequestScopeManager)

Aggregations

ApplicationConfig (com.liferay.faces.util.config.ApplicationConfig)8 ExternalContext (javax.faces.context.ExternalContext)4 ConfiguredServlet (com.liferay.faces.util.config.ConfiguredServlet)3 ConfiguredServletMapping (com.liferay.faces.util.config.ConfiguredServletMapping)3 WebConfig (com.liferay.faces.util.config.WebConfig)3 BeanManager (com.liferay.faces.bridge.bean.BeanManager)2 BeanManagerFactory (com.liferay.faces.bridge.bean.BeanManagerFactory)2 FacesConfig (com.liferay.faces.util.config.FacesConfig)2 HashSet (java.util.HashSet)2 PortletContext (javax.portlet.PortletContext)2 PreDestroyInvoker (com.liferay.faces.bridge.bean.PreDestroyInvoker)1 PreDestroyInvokerFactory (com.liferay.faces.bridge.bean.PreDestroyInvokerFactory)1 PortletContextAdapter (com.liferay.faces.bridge.context.internal.PortletContextAdapter)1 BridgeRequestScopeManager (com.liferay.faces.bridge.scope.internal.BridgeRequestScopeManager)1 BridgeRequestScopeManagerFactory (com.liferay.faces.bridge.scope.internal.BridgeRequestScopeManagerFactory)1 MultiPartConfig (com.liferay.faces.util.config.MultiPartConfig)1 Product (com.liferay.faces.util.product.Product)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 FacesContext (javax.faces.context.FacesContext)1