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