0

I want to to gzip when size of output stream is greater than 100 bytes . I am trying various approaches but couldn’t get how can I get content-length from output stream .Value from Content-length in httpheader is incorrect

Could you please help me how can I find length of output stream

@Provider
@Compress
public class GzipInterceptor implements WriterInterceptor {

  @Context
  private HttpHeaders httpHeaders;

  @Override
  public void aroundWriteTo(WriterInterceptorContext context)
      throws IOException, WebApplicationException {

    List<String> acceptEncoding = null;
    if (httpHeaders != null) {
      MultivaluedMap<String, String> requestHeaders = httpHeaders.getRequestHeaders();
      acceptEncoding = requestHeaders.get(HttpHeaders.ACCEPT_ENCODING);
    }

    if (acceptEncoding != null) {
      for (String s : acceptEncoding) {

        if (s.contains(“gzip”)) {
            MultivaluedMap<String, Object> headers = context.getHeaders();
            OutputStream os = context.getOutputStream();
            if(//content-legth greater than 100 bytes)//I need some help here {
            headers.add(HttpHeaders.CONTENT_ENCODING, “”gzip”);
            context.setOutputStream(new GZIPOutputStream(os));
           }
          break;
        }
      }
    }

    context.proceed();
  }

}

Thanks for your help

santosh jk
  • 111
  • 1
  • 11
  • you're doing it wrong. There is no need to check if it's over 100b. Just gzip everything and follow the instructions already provided: https://stackoverflow.com/questions/55403910/gzip-encoding-in-jersey – Kamil Janowski Jun 01 '20 at 19:04
  • Does this answer your question? [GZip encoding in Jersey](https://stackoverflow.com/questions/55403910/gzip-encoding-in-jersey) – Kamil Janowski Jun 01 '20 at 19:04

1 Answers1

0

Instead of putting custom logic to enable GZip compression, you can directly enable gzip compression in your web/application server. Unless you have some other specific reason for doing the current approach.

For instance, the server is Apache Tomcat, you could do this (https://examples.javacodegeeks.com/enterprise-java/tomcat/enable-gzip-compression-apache-tomcat/).