Code cleanup and JavaDocs fixes.

This commit is contained in:
Simone Bordet 2017-03-29 16:04:43 +02:00
parent 89e51aadbe
commit 7deb281aa0
1 changed files with 24 additions and 84 deletions

View File

@ -59,31 +59,25 @@ import org.eclipse.jetty.util.log.Logger;
*/ */
public class GzipHandler extends HandlerWrapper implements GzipFactory public class GzipHandler extends HandlerWrapper implements GzipFactory
{ {
public static final String GZIP = "gzip";
public static final String DEFLATE = "deflate";
public static final int DEFAULT_MIN_GZIP_SIZE=16;
private static final Logger LOG = Log.getLogger(GzipHandler.class); private static final Logger LOG = Log.getLogger(GzipHandler.class);
public final static String GZIP = "gzip";
public final static String DEFLATE = "deflate";
public final static int DEFAULT_MIN_GZIP_SIZE=16;
private int _minGzipSize=DEFAULT_MIN_GZIP_SIZE; private int _minGzipSize=DEFAULT_MIN_GZIP_SIZE;
private int _compressionLevel=Deflater.DEFAULT_COMPRESSION; private int _compressionLevel=Deflater.DEFAULT_COMPRESSION;
private boolean _checkGzExists = true; private boolean _checkGzExists = true;
private boolean _syncFlush = false; private boolean _syncFlush = false;
private int _inflateBufferSize = -1; private int _inflateBufferSize = -1;
private EnumSet<DispatcherType> _dispatchers = EnumSet.of(DispatcherType.REQUEST); private EnumSet<DispatcherType> _dispatchers = EnumSet.of(DispatcherType.REQUEST);
// non-static, as other GzipHandler instances may have different configurations // non-static, as other GzipHandler instances may have different configurations
private final ThreadLocal<Deflater> _deflater = new ThreadLocal<>(); private final ThreadLocal<Deflater> _deflater = new ThreadLocal<>();
private final IncludeExclude<String> _agentPatterns=new IncludeExclude<>(RegexSet.class); private final IncludeExclude<String> _agentPatterns=new IncludeExclude<>(RegexSet.class);
private final IncludeExclude<String> _methods = new IncludeExclude<>(); private final IncludeExclude<String> _methods = new IncludeExclude<>();
private final IncludeExclude<String> _paths = new IncludeExclude<>(PathSpecSet.class); private final IncludeExclude<String> _paths = new IncludeExclude<>(PathSpecSet.class);
private final IncludeExclude<String> _mimeTypes = new IncludeExclude<>(); private final IncludeExclude<String> _mimeTypes = new IncludeExclude<>();
private HttpField _vary; private HttpField _vary;
/* ------------------------------------------------------------ */
/** /**
* Instantiates a new gzip handler. * Instantiates a new gzip handler.
* The excluded Mime Types are initialized to common known * The excluded Mime Types are initialized to common known
@ -110,12 +104,13 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
_mimeTypes.exclude("application/brotli"); _mimeTypes.exclude("application/brotli");
_mimeTypes.exclude("application/x-xz"); _mimeTypes.exclude("application/x-xz");
_mimeTypes.exclude("application/x-rar-compressed"); _mimeTypes.exclude("application/x-rar-compressed");
if (LOG.isDebugEnabled())
LOG.debug("{} mime types {}",this,_mimeTypes); LOG.debug("{} mime types {}",this,_mimeTypes);
_agentPatterns.exclude(".*MSIE 6.0.*"); _agentPatterns.exclude(".*MSIE 6.0.*");
} }
/* ------------------------------------------------------------ */
/** /**
* @param patterns Regular expressions matching user agents to exclude * @param patterns Regular expressions matching user agents to exclude
*/ */
@ -124,7 +119,6 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
_agentPatterns.exclude(patterns); _agentPatterns.exclude(patterns);
} }
/* ------------------------------------------------------------ */
/** /**
* @param methods The methods to exclude in compression * @param methods The methods to exclude in compression
*/ */
@ -134,28 +128,24 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
_methods.exclude(m); _methods.exclude(m);
} }
/* ------------------------------------------------------------ */
public EnumSet<DispatcherType> getDispatcherTypes() public EnumSet<DispatcherType> getDispatcherTypes()
{ {
return _dispatchers; return _dispatchers;
} }
/* ------------------------------------------------------------ */
public void setDispatcherTypes(EnumSet<DispatcherType> dispatchers) public void setDispatcherTypes(EnumSet<DispatcherType> dispatchers)
{ {
_dispatchers = dispatchers; _dispatchers = dispatchers;
} }
/* ------------------------------------------------------------ */
public void setDispatcherTypes(DispatcherType... dispatchers) public void setDispatcherTypes(DispatcherType... dispatchers)
{ {
_dispatchers = EnumSet.copyOf(Arrays.asList(dispatchers)); _dispatchers = EnumSet.copyOf(Arrays.asList(dispatchers));
} }
/* ------------------------------------------------------------ */
/** /**
* Set the mime types. * Adds mime types to the excluded list.
*
* @param types The mime types to exclude (without charset or other parameters). * @param types The mime types to exclude (without charset or other parameters).
* For backward compatibility the mimetypes may be comma separated strings, but this * For backward compatibility the mimetypes may be comma separated strings, but this
* will not be supported in future versions. * will not be supported in future versions.
@ -166,9 +156,8 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
_mimeTypes.exclude(StringUtil.csvSplit(t)); _mimeTypes.exclude(StringUtil.csvSplit(t));
} }
/* ------------------------------------------------------------ */
/** /**
* Add path to excluded paths list. * Add paths to excluded paths list.
* <p> * <p>
* There are 2 syntaxes supported, Servlet <code>url-pattern</code> based, and * There are 2 syntaxes supported, Servlet <code>url-pattern</code> based, and
* Regex based. This means that the initial characters on the path spec * Regex based. This means that the initial characters on the path spec
@ -198,16 +187,14 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
_paths.exclude(StringUtil.csvSplit(p)); _paths.exclude(StringUtil.csvSplit(p));
} }
/* ------------------------------------------------------------ */
/** /**
* @param patterns Regular expressions matching user agents to exclude * @param patterns Regular expressions matching user agents to include
*/ */
public void addIncludedAgentPatterns(String... patterns) public void addIncludedAgentPatterns(String... patterns)
{ {
_agentPatterns.include(patterns); _agentPatterns.include(patterns);
} }
/* ------------------------------------------------------------ */
/** /**
* @param methods The methods to include in compression * @param methods The methods to include in compression
*/ */
@ -217,7 +204,6 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
_methods.include(m); _methods.include(m);
} }
/* ------------------------------------------------------------ */
/** /**
* @return True if {@link Deflater#SYNC_FLUSH} is used, else {@link Deflater#NO_FLUSH} * @return True if {@link Deflater#SYNC_FLUSH} is used, else {@link Deflater#NO_FLUSH}
*/ */
@ -226,7 +212,6 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
return _syncFlush; return _syncFlush;
} }
/* ------------------------------------------------------------ */
/** /**
* <p>Set the {@link Deflater} flush mode to use. {@link Deflater#SYNC_FLUSH} * <p>Set the {@link Deflater} flush mode to use. {@link Deflater#SYNC_FLUSH}
* should be used if the application wishes to stream the data, but this may * should be used if the application wishes to stream the data, but this may
@ -238,7 +223,6 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
_syncFlush = syncFlush; _syncFlush = syncFlush;
} }
/* ------------------------------------------------------------ */
/** /**
* Add included mime types. Inclusion takes precedence over * Add included mime types. Inclusion takes precedence over
* exclusion. * exclusion.
@ -252,9 +236,8 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
_mimeTypes.include(StringUtil.csvSplit(t)); _mimeTypes.include(StringUtil.csvSplit(t));
} }
/* ------------------------------------------------------------ */
/** /**
* Add path specs to include. * Adds paths specs to the included list.
* <p> * <p>
* There are 2 syntaxes supported, Servlet <code>url-pattern</code> based, and * There are 2 syntaxes supported, Servlet <code>url-pattern</code> based, and
* Regex based. This means that the initial characters on the path spec * Regex based. This means that the initial characters on the path spec
@ -270,7 +253,7 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
* <li>All other syntaxes are unsupported</li> * <li>All other syntaxes are unsupported</li>
* </ul> * </ul>
* <p> * <p>
* Note: inclusion takes precedence over exclude. * Note: inclusion takes precedence over exclusion.
* *
* @param pathspecs Path specs (as per servlet spec) to include. If a * @param pathspecs Path specs (as per servlet spec) to include. If a
* ServletContext is available, the paths are relative to the context path, * ServletContext is available, the paths are relative to the context path,
@ -282,7 +265,6 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
_paths.include(StringUtil.csvSplit(p)); _paths.include(StringUtil.csvSplit(p));
} }
/* ------------------------------------------------------------ */
@Override @Override
protected void doStart() throws Exception protected void doStart() throws Exception
{ {
@ -290,19 +272,16 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
super.doStart(); super.doStart();
} }
/* ------------------------------------------------------------ */
public boolean getCheckGzExists() public boolean getCheckGzExists()
{ {
return _checkGzExists; return _checkGzExists;
} }
/* ------------------------------------------------------------ */
public int getCompressionLevel() public int getCompressionLevel()
{ {
return _compressionLevel; return _compressionLevel;
} }
/* ------------------------------------------------------------ */
@Override @Override
public Deflater getDeflater(Request request, long content_length) public Deflater getDeflater(Request request, long content_length)
{ {
@ -344,87 +323,73 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
return df; return df;
} }
/* ------------------------------------------------------------ */
public String[] getExcludedAgentPatterns() public String[] getExcludedAgentPatterns()
{ {
Set<String> excluded=_agentPatterns.getExcluded(); Set<String> excluded=_agentPatterns.getExcluded();
return excluded.toArray(new String[excluded.size()]); return excluded.toArray(new String[excluded.size()]);
} }
/* ------------------------------------------------------------ */
public String[] getExcludedMethods() public String[] getExcludedMethods()
{ {
Set<String> excluded=_methods.getExcluded(); Set<String> excluded=_methods.getExcluded();
return excluded.toArray(new String[excluded.size()]); return excluded.toArray(new String[excluded.size()]);
} }
/* ------------------------------------------------------------ */
public String[] getExcludedMimeTypes() public String[] getExcludedMimeTypes()
{ {
Set<String> excluded=_mimeTypes.getExcluded(); Set<String> excluded=_mimeTypes.getExcluded();
return excluded.toArray(new String[excluded.size()]); return excluded.toArray(new String[excluded.size()]);
} }
/* ------------------------------------------------------------ */
public String[] getExcludedPaths() public String[] getExcludedPaths()
{ {
Set<String> excluded=_paths.getExcluded(); Set<String> excluded=_paths.getExcluded();
return excluded.toArray(new String[excluded.size()]); return excluded.toArray(new String[excluded.size()]);
} }
/* ------------------------------------------------------------ */
public String[] getIncludedAgentPatterns() public String[] getIncludedAgentPatterns()
{ {
Set<String> includes=_agentPatterns.getIncluded(); Set<String> includes=_agentPatterns.getIncluded();
return includes.toArray(new String[includes.size()]); return includes.toArray(new String[includes.size()]);
} }
/* ------------------------------------------------------------ */
public String[] getIncludedMethods() public String[] getIncludedMethods()
{ {
Set<String> includes=_methods.getIncluded(); Set<String> includes=_methods.getIncluded();
return includes.toArray(new String[includes.size()]); return includes.toArray(new String[includes.size()]);
} }
/* ------------------------------------------------------------ */
public String[] getIncludedMimeTypes() public String[] getIncludedMimeTypes()
{ {
Set<String> includes=_mimeTypes.getIncluded(); Set<String> includes=_mimeTypes.getIncluded();
return includes.toArray(new String[includes.size()]); return includes.toArray(new String[includes.size()]);
} }
/* ------------------------------------------------------------ */
public String[] getIncludedPaths() public String[] getIncludedPaths()
{ {
Set<String> includes=_paths.getIncluded(); Set<String> includes=_paths.getIncluded();
return includes.toArray(new String[includes.size()]); return includes.toArray(new String[includes.size()]);
} }
/* ------------------------------------------------------------ */
@Deprecated @Deprecated
public String[] getMethods() public String[] getMethods()
{ {
return getIncludedMethods(); return getIncludedMethods();
} }
/* ------------------------------------------------------------ */
/** /**
* Get the minimum response size. * @return minimum response size that triggers compression
*
* @return minimum response size
*/ */
public int getMinGzipSize() public int getMinGzipSize()
{ {
return _minGzipSize; return _minGzipSize;
} }
/* ------------------------------------------------------------ */
protected HttpField getVaryField() protected HttpField getVaryField()
{ {
return _vary; return _vary;
} }
/* ------------------------------------------------------------ */
/** /**
* @return size in bytes of the buffer to inflate compressed request, or 0 for no inflation. * @return size in bytes of the buffer to inflate compressed request, or 0 for no inflation.
*/ */
@ -433,7 +398,6 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
return _inflateBufferSize; return _inflateBufferSize;
} }
/* ------------------------------------------------------------ */
/** /**
* @param size size in bytes of the buffer to inflate compressed request, or 0 for no inflation. * @param size size in bytes of the buffer to inflate compressed request, or 0 for no inflation.
*/ */
@ -442,10 +406,6 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
_inflateBufferSize = size; _inflateBufferSize = size;
} }
/* ------------------------------------------------------------ */
/**
* @see org.eclipse.jetty.server.handler.HandlerWrapper#handle(java.lang.String, org.eclipse.jetty.server.Request, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
@Override @Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{ {
@ -568,12 +528,9 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
} }
} }
/* ------------------------------------------------------------ */
/** /**
* Checks to see if the userAgent is excluded
*
* @param ua the user agent * @param ua the user agent
* @return boolean true if excluded * @return whether compressing is allowed for the given user agent
*/ */
protected boolean isAgentGzipable(String ua) protected boolean isAgentGzipable(String ua)
{ {
@ -583,20 +540,15 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
return _agentPatterns.test(ua); return _agentPatterns.test(ua);
} }
/* ------------------------------------------------------------ */
@Override @Override
public boolean isMimeTypeGzipable(String mimetype) public boolean isMimeTypeGzipable(String mimetype)
{ {
return _mimeTypes.test(mimetype); return _mimeTypes.test(mimetype);
} }
/* ------------------------------------------------------------ */
/** /**
* Checks to see if the path is included or not excluded * @param requestURI the request uri
* * @return whether compressing is allowed for the given the path
* @param requestURI
* the request uri
* @return boolean true if gzipable
*/ */
protected boolean isPathGzipable(String requestURI) protected boolean isPathGzipable(String requestURI)
{ {
@ -606,7 +558,6 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
return _paths.test(requestURI); return _paths.test(requestURI);
} }
/* ------------------------------------------------------------ */
@Override @Override
public void recycle(Deflater deflater) public void recycle(Deflater deflater)
{ {
@ -615,9 +566,8 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
_deflater.set(deflater); _deflater.set(deflater);
} }
/* ------------------------------------------------------------ */
/** /**
* @param checkGzExists If true, check if a static gz file exists for * @param checkGzExists whether to check if a static gz file exists for
* the resource that the DefaultServlet may serve as precompressed. * the resource that the DefaultServlet may serve as precompressed.
*/ */
public void setCheckGzExists(boolean checkGzExists) public void setCheckGzExists(boolean checkGzExists)
@ -625,7 +575,6 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
_checkGzExists = checkGzExists; _checkGzExists = checkGzExists;
} }
/* ------------------------------------------------------------ */
/** /**
* @param compressionLevel The compression level to use to initialize {@link Deflater#setLevel(int)} * @param compressionLevel The compression level to use to initialize {@link Deflater#setLevel(int)}
*/ */
@ -634,7 +583,6 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
_compressionLevel = compressionLevel; _compressionLevel = compressionLevel;
} }
/* ------------------------------------------------------------ */
/** /**
* @param patterns Regular expressions matching user agents to exclude * @param patterns Regular expressions matching user agents to exclude
*/ */
@ -644,19 +592,16 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
addExcludedAgentPatterns(patterns); addExcludedAgentPatterns(patterns);
} }
/* ------------------------------------------------------------ */
/** /**
* @param method to exclude * @param methods the HTTP methods to exclude
*/ */
public void setExcludedMethods(String... method) public void setExcludedMethods(String... methods)
{ {
_methods.getExcluded().clear(); _methods.getExcluded().clear();
_methods.exclude(method); _methods.exclude(methods);
} }
/* ------------------------------------------------------------ */
/** /**
* Set the mime types.
* @param types The mime types to exclude (without charset or other parameters) * @param types The mime types to exclude (without charset or other parameters)
*/ */
public void setExcludedMimeTypes(String... types) public void setExcludedMimeTypes(String... types)
@ -665,7 +610,6 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
_mimeTypes.exclude(types); _mimeTypes.exclude(types);
} }
/* ------------------------------------------------------------ */
/** /**
* @param pathspecs Path specs (as per servlet spec) to exclude. If a * @param pathspecs Path specs (as per servlet spec) to exclude. If a
* ServletContext is available, the paths are relative to the context path, * ServletContext is available, the paths are relative to the context path,
@ -677,7 +621,6 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
_paths.exclude(pathspecs); _paths.exclude(pathspecs);
} }
/* ------------------------------------------------------------ */
/** /**
* @param patterns Regular expressions matching user agents to include * @param patterns Regular expressions matching user agents to include
*/ */
@ -687,7 +630,6 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
addIncludedAgentPatterns(patterns); addIncludedAgentPatterns(patterns);
} }
/* ------------------------------------------------------------ */
/** /**
* @param methods The methods to include in compression * @param methods The methods to include in compression
*/ */
@ -697,10 +639,9 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
_methods.include(methods); _methods.include(methods);
} }
/* ------------------------------------------------------------ */
/** /**
* Set included mime types. Inclusion takes precedence over * Sets included mime types. Inclusion takes precedence over exclusion.
* exclusion. *
* @param types The mime types to include (without charset or other parameters) * @param types The mime types to include (without charset or other parameters)
*/ */
public void setIncludedMimeTypes(String... types) public void setIncludedMimeTypes(String... types)
@ -709,9 +650,9 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
_mimeTypes.include(types); _mimeTypes.include(types);
} }
/* ------------------------------------------------------------ */
/** /**
* Set the path specs to include. Inclusion takes precedence over exclusion. * Set the path specs to include. Inclusion takes precedence over exclusion.
*
* @param pathspecs Path specs (as per servlet spec) to include. If a * @param pathspecs Path specs (as per servlet spec) to include. If a
* ServletContext is available, the paths are relative to the context path, * ServletContext is available, the paths are relative to the context path,
* otherwise they are absolute * otherwise they are absolute
@ -722,9 +663,8 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
_paths.include(pathspecs); _paths.include(pathspecs);
} }
/* ------------------------------------------------------------ */
/** /**
* Set the minimum response size to trigger dynamic compresssion * Set the minimum response size to trigger dynamic compression
* *
* @param minGzipSize minimum response size in bytes * @param minGzipSize minimum response size in bytes
*/ */