2

I'm using a connection created by ThreadSafeClientConnManager (Apache httpcomponents 4.1.1). The response is chunked (which I expect), as is determined by response.getEntity().isChunked()

However, there is no way to get the footers/trailers (which are necessary for our application). Since the response was chunked, I would expect the entity contents to be of type ChunkedInputStream, however the default request director and executor classes used by the client wrap the original response entity (which, from looking at the httpcomponents source would have been a ChunkedInputStream) in a BasicManagedEntity.

In short, I am no longer able to get the footers/trailers off of the response, as BasicManagedEntity does not make the underlying entity available for use. Does anyone know how to work around this?

For reference, see:

  • org.apache.http.impl.client.DefaultRequestDirector.java, lines 523-525
  • org.apache.http.impl.entity.EntityDeserializer.java, lines 93-96
skaffman
  • 398,947
  • 96
  • 818
  • 769
Brian
  • 3,457
  • 4
  • 31
  • 41
  • Side note: I've fixed this for now by implementing various subclasses and wrappers for HttpClient, HttpResponse, and HttpRequestExecutor, but I'm still looking for a better solution! – Brian Jul 07 '11 at 22:31

2 Answers2

4

One can use an HTTP response interceptor in order to access to the chunked content stream and response footers.

httpclient.addResponseInterceptor(new HttpResponseInterceptor() {

public void process(
        final HttpResponse response,
        final HttpContext context) throws HttpException, IOException {
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        InputStream instream = entity.getContent();
        if (instream instanceof ChunkedInputStream) {
            Header[] footers = ((ChunkedInputStream) instream).getFooters();
        }
    }
}

});

ok2c
  • 26,450
  • 5
  • 63
  • 71
0

As described in the answer, this can be done when using the deprecated DefaultHttpClient. For the newer non-deprecated HttpClients, there is a bug https://issues.apache.org/jira/browse/HTTPCLIENT-1992 which prevents trailers from being accessed in version 4.5. This bug has been fixed in 5.0

So in v4.5, below won't work.

 CloseableHttpClient httpclient = HttpClients.custom().addInterceptorFirst(
            (org.apache.http.HttpResponse response, HttpContext context) -> {
                InputStream instream = response.getEntity().getContent();
                if (instream instanceof ChunkedInputStream) {
                    //Code will never run for v4.5
                }
            }
    ).build();
CamW
  • 3,223
  • 24
  • 34