com.googlecode.webutilities.filters
Class YUIMinFilter

java.lang.Object
  extended by com.googlecode.webutilities.filters.common.AbstractFilter
      extended by com.googlecode.webutilities.filters.YUIMinFilter
All Implemented Interfaces:
IgnoreAcceptContext, javax.servlet.Filter

public class YUIMinFilter
extends AbstractFilter

The YUIMinFilter is implemented as Servlet Filter to enable on the fly minification of JS and CSS resources using YUICompressor.

Using the YUIMinFilter the JS and CSS resources can be minified in realtime by adding this filter on them.

Usage

Put the webutilities-x.y.z.jar and yuicompressor-x.y.z.jar (See dependency mentioned below) in your classpath (WEB-INF/lib folder of your webapp).

Declare this filter in your web.xml ( web descriptor file)

 ...
 <filter>
        <filter-name>yuiMinFilter</filter-name>
        <filter-class>com.googlecode.webutilities.filters.YUIMinFilter</filter-class>
        <!-- All the init params are optional and are equivalent to YUICompressor command line options -->
        <init-param>
                <param-name>lineBreak</param-name>
                <param-value>8000</param-value>
        </init-param>
 </filter>
 ...
 
Map this filter on your JS and CSS resources
 ...
 <filter-mapping>
   <filter-name>yuiMinFilter</filter-name>
   <url-pattern>*.js</url-pattern>
   <url-pattern>*.json</url-pattern>
   <url-pattern>*.css</url-pattern>
 </filter-mapping>
 ...
 

And you are all done! All your JS and CSS files should get minified on the fly.

Init Parameters

All the init parameters are optional and explained below.

  lineBreak - equivalent to YUICompressor --line-break. Insert a line break after the specified column number
  noMunge - equivalent to YUICompressor --nomunge. Minify only, do not obfuscate. Default false.
  preserveSemi - equivalent to YUICompressor --preserve-semi. Preserve all semicolons. Default false.
  disableOptimizations - equivalent to YUICompressor --disable-optimizations. Disable all micro optimizations. Default false.
  useCache - to cache the earlier minified contents and serve from cache. Default true.
  charset - to use specified charset
 

Dependency

The YUIMinFilter depends on servlet-api and YUICompressor jar to be in the classpath.

servlet-api.jar - Must be already present in your webapp classpath

yuicompressor-x.y.z.jar - Download and put appropriate version of this jar in your classpath (in WEB-INF/lib)

Limitations

As a best practice you should also add appropriate expires header on static resources so that browser caches them and doesn't request them again and again. You can use the JSCSSMergeServlet from webutilities.jar to add expires header on JS and CSS. It also helps combines multiple JS or CSS requests in one HTTP request. See JSCSSMergeServlet for details.

Visit http://code.google.com/p/webutilities/wiki/YUIMinFilter for more details.

Version:
1.0
Author:
rpatil

Field Summary
 
Fields inherited from class com.googlecode.webutilities.filters.common.AbstractFilter
filterConfig
 
Constructor Summary
YUIMinFilter()
           
 
Method Summary
 void doFilter(javax.servlet.ServletRequest req, javax.servlet.ServletResponse resp, javax.servlet.FilterChain chain)
           
 void init(javax.servlet.FilterConfig config)
           
 
Methods inherited from class com.googlecode.webutilities.filters.common.AbstractFilter
destroy, isMIMEAccepted, isURLAccepted, isUserAgentAccepted
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

YUIMinFilter

public YUIMinFilter()
Method Detail

doFilter

public void doFilter(javax.servlet.ServletRequest req,
                     javax.servlet.ServletResponse resp,
                     javax.servlet.FilterChain chain)
              throws IOException,
                     javax.servlet.ServletException
Throws:
IOException
javax.servlet.ServletException

init

public void init(javax.servlet.FilterConfig config)
          throws javax.servlet.ServletException
Specified by:
init in interface javax.servlet.Filter
Overrides:
init in class AbstractFilter
Throws:
javax.servlet.ServletException


Copyright © 2011. All Rights Reserved.