Package org.zkoss.web.util.resource

## Class ClassWebResource

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

-     public class ClassWebResource
      extends java.lang.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(javax.servlet.ServletContext, java.lang.String)` to init the member when Servlet.init() is called.
  3. Calling `service(javax.servlet.http.HttpServletRequest, javax.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 int` | `FILTER_INCLUDE` | Indicates that the filter is applicable if the request is dispatched due to the inclusion.
    `static int` | `FILTER_REQUEST` | Indicates that the filter is applicable if the request comes directly from the client.
    `static java.lang.String` | `PATH_PREFIX` | The prefix of path of web resources ("/web").

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method | Description
    --- | --- | ---
    `Extendlet` | `addExtendlet​(java.lang.String ext, Extendlet extlet)` | Adds an `Extendlet` (a.k.a., resource processor) to process the resource of the specified extension.
    `void` | `addFilter​(java.lang.String ext, Filter filter, int flags)` | Adds a filter (`Filter`) to perform filtering task for the resource of the specified extension.
    `static java.net.URL` | `getClassResource​(java.lang.String uri)` | Returns the URL of the resource of the specified URI by searching only the class path (with `PATH_PREFIX`).
    `static java.io.InputStream` | `getClassResourceAsStream​(java.lang.String uri)` | Returns the resource in a stream of the specified URI by searching only the class path (with `PATH_PREFIX`).
    `java.lang.String[]` | `getCompress()` | Returns the extension that shall be compressed if the browser supports the compression encoding (accept-encoding).
    `java.lang.String` | `getEncodeURLPrefix()` | Returns the prefix used to encoding the URL, or null if no prefix.
    `Extendlet` | `getExtendlet​(java.lang.String ext)` | Returns the Extendlet (a.k.a., resource processor) of the specified extension, or null if not associated.
    `Extendlet` | `getExtendlet​(java.lang.String ext, boolean lookup)` | Returns the Extendlet (aka., resource processor) of the specified extension, or null if not associated.
    `Locator` | `getExtraLocator()` | Returns the extra locator, or null if not available.
    `Filter[]` | `getFilters​(java.lang.String ext, int flag)` | Returns an array of the filters (`Filter`) of the specified extension, or null if not associated.
    `static ClassWebResource` | `getInstance​(javax.servlet.ServletContext ctx, java.lang.String mappingURI)` | Returns the instance (singleton in the whole app) for handling resources located in class path.
    `java.net.URL` | `getResource​(java.lang.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`).
    `java.io.InputStream` | `getResourceAsStream​(java.lang.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`).
    `boolean` | `isDebugJS()` | Returns whether to debug JavaScript files.
    `boolean` | `isSourceMapEnabled()` | Returns whether source map is enabled or not.
    `java.lang.String` | `modifyPath​(java.lang.String path)` | Internal used only.
    `Extendlet` | `removeExtendlet​(java.lang.String ext)` | Removes the `Extendlet` (a.k.a., resource processor) for the specified extension.
    `boolean` | `removeFilter​(java.lang.String ext, Filter filter, int flags)` | Removes the filter (`Filter`) for the specified extension.
    `void` | `service​(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)` | Process the request by retrieving the path from the path info.
    `void` | `service​(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, java.lang.String path)` | Process the request with the specified path.
    `void` | `setCompress​(java.lang.String[] exts)` | 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` | `setEncodeURLPrefix​(java.lang.String prefix)` | Sets the prefix used to encoding the URL.
    `void` | `setExtraLocator​(Locator loc)` | Sets the extra locator.
    `void` | `setMappingURI​(java.lang.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 Detail

    - #### PATH_PREFIX

          public static final java.lang.String PATH_PREFIX

      The prefix of path of web resources ("/web").
      See Also:
        Constant Field Values

    - #### FILTER_REQUEST

          public static final int FILTER_REQUEST

      Indicates that the filter is applicable if the request comes directly from the client.
      Since:
        3.5.1
      See Also:
        `addFilter(java.lang.String, org.zkoss.web.util.resource.Filter, int)`, Constant Field Values

    - #### FILTER_INCLUDE

          public static final int FILTER_INCLUDE

      Indicates 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:
        `addFilter(java.lang.String, org.zkoss.web.util.resource.Filter, int)`, Constant Field Values

  - ### Method Detail

    - #### getClassResource

          public static java.net.URL getClassResource​(java.lang.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 java.io.InputStream getClassResourceAsStream​(java.lang.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 java.net.URL getResource​(java.lang.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 java.io.InputStream getResourceAsStream​(java.lang.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​(javax.servlet.ServletContext ctx,
                                                           java.lang.String mappingURI)

      Returns the instance (singleton in the whole app) for handling resources located in class path.

    - #### getExtraLocator

          public 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​(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​(javax.servlet.http.HttpServletRequest request,
                              javax.servlet.http.HttpServletResponse response)
                       throws javax.servlet.ServletException,
                              java.io.IOException

      Process the request by retrieving the path from the path info. It invokes `Https.getThisPathInfo(javax.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:
        `javax.servlet.ServletException`
        `java.io.IOException`
      Since:
        2.4.1

    - #### service

          public void service​(javax.servlet.http.HttpServletRequest request,
                              javax.servlet.http.HttpServletResponse response,
                              java.lang.String path)
                       throws javax.servlet.ServletException,
                              java.io.IOException

      Process the request with the specified path.
      Parameters:
        `path` - the path related to the class path
      Throws:
        `javax.servlet.ServletException`
        `java.io.IOException`
      Since:
        3.0.0

    - #### getExtendlet

          public Extendlet getExtendlet​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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 java.lang.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(boolean)`

    - #### 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(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)` is called to load abc.js, and `isDebugJS()`, then `service(javax.servlet.http.HttpServletRequest, javax.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​(java.lang.String mappingURI)

      Called by WebManager#setUpdateUri when WebManager is created by HttpSessionListener#contextInitialized
      Parameters:
        `mappingURI` - mapping URI excluding PATH_PREFIX.

    - #### setEncodeURLPrefix

          public void setEncodeURLPrefix​(java.lang.String prefix)

      Sets the prefix used to encoding the URL. The prefix will be encoded when `ExtendletContext.encodeURL(javax.servlet.ServletRequest, javax.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 java.lang.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:
        `ExtendletContext.encodeURL(javax.servlet.ServletRequest, javax.servlet.ServletResponse, java.lang.String)`

    - #### modifyPath

          public java.lang.String modifyPath​(java.lang.String path)
                                      throws java.io.UnsupportedEncodingException

      Internal used only.
      Throws:
        `java.io.UnsupportedEncodingException`
      Since:
        10.0.0

    - #### isSourceMapEnabled

          public boolean isSourceMapEnabled()

      Returns whether source map is enabled or not.
      Since:
        10.0.0