users@jersey.java.net

[Jersey] GZipContentEncodingFilter and Viewables

From: Martin Eigenbrodt <martineigenbrodt_at_googlemail.com>
Date: Wed, 2 May 2012 09:51:18 +0200

Hi all,

I have some issues with the GZipContentEncodingFIlter and Viewables.
Within my Resource I return a Viewable like:

        return Response.ok(new Viewable("/foo", model)).build();


Unfortunately the gzip Filter commits the response before the jsp is rendered.

Servlet.service() for servlet [default] in context with path [/foo] threw exception [com.sun.jersey.api.container.ContainerException: java.lang.IllegalStateException: Cannot forward after response has been committed] with root cause
java.lang.IllegalStateException: Cannot forward after response has been committed
        at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:339) ~[catalina.jar:7.0.26]
        at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:329) ~[catalina.jar:7.0.26]
        at com.sun.jersey.server.impl.container.servlet.RequestDispatcherWrapper.forward(RequestDispatcherWrapper.java:82) ~[jersey-servlet-1.12.jar:1.12]
        at com.sun.jersey.server.impl.container.servlet.JSPTemplateProcessor.writeTo(JSPTemplateProcessor.java:132) ~[jersey-servlet-1.12.jar:1.12]
        at com.sun.jersey.server.impl.container.servlet.JSPTemplateProcessor.writeTo(JSPTemplateProcessor.java:64) ~[jersey-servlet-1.12.jar:1.12]
        at com.sun.jersey.spi.template.ResolvedViewable.writeTo(ResolvedViewable.java:103) ~[jersey-server-1.12.jar:1.12]
        at com.sun.jersey.server.impl.template.ViewableMessageBodyWriter.writeTo(ViewableMessageBodyWriter.java:83) ~[jersey-server-1.12.jar:1.12]
        at com.sun.jersey.server.impl.template.ViewableMessageBodyWriter.writeTo(ViewableMessageBodyWriter.java:62) ~[jersey-server-1.12.jar:1.12]
        at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:306) ~[jersey-server-1.12.jar:1.12]
        at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1451) ~[jersey-server-1.12.jar:1.12]
        at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1363) ~[jersey-server-1.12.jar:1.12]
        at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1353) ~[jersey-server-1.12.jar:1.12]
        at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:414) ~[jersey-servlet-1.12.jar:1.12]
        at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537) ~[jersey-servlet-1.12.jar:1.12]
        at com.sun.jersey.spi.container.servlet.ServletContainer.doFilter(ServletContainer.java:895) ~[jersey-servlet-1.12.jar:1.12]
        at com.sun.jersey.spi.container.servlet.ServletContainer.doFilter(ServletContainer.java:843) ~[jersey-servlet-1.12.jar:1.12]
        at com.sun.jersey.spi.container.servlet.ServletContainer.doFilter(ServletContainer.java:804) ~[jersey-servlet-1.12.jar:1.12]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) ~[catalina.jar:7.0.26]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) ~[catalina.jar:7.0.26]
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224) ~[catalina.jar:7.0.26]
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169) [catalina.jar:7.0.26]
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) [catalina.jar:7.0.26]
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168) [catalina.jar:7.0.26]
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98) [catalina.jar:7.0.26]
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927) [catalina.jar:7.0.26]
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) [catalina.jar:7.0.26]
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) [catalina.jar:7.0.26]
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987) [tomcat-coyote.jar:7.0.26]
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579) [tomcat-coyote.jar:7.0.26]
        at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307) [tomcat-coyote.jar:7.0.26]
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [na:1.6.0_31]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [na:1.6.0_31]
        at java.lang.Thread.run(Thread.java:680) [na:1.6.0_31]


My pesonal workaround is to patch the Filter:
(from line 104:)
                if (!(response.getEntity() instanceof Viewable)) {
                    response.getHttpHeaders().add(HttpHeaders.CONTENT_ENCODING, "gzip");
                    response.setContainerResponseWriter(
                        new Adapter(response.getContainerResponseWriter()));
                }


Viewables will not be gziped, but thats fine for me. Let me know if I'm missing something or if there is a better solution.

Regards,

Martin