What could be more useful is a more general solution in a new module based
on Apache Commons Compress that calls
org.apache.commons.compress.compressors.CompressorStreamFactory.createCompressorOutputStream(String,
OutputStream).

That would give you support for:

GZIP
BZIP2
XZ
PACK200
LZMA
DEFLATE
SNAPPY_FRAMED
LZ4_BLOCK
LZ4_FRAMED
ZSTANDARD

Gary

On Mon, May 7, 2018 at 9:15 AM, Philippe Mouawad <
p.moua...@ubik-ingenierie.com> wrote:

> Hello,
> There are sometimes a requirement to automatically gzip the request body
> whenever Content-Encoding is set to gzip.
>
> Would you find it interesting to add this feature to HttpClient ?
>
> The implementation would look like:
>
>     private static final class GzipHttpRequestInterceptor implements
> HttpRequestInterceptor {
>         @Override
>         public void process(HttpRequest request, HttpContext context)
> throws HttpException, IOException {
>             if(request instanceof HttpEntityEnclosingRequest) {
>                 Header header = request.getFirstHeader("
> Content-Encoding");
>                 if(header != null && "gzip".equals(header.getValue())) {
>                     HttpEntityEnclosingRequest enclosingRequest =
> (HttpEntityEnclosingRequest) request;
>                     HttpEntity entity = enclosingRequest.getEntity();
>                     ByteArrayOutputStream out = new
> ByteArrayOutputStream();
>                     try (GZIPOutputStream gzipOS = new
> GZIPOutputStream(out)) {
>                         entity.writeTo(gzipOS);
>                     }
>                     enclosingRequest.setEntity(new
> ByteArrayEntity(out.toByteArray()));
>                 }
>             }
>         }
>     }
>
>
> Use could add it to processing like this:
>
>    - builder.addInterceptorFirst(new GzipHttpRequestInterceptor() );
>
> Or it could be enabled by default.
>
>
> Questions:
>
>    - Would you accept it as a PR ?
>    - Do you see immediate improvements in the provided code ?
>
> Thanks
> --
> Regards
> Philippe Mouawad.
> Ubik-Ingénierie
>

Reply via email to