mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-14 01:15:44 +01:00
added option to disable buffer for gzip filter
This commit is contained in:
@@ -29,12 +29,17 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
package sonia.scm.filter;
|
package sonia.scm.filter;
|
||||||
|
|
||||||
//~--- non-JDK imports --------------------------------------------------------
|
//~--- non-JDK imports --------------------------------------------------------
|
||||||
|
|
||||||
import com.google.inject.Singleton;
|
import com.google.inject.Singleton;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import sonia.scm.util.WebUtil;
|
import sonia.scm.util.WebUtil;
|
||||||
import sonia.scm.web.filter.HttpFilter;
|
import sonia.scm.web.filter.HttpFilter;
|
||||||
|
|
||||||
@@ -56,6 +61,27 @@ import javax.servlet.http.HttpServletResponse;
|
|||||||
public class GZipFilter extends HttpFilter
|
public class GZipFilter extends HttpFilter
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* the logger for GZipFilter
|
||||||
|
*/
|
||||||
|
private static final Logger logger =
|
||||||
|
LoggerFactory.getLogger(GZipFilter.class);
|
||||||
|
|
||||||
|
//~--- get methods ----------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method description
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public GZipFilterConfig getConfig()
|
||||||
|
{
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
|
||||||
|
//~--- methods --------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method description
|
* Method description
|
||||||
*
|
*
|
||||||
@@ -74,7 +100,13 @@ public class GZipFilter extends HttpFilter
|
|||||||
{
|
{
|
||||||
if (WebUtil.isGzipSupported(request))
|
if (WebUtil.isGzipSupported(request))
|
||||||
{
|
{
|
||||||
GZipResponseWrapper wrappedResponse = new GZipResponseWrapper(response);
|
if (logger.isTraceEnabled())
|
||||||
|
{
|
||||||
|
logger.trace("compress output with gzip");
|
||||||
|
}
|
||||||
|
|
||||||
|
GZipResponseWrapper wrappedResponse = new GZipResponseWrapper(response,
|
||||||
|
config);
|
||||||
|
|
||||||
chain.doFilter(request, wrappedResponse);
|
chain.doFilter(request, wrappedResponse);
|
||||||
wrappedResponse.finishResponse();
|
wrappedResponse.finishResponse();
|
||||||
@@ -84,4 +116,9 @@ public class GZipFilter extends HttpFilter
|
|||||||
chain.doFilter(request, response);
|
chain.doFilter(request, response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//~--- fields ---------------------------------------------------------------
|
||||||
|
|
||||||
|
/** Field description */
|
||||||
|
private GZipFilterConfig config = new GZipFilterConfig();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,71 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2010, Sebastian Sdorra All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* 1. Redistributions of source code must retain the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer. 2. Redistributions in
|
||||||
|
* binary form must reproduce the above copyright notice, this list of
|
||||||
|
* conditions and the following disclaimer in the documentation and/or other
|
||||||
|
* materials provided with the distribution. 3. Neither the name of SCM-Manager;
|
||||||
|
* nor the names of its contributors may be used to endorse or promote products
|
||||||
|
* derived from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
|
||||||
|
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
* http://bitbucket.org/sdorra/scm-manager
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
package sonia.scm.filter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sebastian Sdorra
|
||||||
|
* @since 1.16
|
||||||
|
*/
|
||||||
|
public class GZipFilterConfig
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method description
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public boolean isBufferRequest()
|
||||||
|
{
|
||||||
|
return bufferRequest;
|
||||||
|
}
|
||||||
|
|
||||||
|
//~--- set methods ----------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method description
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param bufferRequest
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public void setBufferRequest(boolean bufferRequest)
|
||||||
|
{
|
||||||
|
this.bufferRequest = bufferRequest;
|
||||||
|
}
|
||||||
|
|
||||||
|
//~--- fields ---------------------------------------------------------------
|
||||||
|
|
||||||
|
/** Field description */
|
||||||
|
private boolean bufferRequest = true;
|
||||||
|
}
|
||||||
@@ -35,6 +35,9 @@ package sonia.scm.filter;
|
|||||||
|
|
||||||
//~--- non-JDK imports --------------------------------------------------------
|
//~--- non-JDK imports --------------------------------------------------------
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import sonia.scm.util.IOUtil;
|
import sonia.scm.util.IOUtil;
|
||||||
|
|
||||||
//~--- JDK imports ------------------------------------------------------------
|
//~--- JDK imports ------------------------------------------------------------
|
||||||
@@ -55,6 +58,14 @@ import javax.servlet.http.HttpServletResponse;
|
|||||||
public class GZipResponseStream extends ServletOutputStream
|
public class GZipResponseStream extends ServletOutputStream
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* the logger for GZipResponseStream
|
||||||
|
*/
|
||||||
|
private static final Logger logger =
|
||||||
|
LoggerFactory.getLogger(GZipResponseStream.class);
|
||||||
|
|
||||||
|
//~--- constructors ---------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs ...
|
* Constructs ...
|
||||||
*
|
*
|
||||||
@@ -64,13 +75,49 @@ public class GZipResponseStream extends ServletOutputStream
|
|||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public GZipResponseStream(HttpServletResponse response) throws IOException
|
public GZipResponseStream(HttpServletResponse response) throws IOException
|
||||||
|
{
|
||||||
|
this(response, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs ...
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param response
|
||||||
|
* @param config
|
||||||
|
*
|
||||||
|
* @throws IOException
|
||||||
|
* @since 1.16
|
||||||
|
*/
|
||||||
|
public GZipResponseStream(HttpServletResponse response,
|
||||||
|
GZipFilterConfig config)
|
||||||
|
throws IOException
|
||||||
{
|
{
|
||||||
super();
|
super();
|
||||||
closed = false;
|
closed = false;
|
||||||
this.response = response;
|
this.response = response;
|
||||||
this.output = response.getOutputStream();
|
response.addHeader("Content-Encoding", "gzip");
|
||||||
baos = new ByteArrayOutputStream();
|
|
||||||
gzipstream = new GZIPOutputStream(baos);
|
if ((config == null) || config.isBufferRequest())
|
||||||
|
{
|
||||||
|
if (logger.isTraceEnabled())
|
||||||
|
{
|
||||||
|
logger.trace("use buffered gzip stream");
|
||||||
|
}
|
||||||
|
|
||||||
|
this.output = response.getOutputStream();
|
||||||
|
baos = new ByteArrayOutputStream();
|
||||||
|
gzipstream = new GZIPOutputStream(baos);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (logger.isTraceEnabled())
|
||||||
|
{
|
||||||
|
logger.trace("use unbuffered gzip stream");
|
||||||
|
}
|
||||||
|
|
||||||
|
gzipstream = new GZIPOutputStream(response.getOutputStream());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//~--- methods --------------------------------------------------------------
|
//~--- methods --------------------------------------------------------------
|
||||||
@@ -90,20 +137,23 @@ public class GZipResponseStream extends ServletOutputStream
|
|||||||
}
|
}
|
||||||
|
|
||||||
gzipstream.finish();
|
gzipstream.finish();
|
||||||
|
gzipstream.close();
|
||||||
|
|
||||||
byte[] bytes = baos.toByteArray();
|
if (baos != null)
|
||||||
|
|
||||||
response.addIntHeader("Content-Length", bytes.length);
|
|
||||||
response.addHeader("Content-Encoding", "gzip");
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
output.write(bytes);
|
byte[] bytes = baos.toByteArray();
|
||||||
output.flush();
|
|
||||||
}
|
response.addIntHeader("Content-Length", bytes.length);
|
||||||
finally
|
|
||||||
{
|
try
|
||||||
IOUtil.close(output);
|
{
|
||||||
|
output.write(bytes);
|
||||||
|
output.flush();
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
IOUtil.close(output);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
closed = true;
|
closed = true;
|
||||||
|
|||||||
@@ -67,6 +67,22 @@ public class GZipResponseWrapper extends HttpServletResponseWrapper
|
|||||||
origResponse = response;
|
origResponse = response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs ...
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param response
|
||||||
|
* @since 1.16
|
||||||
|
* @param config
|
||||||
|
*/
|
||||||
|
public GZipResponseWrapper(HttpServletResponse response,
|
||||||
|
GZipFilterConfig config)
|
||||||
|
{
|
||||||
|
super(response);
|
||||||
|
origResponse = response;
|
||||||
|
this.config = config;
|
||||||
|
}
|
||||||
|
|
||||||
//~--- methods --------------------------------------------------------------
|
//~--- methods --------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -100,6 +116,17 @@ public class GZipResponseWrapper extends HttpServletResponseWrapper
|
|||||||
|
|
||||||
//~--- get methods ----------------------------------------------------------
|
//~--- get methods ----------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method description
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public GZipFilterConfig getConfig()
|
||||||
|
{
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method description
|
* Method description
|
||||||
*
|
*
|
||||||
@@ -175,11 +202,14 @@ public class GZipResponseWrapper extends HttpServletResponseWrapper
|
|||||||
*/
|
*/
|
||||||
private GZipResponseStream createOutputStream() throws IOException
|
private GZipResponseStream createOutputStream() throws IOException
|
||||||
{
|
{
|
||||||
return new GZipResponseStream(origResponse);
|
return new GZipResponseStream(origResponse, config);
|
||||||
}
|
}
|
||||||
|
|
||||||
//~--- fields ---------------------------------------------------------------
|
//~--- fields ---------------------------------------------------------------
|
||||||
|
|
||||||
|
/** Field description */
|
||||||
|
protected GZipFilterConfig config = null;
|
||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
protected HttpServletResponse origResponse = null;
|
protected HttpServletResponse origResponse = null;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user