Class ClassWebResource
Typical use:
- Declares a member in the servlet to serve the request for the resource located in class path.
- Invoke
getInstance(jakarta.servlet.ServletContext, java.lang.String)to init the member when Servlet.init() is called. - Calling
service(jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse)when a request is receive.
Note: it assumes the file being loaded and the output stream is encoded in UTF-8 unless the extendlet handles it differently.
- Author:
- tomyeh
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intIndicates that the filter is applicable if the request is dispatched due to the inclusion.static final intIndicates that the filter is applicable if the request comes directly from the client.static final StringThe prefix of path of web resources ("/web"). -
Method Summary
Modifier and TypeMethodDescriptionaddExtendlet(String ext, Extendlet extlet) Adds anExtendlet(a.k.a., resource processor) to process the resource of the specified extension.voidAdds a filter (Filter) to perform filtering task for the resource of the specified extension.static URLgetClassResource(String uri) Returns the URL of the resource of the specified URI by searching only the class path (withPATH_PREFIX).static InputStreamReturns the resource in a stream of the specified URI by searching only the class path (withPATH_PREFIX).String[]Returns the extension that shall be compressed if the browser supports the compression encoding (accept-encoding).Returns the prefix used to encoding the URL, or null if no prefix.getExtendlet(String ext) Returns the Extendlet (a.k.a., resource processor) of the specified extension, or null if not associated.getExtendlet(String ext, boolean lookup) Returns the Extendlet (aka., resource processor) of the specified extension, or null if not associated.org.zkoss.util.resource.LocatorReturns the extra locator, or null if not available.Filter[]getFilters(String ext, int flag) Returns an array of the filters (Filter) of the specified extension, or null if not associated.static final ClassWebResourcegetInstance(jakarta.servlet.ServletContext ctx, String mappingURI) Returns the instance (singleton in the whole app) for handling resources located in class path.getResource(String uri) Returns the URL of the resource of the specified URI by searching the extra locator, if any, and then the class path (withPATH_PREFIX).Returns the resource in a stream of the specified URI by searching the extra locator, if any, and then, the class path (withPATH_PREFIX).booleanReturns whether to debug JavaScript files.booleanReturns whether source map is enabled or not.modifyPath(String path) Internal used only.removeExtendlet(String ext) Removes theExtendlet(a.k.a., resource processor) for the specified extension.booleanremoveFilter(String ext, Filter filter, int flags) Removes the filter (Filter) for the specified extension.voidservice(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) Process the request by retrieving the path from the path info.voidservice(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, String path) Process the request with the specified path.voidsetCompress(String[] exts) Sets the extension that shall be compressed if the browser supports the compression encoding (accept-encoding).voidsetDebugJS(boolean debug) Sets whether to debug JavaScript files.voidsetEncodeURLPrefix(String prefix) Sets the prefix used to encoding the URL.voidsetExtraLocator(org.zkoss.util.resource.Locator loc) Sets the extra locator.voidsetMappingURI(String mappingURI) Called by WebManager#setUpdateUri when WebManager is created by HttpSessionListener#contextInitialized
-
Field Details
-
PATH_PREFIX
The prefix of path of web resources ("/web").- See Also:
-
FILTER_REQUEST
public static final int FILTER_REQUESTIndicates that the filter is applicable if the request comes directly from the client.- Since:
- 3.5.1
- See Also:
-
FILTER_INCLUDE
public static final int FILTER_INCLUDEIndicates that the filter is applicable if the request is dispatched due to the inclusion. In other words, it is included by other servlet.- Since:
- 3.5.1
- See Also:
-
-
Method Details
-
getClassResource
Returns the URL of the resource of the specified URI by searching only the class path (withPATH_PREFIX).On the other hand,
getResource(java.lang.String)will search the extra locator first (getExtraLocator()) and then the class path.- Since:
- 5.0.0
-
getClassResourceAsStream
Returns the resource in a stream of the specified URI by searching only the class path (withPATH_PREFIX).On the other hand,
getResourceAsStream(java.lang.String)will search the extra locator first (getExtraLocator()) and then the class path.- Since:
- 5.0.0
-
getResource
Returns the URL of the resource of the specified URI by searching the extra locator, if any, and then the class path (withPATH_PREFIX).This method becomes non-static since 5.0.0, and it will search the extra locator (
getExtraLocator()) first. -
getResourceAsStream
Returns the resource in a stream of the specified URI by searching the extra locator, if any, and then, the class path (withPATH_PREFIX).This method becomes non-static since 5.0.0, and it will search the extra locator (
getExtraLocator()) first. -
getInstance
public static final ClassWebResource getInstance(jakarta.servlet.ServletContext ctx, String mappingURI) Returns the instance (singleton in the whole app) for handling resources located in class path. -
getExtraLocator
public org.zkoss.util.resource.Locator getExtraLocator()Returns the extra locator, or null if not available. The extra locator, if specified, has the higher priority than the class path.Default: null.
- Since:
- 5.0.0
-
setExtraLocator
public void setExtraLocator(org.zkoss.util.resource.Locator loc) Sets the extra locator. The extra locator, if specified, has the higher priority than the class path.Default: null.
- Since:
- 5.0.0
-
service
public void service(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws jakarta.servlet.ServletException, IOException Process the request by retrieving the path from the path info. It invokesHttps.getThisPathInfo(jakarta.servlet.ServletRequest)to retrieve the path info, and then invokeservice(HttpServletRequest,HttpServletResponse,String).If the path info is not found, nothing is generated.
- Throws:
jakarta.servlet.ServletExceptionIOException- Since:
- 2.4.1
-
service
public void service(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, String path) throws jakarta.servlet.ServletException, IOException Process the request with the specified path.- Parameters:
path- the path related to the class path- Throws:
jakarta.servlet.ServletExceptionIOException- Since:
- 3.0.0
-
getExtendlet
Returns the Extendlet (aka., resource processor) of the specified extension, or null if not associated.- Parameters:
ext- the extension, e.g, "js" and "css.dsp".lookup- whether to search different combination of the given extension. For example, if the extension is "js.dsp" andlookupis true, it first searches any extendlet registered for "js.dsp". If not found, it searches any extendlet for "dsp". However, iflookupis false, it searches only "js.dsp".- Returns:
- the Extendlet (aka., resource processor), or null if not associated yet.
- Since:
- 6.0.0
-
getExtendlet
Returns the Extendlet (a.k.a., resource processor) of the specified extension, or null if not associated.It is a shortcut of
getExtendlet(ext, true).- Parameters:
ext- the extension, e.g, "js" and "css.dsp".- Returns:
- the Extendlet (a.k.a., resource processor), or null if not associated yet.
- Since:
- 2.4.1
-
addExtendlet
Adds anExtendlet(a.k.a., resource processor) to process the resource of the specified extension.- Parameters:
ext- the extension, e.g, "js" and "css".extlet- the Extendlet (a.k.a., resource processor) to add- Returns:
- the previous Extendlet, or null if not associated before.
- Since:
- 2.4.1
-
removeExtendlet
Removes theExtendlet(a.k.a., resource processor) for the specified extension.- Parameters:
ext- the extension, e.g, "js" and "css.dsp".- Returns:
- the previous Extendlet, or null if no Extendlet was associated with the specified extension.
- Since:
- 2.4.1
-
getFilters
Returns an array of the filters (Filter) of the specified extension, or null if not associated.Note: if the extension is "js.dsp", then it searches any filters registered for "js.dsp". If not found, it searches any filters for "dsp". In other words, "dsp" is searched only if no filter is registered for "js.dsp".
- Parameters:
ext- the extension, such as "js" and "css.dsp".flag- eitherFILTER_REQUESTor andFILTER_INCLUDE. If 0,FILTER_REQUESTis assumed.- Since:
- 3.5.1
-
addFilter
Adds a filter (Filter) to perform filtering task for the resource of the specified extension.Unlike
addExtendlet(java.lang.String, org.zkoss.web.util.resource.Extendlet), multiple filters can be applied to the same extension. The first one being added will be called first.- Parameters:
ext- the extensionfilter- the filterflags- a combination ofFILTER_REQUESTandFILTER_INCLUDE. If 0,FILTER_REQUESTis assumed.- Since:
- 3.5.1
-
removeFilter
Removes the filter (Filter) for the specified extension.- Parameters:
flags- a combination ofFILTER_REQUESTandFILTER_INCLUDE. If 0,FILTER_REQUESTis assumed.- Returns:
- whether the filter has been removed successfully.
- Since:
- 3.5.1
-
setCompress
Sets the extension that shall be compressed if the browser supports the compression encoding (accept-encoding).Default: null (no compression at all).
- Parameters:
exts- an array of extensions, e.g., {"js", "css", "html"}. If null or zero-length, it means no compression at all.- Since:
- 2.4.1
-
getCompress
Returns the extension that shall be compressed if the browser supports the compression encoding (accept-encoding).Default: null (no compression at all).
- Since:
- 2.4.1
-
isDebugJS
public boolean isDebugJS()Returns whether to debug JavaScript files. If true, it means the original (i.e., uncompressed) JavaScript files shall be loaded instead of compressed JavaScript files.- Since:
- 3.0.4
- See Also:
-
setDebugJS
public void setDebugJS(boolean debug) Sets whether to debug JavaScript files.Default: false.
If true is specified, it will try to load the original Java (i.e., uncompressed) file instead of the compressed one. For example, if
service(jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse)is called to load abc.js, andisDebugJS(), thenservice(jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse)will try to load abc.src.js first. If not found, it loads ab.js instead.If
isDebugJS()is false (default), abc.js is always loaded.- Parameters:
debug- whether to debug JavaScript files. If true, the original JavaScript files shall be loaded instead of the compressed files.- Since:
- 3.0.4
-
setMappingURI
Called by WebManager#setUpdateUri when WebManager is created by HttpSessionListener#contextInitialized- Parameters:
mappingURI- mapping URI excluding PATH_PREFIX.
-
setEncodeURLPrefix
Sets the prefix used to encoding the URL. The prefix will be encoded whenExtendletContext.encodeURL(jakarta.servlet.ServletRequest, jakarta.servlet.ServletResponse, java.lang.String)is called. The prefix is usually the build number, such that browser won't use the wrong cached versionDefault: null (no special encoding).
- Parameters:
prefix- the prefix used to encode URL for the extendlet. If prefix is not empty and doesn't start with '/', it will be prefixed with '/'.- Since:
- 3.6.3
-
getEncodeURLPrefix
Returns the prefix used to encoding the URL, or null if no prefix. If an non-null prefix is returned, it must start with '/'. -
modifyPath
Internal used only.- Throws:
UnsupportedEncodingException- Since:
- 10.0.0
-
isSourceMapEnabled
public boolean isSourceMapEnabled()Returns whether source map is enabled or not.- Since:
- 10.0.0
-