@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {

		final String acceptEncoding;
		if (request instanceof HttpServletRequest) {
			final HttpServletRequest req = (HttpServletRequest)request;

			final String contentEncoding = req.getHeader("Content-Encoding");
			acceptEncoding = req.getHeader("Accept-Encoding");

			if (contentEncoding != null && contentEncoding.toLowerCase().indexOf("gzip") > -1 && this.decompressRequest)
				request = new GZIPRequestWrapper(req);
		} else
			acceptEncoding = null;

		if (response instanceof HttpServletResponse) {
			if (acceptEncoding != null && acceptEncoding.toLowerCase().indexOf("gzip") > -1 && this.compressResponse)
				response = new GZIPResponseWrapper((HttpServletResponse)response);
		}

		chain.doFilter(request, response);

		if (response instanceof GZIPResponseWrapper)
			((GZIPResponseWrapper)response).finish();
	}