- All Implemented Interfaces:
ComponentSystemEventListener,FacesListener,FacesWrapper<Renderer>,EventListener
The CorsAwareResourceRenderer is intended as an extension to the standard script and stylesheet resource renderer
in order to add the crossorigin and integrity attributes as a pass-through attribute.
By default, the crossorigin attribute will always be set to anonymous and the
integrity attribute is only set when the ResourceHandler.createResource(String) returns an
instance of CDNResource. It will then be set with a base64 encoded sha384 hash.
This includes declarative resources created by <h:outputScript> and <h:outputStylesheet>,
annotated resources created by ResourceDependency, combined resources created by CombinedResourceHandler,
deferred scripts created by DeferredScript and critical stylesheets created by CriticalStylesheet.
Basically any resource which will be served by ResourceHandler.createResource(String).
Installation
You do not need to explicitly register this renderer in your faces-config.xml. It's already automatically
registered.
Configuration
Currently only the following context parameter is available:
"org.omnifaces.DEFAULT_CROSSORIGIN".
This sets the desired value of crossorigin attribute of combined script resources. Supported values are
specified in MDN. An empty
string is also allowed, it will then completely skip the task of the current renderer. The default value when the
context parameter is not set is anonymous (i.e. no cookies are transferred at all).
Usage
Eveything is automatic. In case you wish to override the default/configured outcome of one of the attributes on a specific resource component, then simply explicitly set it as a passthrough attribute yourself. For example,
<... xmlns:h="jakarta.faces.html" xmlns:a="jakarta.faces.passthrough"> <h:outputScript name="..." a:crossorigin="use-credentials" />
- Since:
- 5.0
- Author:
- Bauke Scholtz
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe default value of the 'crossorigin' attribute.static final StringThe context parameter name to specify the value of the 'crossorigin' attribute for all resources.Fields inherited from class jakarta.faces.render.Renderer
PASSTHROUGH_RENDERER_LOCALNAME_KEY -
Constructor Summary
ConstructorsConstructorDescriptionDo not use this constructor.CorsAwareResourceRenderer(Renderer<?> wrapped) Creates a new instance of this CORS resource renderer which wraps the given resource renderer. -
Method Summary
Modifier and TypeMethodDescriptionvoidencodeBegin(FacesContext context, UIComponent component) When the associated resource has a non-nullname and no explicitly set passthrough attributes forcrossoriginnorintegrity, then set these as new passthrough attributes so that the default script/stylesheet resource renderer will write them.static StringgetCrossorigin(FacesContext context) Returns the configured crossorigin.static StringgetIntegrityIfNecessary(FacesContext context, Resource resource) Returns the integrity of the given resource if necessary.static booleanisNeedsIntegrity(FacesContext context) Returns whether the integrity is needed.voidprocessEvent(ComponentSystemEvent event) If the wrapped script/stylesheet resource renderer is an instance ofComponentSystemEventListenerthen delegate the component system event to it.Methods inherited from class jakarta.faces.render.RendererWrapper
convertClientId, decode, encodeChildren, encodeEnd, getConvertedValue, getRendersChildren, getWrapped
-
Field Details
-
DEFAULT_CROSSORIGIN
The default value of the 'crossorigin' attribute.- See Also:
-
PARAM_NAME_CROSSORIGIN
The context parameter name to specify the value of the 'crossorigin' attribute for all resources. The value defaults to "anonymous".- See Also:
-
-
Constructor Details
-
CorsAwareResourceRenderer
public CorsAwareResourceRenderer()Do not use this constructor. It's merely there forComponentSystemEventListener. -
CorsAwareResourceRenderer
Creates a new instance of this CORS resource renderer which wraps the given resource renderer.- Parameters:
wrapped- The resource renderer to be wrapped.
-
-
Method Details
-
processEvent
If the wrapped script/stylesheet resource renderer is an instance ofComponentSystemEventListenerthen delegate the component system event to it. Generally these will further relocate the component resource depending on theirtargetattribute.- Specified by:
processEventin interfaceComponentSystemEventListener- Throws:
AbortProcessingException
-
encodeBegin
When the associated resource has a non-nullname and no explicitly set passthrough attributes forcrossoriginnorintegrity, then set these as new passthrough attributes so that the default script/stylesheet resource renderer will write them.- Overrides:
encodeBeginin classRendererWrapper- Throws:
IOException
-
getCrossorigin
Returns the configured crossorigin. Defaults to "anonymous".- Parameters:
context- The involved faces context.- Returns:
- The configured crossorigin.
-
isNeedsIntegrity
Returns whether the integrity is needed. Defaults totrue.- Parameters:
context- The involved faces context.- Returns:
- Whether the integrity is needed.
-
getIntegrityIfNecessary
Returns the integrity of the given resource if necessary. It will only return a base64 encoded sha384 hash when the given resource is an instance ofCDNResourceand thegetCrossorigin(FacesContext)equals to "anonymous".- Parameters:
context- The involved faces context.resource- The resource to get integrity for.- Returns:
- The integrity of the given resource if necessary.
-