enable gzip compression

This commit is contained in:
Sebastian Sdorra
2010-12-28 15:45:29 +01:00
parent 2a2696bf84
commit a43d9a58a4
3 changed files with 60 additions and 48 deletions

View File

@@ -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

View File

@@ -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");
output.write(bytes);
output.flush();
output.close();
try
{
output.write(bytes);
output.flush();
}
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 */

View File

@@ -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;