Search in sources :

Example 1 with ResourceHandler

use of jakarta.faces.application.ResourceHandler in project myfaces by apache.

the class RendererUtils method getIconSrc.

/**
 * Checks for name/library attributes on component and if they are avaliable,
 * creates {@link Resource} and returns it's path suitable for rendering.
 * If component doesn't have name/library gets value for attribute named <code>attributeName</code>
 * returns it processed with {@link #toResourceUri(jakarta.faces.context.FacesContext, java.lang.Object)}
 *
 * @param facesContext a {@link FacesContext}
 * @param component a {@link UIComponent}
 * @param attributeName name of attribute that represents "image", "icon", "source", ...
 */
public static String getIconSrc(final FacesContext facesContext, final UIComponent component, final String attributeName) {
    // JSF 2.0: if "name" attribute is available, treat as a resource reference.
    final Map<String, Object> attributes = component.getAttributes();
    final String resourceName = (String) attributes.get(ComponentAttrs.NAME_ATTR);
    if (resourceName != null && (resourceName.length() > 0)) {
        final ResourceHandler resourceHandler = facesContext.getApplication().getResourceHandler();
        final Resource resource;
        final String libraryName = (String) component.getAttributes().get(ComponentAttrs.LIBRARY_ATTR);
        if ((libraryName != null) && (libraryName.length() > 0)) {
            resource = resourceHandler.createResource(resourceName, libraryName);
        } else {
            resource = resourceHandler.createResource(resourceName);
        }
        if (resource == null) {
            // show a message
            if (facesContext.isProjectStage(ProjectStage.Development)) {
                String summary = "Unable to find resource: " + resourceName;
                if (libraryName != null) {
                    summary = summary + " from library: " + libraryName;
                }
                facesContext.addMessage(component.getClientId(facesContext), new FacesMessage(FacesMessage.SEVERITY_WARN, summary, summary));
            }
            return RES_NOT_FOUND;
        } else {
            return resource.getRequestPath();
        }
    } else {
        String value = (String) component.getAttributes().get(attributeName);
        return toResourceUri(facesContext, value);
    }
}
Also used : Resource(jakarta.faces.application.Resource) ResourceHandler(jakarta.faces.application.ResourceHandler) FacesMessage(jakarta.faces.application.FacesMessage)

Example 2 with ResourceHandler

use of jakarta.faces.application.ResourceHandler in project mojarra by eclipse-ee4j.

the class ResourceELResolver method getValue.

// ------------------------------------------------- Methods from ELResolver
/**
 * If base and property are not <code>null</code> and base is an instance of {@link ResourceHandler}, perform the
 * following:
 * <ul>
 * <li>If <code>property</code> doesn't contain <code>:</code> treat <code>property</code> as the resource name and pass
 * <code>property</code> to {@link ResourceHandler#createResource(String)}</li>
 * <li>If <code>property</code> contains a single <code>:</code> treat the content before the <code>:</code> as the
 * library name, and the content after the <code>:</code> to be the resource name and pass both to
 * {@link jakarta.faces.application.ResourceHandler#createResource(String, String)}</li>
 * <li>If <code>property</code> contains more than one <code>:</code> then throw a <code>ELException</code></li>
 * <li>If one of the above steps resulted in the creation of a {@link Resource} instance, call
 * <code>ELContext.setPropertyResolved(true)</code> and return the result of
 * {@link jakarta.faces.application.Resource#getRequestPath()}</li>
 * </ul>
 *
 * @see ELResolver#getValue(jakarta.el.ELContext, Object, Object)
 */
@Override
public Object getValue(ELContext context, Object base, Object property) {
    if (base == null && property == null) {
        // ?????
        String message = MessageUtils.getExceptionMessageString(MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "base and property");
        throw new PropertyNotFoundException(message);
    }
    Object ret = null;
    if (base instanceof ResourceHandler) {
        ResourceHandler handler = (ResourceHandler) base;
        String prop = property.toString();
        Resource res;
        if (!prop.contains(":")) {
            res = handler.createResource(prop);
        } else {
            if (!isPropertyValid(prop)) {
                // RELEASE_PENDING i18n
                throw new ELException("Invalid resource format.  Property " + prop + " contains more than one colon (:)");
            }
            Map<String, Object> appMap = FacesContext.getCurrentInstance().getExternalContext().getApplicationMap();
            String[] parts = Util.split(appMap, prop, ":");
            // be supported.
            if (null != parts[0] && parts[0].equals("this")) {
                FacesContext facesContext = FacesContext.getCurrentInstance();
                UIComponent currentComponent = UIComponent.getCurrentCompositeComponent(facesContext);
                Resource componentResource = (Resource) currentComponent.getAttributes().get(Resource.COMPONENT_RESOURCE_KEY);
                if (null != componentResource) {
                    String libName = null;
                    if (null != (libName = componentResource.getLibraryName())) {
                        parts[0] = libName;
                    }
                }
            }
            res = handler.createResource(parts[1], parts[0]);
        }
        if (res != null) {
            FacesContext facesContext = (FacesContext) context.getContext(FacesContext.class);
            ExternalContext extContext = facesContext.getExternalContext();
            ret = extContext.encodeResourceURL(res.getRequestPath());
        }
        context.setPropertyResolved(true);
    }
    return ret;
}
Also used : FacesContext(jakarta.faces.context.FacesContext) PropertyNotFoundException(jakarta.el.PropertyNotFoundException) ExternalContext(jakarta.faces.context.ExternalContext) Resource(jakarta.faces.application.Resource) UIComponent(jakarta.faces.component.UIComponent) ResourceHandler(jakarta.faces.application.ResourceHandler) ELException(jakarta.el.ELException)

Example 3 with ResourceHandler

use of jakarta.faces.application.ResourceHandler in project mojarra by eclipse-ee4j.

the class FacesServlet method executeLifecyle.

private void executeLifecyle(FacesContext context) throws IOException, ServletException {
    try {
        ResourceHandler handler = context.getApplication().getResourceHandler();
        if (handler.isResourceRequest(context)) {
            handler.handleResourceRequest(context);
        } else {
            lifecycle.attachWindow(context);
            lifecycle.execute(context);
            lifecycle.render(context);
        }
    } catch (FacesException e) {
        Throwable t = e.getCause();
        if (t == null) {
            throw new ServletException(e.getMessage(), e);
        }
        if (t instanceof ServletException) {
            throw (ServletException) t;
        }
        if (t instanceof IOException) {
            throw (IOException) t;
        }
        throw new ServletException(t.getMessage(), t);
    }
}
Also used : ServletException(jakarta.servlet.ServletException) ResourceHandler(jakarta.faces.application.ResourceHandler) IOException(java.io.IOException) FacesException(jakarta.faces.FacesException)

Example 4 with ResourceHandler

use of jakarta.faces.application.ResourceHandler in project mojarra by eclipse-ee4j.

the class RenderKitUtils method installFacesJsIfNecessary.

/**
 * <p>
 * Only install the Faces script resource if it doesn't exist. The resource component will be installed with the target
 * "head".
 *
 * @param context the <code>FacesContext</code> for the current request
 */
public static void installFacesJsIfNecessary(FacesContext context) {
    if (isFacesJsInstalled(context)) {
        return;
    }
    ResourceHandler resourceHandler = context.getApplication().getResourceHandler();
    if (resourceHandler.isResourceRendered(context, FACES_SCRIPT_RESOURCE_NAME, FACES_SCRIPT_LIBRARY_NAME)) {
        return;
    }
    context.getViewRoot().addComponentResource(context, createFacesJs(), "head");
}
Also used : ResourceHandler(jakarta.faces.application.ResourceHandler)

Example 5 with ResourceHandler

use of jakarta.faces.application.ResourceHandler in project mojarra by eclipse-ee4j.

the class RenderKitUtils method renderFacesJsIfNecessary.

/**
 * <p>
 * Renders the Javascript necessary to add and remove request parameters to the current form.
 * </p>
 *
 * @param context the <code>FacesContext</code> for the current request
 * @throws java.io.IOException if an error occurs writing to the response
 */
public static void renderFacesJsIfNecessary(FacesContext context) throws IOException {
    if (isFacesJsInstalled(context)) {
        return;
    }
    ResourceHandler resourceHandler = context.getApplication().getResourceHandler();
    if (resourceHandler.isResourceRendered(context, FACES_SCRIPT_RESOURCE_NAME, FACES_SCRIPT_LIBRARY_NAME)) {
        return;
    }
    // Since we've now determined that it's not in the page, we need to manually render it.
    createFacesJs().encodeAll(context);
    resourceHandler.markResourceRendered(context, FACES_SCRIPT_RESOURCE_NAME, FACES_SCRIPT_LIBRARY_NAME);
}
Also used : ResourceHandler(jakarta.faces.application.ResourceHandler)

Aggregations

ResourceHandler (jakarta.faces.application.ResourceHandler)53 PrintWriter (java.io.PrintWriter)34 Resource (jakarta.faces.application.Resource)27 IOException (java.io.IOException)14 ServletException (jakarta.servlet.ServletException)13 FacesContext (jakarta.faces.context.FacesContext)5 TCKResourceHandler (com.sun.ts.tests.jsf.common.resourcehandler.TCKResourceHandler)4 Application (jakarta.faces.application.Application)4 FacesException (jakarta.faces.FacesException)3 FacesMessage (jakarta.faces.application.FacesMessage)3 UIComponent (jakarta.faces.component.UIComponent)3 WebConfiguration (com.sun.faces.config.WebConfiguration)2 ELException (jakarta.el.ELException)2 PostRestoreStateEvent (jakarta.faces.event.PostRestoreStateEvent)2 Map (java.util.Map)2 Util.getLocaleFromString (com.sun.faces.util.Util.getLocaleFromString)1 PropertyNotFoundException (jakarta.el.PropertyNotFoundException)1 ApplicationFactory (jakarta.faces.application.ApplicationFactory)1 ApplicationWrapper (jakarta.faces.application.ApplicationWrapper)1 ConfigurableNavigationHandler (jakarta.faces.application.ConfigurableNavigationHandler)1