gr.abiss.mvn.plugins.jstools.web
Class JavascriptDependencyFilter

Package class diagram package JavascriptDependencyFilter
java.lang.Object
  extended by gr.abiss.mvn.plugins.jstools.web.JavascriptDependencyFilter
All Implemented Interfaces:
javax.servlet.Filter

public class JavascriptDependencyFilter
extends Object
implements javax.servlet.Filter

A Servlet Filter that loads Javascript files or other static resources from the runtime classpath and uses them to build an HTTP response. Web applications can, for example, include JS dependencies in their Maven POM and the filter will resolve those at runtime, cache them and produce the appropriate HTTP response. The filter will only serve classpath resource types with file extentions you configure it to, (see the allowedExtentions init-param) and with the Cache-Control HTTP header you specify (see the cacheControl init-param). Note that the filter will only handle HTTP GET and HTTP HEAD requests. All other HTTP request methods will be server with a "Non Implemented" (501) HTTP response error. To add the filter in your application, add the folowing in your web.xml or, in case you use XDoclet (webdoclet), your filters.xml merge file:

    <filter> 
       <filter-name>JavascriptDependencyFilter</filter-name>
       <filter-class>gr.abiss.mvn.plugins.jstools.web.JavascriptDependencyFilter</filter-class>
       <init-param> 
          <!-- 
             Optional, tells the filter which file extentions are allowed.
             The default are the sample values below (if you just want those,
             you don't have to configure the filter).
             Note that the Filter will NOT load resources from META-INF.
          --> 
          <param-name>allowedExtentions</param-name>
          <param-value>js png jpg gif txt html htm xml xsl xslt svg svgz swf</param-value> 
      </init-param>
       <init-param> 
          <!-- 
             Optional, used to configure the Cache-Control HTTP header. 
             Default is "max-age=86400" 
          --> 
          <param-name>cacheControl</param-name>
          <param-value>max-age=86400</param-value> 
      </init-param>
       <init-param> 
          <!-- 
             Optional, enable gzip compression for browsers that support it
             (based on HTTP request headers). Default is true.
          --> 
          <param-name>enableGzip</param-name>
          <param-value>true</param-value> 
      </init-param>
       <init-param> 
          <!-- 
             Optional, control whether resources are cached to
             avoid I/O overhead every time they are requested. Default is true.
          --> 
          <param-name>enableCache</param-name>
          <param-value>true</param-value> 
      </init-param>
       <init-param> 
          <!-- 
             Optional, control whether gzipped resources are cached to
             avoid GZIPing overhead every time they are requested. Default is true.
          --> 
          <param-name>enableGzipedCache</param-name>
          <param-value>true</param-value> 
      </init-param>
      <init-param>
          <!-- 
             Optional, tells the filter whether to send an HTTP 404 ("not found") code 
             or let go of the filter chain to the application if no matching resource
             is found in the classpath. Possible values are {true, false}.
             The default is to send a 404 (true). 
          --> 
          <param-name>send404</param-name>
          <param-value>true</param-value> 
      </init-param>
       <init-param> 
          <!--
             Optional (mandatory for arbitary url-pattern in filter-mapping). 
             Used to tell the plugin what base URL fragment to ignore when looking 
             for Javascript resources. Must match the url-pattern in filter-mapping, 
             see below. Default is /lib/js
          --> 
          <param-name>basePath</param-name>
          <param-value>/lib/js/</param-value> 
      </init-param> 
    </filter>
 
The servlet container also needs a mapping for the filter. You really don't want the filter intercepting all HTTP requests! The URL pattern in the filter mapping must be exactly the same with the value of the basePath inialization parameter:
    <filter-mapping>
      <filter-name>JavascriptDependencyFilter</filter-name>
      <!-- matches the default value of the basePath
 
 init - param
 
      <url-pattern>/lib/js/</url-pattern>
    </filter-mapping>
 
This helps in inly invoking the filter for resources that are actually in the classpath. To clarify, suppose an HTML response has a script tag like this:
    <script type="text/javascript" 
       src="/lib/js/gr/abiss/js/sarissa/sarissa.js"> 
    <script&gt
 
The browser will try to load the script making an HTTP GET for that URL. The server will have the filter intercept the request since it matches the URL pattern of the filter mapping. The filter will then do the following:
  1. Obtain the request URI
  2. Substring the URL removing the part that matches the value of the basePath initialization parameter
  3. Use the result to lookup the corresponding resource in it's cache. Load the resource and put it in the cache if not already there
  4. If the resource exists make an HTTP response with it, otherwise let the filter chain go or send a 404 depending on the value of the send404 initialization parameter.

  5. Field Summary
    protected  Set<String> allowedExtentions
              init-param accepting a whitespace-seperated list of allowed file extentions.
    protected  String basePath
              init-param for the filter's base mapping path.
    protected static Map<String,byte[]> cache
              The Javascript resources cache, used for binrary resources or when client does not support a GZIPed response
    protected  boolean enableGzip
              init-param to control whether to send textual GZIPed textual content to increase HTTP performance.
    protected static Map<String,byte[]> gzipedCache
              The Javascript GZIPed resources cache, used for non-binary resources when client supports it
    protected static Map<String,String> MIME_TYPE_MAPPINGS
              The knwon MIME types map for this filter (file extention, MIME type)
    protected  boolean send404
              init-param to control whether to send an HTTP 404 ("not found") code or let go of the filter chain to the application in case the requested resource cannot be found.
     
    Constructor Summary
    JavascriptDependencyFilter()
               
     
    Method Summary
     void destroy()
              Clean up by doing nothing :-)
     void doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain chain)
               
    protected  String getMimeTypeFromFileExtention(String extention)
              Get the MIME type for the given file extention
     void init(javax.servlet.FilterConfig filterConfig)
              Initialize and configure based on any init parameters available
     
    Methods inherited from class java.lang.Object
    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
     

    Field Detail

    MIME_TYPE_MAPPINGS

    protected static Map<String,String> MIME_TYPE_MAPPINGS
    The knwon MIME types map for this filter (file extention, MIME type)


    cache

    protected static Map<String,byte[]> cache
    The Javascript resources cache, used for binrary resources or when client does not support a GZIPed response


    gzipedCache

    protected static Map<String,byte[]> gzipedCache
    The Javascript GZIPed resources cache, used for non-binary resources when client supports it


    allowedExtentions

    protected Set<String> allowedExtentions
    init-param accepting a whitespace-seperated list of allowed file extentions. The default value is js png jpg gif txt html htm xml xsl xslt svg svgz swf initialization parameter.


    basePath

    protected String basePath
    init-param for the filter's base mapping path. Must be the same as the url-pattern mapping for the filter, e.g. /lib/js


    send404

    protected boolean send404
    init-param to control whether to send an HTTP 404 ("not found") code or let go of the filter chain to the application in case the requested resource cannot be found. Possible values are {true, false} The default behaviour is to send a 404 (true)


    enableGzip

    protected boolean enableGzip
    init-param to control whether to send textual GZIPed textual content to increase HTTP performance. Possible values are {true, false} The default true

    Constructor Detail

    JavascriptDependencyFilter

    public JavascriptDependencyFilter()
    Method Detail

    getMimeTypeFromFileExtention

    protected String getMimeTypeFromFileExtention(String extention)
    Get the MIME type for the given file extention

    Parameters:
    extention - The file extention
    Returns:
    The MIME type for the file extention if permitted/known possible.

    doFilter

    public void doFilter(javax.servlet.ServletRequest request,
                         javax.servlet.ServletResponse response,
                         javax.servlet.FilterChain chain)
                  throws IOException,
                         javax.servlet.ServletException
    Specified by:
    doFilter in interface javax.servlet.Filter
    Throws:
    IOException
    javax.servlet.ServletException
    See Also:
    Filter.doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)

    init

    public void init(javax.servlet.FilterConfig filterConfig)
              throws javax.servlet.ServletException
    Initialize and configure based on any init parameters available

    Specified by:
    init in interface javax.servlet.Filter
    Throws:
    javax.servlet.ServletException
    See Also:
    Filter.init(javax.servlet.FilterConfig)

    destroy

    public void destroy()
    Clean up by doing nothing :-)

    Specified by:
    destroy in interface javax.servlet.Filter
    See Also:
    Filter.destroy()

    © 2007-2007 Abiss.gr