org.zkoss.web.util.resource
Class ClassWebResource

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

public class ClassWebResource
extends java.lang.Object

Used to access resouces 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 or ClassWebServlet.

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
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
 Extendlet addExtendlet(java.lang.String ext, Extendlet extlet)
          Adds an Extendlet (aka., 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.
 java.lang.String[] getCompress()
          Returns the extension that shall be compressed if the browser supports the compression encoding (accept-encoding).
 Extendlet getExtendlet(java.lang.String ext)
          Returns the Extendlet (aka., resource processor) of the specified extension, or null if not associated.
 Filter[] getFilters(java.lang.String ext, int flag)
          Returns an array of the filters (Filter) of the speficied extension, or null if not associated.
static ClassWebResource getInstance(javax.servlet.ServletContext ctx, java.lang.String mappingURI)
          Returns the instance (singlton in the whole app) for handling resources located in class path.
static java.net.URL getResource(java.lang.String uri)
          Returns the URL of the resource of the specified URI by searching the class path (with PATH_PREFIX).
static java.io.InputStream getResourceAsStream(java.lang.String uri)
          Returns the resource in a stream of the specified URI by searching the class path (with PATH_PREFIX).
 boolean isDebugJS()
          Returns whether to debug JavaScript files.
 Extendlet removeExtendlet(java.lang.String ext)
          Removes the Extendlet (aka., 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.
 
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

getResource

public static java.net.URL getResource(java.lang.String uri)
Returns the URL of the resource of the specified URI by searching the class path (with PATH_PREFIX).


getResourceAsStream

public static java.io.InputStream getResourceAsStream(java.lang.String uri)
Returns the resource in a stream of the specified URI by searching the class path (with PATH_PREFIX).


getInstance

public static final ClassWebResource getInstance(javax.servlet.ServletContext ctx,
                                                 java.lang.String mappingURI)
Returns the instance (singlton in the whole app) for handling resources located in class path.


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)
Returns the Extendlet (aka., resource processor) of the specified extension, or null if not associated.

Note: if the extension is "js.dsp", then it searches any extendlet registered for "js.dsp". If not found, it searches any extendlet for "dsp". In other words, "dsp" is searched only if no extendlet is registered for "js.dsp".

Parameters:
ext - the extension, e.g, "js" and "css.dsp".
Returns:
the Extendlet (aka., 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 (aka., resource processor) to process the resource of the specified extension.

Parameters:
ext - the extension, e.g, "js" and "css".
extlet - the Extendlet (aka., resouce 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 (aka., 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 speficied 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.org.js first. If not found, it load ab.js insted.

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


Copyright © 2005-2009 Potix Corporation. All Rights Reserved. SourceForge.net Logo