users@jersey.java.net

[Jersey] ChunkedOutput not producing Chunked output on browser

From: gschandok <gschandok07_at_gmail.com>
Date: Wed, 3 Dec 2014 20:28:41 -0700 (MST)

Hi,

I am using org.glassfish.jersey.server.ChunkedOutput to get the chunked
response to my request. When I hit the URL through browser, instead of
getting output as separate chunks, I am getting all the chunks at once.
But when I use a Test Client to hit the resource, I get the output as
separate chunks.

Server Used: Glassfish 4.0
Jersey version 2.13

Resource method is as follows:

@GET
@Path("chunk")
public ChunkedOutput<String> getChunkedResponse(@Context HttpServletRequest
request) {

    final ChunkedOutput<String> output = new ChunkedOutput<String>(
            String.class);

    new Thread() {
        public void run() {
            try {
                Thread.sleep(2000);
                String chunk;
                String arr[] = { "America\r\n", "London\r\n", "Delhi\r\n",
"null" };
                int i = 0;
                while (!(chunk = arr[i]).equals("null")) {
                    output.write(chunk);
                    i++;
                    Thread.sleep(2000);
                }
            } catch (IOException e) {
                logger.error("IOException : ", e);
            } catch (InterruptedException e) {
                logger.error("InterruptedException : ", e);
                e.printStackTrace();
            } finally {
                try {
                    output.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    logger.error("IOException IN finally : ", e);
                }
            }
        }
    }.start();

    // the output will be probably returned even before
    // a first chunk is written by the new thread
    return output;
}

Test Client method is as follows:

  private static void testChunkedResponse(WebTarget target){
      final Response response = target.path("restRes").path("chunk")
                .request().get();
        final ChunkedInput<String> chunkedInput =
                response.readEntity(new
GenericType<ChunkedInput&lt;String>>() {});
        String chunk;
        while ((chunk = chunkedInput.read()) != null) {
            logger.info("Next chunk received: " + chunk);
        }
  }

Can someone please help me understand why response is not getting chunked on
browser and what can be done about it?




--
View this message in context: http://jersey.576304.n2.nabble.com/ChunkedOutput-not-producing-Chunked-output-on-browser-tp7582908.html
Sent from the Jersey mailing list archive at Nabble.com.