hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Johannes Kienzle <jkien...@salesforce.com>
Subject HttpAsyncClient 4.0-beta4 - GZip decompression doesn't work anymore
Date Tue, 08 Oct 2013 21:13:55 GMT
Hello,

it appears that in the latest version of the HttpAsyncClient (4.0-beta4),
adding a response interceptor for GZip decompression (GzipDecompressingEntity)
doesn't work anymore. Getting the HttpEntity from the response returns the
compressed gzip stream, rather than letting the interceptor decompress it.
It worked fine in previous releases. The same code also works fine with the
regular HttpClient (tested 4.3 and 4.3.1) as seen below.

Note that the request interceptor works fine and is utilized correctly (see
tests below).

Am I doing something wrong? Here are two tests to show the behavior, one
with HttpClient which works, one with HttpAsyncClient which doesn't work,
adopted from the official examples:

@Test
    public void testGZipSync() throws Exception {
     CloseableHttpClient httpclient = HttpClients.custom()
                .addInterceptorFirst(new HttpRequestInterceptor() {

                public void process(
                        final HttpRequest request,
                        final HttpContext context) throws HttpException,
IOException {
                    if (!request.containsHeader("Accept-Encoding")) {
                        request.addHeader("Accept-Encoding", "gzip");
                    }

                }}).addInterceptorFirst(new HttpResponseInterceptor() {

                public void process(
                        final HttpResponse response,
                        final HttpContext context) throws HttpException,
IOException {
                    HttpEntity entity = response.getEntity();
                    if (entity != null) {
                        Header ceheader = entity.getContentEncoding();
                        if (ceheader != null) {
                            HeaderElement[] codecs = ceheader.getElements();
                            for (int i = 0; i < codecs.length; i++) {
                                if
(codecs[i].getName().equalsIgnoreCase("gzip")) {
                                    response.setEntity(
                                            new
GzipDecompressingEntity(response.getEntity()));
                                    return;
                                }
                            }
                        }
                    }
                }

            }).build();
        try {
            HttpGet httpget = new HttpGet("http://www.apache.org/");
            CloseableHttpResponse response = httpclient.execute(httpget);
            try {
                HttpEntity entity = response.getEntity();

                if (entity != null) {
                    // this content is correct
                    String content = EntityUtils.toString(entity);
                    System.out.println(content);
                }
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
    }

    @Test
    public void testGZipAsync() throws Exception {
     CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom()
                .addInterceptorFirst(new HttpRequestInterceptor() {

                public void process(
                        final HttpRequest request,
                        final HttpContext context) throws HttpException,
IOException {
                    if (!request.containsHeader("Accept-Encoding")) {
                        request.addHeader("Accept-Encoding", "gzip");
                    }

                }}).addInterceptorFirst(new HttpResponseInterceptor() {

                public void process(
                        final HttpResponse response,
                        final HttpContext context) throws HttpException,
IOException {
                    HttpEntity entity = response.getEntity();
                    if (entity != null) {
                        Header ceheader = entity.getContentEncoding();
                        if (ceheader != null) {
                            HeaderElement[] codecs = ceheader.getElements();
                            for (int i = 0; i < codecs.length; i++) {
                                if
(codecs[i].getName().equalsIgnoreCase("gzip")) {
                                    response.setEntity(
                                            new
GzipDecompressingEntity(response.getEntity()));
                                    return;
                                }
                            }
                        }
                    }
                }

            }).build();

        httpclient.start();
        try {
            final HttpGet request = new HttpGet("http://www.apache.org/");
            final Future<HttpResponse> future = httpclient.execute(request,
null);
            final HttpResponse response = future.get();
            HttpEntity entity = response.getEntity();

            if (entity != null) {
                // this content is incorrect! It's not being decompressed.
                String content = EntityUtils.toString(entity);
                System.out.println(content);
            }
        } finally {
            httpclient.close();
        }
    }


Thanks,
Johannes

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message