2

The Java servlet API does not provide a getStatus method for HttpServletResponse until version 3.0. I have created a HttpServletResponseWrapper with getStatus to wrap HttpServletResponse and catch the status when it is set.

This does not work with my Jersey 2 servlet.

My HttpServletResponseWrapper is passed via the doFilter(request, wrapperResponse) of my Filter. The Filter is called but the getStatus method is not called when a Jersey RESTful Servlet is the endpoint.

Is there any configuration I have missed?

I use the response builder to return the result and set the status.

Response.status(404).build(); Response.status(200).type(mediaType).entity(theEntity).build();

Best Regards Jochen

ScubaInstructor
  • 493
  • 1
  • 5
  • 13

1 Answers1

1

You don't need a HttpServletResponseWrapper for GZIP compression. It could be achieved with a WriterInterceptor from JAX-RS:

public class GZIPWriterInterceptor implements WriterInterceptor {

    @Override
    public void aroundWriteTo(WriterInterceptorContext context)
                throws IOException, WebApplicationException {
        final OutputStream outputStream = context.getOutputStream();
        context.setOutputStream(new GZIPOutputStream(outputStream));
        context.proceed();
    }
}

Then register the WriterInterceptor in your ResourceConfig / Application subclass:

@ApplicationPath("/api")
public class MyApplication extends ResourceConfig {

    public MyApplication() {
        register(GZIPWriterInterceptor.class);
    }
}

To bind the interceptor to certain resource methods or classes, you could use name binding annotations.

cassiomolin
  • 124,154
  • 35
  • 280
  • 359
  • Got it working. That WriterInterceptor is only triggered when I send an entity is great, so my 404 and 204 case is covered. But how can I skip this gzip if the request does not have the Accept-Encoding:gzip, deflate, br header? – ScubaInstructor Oct 12 '17 at 07:02
  • @ScubaInstructor You should be able to inject the request headers in your interceptor using `@Context HttpHeaders httpHeaders`. – cassiomolin Oct 12 '17 at 09:43
  • I will go now with this solution EncodingFilter.enableFor(this, GZipEncoder.class, DeflateEncoder.class); and skip the WriterInterceptor. – ScubaInstructor Oct 12 '17 at 09:44