Class ClassWebResource

java.lang.Object
org.zkoss.web.util.resource.ClassWebResource

public class ClassWebResource extends Object
Used to access resources located in class path and under /web. It doesn't work alone. Rather, it is a helper for servlet, such as ZK's update servlet.

Typical use:

  1. Declares a member in the servlet to serve the request for the resource located in class path.
  2. Invoke getInstance(jakarta.servlet.ServletContext, java.lang.String) to init the member when Servlet.init() is called.
  3. 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

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Indicates that the filter is applicable if the request is dispatched due to the inclusion.
    static final int
    Indicates that the filter is applicable if the request comes directly from the client.
    static final String
    The prefix of path of web resources ("/web").
  • Method Summary

    Modifier and Type
    Method
    Description
    Adds an Extendlet (a.k.a., resource processor) to process the resource of the specified extension.
    void
    addFilter(String ext, Filter filter, int flags)
    Adds a filter (Filter) to perform filtering task for the resource of the specified extension.
    static URL
    Returns the URL of the resource of the specified URI by searching only the class path (with PATH_PREFIX).
    Returns the resource in a stream of the specified URI by searching only the class path (with PATH_PREFIX).
    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.
    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.Locator
    Returns the extra locator, or null if not available.
    getFilters(String ext, int flag)
    Returns an array of the filters (Filter) of the specified extension, or null if not associated.
    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.
    Returns the URL of the resource of the specified URI by searching the extra locator, if any, and then the class path (with PATH_PREFIX).
    Returns the resource in a stream of the specified URI by searching the extra locator, if any, and then, the class path (with PATH_PREFIX).
    boolean
    Returns whether to debug JavaScript files.
    boolean
    Returns whether source map is enabled or not.
    Internal used only.
    Removes the Extendlet (a.k.a., resource processor) for the specified extension.
    boolean
    removeFilter(String ext, Filter filter, int flags)
    Removes the filter (Filter) for the specified extension.
    void
    service(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response)
    Process the request by retrieving the path from the path info.
    void
    service(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, String path)
    Process the request with the specified path.
    void
    Sets the extension that shall be compressed if the browser supports the compression encoding (accept-encoding).
    void
    setDebugJS(boolean debug)
    Sets whether to debug JavaScript files.
    void
    Sets the prefix used to encoding the URL.
    void
    setExtraLocator(org.zkoss.util.resource.Locator loc)
    Sets the extra locator.
    void
    setMappingURI(String mappingURI)
    Called by WebManager#setUpdateUri when WebManager is created by HttpSessionListener#contextInitialized

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Method Details

    • getClassResource

      public static URL getClassResource(String uri)
      Returns the URL of the resource of the specified URI by searching only the class path (with PATH_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

      public static InputStream getClassResourceAsStream(String uri)
      Returns the resource in a stream of the specified URI by searching only the class path (with PATH_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

      public URL 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 (with PATH_PREFIX).

      This method becomes non-static since 5.0.0, and it will search the extra locator (getExtraLocator()) first.

    • getResourceAsStream

      public InputStream getResourceAsStream(String uri)
      Returns the resource in a stream of the specified URI by searching the extra locator, if any, and then, the class path (with PATH_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 invokes Https.getThisPathInfo(jakarta.servlet.ServletRequest) to retrieve the path info, and then invoke service(HttpServletRequest,HttpServletResponse,String).

      If the path info is not found, nothing is generated.

      Throws:
      jakarta.servlet.ServletException
      IOException
      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.ServletException
      IOException
      Since:
      3.0.0
    • getExtendlet

      public Extendlet getExtendlet(String ext, boolean lookup)
      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" and lookup is true, it first searches any extendlet registered for "js.dsp". If not found, it searches any extendlet for "dsp". However, if lookup is false, it searches only "js.dsp".
      Returns:
      the Extendlet (aka., resource processor), or null if not associated yet.
      Since:
      6.0.0
    • getExtendlet

      public Extendlet getExtendlet(String ext)
      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

      public Extendlet addExtendlet(String ext, Extendlet extlet)
      Adds an Extendlet (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

      public Extendlet removeExtendlet(String ext)
      Removes the Extendlet (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

      public Filter[] getFilters(String ext, int flag)
      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 - either FILTER_REQUEST or and FILTER_INCLUDE. If 0, FILTER_REQUEST is assumed.
      Since:
      3.5.1
    • addFilter

      public void addFilter(String ext, Filter filter, int flags)
      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 extension
      filter - the filter
      flags - a combination of FILTER_REQUEST and FILTER_INCLUDE. If 0, FILTER_REQUEST is assumed.
      Since:
      3.5.1
    • removeFilter

      public boolean removeFilter(String ext, Filter filter, int flags)
      Removes the filter (Filter) for the specified extension.
      Parameters:
      flags - a combination of FILTER_REQUEST and FILTER_INCLUDE. If 0, FILTER_REQUEST is assumed.
      Returns:
      whether the filter has been removed successfully.
      Since:
      3.5.1
    • setCompress

      public void setCompress(String[] exts)
      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

      public String[] 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, and isDebugJS(), then service(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

      public void setMappingURI(String mappingURI)
      Called by WebManager#setUpdateUri when WebManager is created by HttpSessionListener#contextInitialized
      Parameters:
      mappingURI - mapping URI excluding PATH_PREFIX.
    • setEncodeURLPrefix

      public void setEncodeURLPrefix(String prefix)
      Sets the prefix used to encoding the URL. The prefix will be encoded when ExtendletContext.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 version

      Default: 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

      public String 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 '/'.
      Since:
      3.6.3
      See Also:
    • modifyPath

      public String modifyPath(String path) throws UnsupportedEncodingException
      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