Uploaded image for project: 'MyFaces Core'
  1. MyFaces Core
  2. MYFACES-4527

jakarta.servlet.ServletException: setBufferSize() called after first write to Output Stream/Writer

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.2.15, 2.3.10, 2.3-next-M7, 4.0.0-RC2
    • 2.3.11, 3.0.3, 2.3-next-M8, 4.0.0-RC3
    • None
    • None

    Description

      This was discovered in TCK: Spec1135IT.

      Test: https://github.com/jakartaee/faces/blob/4.0.1/tck/faces23/systemEvent/src/test/java/ee/jakarta/tck/faces/test/servlet40/systemevent/Spec1135IT.java#L71

      App Code: https://github.com/jakartaee/faces/tree/master/tck/faces23/systemEvent/src/main ( App was updated, using master branch for latest code)

          public void pre(ComponentSystemEvent event) throws IOException {
              facesContext.setResponseWriter(facesContext.getRenderKit().createResponseWriter(externalContext.getResponseOutputWriter(), "text/html", "UTF-8"));
              facesContext.getResponseWriter().write("<!-- pre -->");
          }
      

      The app uses has a method ("pre") which is invoked before the view has started to be rendered. This method creates a response writer of its own, and then it writes a block of text. Then, by the time MyFaces is ready to create the response writer and set the buffer size, it's already been written to. Thus the ServletException occurs.

      Per the Servlet Spec:
      setBufferSize throws an IllegalStateException if this method is called after content has been written

      https://jakarta.ee/specifications/servlet/6.0/apidocs/jakarta.servlet/jakarta/servlet/servletresponse#setBufferSize(int)

      Solution seems to be to create a response writer if none exist.
      https://github.com/apache/myfaces/blob/db37634747e11ba24216ea384c33ad3c6d2cefcb/impl/src/main/java/org/apache/myfaces/view/facelets/FaceletViewDeclarationLanguage.java#L1747

      Attachments

        1. test-faces23-systemEvent.war
          6 kB
          Volodymyr Siedlecki

        Issue Links

          Activity

            People

              volosied Volodymyr Siedlecki
              volosied Volodymyr Siedlecki
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: