Skip to content

Commit

Permalink
1929395 - replace the java webparts compression filter with an embedd…
Browse files Browse the repository at this point in the history
…ed version based off of a tomcat example filter.
  • Loading branch information
chenson42 committed Mar 30, 2008
1 parent ab1dfe1 commit ffd79ae
Show file tree
Hide file tree
Showing 8 changed files with 1,145 additions and 712 deletions.
1,318 changes: 622 additions & 696 deletions symmetric/pom.xml

Large diffs are not rendered by default.

Expand Up @@ -126,6 +126,7 @@ public boolean isSpringManaged() {
* Returns true if this is a container managed resource.
* @return
*/
@SuppressWarnings("unchecked")
public IServletResource getSpringBean() {
IServletResource retVal = this;
if (!isSpringManaged()) {
Expand Down
Expand Up @@ -57,15 +57,17 @@
* <list/>
* <property/>
* <property name="disabled" value="boolean" />
* <property name="compressType" value="string" />
* <property name="compressionThreshold" value="int" />
* </bean>
* </pre>
*/
public class CompressionFilter extends AbstractFilter {

private static final Log logger = LogFactory.getLog(CompressionFilter.class);

private javawebparts.filter.CompressionFilter delegate;
private String compressType;
private org.jumpmind.symmetric.web.compression.CompressionFilter delegate;

protected int compressionThreshold;

@Override
public boolean isContainerCompatible()
Expand All @@ -91,15 +93,11 @@ public void destroy() {
@Override
public void init(final FilterConfig filterConfig) throws ServletException {
super.init(filterConfig);
delegate = new javawebparts.filter.CompressionFilter();
delegate = new org.jumpmind.symmetric.web.compression.CompressionFilter();

delegate.init(new CompressionFilterConfig(filterConfig));
}

public void setCompressType(String compressType) {
this.compressType = compressType;
}

@Override
protected Log getLogger()
{
Expand All @@ -115,8 +113,8 @@ private CompressionFilterConfig(FilterConfig filterConfig) {
this.filterConfig = filterConfig;
initParameterNames = IteratorUtils.toList(new EnumerationIterator(
filterConfig.getInitParameterNames()));
if (compressType != null) {
initParameterNames.add(compressType);
if (compressionThreshold > 0) {
initParameterNames.add("compressionThreshold");
}
}

Expand All @@ -125,8 +123,8 @@ public String getFilterName() {
}

public String getInitParameter(String name) {
if (compressType != null && "compressType".equals(name)) {
return compressType;
if (compressionThreshold > 0 && "compressionThreshold".equals(name)) {
return Integer.toString(compressionThreshold);
}
return filterConfig.getInitParameter(name);
}
Expand All @@ -139,4 +137,8 @@ public ServletContext getServletContext() {
return filterConfig.getServletContext();
}
}

public void setCompressionThreshold(int compressionThreshold) {
this.compressionThreshold = compressionThreshold;
}
}
Expand Up @@ -232,6 +232,7 @@ public boolean isSpringManaged() {
* Returns true if this is a container managed resource.
* @return
*/
@SuppressWarnings("unchecked")
public IServletResource getSpringBean() {
IServletResource retVal = this;
if (!isSpringManaged()) {
Expand Down
@@ -0,0 +1,215 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jumpmind.symmetric.web.compression;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* Implementation of <code>javax.servlet.Filter</code> used to compress
* the ServletResponse if it is bigger than a threshold.
*
* This package is derived from the Jakarta
* <a href="http://jakarta.apache.org/tomcat">Tomcat</a>
* examples compression filter and is distributed in SymmetricDS for convenience.
*
* @author Amy Roh
* @author Dmitri Valdin
* @version $Revision: 466607 $, $Date: 2006-10-21 17:09:50 -0600 (Sat, 21 Oct 2006) $
*/

public class CompressionFilter implements Filter {

static final Log logger = LogFactory.getLog(CompressionFilter.class);

/**
* The filter configuration object we are associated with. If this value
* is null, this filter instance is not currently configured.
*/
private FilterConfig config = null;

/**
* Minimal reasonable threshold
*/
private final int minThreshold = 128;

/**
* The threshold number to compress
*/
protected int compressionThreshold;

/**
* Place this filter into service.
*
* @param filterConfig The filter configuration object
*/

public void init(FilterConfig filterConfig) {

config = filterConfig;
if (filterConfig != null) {
String str = filterConfig.getInitParameter("compressionThreshold");
if (str != null) {
compressionThreshold = Integer.parseInt(str);
if (compressionThreshold != 0 && compressionThreshold < minThreshold) {
if (logger.isDebugEnabled()) {
logger.debug("compressionThreshold should be either 0 - no compression or >= "
+ minThreshold);
logger.debug("compressionThreshold set to " + minThreshold);
}
compressionThreshold = minThreshold;
}
} else {
compressionThreshold = 0;
}

} else {
compressionThreshold = 0;
}

}

/**
* Take this filter out of service.
*/
public void destroy() {

this.config = null;

}

/**
* The <code>doFilter</code> method of the Filter is called by the container
* each time a request/response pair is passed through the chain due
* to a client request for a resource at the end of the chain.
* The FilterChain passed into this method allows the Filter to pass on the
* request and response to the next entity in the chain.<p>
* This method first examines the request to check whether the client support
* compression. <br>
* It simply just pass the request and response if there is no support for
* compression.<br>
* If the compression support is available, it creates a
* CompressionServletResponseWrapper object which compresses the content and
* modifies the header if the content length is big enough.
* It then invokes the next entity in the chain using the FilterChain object
* (<code>chain.doFilter()</code>), <br>
**/

@SuppressWarnings("unchecked")
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
ServletException {

if (logger.isDebugEnabled()) {
logger.debug("@doFilter");
}

if (compressionThreshold == 0) {
if (logger.isDebugEnabled()) {
logger.debug("doFilter gets called, but compressionTreshold is set to 0 - no compression");
}
chain.doFilter(request, response);
return;
}

boolean supportCompression = false;
if (request instanceof HttpServletRequest) {
if (logger.isDebugEnabled()) {
logger.debug("requestURI = " + ((HttpServletRequest) request).getRequestURI());
}

// Are we allowed to compress ?
String s = (String) ((HttpServletRequest) request).getParameter("gzip");
if ("false".equals(s)) {
if (logger.isDebugEnabled()) {
logger.debug("got parameter gzip=false --> don't compress, just chain filter");
}
chain.doFilter(request, response);
return;
}

Enumeration e = ((HttpServletRequest) request).getHeaders("Accept-Encoding");
while (e.hasMoreElements()) {
String name = (String) e.nextElement();
if (name.indexOf("gzip") != -1) {
if (logger.isDebugEnabled()) {
logger.debug("supports compression");
}
supportCompression = true;
} else {
if (logger.isDebugEnabled()) {
logger.debug("no support for compresion");
}
}
}
}

if (!supportCompression) {
if (logger.isDebugEnabled()) {
logger.debug("doFilter gets called wo compression");
}
chain.doFilter(request, response);
return;
} else {
if (response instanceof HttpServletResponse) {
CompressionServletResponseWrapper wrappedResponse = new CompressionServletResponseWrapper(
(HttpServletResponse) response);
wrappedResponse.setCompressionThreshold(compressionThreshold);
if (logger.isDebugEnabled()) {
logger.debug("doFilter gets called with compression");
}
try {
chain.doFilter(request, wrappedResponse);
} finally {
wrappedResponse.finishResponse();
}
return;
}
}
}

/**
* Set filter config
* This function is equivalent to init. Required by Weblogic 6.1
*
* @param filterConfig The filter configuration object
*/
public void setFilterConfig(FilterConfig filterConfig) {
init(filterConfig);
}

/**
* Return filter config
* Required by Weblogic 6.1
*/
public FilterConfig getFilterConfig() {
return config;
}

}

0 comments on commit ffd79ae

Please sign in to comment.