Top Description Fields Constructors Methods
com.sun.faces.application.resource

public Class ResourceHandlerImpl

extends ResourceHandler
Class Inheritance
Imports
com.sun.faces.application.ApplicationAssociate, com.sun.faces.config.WebConfiguration, com.sun.faces.util.FacesLogger, .RequestStateManager, .Util, java.io.IOException, .InputStream, java.nio.ByteBuffer, java.nio.channels.Channels, .ReadableByteChannel, .WritableByteChannel, java.util.ArrayList, .List, .Map, java.util.logging.Level, .Logger, java.util.regex.Pattern, java.util.stream.Stream, javax.faces.application.Resource, .ResourceHandler, .ResourceVisitOption, javax.faces.context.ExternalContext, .FacesContext

This is the default implementation of ResourceHandler.

Field Summary

Modifier and TypeField and Description
private long
pack-priv List<Pattern>
private static final Logger
pack-priv ResourceManager
private long
private WebConfiguration
Inherited from javax.faces.application.ResourceHandler:
JSF_SCRIPT_LIBRARY_NAMEJSF_SCRIPT_RESOURCE_NAMELOCALE_PREFIXRESOURCE_CONTRACT_XMLRESOURCE_EXCLUDES_DEFAULT_VALUERESOURCE_EXCLUDES_PARAM_NAMERESOURCE_IDENTIFIERWEBAPP_CONTRACTS_DIRECTORY_PARAM_NAMEWEBAPP_RESOURCES_DIRECTORY_PARAM_NAME

Constructor Summary

AccessConstructor and Description
public
ResourceHandlerImpl()

Creates a new instance of ResourceHandlerImpl

Method Summary

Modifier and TypeMethod and Description
private ByteBuffer
public Resource
createResource(String
the name of the resource.
resourceName
)

Implements abstract javax.faces.application.ResourceHandler.createResource.

Create an instance of ViewResource given the argument resourceName.

public Resource
createResource(String
the name of the resource.
resourceName
,
String
the name of the library (or contract) in which this resource resides, may be null. If there is a conflict between the name of a resource library and a resource library contract, the resource library takes precedence. May not include relative paths, such as "../".
libraryName
)

Implements abstract javax.faces.application.ResourceHandler.createResource.

Create an instance of Resource with a resourceName given by the value of the argument resourceName that is a member of the library named by the argument libraryName.

public Resource
createResource(String
the name of the resource.
resourceName
,
String
the name of the library in which this resource resides, may be null. May not include relative paths, such as "../".
libraryName
,
String
the mime content that this Resource instance will return from Resource#getContentType. If the value is null, The content-type of the resource is derived by passing the resourceName to javax.faces.context.ExternalContext#getMimeType
contentType
)

Implements abstract javax.faces.application.ResourceHandler.createResource.

Create an instance of Resource with a resourceName given by the value of the argument resourceName that is a member of the library named by the argument libraryName that claims to have the content-type given by the argument content-type.

public Resource
createResourceFromId(String
the resource identifier of the resource.
resourceId
)

Overrides javax.faces.application.ResourceHandler.createResourceFromId.

Create an instance of Resource given the argument resourceId.

public Resource
createViewResource(FacesContext
the FacesContext for this request.
facesContext
,
String
the name of the resource to be interpreted as a view by the javax.faces.view.ViewDeclarationLanguage.
resourceName
)

Overrides javax.faces.application.ResourceHandler.createViewResource.

Create an instance of Resource given the argument resourceName, which may contain "/" characters.

private String

Returns:

the content type for this resource
getContentType
(FacesContext ctx, String
the resource of interest. The resourceName in question may consist of zero or more path elements such that resourceName could be something like path1/path2/resource.jpg or resource.jpg
resourceName
)

pack-priv long

Returns:

the time when the ResourceHandler was instantiated (in milliseconds)
getCreationTime
()

This method is leveraged by ResourceImpl to detemine if a resource has been upated.

public String
getRendererTypeForResourceName(String
the resource name.
resourceName
)

Implements abstract javax.faces.application.ResourceHandler.getRendererTypeForResourceName.

Return the renderer-type for a javax.faces.render.Renderer that is capable of rendering this resource.

public Stream<String>
getViewResources(FacesContext
The FacesContext for this request.
facesContext
,
String
The initial path from which to start looking for view resources
path
,
ResourceVisitOption...
The options to influence the traversal. See ResourceVisitOption for details on those.
options
)

Overrides javax.faces.application.ResourceHandler.getViewResources.

Return a Stream possibly lazily populated by walking the resource tree rooted at a given initial path.

public Stream<String>
getViewResources(FacesContext
The FacesContext for this request.
facesContext
,
String
The initial path from which to start looking for view resources
path
,
int
The absolute maximum depth of nested directories to visit counted from the root (/).
maxDepth
,
ResourceVisitOption...
The options to influence the traversal. See ResourceVisitOption for details on those.
options
)

Overrides javax.faces.application.ResourceHandler.getViewResources.

Return a Stream possibly lazily populated by walking the resource tree rooted at a given initial path.

pack-priv WebConfiguration

Returns:

the WebConfiguration for this application
getWebConfig
()

Utility method leveraged by ResourceImpl to reduce the cost of looking up the WebConfiguration per-instance.

private void
public void
handleResourceRequest(FacesContext context)

Implements abstract javax.faces.application.ResourceHandler.handleResourceRequest.

This method specifies the contract for satisfying resource requests.

private void
initExclusions(Map<String, Object> appMap)

Initialize the exclusions for this application.

private void
private boolean

Returns:

true if the request matces an excluded resource, otherwise false
isExcluded
(String
the normalized request path as returned by normalizeResourceRequest(javax.faces.context.FacesContext)
resourceId
)

public boolean
isResourceRequest(FacesContext context)

Implements abstract javax.faces.application.ResourceHandler.isResourceRequest.

Return true if the current request is a resource request.

public boolean
libraryExists(String
the library name.
libraryName
)

Implements abstract javax.faces.application.ResourceHandler.libraryExists.

Return true if the resource library named by the argument libraryName can be found.

private boolean
private void
logMissingResource(FacesContext
the FacesContext for the current request
ctx
,
String
the resource name
resourceName
,
String
the resource library
libraryName
,
Throwable
the exception caught when attempting to find the resource
t
)

Log a message indicating a particular resource (reference by name and/or library) could not be found.

private void
logMissingResource(FacesContext
the FacesContext for the current request
ctx
,
String
the resource name
resourceId
,
Throwable
the exception caught when attempting to find the resource
t
)

Log a message indicating a particular resource (reference by name and/or library) could not be found.

private String

Returns:

the request path without JSF invocation information
normalizeResourceRequest
(FacesContext
the FacesContext for the current request
context
)

Normalize the request path to exclude JSF invocation information.

private void
private void
send404(FacesContext ctx, String resourceName, String libraryName, boolean logMessage)

private void
send404(FacesContext ctx, String resourceName, String libraryName, Throwable t, boolean logMessage)

pack-priv void
setCreationTime(long
the time in milliseconds
creationTime
)

This method is here soley for the purpose of unit testing and will not be invoked during normal runtime.

Inherited from javax.faces.application.ResourceHandler:
isResourceRenderedisResourceURLmarkResourceRendered

Field Detail

creationTimeback to summary
private long creationTime
excludePatternsback to summary
pack-priv List<Pattern> excludePatterns
LOGGERback to summary
private static final Logger LOGGER
managerback to summary
pack-priv ResourceManager manager
maxAgeback to summary
private long maxAge
webconfigback to summary
private WebConfiguration webconfig

Constructor Detail

ResourceHandlerImplback to summary
public ResourceHandlerImpl()

Creates a new instance of ResourceHandlerImpl

Method Detail

allocateByteBufferback to summary
private ByteBuffer allocateByteBuffer()
createResourceback to summary
public Resource createResource(String resourceName)

Implements abstract javax.faces.application.ResourceHandler.createResource.

Doc from javax.faces.application.ResourceHandler.createResource.

Create an instance of ViewResource given the argument resourceName. The content-type of the resource is derived by passing the resourceName to javax.faces.context.ExternalContext#getMimeType

The algorithm specified in section JSF.2.6.1.4 of the spec prose document linked in the overview summary must be executed to create the Resource. New requirements were introduced in version 2.2 of the specification. For historical reasons, this method operate correctly when the argument resourceName is of the form libraryName/resourceName, even when resourceName contains '/' characters.

Parameters
resourceName:String

the name of the resource.

Returns:Resource

a newly created Resource instance, suitable for use in encoding or decoding the named resource.

Annotations
@Override
See Also
ResourceHandler#createResource(String)
createResourceback to summary
public Resource createResource(String resourceName, String libraryName)

Implements abstract javax.faces.application.ResourceHandler.createResource.

Doc from javax.faces.application.ResourceHandler.createResource.

Create an instance of Resource with a resourceName given by the value of the argument resourceName that is a member of the library named by the argument libraryName. The content-type of the resource is derived by passing the resourceName to javax.faces.context.ExternalContext#getMimeType.

The algorithm specified in section JSF.2.6.1.4 of the spec prose document linked in the overview summary must be executed to create the Resource. New requirements were introduced in version 2.2 of the specification.

Parameters
resourceName:String

the name of the resource.

libraryName:String

the name of the library (or contract) in which this resource resides, may be null. If there is a conflict between the name of a resource library and a resource library contract, the resource library takes precedence. May not include relative paths, such as "../".

Returns:Resource

a newly created Resource instance, suitable for use in encoding or decoding the named resource.

Annotations
@Override
See Also
ResourceHandler#createResource(String, String)
createResourceback to summary
public Resource createResource(String resourceName, String libraryName, String contentType)

Implements abstract javax.faces.application.ResourceHandler.createResource.

Doc from javax.faces.application.ResourceHandler.createResource.

Create an instance of Resource with a resourceName given by the value of the argument resourceName that is a member of the library named by the argument libraryName that claims to have the content-type given by the argument content-type.

The algorithm specified in section JSF.2.6.1.4 of the spec prose document linked in the overview summary must be executed to create the Resource. New requirements were introduced in version 2.2 of the specification.

Parameters
resourceName:String

the name of the resource.

libraryName:String

the name of the library in which this resource resides, may be null. May not include relative paths, such as "../".

contentType:String

the mime content that this Resource instance will return from Resource#getContentType. If the value is null, The content-type of the resource is derived by passing the resourceName to javax.faces.context.ExternalContext#getMimeType

Returns:Resource

a newly created Resource instance, suitable for use in encoding or decoding the named resource.

Annotations
@Override
See Also
ResourceHandler#createResource(String, String, String)
createResourceFromIdback to summary
public Resource createResourceFromId(String resourceId)

Overrides javax.faces.application.ResourceHandler.createResourceFromId.

Doc from javax.faces.application.ResourceHandler.createResourceFromId.

Create an instance of Resource given the argument resourceId. The content-type of the resource is derived by passing the resourceName to javax.faces.context.ExternalContext#getMimeType

The resource must be identified according to the specification in JSF.2.6.1.3 of the spec prose document linked in the overview summary. New requirements were introduced in version 2.2 of the specification.

Parameters
resourceId:String

the resource identifier of the resource.

Returns:Resource

a newly created Resource instance, suitable for use in encoding or decoding the named resource.

Annotations
@Override
See Also
ResourceHandler#createResourceFromId(String)
createViewResourceback to summary
public Resource createViewResource(FacesContext facesContext, String resourceName)

Overrides javax.faces.application.ResourceHandler.createViewResource.

Doc from javax.faces.application.ResourceHandler.createViewResource.

Create an instance of Resource given the argument resourceName, which may contain "/" characters. The javax.faces.view.ViewDeclarationLanguage calls this method when it needs to load a view from a persistent store, such as a filesystem. This method is functionality equivalent to createResource(java.lang.String), but all callsites that need to load VDL views must use this method so that classes that want to decorate the ResourceHandler in order to only affect the loading of views may do so without affecting the processing of other kinds of resources, such as scripts and stylesheets. A javax.faces.context.FacesContext must be present before calling this method. To preserve compatibility with prior revisions of the specification, a default implementation must be provided that calls createResource(java.lang.String).

The default implementation must look for the resource in the following places, in this order.

  • Considering resource library contracts (at the locations specified in the spec prose document section Resource Library Contracts in the Request Processing Lifecycle chapter).

  • Considering the web app root.

  • Considering faces flows (at the locations specified in the spec prose document section Faces Flows in the Using JSF in Web Applications chapter).

Call FacesContext#getResourceLibraryContracts. If the result is non-null and not empty, for each value in the list, treat the value as the name of a resource library contract. If the argument resoureName exists as a resource in the resource library contract, return it. Otherwise, return the resource (not in the resource library contract), if found. Otherwise, return null.

Parameters
facesContext:FacesContext

the FacesContext for this request.

resourceName:String

the name of the resource to be interpreted as a view by the javax.faces.view.ViewDeclarationLanguage.

Returns:Resource

a newly created ViewResource instance, suitable for use by the javax.faces.view.ViewDeclarationLanguage.

Annotations
@Override
getContentTypeback to summary
private String getContentType(FacesContext ctx, String resourceName)
Parameters
resourceName:String

the resource of interest. The resourceName in question may consist of zero or more path elements such that resourceName could be something like path1/path2/resource.jpg or resource.jpg

Returns:String

the content type for this resource

getCreationTimeback to summary
pack-priv long getCreationTime()

This method is leveraged by ResourceImpl to detemine if a resource has been upated. In short, a resource has been updated if the timestamp is newer than the timestamp of the ResourceHandler creation time.

Returns:long

the time when the ResourceHandler was instantiated (in milliseconds)

Annotations
@SuppressWarnings:UnusedDeclaration
getRendererTypeForResourceNameback to summary
public String getRendererTypeForResourceName(String resourceName)

Implements abstract javax.faces.application.ResourceHandler.getRendererTypeForResourceName.

Doc from javax.faces.application.ResourceHandler.getRendererTypeForResourceName.

Return the renderer-type for a javax.faces.render.Renderer that is capable of rendering this resource. The default implementation must return values according to the following table. If no renderer-type can be determined, null must be returned.

resource name to renderer-type mapping
example resource name renderer-type
mycomponent.js javax.faces.resource.Script
mystyle.css javax.faces.resource.Stylesheet
Parameters
resourceName:String

the resource name.

Returns:String

the renderer type.

Annotations
@Override
getViewResourcesback to summary
public Stream<String> getViewResources(FacesContext facesContext, String path, ResourceVisitOption... options)

Overrides javax.faces.application.ResourceHandler.getViewResources.

Doc from javax.faces.application.ResourceHandler.getViewResources.

Return a Stream possibly lazily populated by walking the resource tree rooted at a given initial path. The resource tree is traversed breadth-first, the elements in the stream are view resource names that would yield a ViewResource when passed into ResourceHandler#createViewResource as the resourceName parameter.

This method works as if invoking it were equivalent to evaluating the expression:

getViewResources(facesContext, start, Integer.MAX_VALUE, options)
Put differently, it visits all levels of the resource tree.
Parameters
facesContext:FacesContext

The FacesContext for this request.

path:String

The initial path from which to start looking for view resources

options:ResourceVisitOption[]

The options to influence the traversal. See ResourceVisitOption for details on those.

Returns:Stream<String>

the Stream of view resource names

Annotations
@Override
See Also
ResourceHandler#getViewResources(FacesContext, String, ResourceVisitOption...)
getViewResourcesback to summary
public Stream<String> getViewResources(FacesContext facesContext, String path, int maxDepth, ResourceVisitOption... options)

Overrides javax.faces.application.ResourceHandler.getViewResources.

Doc from javax.faces.application.ResourceHandler.getViewResources.

Return a Stream possibly lazily populated by walking the resource tree rooted at a given initial path. The resource tree is traversed breadth-first, the elements in the stream are view resource names that would yield a ViewResource when passed into ResourceHandler#createViewResource as the resourceName parameter.

The maxDepth parameter is the maximum depth of directory levels to visit beyond the initial path, which is always visited. The value is relative to the root (/), not to the given initial path. E.g. given maxDepth = 3 and initial path /foo/, visiting will proceed up to /foo/bar/, where / counts as depth 1, /foo/ as depth 2 and /foo/bar/ as depth 3. A value lower or equal to the depth of the initial path means that only the initial path is visited. A value of MAX_VALUE may be used to indicate that all levels should be visited.

Parameters
facesContext:FacesContext

The FacesContext for this request.

path:String

The initial path from which to start looking for view resources

maxDepth:int

The absolute maximum depth of nested directories to visit counted from the root (/).

options:ResourceVisitOption[]

The options to influence the traversal. See ResourceVisitOption for details on those.

Returns:Stream<String>

the Stream of view resource names

Annotations
@Override
See Also
ResourceHandler#getViewResources(FacesContext, String, int, ResourceVisitOption...)
getWebConfigback to summary
pack-priv WebConfiguration getWebConfig()

Utility method leveraged by ResourceImpl to reduce the cost of looking up the WebConfiguration per-instance.

Returns:WebConfiguration

the WebConfiguration for this application

Annotations
@SuppressWarnings:UnusedDeclaration
handleHeadersback to summary
private void handleHeaders(FacesContext ctx, Resource resource)
handleResourceRequestback to summary
public void handleResourceRequest(FacesContext context) throws IOException

Implements abstract javax.faces.application.ResourceHandler.handleResourceRequest.

Doc from javax.faces.application.ResourceHandler.handleResourceRequest.

This method specifies the contract for satisfying resource requests. This method is called from javax.faces.webapp.FacesServlet#service after that method determines the current request is a resource request by calling isResourceRequest. Thus, handleResourceRequest may assume that the current request is a resource request.

The default implementation must implement an algorithm semantically identical to the following algorithm.

For discussion, in all cases when a status code is to be set, this spec talks only using the Servlet API, but it is understood that in a portlet environment the appropriate equivalent API must be used.
  • If the resourceIdentifier ends with any of the extensions listed in the value of the RESOURCE_EXCLUDES_PARAM_NAME init parameter, HttpServletRequest.SC_NOT_FOUND must be passed to HttpServletResponse.setStatus(), then handleResourceRequest must immediately return.

  • Extract the resourceName from the resourceIdentifier by taking the substring of resourceIdentifier that starts at RESOURCE_IDENTIFIER.length() + 1 and goes to the end of resourceIdentifier. If no resourceName can be extracted, HttpServletRequest.SC_NOT_FOUND must be passed to HttpServletResponse.setStatus(), then handleResourceRequest must immediately return.

  • Extract the libraryName from the request by looking in the request parameter map for an entry under the key "ln", without the quotes. If found, use its value as the libraryName.

  • If resourceName and libraryName are present, call createResource(String, String) to create the Resource. If only resourceName is present, call createResource(String) to create the Resource. If the Resource cannot be successfully created, HttpServletRequest.SC_NOT_FOUND must be passed to HttpServletResponse.setStatus(), then handleResourceRequest must immediately return.

  • Call Resource#userAgentNeedsUpdate. If this method returns false, HttpServletRequest.SC_NOT_MODIFIED must be passed to HttpServletResponse.setStatus(), then handleResourceRequest must immediately return.

  • Pass the result of Resource#getContentType to HttpServletResponse.setContentType.

  • Call Resource#getResponseHeaders. For each entry in this Map, call HttpServletResponse.setHeader(), passing the key as the first argument and the value as the second argument.

  • Call Resource#getInputStream and serve up the bytes of the resource to the response.

  • Call HttpServletResponse.setContentLength() passing the byte count of the resource.

  • If an IOException is thrown during any of the previous steps, log a descriptive, localized message, including the resourceName and libraryName (if present). Then, HttpServletRequest.SC_NOT_FOUND must be passed to HttpServletResponse.setStatus(), then handleResourceRequest must immediately return.

  • In all cases in this method, any streams, channels, sockets, or any other IO resources must be closed before this method returns.

Parameters
context:FacesContext

the javax.faces.context.FacesContext for this request

Annotations
@Override
Exceptions
IOException:
when an I/O error occurs.
See Also
javax.faces.application.ResourceHandler#handleResourceRequest(javax.faces.context.FacesContext)
initExclusionsback to summary
private void initExclusions(Map<String, Object> appMap)

Initialize the exclusions for this application. If no explicit exclusions are configured, the defaults of

  • .class
  • .properties
  • .xhtml
  • .jsp
  • .jspx
    • will be used.
initMaxAgeback to summary
private void initMaxAge()
isExcludedback to summary
private boolean isExcluded(String resourceId)
Parameters
resourceId:String

the normalized request path as returned by normalizeResourceRequest(javax.faces.context.FacesContext)

Returns:boolean

true if the request matces an excluded resource, otherwise false

isResourceRequestback to summary
public boolean isResourceRequest(FacesContext context)

Implements abstract javax.faces.application.ResourceHandler.isResourceRequest.

Doc from javax.faces.application.ResourceHandler.isResourceRequest.

Return true if the current request is a resource request. This method is called by javax.faces.webapp.FacesServlet#service to determine if this request is a view request or a resource request.

Parameters
context:FacesContext

the javax.faces.context.FacesContext for this request

Returns:boolean

true if the current request is a resource request, false otherwise.

Annotations
@Override
See Also
ResourceHandler#isResourceRequest(javax.faces.context.FacesContext)
libraryExistsback to summary
public boolean libraryExists(String libraryName)

Implements abstract javax.faces.application.ResourceHandler.libraryExists.

Doc from javax.faces.application.ResourceHandler.libraryExists.

Return true if the resource library named by the argument libraryName can be found. If there is a localePrefix for this application, as defined in LOCALE_PREFIX, first look for the library with the prefix. If no such library is found, look for the library without the prefix. This allows developers to avoid duplication of files. For example, consider the case where the developer wants to have a resource library containing a localized image resource and a non-localized script resource. By checking both locations for the existence of the library, along with other spec changes in section 2.6.1.4, this scenario is enabled.

Parameters
libraryName:String

the library name.

Returns:boolean

true if the library exists, false otherwise.

Annotations
@Override
libraryNameIsSafeback to summary
private boolean libraryNameIsSafe(String libraryName)
logMissingResourceback to summary
private void logMissingResource(FacesContext ctx, String resourceName, String libraryName, Throwable t)

Log a message indicating a particular resource (reference by name and/or library) could not be found. If this was due to an exception, the exception provided will be logged as well.

Parameters
ctx:FacesContext

the FacesContext for the current request

resourceName:String

the resource name

libraryName:String

the resource library

t:Throwable

the exception caught when attempting to find the resource

logMissingResourceback to summary
private void logMissingResource(FacesContext ctx, String resourceId, Throwable t)

Log a message indicating a particular resource (reference by name and/or library) could not be found. If this was due to an exception, the exception provided will be logged as well.

Parameters
ctx:FacesContext

the FacesContext for the current request

resourceId:String

the resource name

t:Throwable

the exception caught when attempting to find the resource

normalizeResourceRequestback to summary
private String normalizeResourceRequest(FacesContext context)

Normalize the request path to exclude JSF invocation information. If the FacesServlet servicing this request was prefix mapped, then the path to the FacesServlet will be removed. If the FacesServlet servicing this request was extension mapped, then the extension will be trimmed off.

Parameters
context:FacesContext

the FacesContext for the current request

Returns:String

the request path without JSF invocation information

send304back to summary
private void send304(FacesContext ctx)
send404back to summary
private void send404(FacesContext ctx, String resourceName, String libraryName, boolean logMessage)
send404back to summary
private void send404(FacesContext ctx, String resourceName, String libraryName, Throwable t, boolean logMessage)
setCreationTimeback to summary
pack-priv void setCreationTime(long creationTime)

This method is here soley for the purpose of unit testing and will not be invoked during normal runtime.

Parameters
creationTime:long

the time in milliseconds

Annotations
@SuppressWarnings:UnusedDeclaration