users@jersey.java.net

[Jersey] Re: ChunkedOutput not producing Chunked output on browser

From: Marek Potociar <marek.potociar_at_oracle.com>
Date: Thu, 4 Dec 2014 23:17:56 +0100

The most likely reason is that the browser is caching the response data and only displays the result once all the response data are received.

Marek

> On 04 Dec 2014, at 04:28, gschandok <gschandok07_at_gmail.com> wrote:
>
> 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.