title: How to selectively GZIP encode POST and PUT requests
body: <p>I'm using Jersey on both the server and client of a web application. On the server I have Interceptors as noted in <a href="https://jersey.java.net/documentation/latest/filters-and-interceptors.html" rel="nofollow">https://jersey.java.net/documentation/latest/filters-and-interceptors.html</a> 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? </p>
  
  <p>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). </p>
  
  <p>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?). </p>
  
  <p>Here's what I have so far, but it does not work since my header is removed:</p>
  
  <pre><code>class GzipWriterClientInterceptor implements WriterInterceptor {
      private static final Set&lt;String&gt; 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();
      }
  }
  </code></pre>
  
  <p>Sample Request:</p>
  
  <pre><code>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))
  </code></pre>
  
  <p>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. </p>