Question

In my Spring application I use org.springframework.web.filter.ShallowEtagHeaderFilter to add ETags. That works great, except when I output REALLY large data. Then my application runs out of memory and terminates the JVM! If I remove the filter, everything works great.

But I really like having ETags, so how can I make a filter definition in web.xml that filters the entire servlet except for a few URL mappings? My filter looks like this at the moment:

<filter> 
    <filter-name>etagFilter</filter-name> 
    <filter-class>org.springframework.web.filter.ShallowEtagHeaderFilter</filter-class> 
</filter> 

<filter-mapping> 
    <filter-name>etagFilter</filter-name> 
    <servlet-name>MyWebApp</servlet-name>
</filter-mapping>

Cheers

Nik

Was it helpful?

Solution

There is no way to do it declaratively. I guess you need to override its doFilter() and take a decision programmatically based on request properties.

OTHER TIPS

OncePerRequestFilter has a method called shouldNotFilter() that you can override to do this.

I am doing something similar for some of my filters. Here is a sample web.xml fragment:

<filter>
    <filter-name>hibernateFilter</filter-name>
    <filter-class>com.xyz.config.OpenSessionInViewFilter</filter-class>
    <init-param>
        <param-name>excludePaths</param-name>
        <param-value>/js:/log/</param-value>
    </init-param>
</filter>

And the filter is:

class OpenSessionInViewFilter extends org.springframework.orm.hibernate3.support.OpenSessionInViewFilter {
  @BeanProperty var excludePaths: String = null
  val excludePathList = new mutable.ArrayBuffer[String]

  override def initFilterBean {
    if (excludePaths != null) {
      excludePaths.split(':').foreach(excludePathList += _)
    }
    super.initFilterBean
  }

  override def shouldNotFilter(request: HttpServletRequest) = {
    val httpServletRequest = request.asInstanceOf[HttpServletRequest]
    val servletPathInfo = httpServletRequest.getServletPath + httpServletRequest.getPathInfo
    excludePathList.exists(p => servletPathInfo.startsWith(p)) || DataConfig.noDB
  }

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top