Search in sources :

Example 1 with ConfiguredServletMapping

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

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

the class BridgePhaseResourceImpl method getExplicitFacesServletExtensionMapping.

protected ConfiguredServletMapping getExplicitFacesServletExtensionMapping(String resourceId) {
    ConfiguredServletMapping explicitFacesServletExtensionMapping = null;
    List<ConfiguredServletMapping> facesServletMappings = getConfiguredFacesServletMappings();
    for (ConfiguredServletMapping facesServletMapping : facesServletMappings) {
        if (facesServletMapping.isMatch(resourceId) && facesServletMapping.isExtensionMapped() && !facesServletMapping.isImplicit()) {
            explicitFacesServletExtensionMapping = facesServletMapping;
            break;
        }
    }
    return explicitFacesServletExtensionMapping;
}
Also used : ConfiguredServletMapping(com.liferay.faces.util.config.ConfiguredServletMapping)

Example 3 with ConfiguredServletMapping

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

the class ResourceInnerImpl method getRequestPath.

@Override
public String getRequestPath() {
    // Get the requestPath value from the wrapped resource.
    String wrappedRequestPath = wrappedResource.getRequestPath();
    FacesContext facesContext = FacesContext.getCurrentInstance();
    // append extension-mapped suffixes which have no meaning in a portlet environment.
    if (wrappedRequestPath != null) {
        if (wrappedRequestPath.contains(ResourceHandler.RESOURCE_IDENTIFIER)) {
            // If this resource request was initiated from a ResourceURL (not via the FacesServlet), then
            if (facesContext != null) {
                BridgeConfig bridgeConfig = RequestMapUtil.getBridgeConfig(facesContext);
                List<ConfiguredServletMapping> configuredFacesServletMappings = (List<ConfiguredServletMapping>) bridgeConfig.getAttributes().get(BridgeConfigAttributeMap.CONFIGURED_FACES_SERVLET_MAPPINGS);
                if (configuredFacesServletMappings != null) {
                    for (ConfiguredServletMapping configuredServletMapping : configuredFacesServletMappings) {
                        if (configuredServletMapping.isExtensionMapped()) {
                            String extension = configuredServletMapping.getExtension();
                            // Note: Both Mojarra and MyFaces construct a requestPath that looks something like
                            // "/javax.faces.resource/jsf.js.faces?ln=javax.faces" and so we look for the
                            // ".faces?" as an indicator that ".faces" needs to be removed from the requestPath.
                            String token = extension + "?";
                            int pos = wrappedRequestPath.indexOf(token);
                            // meaning in a portlet environment.
                            if (pos > 0) {
                                wrappedRequestPath = wrappedRequestPath.substring(0, pos) + wrappedRequestPath.substring(pos + extension.length());
                                logger.debug("Removed extension=[{0}] from requestPath=[{1}]", extension, wrappedRequestPath);
                                break;
                            } else // path, then
                            if (wrappedRequestPath.endsWith(extension)) {
                                if (extension.equals(EXTENSION_FACES) && wrappedRequestPath.endsWith(LIBRARY_NAME_JAVAX_FACES)) {
                                // Special case: Don't remove ".faces" if request path ends with "javax.faces"
                                // http://issues.liferay.com/browse/FACES-1202
                                } else {
                                    // Sometimes resources like the ICEfaces bridge.js file don't have a library
                                    // name (ln=) parameter and simply look like this:
                                    // /my-portlet/javax.faces.resource/bridge.js.faces
                                    wrappedRequestPath = wrappedRequestPath.substring(0, wrappedRequestPath.lastIndexOf(extension));
                                    logger.debug("Removed extension=[{0}] from requestPath=[{1}]", extension, wrappedRequestPath);
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
        // If the wrapped request path ends with "org.richfaces" then
        if (wrappedRequestPath.endsWith(ResourceRichFacesImpl.ORG_RICHFACES)) {
            // Check to see if the resource physically exists in the META-INF/resources/org.richfaces folder of the
            // RichFaces JAR. If it does, then this qualifies as a special case in which the
            // ResourceHandlerImpl#fixRichFacesImageURLs(FacesContext, String) method is unable to handle resources
            // such as "node_icon.gif" and the library name must be "org.richfaces.images" instead of
            // "org.richfaces".
            String resourcePath = "META-INF/resources/org.richfaces/" + getResourceName();
            URL resourceURL = getClass().getClassLoader().getResource(resourcePath);
            if (resourceURL != null) {
                wrappedRequestPath = wrappedRequestPath + ".images";
            }
        }
    }
    return wrappedRequestPath;
}
Also used : FacesContext(javax.faces.context.FacesContext) BridgeConfig(javax.portlet.faces.BridgeConfig) ArrayList(java.util.ArrayList) List(java.util.List) ConfiguredServletMapping(com.liferay.faces.util.config.ConfiguredServletMapping) URL(java.net.URL)

Example 4 with ConfiguredServletMapping

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

the class BridgePhaseResourceImpl method getFacesServletPathMapping.

protected ConfiguredServletMapping getFacesServletPathMapping(String resourceId) {
    ConfiguredServletMapping facesServletPathMapping = null;
    List<ConfiguredServletMapping> facesServletMappings = getConfiguredFacesServletMappings();
    for (ConfiguredServletMapping facesServletMapping : facesServletMappings) {
        if (facesServletMapping.isMatch(resourceId) && facesServletMapping.isPathMapped()) {
            facesServletPathMapping = facesServletMapping;
            break;
        }
    }
    return facesServletPathMapping;
}
Also used : ConfiguredServletMapping(com.liferay.faces.util.config.ConfiguredServletMapping)

Example 5 with ConfiguredServletMapping

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

the class BridgePhaseResourceImpl method execute.

@Override
public void execute() throws BridgeDefaultViewNotSpecifiedException, BridgeException {
    logger.debug(Logger.SEPARATOR);
    logger.debug("execute(ResourceRequest, ResourceResponse) portletName=[{0}]", portletName);
    try {
        init(resourceRequest, resourceResponse, Bridge.PortletPhase.RESOURCE_PHASE);
        // resource, then
        if (isJSF2ResourceRequest(facesContext)) {
            logger.debug("Detected JSF2 resource request");
            // Ask the Faces resource handler to copy the contents of the resource to the response.
            handleJSF2ResourceRequest(facesContext);
        } else if ((resourceRequest.getResourceID() != null) && !resourceRequest.getResourceID().equals("wsrp")) {
            logger.debug("Detected non-Faces resource");
            String resourceId = resourceRequest.getResourceID();
            String autoResourceDispatch = portletConfig.getInitParameter("javax.portlet.automaticResourceDispatching");
            if ((autoResourceDispatch != null) && autoResourceDispatch.equalsIgnoreCase("true")) {
                ExternalContext externalContext = facesContext.getExternalContext();
                ResourceValidator resourceValidator = ResourceValidatorFactory.getResourceValidatorInstance(externalContext);
                // resource.
                if (resourceValidator.containsBannedPath(resourceId)) {
                    // Simulate Liferay Portal's behavior for containers like Pluto
                    logger.warn("Invalid request for resource with banned path: resourceId=[{0}]", resourceId);
                    externalContext.setResponseStatus(HttpServletResponse.SC_OK);
                } else // serve the resource.
                if (resourceValidator.isBannedSequence(resourceId)) {
                    logger.warn("Invalid request for resource with banned sequence: resourceId=[{0}]", resourceId);
                    externalContext.setResponseStatus(HttpServletResponse.SC_NOT_FOUND);
                } else // Otherwise, if the resourceId targets a Facelet document, then do not serve the resource.
                if (resourceValidator.isFaceletDocument(facesContext, resourceId)) {
                    logger.warn("Invalid request for Facelet document: resourceId=[{0}]", resourceId);
                    externalContext.setResponseStatus(HttpServletResponse.SC_NOT_FOUND);
                } else // Otherwise,
                {
                    // Sanitize the resource path by removing special characters that indicate URL fragments, URL
                    // query-strings, etc.
                    String resourcePath = resourceId;
                    for (String urlSeparatorChar : URL_SEPARATOR_CHARS) {
                        int pos = resourcePath.indexOf(urlSeparatorChar);
                        if (pos > 0) {
                            resourcePath = resourcePath.substring(0, pos);
                        }
                    }
                    // has been enforced.
                    if (resourcePath.trim().length() == 0) {
                        final Product LIFERAY_PORTAL = ProductFactory.getProductInstance(externalContext, Product.Name.LIFERAY_PORTAL);
                        if (LIFERAY_PORTAL.isDetected()) {
                            logger.warn("Invalid request for resourceId=[] possibly due to Liferay Portal enforcing the portlet.resource.id.banned.paths.regexp property.");
                        } else {
                            logger.warn("Invalid request for resourceId=[].");
                            externalContext.setResponseStatus(HttpServletResponse.SC_NOT_FOUND);
                        }
                    } else // Otherwise,
                    {
                        // portlet, then do not serve the resource.
                        if (resourceValidator.isSelfReferencing(facesContext, resourcePath)) {
                            logger.warn("Invalid request for resource that is self-referencing: resourceId=[{0}]", resourceId);
                            externalContext.setResponseStatus(HttpServletResponse.SC_NOT_FOUND);
                        } else // Otherwise,
                        {
                            // If the resourceId maps to the FacesServlet, then do not serve the resource.
                            boolean mappedToFacesServlet = false;
                            ConfiguredServletMapping explicitFacesServletExtensionMapping = getExplicitFacesServletExtensionMapping(resourcePath);
                            if (explicitFacesServletExtensionMapping != null) {
                                logger.warn("Invalid request for resource that is EXPLICITLY extension-mapped to the FacesServlet: resourceId=[{0}] resourcePath=[{1}] servlet-mapping extension=[{2}]", resourceId, resourcePath, explicitFacesServletExtensionMapping.getExtension());
                                mappedToFacesServlet = true;
                                externalContext.setResponseStatus(HttpServletResponse.SC_NOT_FOUND);
                            } else {
                                ConfiguredServletMapping facesServletPathMapping = getFacesServletPathMapping(resourceId);
                                if (facesServletPathMapping != null) {
                                    logger.warn("Invalid request for resource that is path-mapped to the FacesServlet: resourceId=[{0}] resourcePath=[{1}] servlet-mapping url-pattern=[{2}]", resourceId, resourcePath, facesServletPathMapping.getUrlPattern());
                                    mappedToFacesServlet = true;
                                    externalContext.setResponseStatus(HttpServletResponse.SC_NOT_FOUND);
                                }
                            }
                            // Otherwise, attempt to serve the resource.
                            if (!mappedToFacesServlet) {
                                PortletRequestDispatcher portletRequestDispatcher = portletContext.getRequestDispatcher(resourceId);
                                if (portletRequestDispatcher != null) {
                                    portletRequestDispatcher.forward(resourceRequest, resourceResponse);
                                } else {
                                    logger.warn("Request for non-Faces resource=[{0}] but request dispatcher was null.", resourceId);
                                    externalContext.setResponseStatus(HttpServletResponse.SC_NOT_FOUND);
                                }
                            }
                        }
                    }
                }
            } else {
                logger.warn("Request for non-Faces resource=[{0}] but automatic dispatching is disabled.", resourceId);
            }
        } else // Otherwise, must be an Ajax (partial-submit) request. Though technically a postback type of request,
        // Ajax requests also utilize the portlet RESOURCE_PHASE. Therefore treat it like a postback, and
        // execute the entire Faces lifecycle: RESTORE_VIEW, APPLY_REQUEST_VALUES, PROCESS_VALIDATIONS,
        // UPDATE_MODEL, INVOKE_APPLICATION.
        {
            ExternalContext externalContext = facesContext.getExternalContext();
            if (logger.isDebugEnabled()) {
                String facesAjaxParameter = externalContext.getRequestParameterMap().get(Bridge.FACES_AJAX_PARAMETER);
                if (BooleanHelper.isTrueToken(facesAjaxParameter)) {
                    logger.debug("Detected Ajax ResourceRequest");
                } else {
                    logger.debug("Detected Non-Ajax ResourceRequest");
                }
            }
            String viewId = getFacesViewId(externalContext);
            logger.debug("Running Faces lifecycle for viewId=[{0}]", viewId);
            // Attach the JSF 2.2 client window to the JSF lifecycle so that Faces Flows can be utilized.
            attachClientWindowToLifecycle(facesContext, facesLifecycle);
            // Execute the JSF lifecycle.
            facesLifecycle.execute(facesContext);
            // Also execute the RENDER_RESPONSE phase of the Faces lifecycle, which will ultimately return a
            // DOM-update back to the jsf.js Javascript code that issued the XmlHttpRequest in the first place.
            facesLifecycle.render(facesContext);
            // "javax.portlet.faces.BRIDGE_REQUEST_SCOPE_AJAX_ENABLED" configuration parameter, then
            if (bridgeRequestScope != null) {
                // PROPOSED-FOR-BRIDGE3-API: https://issues.apache.org/jira/browse/PORTLETBRIDGE-202
                bridgeRequestScope.setPortletMode(resourceRequest.getPortletMode());
                // TCK: nonFacesResourceTest
                // TCK: resourceAttrRetainedAfterRedisplayPPRTest -- Preserve the non-excluded request
                // attributes in the BridgeRequestScope so that they can be restored in subsequent render requests.
                bridgeRequestScope.saveState(facesContext);
                maintainBridgeRequestScope(resourceRequest, resourceResponse, BridgeRequestScope.Transport.PORTLET_SESSION_ATTRIBUTE);
            }
            // Spec 6.6 (Namespacing)
            indicateNamespacingToConsumers(facesContext.getViewRoot(), resourceResponse);
        }
    } catch (Throwable t) {
        throw new BridgeException(t);
    } finally {
        cleanup(resourceRequest);
    }
    logger.debug(Logger.SEPARATOR);
}
Also used : PortletRequestDispatcher(javax.portlet.PortletRequestDispatcher) BridgeException(javax.portlet.faces.BridgeException) ResourceValidator(com.liferay.faces.util.application.ResourceValidator) ExternalContext(javax.faces.context.ExternalContext) Product(com.liferay.faces.util.product.Product) ConfiguredServletMapping(com.liferay.faces.util.config.ConfiguredServletMapping)

Aggregations

ConfiguredServletMapping (com.liferay.faces.util.config.ConfiguredServletMapping)7 ApplicationConfig (com.liferay.faces.util.config.ApplicationConfig)3 ExternalContext (javax.faces.context.ExternalContext)3 ConfiguredServlet (com.liferay.faces.util.config.ConfiguredServlet)2 WebConfig (com.liferay.faces.util.config.WebConfig)2 Product (com.liferay.faces.util.product.Product)2 HashSet (java.util.HashSet)2 ResourceValidator (com.liferay.faces.util.application.ResourceValidator)1 FacesConfig (com.liferay.faces.util.config.FacesConfig)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 FacesContext (javax.faces.context.FacesContext)1 PortletRequestDispatcher (javax.portlet.PortletRequestDispatcher)1 BridgeConfig (javax.portlet.faces.BridgeConfig)1 BridgeException (javax.portlet.faces.BridgeException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1