mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-09 06:55:47 +01:00
enable gzip compression
This commit is contained in:
@@ -83,6 +83,7 @@ import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.xml.bind.JAXB;
|
||||
import sonia.scm.filter.GZipFilter;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -182,9 +183,8 @@ public class ScmServletModule extends ServletModule
|
||||
/*
|
||||
* filter(PATTERN_PAGE,
|
||||
* PATTERN_STATIC_RESOURCES).through(StaticResourceFilter.class);
|
||||
* filter(PATTERN_PAGE, PATTERN_COMPRESSABLE).through(GZipFilter.class);
|
||||
* filter(PATTERN_RESTAPI).through(SecurityFilter.class);
|
||||
*/
|
||||
filter(PATTERN_PAGE, PATTERN_COMPRESSABLE).through(GZipFilter.class);
|
||||
filter(PATTERN_RESTAPI, PATTERN_DEBUG).through(SecurityFilter.class);
|
||||
|
||||
// debug servlet
|
||||
|
||||
@@ -29,8 +29,14 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.filter;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import sonia.scm.util.IOUtil;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
@@ -86,11 +92,19 @@ public class GZipResponseStream extends ServletOutputStream
|
||||
|
||||
byte[] bytes = baos.toByteArray();
|
||||
|
||||
response.addHeader("Content-Length", Integer.toString(bytes.length));
|
||||
response.addIntHeader("Content-Length", bytes.length);
|
||||
response.addHeader("Content-Encoding", "gzip");
|
||||
|
||||
try
|
||||
{
|
||||
output.write(bytes);
|
||||
output.flush();
|
||||
output.close();
|
||||
}
|
||||
finally
|
||||
{
|
||||
IOUtil.close(output);
|
||||
}
|
||||
|
||||
closed = true;
|
||||
}
|
||||
|
||||
@@ -102,7 +116,7 @@ public class GZipResponseStream extends ServletOutputStream
|
||||
*/
|
||||
public boolean closed()
|
||||
{
|
||||
return (this.closed);
|
||||
return closed;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -186,6 +200,19 @@ public class GZipResponseStream extends ServletOutputStream
|
||||
gzipstream.write(b, off, len);
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public boolean isClosed()
|
||||
{
|
||||
return closed;
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
|
||||
@@ -29,12 +29,13 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.filter;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import sonia.scm.util.IOUtil;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
@@ -53,12 +54,6 @@ import javax.servlet.http.HttpServletResponseWrapper;
|
||||
public class GZipResponseWrapper extends HttpServletResponseWrapper
|
||||
{
|
||||
|
||||
/** Field description */
|
||||
private static final Logger logger =
|
||||
LoggerFactory.getLogger(GZipResponseWrapper.class);
|
||||
|
||||
//~--- constructors ---------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
@@ -73,42 +68,17 @@ public class GZipResponseWrapper extends HttpServletResponseWrapper
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
public ServletOutputStream createOutputStream() throws IOException
|
||||
{
|
||||
return (new GZipResponseStream(origResponse));
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*/
|
||||
public void finishResponse()
|
||||
{
|
||||
try
|
||||
IOUtil.close(writer);
|
||||
|
||||
if ((stream != null) &&!stream.isClosed())
|
||||
{
|
||||
if (writer != null)
|
||||
{
|
||||
writer.close();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (stream != null)
|
||||
{
|
||||
stream.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
logger.error(ex.getMessage(), ex);
|
||||
IOUtil.close(stream);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,7 +117,7 @@ public class GZipResponseWrapper extends HttpServletResponseWrapper
|
||||
stream = createOutputStream();
|
||||
}
|
||||
|
||||
return (stream);
|
||||
return stream;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -163,7 +133,7 @@ public class GZipResponseWrapper extends HttpServletResponseWrapper
|
||||
{
|
||||
if (writer != null)
|
||||
{
|
||||
return (writer);
|
||||
return writer;
|
||||
}
|
||||
|
||||
if (stream != null)
|
||||
@@ -175,7 +145,7 @@ public class GZipResponseWrapper extends HttpServletResponseWrapper
|
||||
stream = createOutputStream();
|
||||
writer = new PrintWriter(new OutputStreamWriter(stream, "UTF-8"));
|
||||
|
||||
return (writer);
|
||||
return writer;
|
||||
}
|
||||
|
||||
//~--- set methods ----------------------------------------------------------
|
||||
@@ -189,13 +159,28 @@ public class GZipResponseWrapper extends HttpServletResponseWrapper
|
||||
@Override
|
||||
public void setContentLength(int length) {}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
private GZipResponseStream createOutputStream() throws IOException
|
||||
{
|
||||
return new GZipResponseStream(origResponse);
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
protected HttpServletResponse origResponse = null;
|
||||
|
||||
/** Field description */
|
||||
protected ServletOutputStream stream = null;
|
||||
protected GZipResponseStream stream = null;
|
||||
|
||||
/** Field description */
|
||||
protected PrintWriter writer = null;
|
||||
|
||||
Reference in New Issue
Block a user