447515 Remove GzipFilter

misc cleanups in preparation for removal
This commit is contained in:
Greg Wilkins 2014-10-17 15:52:11 +11:00
parent ba7f7589cb
commit 062a5c4113
23 changed files with 86 additions and 144 deletions

View File

@ -109,7 +109,7 @@ public class HttpOutput extends ServletOutputStream implements Runnable
return _filter;
}
public void setFilter(Interceptor filter)
public void setInterceptor(Interceptor filter)
{
_filter=filter;
}

View File

@ -21,7 +21,6 @@ package org.eclipse.jetty.servlets;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.HttpChannel;
import org.eclipse.jetty.server.Request;
/* ------------------------------------------------------------ */

View File

@ -36,8 +36,6 @@ import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.HttpField;
import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;

View File

@ -37,7 +37,6 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.HttpOutput;
import org.eclipse.jetty.util.BufferUtil;
/**
* A servlet that uses the Servlet 3.1 asynchronous IO API to server

View File

@ -31,6 +31,7 @@ import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.AsyncContext;
import javax.servlet.AsyncEvent;
import javax.servlet.AsyncListener;

View File

@ -36,12 +36,12 @@ import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.PreEncodedHttpField;
import org.eclipse.jetty.http.HttpField;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.http.MimeTypes;
import org.eclipse.jetty.http.PreEncodedHttpField;
import org.eclipse.jetty.server.HttpChannel;
import org.eclipse.jetty.server.HttpOutput;
import org.eclipse.jetty.server.Request;
@ -120,6 +120,7 @@ import org.eclipse.jetty.util.log.Logger;
* </dd>
* </dl>
*/
@Deprecated
public class GzipFilter extends UserAgentFilter implements GzipFactory
{
private static final Logger LOG = Log.getLogger(GzipFilter.class);
@ -147,7 +148,7 @@ public class GzipFilter extends UserAgentFilter implements GzipFactory
protected Set<Pattern> _excludedAgentPatterns;
protected Set<String> _excludedPaths;
protected Set<Pattern> _excludedPathPatterns;
protected HttpField _vary=new PreEncodedHttpField(HttpHeader.VARY,HttpHeader.ACCEPT_ENCODING+", "+HttpHeader.USER_AGENT);
protected HttpField _vary=GzipHttpOutputInterceptor.VARY;
/* ------------------------------------------------------------ */
/**
@ -361,7 +362,7 @@ public class GzipFilter extends UserAgentFilter implements GzipFactory
HttpChannel channel = HttpChannel.getCurrentHttpChannel();
HttpOutput out = channel.getResponse().getHttpOutput();
// TODO recycle the GzipHttpOutputFilter
out.setFilter(new GzipHttpOutputInterceptor(this,channel,out.getFilter()));
out.setInterceptor(new GzipHttpOutputInterceptor(this,_vary,channel,out.getFilter()));
super.doFilter(request,response,chain);
@ -435,12 +436,6 @@ public class GzipFilter extends UserAgentFilter implements GzipFactory
return false;
}
@Override
public HttpField getVaryField()
{
return _vary;
}
@Override
public Deflater getDeflater(Request request, long content_length)
{
@ -500,11 +495,6 @@ public class GzipFilter extends UserAgentFilter implements GzipFactory
return _mimeTypes.contains(mimetype) == _excludeMimeTypes;
}
@Override
public int getBufferSize()
{
return _bufferSize;
}
}

View File

@ -23,6 +23,7 @@ import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import javax.servlet.AsyncContext;
import javax.servlet.AsyncEvent;
import javax.servlet.AsyncListener;

View File

@ -53,6 +53,7 @@ import javax.servlet.http.HttpServletRequest;
*
*
*/
@Deprecated
public class UserAgentFilter implements Filter
{
private static final String __defaultPattern = "(?:Mozilla[^\\(]*\\(compatible;\\s*+([^;]*);.*)|(?:.*?([^\\s]+/[^\\s]+).*)";

View File

@ -20,19 +20,13 @@ package org.eclipse.jetty.servlets.gzip;
import java.util.zip.Deflater;
import org.eclipse.jetty.http.HttpField;
import org.eclipse.jetty.server.Request;
public interface GzipFactory
{
int getBufferSize();
HttpField getVaryField();
Deflater getDeflater(Request request, long content_length);
boolean isExcludedMimeType(String asciiToLowerCase);
boolean isExcludedMimeType(String mimetype);
void recycle(Deflater deflater);
}

View File

@ -20,28 +20,15 @@ package org.eclipse.jetty.servlets.gzip;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.regex.Pattern;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.GZIPOutputStream;
import javax.servlet.AsyncEvent;
import javax.servlet.AsyncListener;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@ -55,7 +42,7 @@ import org.eclipse.jetty.server.HttpChannel;
import org.eclipse.jetty.server.HttpOutput;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.HandlerWrapper;
import org.eclipse.jetty.servlets.GzipFilter;
import org.eclipse.jetty.util.ConcurrentHashSet;
import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
@ -71,28 +58,24 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
public final static String ETAG = "o.e.j.s.GzipFilter.ETag";
public final static int DEFAULT_MIN_GZIP_SIZE=16;
protected final Set<String> _mimeTypes=new HashSet<>();
protected boolean _excludeMimeTypes;
protected int _bufferSize=32*1024;
protected int _minGzipSize=DEFAULT_MIN_GZIP_SIZE;
protected int _deflateCompressionLevel=Deflater.DEFAULT_COMPRESSION;
protected boolean _checkGzExists = true;
private final Set<String> _mimeTypes=new HashSet<>();
private boolean _excludeMimeTypes;
private int _minGzipSize=DEFAULT_MIN_GZIP_SIZE;
private int _deflateCompressionLevel=Deflater.DEFAULT_COMPRESSION;
private boolean _checkGzExists = true;
// non-static, as other GzipFilter instances may have different configurations
protected final ThreadLocal<Deflater> _deflater = new ThreadLocal<Deflater>();
private final ThreadLocal<Deflater> _deflater = new ThreadLocal<Deflater>();
protected final static ThreadLocal<byte[]> _buffer= new ThreadLocal<byte[]>();
private final MimeTypes _knownMimeTypes= new MimeTypes();
private final Set<String> _methods=new HashSet<>();
private final Set<Pattern> _excludedAgentPatterns=new HashSet<>();
private final Set<Pattern> _excludedPathPatterns=new HashSet<>();
private HttpField _vary=GzipHttpOutputInterceptor.VARY;
private final Set<String> _uaCache = new ConcurrentHashSet<>();
private int _uaCacheSize = 1024;
protected final MimeTypes _knownMimeTypes= new MimeTypes();
protected final Set<String> _methods=new HashSet<>();
protected final Set<String> _excludedAgents=new HashSet<>();
protected final Set<Pattern> _excludedAgentPatterns=new HashSet<>();
protected final Set<String> _excludedPaths=new HashSet<>();
protected final Set<Pattern> _excludedPathPatterns=new HashSet<>();
protected HttpField _vary=new PreEncodedHttpField(HttpHeader.VARY,HttpHeader.ACCEPT_ENCODING+", "+HttpHeader.USER_AGENT);
/* ------------------------------------------------------------ */
/**
* Instantiates a new gzip handler.
@ -113,13 +96,6 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
}
}
@Override
public int getBufferSize()
{
return _bufferSize;
}
@Override
public Deflater getDeflater(Request request, long content_length)
{
@ -175,13 +151,6 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
return _minGzipSize;
}
/* ------------------------------------------------------------ */
@Override
public HttpField getVaryField()
{
return _vary;
}
/* ------------------------------------------------------------ */
/**
* @see org.eclipse.jetty.server.handler.HandlerWrapper#handle(java.lang.String, org.eclipse.jetty.server.Request, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
@ -259,9 +228,8 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
}
HttpChannel channel = HttpChannel.getCurrentHttpChannel();
HttpOutput out = channel.getResponse().getHttpOutput();
// TODO recycle the GzipHttpOutputFilter
out.setFilter(new GzipHttpOutputInterceptor(this,channel,out.getFilter()));
HttpOutput out = channel.getResponse().getHttpOutput();
out.setInterceptor(new GzipHttpOutputInterceptor(this,_vary,channel,out.getFilter()));
_handler.handle(target,baseRequest, request, response);
@ -313,19 +281,20 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
if (ua == null)
return false;
if (_excludedAgents != null)
{
if (_excludedAgents.contains(ua))
{
return true;
}
}
if (_excludedAgentPatterns != null)
{
if (_uaCache.contains(ua))
return true;
for (Pattern pattern : _excludedAgentPatterns)
{
if (pattern.matcher(ua).matches())
{
if (_uaCache.size()>_uaCacheSize)
_uaCache.clear();
_uaCache.add(ua);
return true;
}
}
@ -351,16 +320,7 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
{
if (requestURI == null)
return false;
if (_excludedPaths != null)
{
for (String excludedPath : _excludedPaths)
{
if (requestURI.startsWith(excludedPath))
{
return true;
}
}
}
if (_excludedPathPatterns != null)
{
for (Pattern pattern : _excludedPathPatterns)
@ -383,18 +343,6 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
}
/* ------------------------------------------------------------ */
/**
* Set the buffer size.
*
* @param bufferSize
* buffer size to set
*/
public void setBufferSize(int bufferSize)
{
_bufferSize = bufferSize;
}
/* ------------------------------------------------------------ */
/**
* Set the mime types.
@ -420,17 +368,6 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
return _excludeMimeTypes;
}
public String[] getExcludedPaths()
{
return _excludedPaths.toArray(new String[_excludedPaths.size()]);
}
public void setExcludedPaths(String[] paths)
{
_excludedPaths.clear();
_excludedPaths.addAll(Arrays.asList(paths));
}
public String[] getExcludedPathPatterns()
{
Pattern[] ps = _excludedPathPatterns.toArray(new Pattern[_excludedPathPatterns.size()]);
@ -449,17 +386,6 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
_excludedPathPatterns.add(Pattern.compile(s));
}
public String[] getExcludedAgents()
{
return _excludedAgents.toArray(new String[_excludedAgents.size()]);
}
public void setExcludedAgents(String[] paths)
{
_excludedAgents.clear();
_excludedAgents.addAll(Arrays.asList(paths));
}
public String[] getExcludedAgentPatterns()
{
Pattern[] ps = _excludedAgentPatterns.toArray(new Pattern[_excludedAgentPatterns.size()]);

View File

@ -24,6 +24,7 @@ import java.util.concurrent.atomic.AtomicReference;
import java.util.zip.CRC32;
import java.util.zip.Deflater;
import org.eclipse.jetty.http.HttpField;
import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.MimeTypes;
@ -43,23 +44,39 @@ public class GzipHttpOutputInterceptor implements HttpOutput.Interceptor
public static Logger LOG = Log.getLogger(GzipHttpOutputInterceptor.class);
private final static PreEncodedHttpField CONTENT_ENCODING_GZIP=new PreEncodedHttpField(HttpHeader.CONTENT_ENCODING,"gzip");
private final static byte[] GZIP_HEADER = new byte[] { (byte)0x1f, (byte)0x8b, Deflater.DEFLATED, 0, 0, 0, 0, 0, 0, 0 };
public final static HttpField VARY=new PreEncodedHttpField(HttpHeader.VARY,HttpHeader.ACCEPT_ENCODING+", "+HttpHeader.USER_AGENT);
private enum GZState { MIGHT_COMPRESS, NOT_COMPRESSING, COMMITTING, COMPRESSING, FINISHED};
private final AtomicReference<GZState> _state = new AtomicReference<>(GZState.MIGHT_COMPRESS);
private final CRC32 _crc = new CRC32();
private final GzipFactory _factory;
private final HttpOutput.Interceptor _filter;
private final HttpOutput.Interceptor _interceptor;
private final HttpChannel _channel;
private final HttpField _vary;
private final int _bufferSize;
private Deflater _deflater;
private ByteBuffer _buffer;
public GzipHttpOutputInterceptor(GzipFactory factory, HttpChannel channel, HttpOutput.Interceptor next)
{
this(factory,VARY,channel.getHttpConfiguration().getOutputBufferSize(),channel,next);
}
public GzipHttpOutputInterceptor(GzipFactory factory, HttpChannel channel, HttpOutput.Interceptor filter)
public GzipHttpOutputInterceptor(GzipFactory factory, HttpField vary, HttpChannel channel, HttpOutput.Interceptor next)
{
this(factory,vary,channel.getHttpConfiguration().getOutputBufferSize(),channel,next);
}
public GzipHttpOutputInterceptor(GzipFactory factory, HttpField vary, int bufferSize, HttpChannel channel, HttpOutput.Interceptor next)
{
_factory=factory;
_channel=channel;
_filter=filter;
_interceptor=next;
_vary=vary;
_bufferSize=bufferSize;
}
@Override
@ -72,7 +89,7 @@ public class GzipHttpOutputInterceptor implements HttpOutput.Interceptor
break;
case NOT_COMPRESSING:
_filter.write(content, complete, callback);
_interceptor.write(content, complete, callback);
return;
case COMMITTING:
@ -129,7 +146,7 @@ public class GzipHttpOutputInterceptor implements HttpOutput.Interceptor
{
LOG.debug("{} exclude by status {}",this,sc);
noCompression();
_filter.write(content, complete, callback);
_interceptor.write(content, complete, callback);
return;
}
@ -142,7 +159,7 @@ public class GzipHttpOutputInterceptor implements HttpOutput.Interceptor
{
LOG.debug("{} exclude by mimeType {}",this,ct);
noCompression();
_filter.write(content, complete, callback);
_interceptor.write(content, complete, callback);
return;
}
}
@ -153,7 +170,7 @@ public class GzipHttpOutputInterceptor implements HttpOutput.Interceptor
{
LOG.debug("{} exclude by content-encoding {}",this,ce);
noCompression();
_filter.write(content, complete, callback);
_interceptor.write(content, complete, callback);
return;
}
@ -162,7 +179,7 @@ public class GzipHttpOutputInterceptor implements HttpOutput.Interceptor
{
// We are varying the response due to accept encoding header.
HttpFields fields = _channel.getResponse().getHttpFields();
fields.add(_factory.getVaryField());
fields.add(_vary);
long content_length = _channel.getResponse().getContentLength();
if (content_length<0 && complete)
@ -174,13 +191,13 @@ public class GzipHttpOutputInterceptor implements HttpOutput.Interceptor
{
LOG.debug("{} exclude no deflater",this);
_state.set(GZState.NOT_COMPRESSING);
_filter.write(content, complete, callback);
_interceptor.write(content, complete, callback);
return;
}
fields.put(CONTENT_ENCODING_GZIP);
_crc.reset();
_buffer=_channel.getByteBufferPool().acquire(_factory.getBufferSize(),false);
_buffer=_channel.getByteBufferPool().acquire(_bufferSize,false);
BufferUtil.fill(_buffer,GZIP_HEADER,0,GZIP_HEADER.length);
// Adjust headers
@ -286,7 +303,7 @@ public class GzipHttpOutputInterceptor implements HttpOutput.Interceptor
if (complete)
addTrailer();
_filter.write(_buffer,complete,this);
_interceptor.write(_buffer,complete,this);
return Action.SCHEDULED;
}
}
@ -299,7 +316,7 @@ public class GzipHttpOutputInterceptor implements HttpOutput.Interceptor
public GzipBufferCB(ByteBuffer content, boolean complete, Callback callback)
{
super(callback);
_input=_channel.getByteBufferPool().acquire(Math.min(_factory.getBufferSize(),content.remaining()),false);
_input=_channel.getByteBufferPool().acquire(Math.min(_bufferSize,content.remaining()),false);
_content=content;
_last=complete;
}
@ -357,7 +374,7 @@ public class GzipHttpOutputInterceptor implements HttpOutput.Interceptor
if (finished)
addTrailer();
_filter.write(_buffer,finished,this);
_interceptor.write(_buffer,finished,this);
return Action.SCHEDULED;
}
}

View File

@ -18,8 +18,12 @@
package org.eclipse.jetty.servlets;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@ -30,9 +34,6 @@ import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class DoSFilterTest extends AbstractDoSFilterTest
{
private static final Logger LOG = Log.getLogger(DoSFilterTest.class);

View File

@ -25,6 +25,7 @@ import java.util.List;
import javax.servlet.Filter;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlets.gzip.GzipTester;
import org.eclipse.jetty.servlets.gzip.TestStaticMimeTypeServlet;

View File

@ -19,8 +19,6 @@
package org.eclipse.jetty.servlets;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import javax.servlet.Filter;
import javax.servlet.ServletException;

View File

@ -23,6 +23,7 @@ import java.net.URL;
import java.util.EnumSet;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import javax.servlet.DispatcherType;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;

View File

@ -27,6 +27,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.HttpOutput;
import org.eclipse.jetty.servlets.GzipFilter;
/**
* A sample servlet to serve static content, using a order of construction that has caused problems for

View File

@ -25,6 +25,8 @@ import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.servlets.GzipFilter;
/**
* A sample servlet to serve static content, using a order of construction that has caused problems for
* {@link GzipFilter} in the past.

View File

@ -25,6 +25,8 @@ import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.servlets.GzipFilter;
/**
* A sample servlet to serve static content, using a order of construction that has caused problems for
* {@link GzipFilter} in the past.

View File

@ -25,6 +25,8 @@ import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.servlets.GzipFilter;
/**
* A sample servlet to serve static content, using a order of construction that has caused problems for
* {@link GzipFilter} in the past.

View File

@ -25,6 +25,8 @@ import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.servlets.GzipFilter;
/**
* A sample servlet to serve static content, using a order of construction that has caused problems for
* {@link GzipFilter} in the past.

View File

@ -25,6 +25,8 @@ import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.servlets.GzipFilter;
/**
* A sample servlet to serve static content, using a order of construction that has caused problems for
* {@link GzipFilter} in the past.

View File

@ -25,6 +25,8 @@ import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.servlets.GzipFilter;
/**
* A sample servlet to serve static content, using a order of construction that has caused problems for
* {@link GzipFilter} in the past.

View File

@ -25,6 +25,8 @@ import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.servlets.GzipFilter;
/**
* A sample servlet to serve static content, using a order of construction that has caused problems for
* {@link GzipFilter} in the past.