## org.mortbay.servlet
Class GzipFilter

    java.lang.Object
      org.mortbay.servlet.UserAgentFilter
          org.mortbay.servlet.GzipFilter

All Implemented Interfaces:
  Filter

Direct Known Subclasses:
  IncludableGzipFilter

    public class GzipFilter

extends UserAgentFilter

GZIP Filter This filter will gzip the content of a response iff:

- The filter is mapped to a matching path
- The response status code is >=200 and <300
- The content length is unknown or more than the `minGzipSize` initParameter or the minGzipSize is 0(default)
- The content-type is in the coma separated list of mimeTypes set in the `mimeTypes` initParameter or if no mimeTypes are defined the content-type is not "application/gzip"
- No content-encoding is specified by the resource

Compressing the content can greatly improve the network bandwidth usage, but at a cost of memory and CPU cycles. If this filter is mapped for static content, then use of efficient direct NIO may be prevented, thus use of the gzip mechanism of the `DefaultServlet` is advised instead.

This filter extends `UserAgentFilter` and if the the initParameter `excludedAgents` is set to a comma separated list of user agents, then these agents will be excluded from gzip content.

Author:
  gregw

Nested Class Summary
`class` `GzipFilter.GZIPResponseWrapper`
`static class` `GzipFilter.GzipStream`

Field Summary
`protected int` `_bufferSize`
`protected Set` `_excluded`
`protected Set` `_mimeTypes`
`protected int` `_minGzipSize`

Constructor Summary
`GzipFilter()`

Method Summary
`void` `destroy()` Called by the web container to indicate to a filter that it is being taken out of service.
`void` `doFilter(ServletRequest req, ServletResponse res, FilterChain chain)` The `doFilter` method of the Filter is called by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain.
`void` `init(FilterConfig filterConfig)` Called by the web container to indicate to a filter that it is being placed into service.
`protected GzipFilter.GZIPResponseWrapper` `newGZIPResponseWrapper(HttpServletRequest request, HttpServletResponse response)`

Methods inherited from class org.mortbay.servlet.UserAgentFilter
`getUserAgent, getUserAgent`

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Field Detail

### _mimeTypes

    protected Set _mimeTypes

### _bufferSize

    protected int _bufferSize

### _minGzipSize

    protected int _minGzipSize

### _excluded

    protected Set _excluded

Constructor Detail

### GzipFilter

    public GzipFilter()

Method Detail

### init

    public void init(FilterConfig filterConfig)
              throws ServletException

  Description copied from interface: `Filter`
  Called by the web container to indicate to a filter that it is being placed into service. The servlet container calls the init method exactly once after instantiating the filter. The init method must complete successfully before the filter is asked to do any filtering work.
  The web container cannot place the filter into service if the init method either
  1.Throws a ServletException
  2.Does not return within a time period defined by the web container

  Specified by:
    `init` in interface `Filter`
  Overrides:
    `init` in class `UserAgentFilter`
  Throws:
    `ServletException`

### destroy

    public void destroy()

  Description copied from interface: `Filter`
  Called by the web container to indicate to a filter that it is being taken out of service. This method is only called once all threads within the filter's doFilter method have exited or after a timeout period has passed. After the web container calls this method, it will not call the doFilter method again on this instance of the filter.
  This method gives the filter an opportunity to clean up any resources that are being held (for example, memory, file handles, threads) and make sure that any persistent state is synchronized with the filter's current state in memory.

  Specified by:
    `destroy` in interface `Filter`
  Overrides:
    `destroy` in class `UserAgentFilter`

### doFilter

    public void doFilter(ServletRequest req,
                         ServletResponse res,
                         FilterChain chain)
                  throws IOException,
                         ServletException

  Description copied from interface: `Filter`
  The `doFilter` method of the Filter is called by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain. The FilterChain passed in to this method allows the Filter to pass on the request and response to the next entity in the chain.

  A typical implementation of this method would follow the following pattern:-
  1. Examine the request
  2. Optionally wrap the request object with a custom implementation to filter content or headers for input filtering
  3. Optionally wrap the response object with a custom implementation to filter content or headers for output filtering
  4. a) Either invoke the next entity in the chain using the FilterChain object (`chain.doFilter()`),
  4. b) or not pass on the request/response pair to the next entity in the filter chain to block the request processing
  5. Directly set headers on the response after invocation of the next entity in the filter chain.

  Specified by:
    `doFilter` in interface `Filter`
  Overrides:
    `doFilter` in class `UserAgentFilter`
  Throws:
    `IOException`
    `ServletException`

### newGZIPResponseWrapper

    protected GzipFilter.GZIPResponseWrapper newGZIPResponseWrapper(HttpServletRequest request,
                                                                    HttpServletResponse response)

Copyright © 2008 Mort Bay Consulting. All Rights Reserved.