Uploaded image for project: 'RESTEasy'
  1. RESTEasy
  2. RESTEASY-1721

ServerResponseWriter sets HTTP status 204 even though response was written to HttpServletResponse

XMLWordPrintable

      Suppose that endpoint handler manipulates with HttpServletResponse directly and returns no entity:

      @GET
      @Path("/data")
      public void getData(@Context HttpServletResponse response) {
          response.setStatus(200);
          response.getOutputStream().write(...);
      }
      

      In this case unfortunately ServerResponseWriter overwrites status code to 204 (see line 91), which is not desired, as the client gets HTTP 204 and non-empty body (note that body is small enough to be buffered).

      Is it possible for ServerResponseWriter to learn that the body is already written? If not, consider the following workaround in which the handler should commit the response:

      @GET
      @Path("/data")
      public void getData(@Context HttpServletResponse response) {
          response.setStatus(200);
          response.getOutputStream().write(...);
          response.flushBuffer(); // will commit the response
      }
      

      In this case the corresponding code in ServerResponseWriter can be:

      if (jaxrsResponse.getEntity() == null || response.getOutputStream() == null)
      {
         if (!response.isCommitted())
         {
             // If response is already committed then setting the status/headers has no effect:
             response.setStatus(jaxrsResponse.getStatus());
             commitHeaders(jaxrsResponse, response);
         }
         return;
      }
      

              rsigal@redhat.com Ronald Sigal
              dma_k Dmitry Katsubo (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Created:
                Updated:
                Resolved: