javawebparts.filter
Class JSCompressionFilter

java.lang.Object
  |
  +--javawebparts.filter.JSCompressionFilter
All Implemented Interfaces:
javax.servlet.Filter

public class JSCompressionFilter
extends java.lang.Object
implements javax.servlet.Filter

This filter does some simple javascript compressing in the response. It uses a slightly modified version of JSMin.java from John Reilly which, in it's turn, is a java version of Douglas Crockford's jsmin.c. If you want to know how it works and what is does: www.crockford.com It allows for including or excluding paths from filter functionality.

Init parameters are:


Example configuration in web.xml:

<filter>
  <filter-name>JSCompressionFilter</filter-name>
  <filter-class>javawebparts.filter. JSCompressionFilter</filter-class>
  <init-param>
    <param-name>pathSpec</param-name>
    <param-value>include</param-value>
  </init-param>
  <init-param>
    <param-name>pathList</param-name>
    <param-value>*‍/smalljavascript.js</ param-value>
  </init-param>
</filter>

<filter-mapping>
  <filter-name>JSCompressionFilter</filter-name>
  <url-pattern>/*.js</url-pattern>
</filter-mapping>

Author:
Herman van Rosmalen .

Field Summary
private static org.apache.commons.logging.Log log
          Log instance.
private static java.util.ArrayList pathList
          List of paths for filter functionality determination.
private static java.lang.String pathSpec
          Whether pathList includes or excludes.
 
Constructor Summary
JSCompressionFilter()
           
 
Method Summary
private  byte[] compress(byte[] bytes, boolean isHtml)
          Do compression.
 void destroy()
          Destroy.
 void doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain filterChain)
          Do filter's work.
 void init(javax.servlet.FilterConfig filterConfig)
          Initialize this filter.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

private static org.apache.commons.logging.Log log
Log instance.


pathSpec

private static java.lang.String pathSpec
Whether pathList includes or excludes.


pathList

private static java.util.ArrayList pathList
List of paths for filter functionality determination.

Constructor Detail

JSCompressionFilter

public JSCompressionFilter()
Method Detail

destroy

public void destroy()
Destroy.

Specified by:
destroy in interface javax.servlet.Filter

init

public void init(javax.servlet.FilterConfig filterConfig)
          throws javax.servlet.ServletException
Initialize this filter.

Specified by:
init in interface javax.servlet.Filter
Parameters:
filterConfig - The configuration information for this filter.
Throws:
javax.servlet.ServletException - ServletException.

doFilter

public void doFilter(javax.servlet.ServletRequest request,
                     javax.servlet.ServletResponse response,
                     javax.servlet.FilterChain filterChain)
              throws javax.servlet.ServletException,
                     java.io.IOException
Do filter's work.

Specified by:
doFilter in interface javax.servlet.Filter
Parameters:
request - The current request object.
response - The current response object.
filterChain - The current filter chain.
Throws:
javax.servlet.ServletException - ServletException.
java.io.IOException - IOException.

compress

private byte[] compress(byte[] bytes,
                        boolean isHtml)
Do compression.

Parameters:
bytes - The text to check
isHtml - Are we compressing Html (or a .js file)
Returns:
byte[] The compressed result


Copyright © 2005 Frank W. Zammetti