I have a purely JavaConfig Java webapp that uses class extending from AbstractAnnotationConfigDispatcherServletInitializer. It works great for configuring my dispatcher servlet, but my dispatcher servlet is only mapped to requests with the url pattern "/rest/*". For all other requests (ie css, html, js, etc), there is no spring mapping and everything goes through the default servlet. For these requests, I want to implement a GZip filter. I have used a custom gzip filter I found online years ago in many projects.
With the JavaConfig implementation, I understand that the protected Filter[] getServletFilters() method applies to the dispatcher serlvet... is this incorrect?
What I'm trying to do now is apply the gzip filter to any request not going to the dispatcher servlet. I'm overriding the onStartup method like so:
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
super.onStartup(servletContext);
FilterRegistration.Dynamic encodingFilter = servletContext.addFilter("encodingFilter", new CharacterEncodingFilter());
encodingFilter.setInitParameter("encoding", "UTF-8");
encodingFilter.setInitParameter("forceEncoding", "true");
encodingFilter.addMappingForUrlPatterns(null, true, "/*");
FilterRegistration.Dynamic gzipFilter = servletContext.addFilter("gzipFilter", new GzipFilter());
gzipFilter.addMappingForUrlPatterns(null, true, "/*");
}
The encoding filter works fine, but I get a NullPointerException on the GzipFilter when I add the mapping... the servletContext.addFilter returns null. I thought maybe there was something funky with my custom class and the new JavaConfig implementation, so I found online that people were using a gzip filter included with the ehacache-web package, so I added that as a dependency and attempted to use that filter instead. Same thing... the CharacterEncodingFilter works fine, and the addFilter method properly returns a FilterRegistration.Dynamic object, however the gzip filter still returns null.
Any ideas what is causing this?
EDIT:
Here is my whole config file:
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
super.onStartup(servletContext);
FilterRegistration.Dynamic encodingFilter = servletContext.addFilter("encodingFilter", new CharacterEncodingFilter());
encodingFilter.setInitParameter("encoding", "UTF-8");
encodingFilter.setInitParameter("forceEncoding", "true");
encodingFilter.addMappingForUrlPatterns(null, true, "/*");
FilterRegistration.Dynamic gzipFilter = servletContext.addFilter("gzipFilter", new GzipFilter());
gzipFilter.addMappingForUrlPatterns(null, true, "/*");
}
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[]{SecurityConfig.class, Log4jConfig.class, PersistenceConfig.class, ServiceConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringMvcConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{
"/rest/*",
"/index.html",
"/login.html",
"/admin.html",
"/index/*",
"/login/*",
"/admin/*"
};
}
@Override
protected Filter[] getServletFilters() {
OpenEntityManagerInViewFilter openEntityManagerInViewFilter = new OpenEntityManagerInViewFilter();
openEntityManagerInViewFilter.setBeanName("openEntityManagerInViewFilter");
openEntityManagerInViewFilter.setPersistenceUnitName("HSQL");
return new javax.servlet.Filter[]{openEntityManagerInViewFilter};
}
}
Regardless if I add the GzipFilter to the getServletFilters() or create the FilterRegistration.Dynamic from the servletContext.addFilter() with a URL mapping of "/*", only requests being processed through the dispatcher servlet are being gzipped.
Edit 2:
Here is a URL that is not being gzipped:
http://localhost:8084/swtc/js/ServiceWidget/templates/ServiceWidget.html
Response Headers
Accept-Ranges bytes
Content-Length 399
Content-Type text/html
Date Mon, 07 Apr 2014 18:47:08 GMT
Etag W/"399-1377195848751"
Last-Modified Thu, 22 Aug 2013 18:24:08 GMT
Server Apache-Coyote/1.1
Here is a URL that is being gzipped:
http://localhost:8084/swtc/rest/mapServices/getEnabledServices
Response Headers
Content-Encoding gzip
Content-Length 76
Content-Type application/json;charset=UTF-8
Date Tue, 08 Apr 2014 18:34:11 GMT
Server Apache-Coyote/1.1
Would an option be changing the dispatcher serlvet mapping to /* and just define my js, css, img, etc folders as resources? I feel like this is skirting the issue though... It would be nice to know why I can specify a filter like I used to in web.xml that has nothing to do with Spring.
You haven't given us as much as I'd like but here's what the javadoc for ServletContext#addFilter(String, Filter)
says
Returns: a
FilterRegistration
object that may be used to further configure the given filter, ornull
if this ServletContext already contains a completeFilterRegistration
for a filter with the givenfilterName
or if the same filter instance has already been registered with this or another ServletContext in the same container
You seem to have registered that Filter
or another with the same name already.