Uploaded image for project: 'Sling'
  1. Sling
  2. SLING-8047

Rendered/exported JSON requested via SlingRequestProcessor is not written to output stream

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Won't Fix
    • None
    • None
    • Servlets
    • None

    Description

      While trying to facilitate the SlingRequestProcessor to get a rendered or exported JSON representation of a resource, we ran into the problem of the response always being empty. The HTML rendering worked without any issue.

      (See example code of Get the rendered HTML for an AEM resource, component or page or https://stackoverflow.com/a/34218708)

      Main, observed difference seem to be the method calls to print writer:

      JsonRenderer

      org.apache.sling.servlets.get.impl.helpers.JsonRenderer#render uses #write method of response's print writer.

      ExportServlet (via JacksonExporter)

      org.apache.sling.models.impl.ExportServlet#doGet uses #write method of response's print writer.

      HTMLRenderer

      org.apache.sling.servlets.get.impl.helpers.HtmlRenderer#render uses #print and #println methods of response's print writer.

      Further observations

      When the print writer's autoFlush property is set to true and one uses the #println method instead of #write, the JsonRenderer will flush the print writer's buffer to the output stream. Due to wrapping the response in org.apache.sling.scripting.core.impl.helper.OnDemandWriterResponse (where autoFlush=false) this does not work for the ExportServlet.

      When adding implicit #flush() calls to both classes, both request scenarios will work. According to the servlet specification, it should not be necessary to flush the print writer, as the container must immediately flush all remaining content to the client upon "termination of the service method of the servlet".

      Please find two test cases for (where implicit #flush() calls were added):

      Attachments

        Activity

          People

            jebailey Jason E Bailey
            mundt Philip Mundt
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0h
                0h
                Logged:
                Time Spent - 2h 50m
                2h 50m