2

I'm using Jersey on both the server and client of a web application. On the server I have Interceptors as noted in https://jersey.java.net/documentation/latest/filters-and-interceptors.html to handle GZIP compression going out and coming in. From the server side, it's easy enough to select which resource methods are compressed using the @Compress annotation. However, if I also want to selectively compress entities from the Client to the Server, what's the best way to do that?

I had started adding a Content-Encoding: x-gzip header to the request, but my client side Interceptor does not see that header (presumably because it's not an official client side header).

Before you point to section 10.6 of the Jersey documentation, note that this works for the Server side. Although I could do something similar on the Client, I don't want to restrict it by URL. I'd rather control the compression flag as close to the request as possible (i.e. Header?).

Here's what I have so far, but it does not work since my header is removed:

class GzipWriterClientInterceptor implements WriterInterceptor {
    private static final Set<String> supportedEncodings = new GZipEncoder().getSupportedEncodings(); //support gzip and x-gzip

    @Override
    public void aroundWriteTo(WriterInterceptorContext context)
            throws IOException, WebApplicationException {
        if (supportedEncodings.contains(context.getHeaders().getFirst(HttpHeaderConstants.CONTENT_ENCODING_HEADER))) {
            System.out.println("ZIPPING DATA");
            final OutputStream outputStream = context.getOutputStream();
            context.setOutputStream(new GZIPOutputStream(outputStream));
        } else {
            context.headers.remove(HttpHeaderConstants.CONTENT_ENCODING_HEADER) //remove it since we won't actually be compressing the data
        }
        context.proceed();
    }
}

Sample Request:

Response response = getBaseTarget().path(getBasePath()).path(graphic.uuid.toString())
                .request(DEFAULT_MEDIA_TYPE)
                .header(HttpHeaderConstants.CONTENT_ENCODING_HEADER, MediaTypeConstants.ENCODING_GZIP)
                .put( Entity.entity(graphic, DEFAULT_MEDIA_TYPE))

I also have a logging filter as well that shows all the request headers. I've simplified the above, but all other headers I add are logged.

Domenic D.
  • 5,276
  • 4
  • 30
  • 41
  • Your question is answered in the referred qa – Robert Moskal Dec 30 '15 at 23:54
  • Possible duplicate of [Why can't browser send gzip request?](http://stackoverflow.com/questions/424917/why-cant-browser-send-gzip-request) – Robert Moskal Dec 30 '15 at 23:55
  • Not exactly, but what I'm gathering from the proposed duplicate is that it's really not recommended to send compressed entities to the server. However because I am in control of both the server and client, I know the server will accept a gzip encoded message. For optimum efficiency though, I would only want to compress large entities. – Domenic D. Dec 31 '15 at 03:13
  • They give you the outline of a solution. – Robert Moskal Dec 31 '15 at 03:51
  • [Might be related](http://stackoverflow.com/questions/19794014/why-does-jersey-swallow-my-content-encoding-header?rq=1) – Paul Samsotha Dec 31 '15 at 05:28

0 Answers0