diff --git a/scm-webapp/src/main/java/sonia/scm/ScmServletModule.java b/scm-webapp/src/main/java/sonia/scm/ScmServletModule.java index 9d79ad3b07..ec4d568d51 100644 --- a/scm-webapp/src/main/java/sonia/scm/ScmServletModule.java +++ b/scm-webapp/src/main/java/sonia/scm/ScmServletModule.java @@ -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 diff --git a/scm-webapp/src/main/java/sonia/scm/filter/GZipResponseStream.java b/scm-webapp/src/main/java/sonia/scm/filter/GZipResponseStream.java index 142a5281e2..0dfc360322 100644 --- a/scm-webapp/src/main/java/sonia/scm/filter/GZipResponseStream.java +++ b/scm-webapp/src/main/java/sonia/scm/filter/GZipResponseStream.java @@ -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 */ diff --git a/scm-webapp/src/main/java/sonia/scm/filter/GZipResponseWrapper.java b/scm-webapp/src/main/java/sonia/scm/filter/GZipResponseWrapper.java index 40bd9b8aaf..f42d25ff17 100644 --- a/scm-webapp/src/main/java/sonia/scm/filter/GZipResponseWrapper.java +++ b/scm-webapp/src/main/java/sonia/scm/filter/GZipResponseWrapper.java @@ -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;