Skip to content

Instantly share code, notes, and snippets.

@superbob
Created February 21, 2013 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save superbob/5005291 to your computer and use it in GitHub Desktop.
Save superbob/5005291 to your computer and use it in GitHub Desktop.
Servlet filter inhibitor, usefull when you want to bypass an undesired filter sometimes.
/*
*/
package com.github.superbob.demo.servlets.filter;
import java.io.IOException;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author superbob
*
*/
public class FilterInhibitor implements Filter {
private static final Logger logger = LoggerFactory.getLogger(FilterInhibitor.class);
private static final String FILTER_PARAM_CLASS = "inhibitor.filterClass";
private static final String FILTER_PARAM_HEADER = "inhibitor.header";
private static final String HEADER_DEFAULT = "Inhibit";
private String header = HEADER_DEFAULT;
private Filter filter;
/* (non-Javadoc)
* @see javax.servlet.Filter#destroy()
*/
@Override
public void destroy() {
logger.info("Destroying filter inhibitor");
logger.info("Destroying real filter");
filter.destroy();
logger.info("Destroyed !");
}
/* (non-Javadoc)
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
*/
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
if (allow(arg0, arg1, arg2)) {
filter.doFilter(arg0, arg1, arg2);
} else {
arg2.doFilter(arg0, arg1);
}
}
/* (non-Javadoc)
* @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
*/
@Override
public void init(FilterConfig arg0) throws ServletException {
logger.info("Initializing filter inhibitor");
try {
logger.info("Loading real filter : " + arg0.getInitParameter(FILTER_PARAM_CLASS));
this.filter = (Filter) Class.forName(arg0.getInitParameter(FILTER_PARAM_CLASS)).newInstance();
} catch (InstantiationException e) {
logger.error("exception while initializing inhibitor filter", e);
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
logger.error("exception while initializing inhibitor filter", e);
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
logger.error("ClassNotFoundException while initializing inhibitor filter, check your inhibitor filter configuration in web.xml and your classpath", e);
throw new RuntimeException(e);
}
final String paramHeader = arg0.getInitParameter(FILTER_PARAM_HEADER);
if (paramHeader != null) {
this.header = paramHeader;
}
logger.info("Filtering on header : " + this.header);
logger.info("Initializing real filter");
filter.init(arg0);
logger.info("Inhibitor engaged !");
}
private final boolean allow(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) {
if (HttpServletRequest.class.isAssignableFrom(arg0.getClass())) {
HttpServletRequest httpReq = (HttpServletRequest) arg0;
return !Boolean.parseBoolean(httpReq.getHeader(header));
}
return true;
}
}
<!-- Struts 2 -->
<!-- Struts 2 messes with file uploaded when using spring,
so an inhibitor is used to bypass its filtering when a given header is found in the request -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>com.github.superbob.demo.servlets.filter.FilterInhibitor</filter-class>
<!-- This is the real filter (struts2) -->
<init-param>
<param-name>inhibitor.filterClass</param-name>
<param-value>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</param-value>
</init-param>
<!-- This is the header to look for, when it is set to true, the bypass will occur,
otherwise the filter (struts2) will be called transparently -->
<init-param>
<param-name>inhibitor.header</param-name>
<param-value>Inhibit</param-value>
</init-param>
<!-- All the specified parameters are transfered to the filter (struts2) initialization -->
<init-param>
<param-name>actionPackages</param-name>
<param-value>com.github.superbob.demo.struts.actions</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
@evazquezma
Copy link

I think this is an ingenious solution. With your permission, I tried to improve a bit to make it more reusable.

I also publish my code in github (https://github.com/evazquezma/JEE6/tree/master/wrapperFilter) and http://trabajosdesisifo.blogspot.com.es/2015/01/java-servlet-byapssfilter-filter.html.

Regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment