    001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_testing/src/org/deegree/enterprise/servlet/GZIPFilter.java $
    002    /*----------------------------------------------------------------------------
    003     This file originated as work from Jayson Falkner.
    004
    005     Copyright 2003 Jayson Falkner (jayson@jspinsider.com)
    006     This code is from "Servlets and JavaServer pages; the J2EE Web Tier",
    007     http://www.jspbook.com. You may freely use the code both commercially
    008     and non-commercially. If you like the code, please pick up a copy of
    009     the book and help support the authors, development of more free code,
    010     and the JSP/Servlet/J2EE community.
    011     ----------------------------------------------------------------------------*/
    012    package org.deegree.enterprise.servlet;
    013
    014    import java.io.IOException;
    015
    016    import javax.servlet.Filter;
    017    import javax.servlet.FilterChain;
    018    import javax.servlet.FilterConfig;
    019    import javax.servlet.ServletException;
    020    import javax.servlet.ServletRequest;
    021    import javax.servlet.ServletResponse;
    022    import javax.servlet.http.HttpServletRequest;
    023    import javax.servlet.http.HttpServletResponse;
    024
    025    import org.deegree.framework.log.ILogger;
    026    import org.deegree.framework.log.LoggerFactory;
    027
    028    /**
    029     * ServletFilter that enables compression of response content, if the client supports GZIP compression
    030     * (accept-encoding=gzip ...).
    031     *
    032     * @author <a href="mailto:jayson@jspinsider.com">Jayson Falkner</a>
    033     * @author last edited by: $Author: mschneider $
    034     *
    035     * @version $Revision: 20326 $, $Date: 2009-10-22 18:41:56 +0200 (Do, 22. Okt 2009) $
    036     */
    037    public class GZIPFilter implements Filter {
    038
    039        private static final ILogger LOG = LoggerFactory.getLogger( GZIPFilter.class );
    040
    041        /**
    042         * @param req
    043         * @param res
    044         * @param chain
    045         * @throws IOException
    046         * @throws ServletException
    047         */
    048        public void doFilter( ServletRequest req, ServletResponse res, FilterChain chain )
    049                                throws IOException, ServletException {
    050            if ( req instanceof HttpServletRequest ) {
    051                HttpServletRequest request = (HttpServletRequest) req;
    052                HttpServletResponse response = (HttpServletResponse) res;
    053                String ae = request.getHeader( "accept-encoding" );
    054                if ( ae != null && ae.indexOf( "gzip" ) != -1 ) {
    055                    LOG.logDebug( "GZIP supported, compressing." );
    056                    GZIPResponseWrapper wrappedResponse = new GZIPResponseWrapper( response );
    057                    chain.doFilter( req, wrappedResponse );
    058                    wrappedResponse.finishResponse();
    059                    return;
    060                }
    061                chain.doFilter( req, res );
    062            }
    063        }
    064
    065        /**
    066         * @param filterConfig
    067         */
    068        public void init( FilterConfig filterConfig ) {
    069            // noop
    070        }
    071
    072        /**
    073         *
    074         */
    075        public void destroy() {
    076            // noop
    077        }
    078    }