package org.realityforge.gwt.cache_filter;

import java.io.File;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Filter to redirect all files to their corresponding .gz file, if it exists, and serve
 * it with the proper encoding / content type. Works best if called after GWTCacheControlFilter
 */
@WebFilter( filterName = "GWTGzipControlFilter" )
public class GWTGzipControlFilter
  extends AbstractFilter
{
  private static String gzExt = ".gz";

  public static void setGzipExtension( String newExt )
  {
    if ( newExt != null )
    {
      gzExt = newExt;
    }
  }

  @Override
  public void doFilter( final ServletRequest servletRequest,
                        final ServletResponse servletResponse,
                        final FilterChain filterChain )
    throws IOException, ServletException
  {
    final HttpServletRequest request = (HttpServletRequest) servletRequest;
    final HttpServletResponse response = (HttpServletResponse) servletResponse;

    if ( !acceptsGzip( request ) )
    {
      filterChain.doFilter( request, response );
    }
    else
    {
      final String resourcePath = request.getServletPath();
      final String realPath = request.getServletContext().getRealPath( resourcePath );
      final File file = null == realPath ? null : new File( realPath );
      if ( null == file || !file.exists() || file.isDirectory() )
      {
        filterChain.doFilter( request, response );
      }
      else if ( !resourcePath.endsWith( gzExt ) )
      {
        final String gzippedPath = realPath + gzExt;
        final File gzippedFile = new File( gzippedPath );

        if ( !gzippedFile.exists() || gzippedFile.isDirectory() )
        {
          filterChain.doFilter( request, servletResponse );
        }
        else
        {
          final RequestDispatcher dispatcher =
            request.getServletContext().getRequestDispatcher( resourcePath + gzExt );
          response.setHeader( "Content-Encoding", "gzip" );
          dispatcher.include( request, response );
        }
      }
    }
  }

  public static boolean acceptsGzip( final HttpServletRequest request )
  {
    final String header = request.getHeader( "Accept-Encoding" );
    return null != header && header.contains( "gzip" );
  }
}

@@ -736,153 +736,44 @@
 tic 
-String gzExt = %22.gz%22;%0A%0A  public static void setGzipExtension( String newExt )%0A  %7B%0A    if ( newExt != null )%0A    %7B%0A      gzExt = newExt;%0A    %7D%0A  %7D
+final String GZIP_EXTENSION = %22.gz%22;
 %0A%0A  
@@ -1520,16 +1520,81 @@
  file %7C%7C
+%0A           resourcePath.endsWith( GZIP_EXTENSION ) %7C%7C%0A          
  !file.e
@@ -1603,16 +1603,27 @@
 sts() %7C%7C
+%0A          
  file.is
@@ -1717,47 +1717,8 @@
 else
- if ( !resourcePath.endsWith( gzExt ) )
 %0A   
@@ -1768,21 +1768,30 @@
 lPath + 
-gzExt
+GZIP_EXTENSION
 ;%0A      
@@ -2140,13 +2140,22 @@
 h + 
-gzExt
+GZIP_EXTENSION
  );%0A

Rework to assume gzip extension is constant