Class Response


  • public final class Response
    extends java.lang.Object
    Response object.
    • Constructor Summary

      Constructors 
      Constructor Description
      Response()
      Default constructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      void action​(ActionCode actionCode, java.lang.Object param)
      Perform an action on this response.
      void addHeader​(java.lang.String name, java.lang.String value)
      Add a response header with the given name and value.
      void addHeader​(java.lang.String name, java.lang.String value, java.nio.charset.Charset charset)
      Add a response header with the given name, value, and charset.
      boolean checkRegisterForWrite()
      Check and register for write interest.
      void commit()
      Signal that we're done with the headers, and body will follow.
      boolean containsHeader​(java.lang.String name)
      Does the response contain the given header.
      void doWrite​(java.nio.ByteBuffer chunk)
      Write a chunk of bytes.
      long getBytesWritten​(boolean flush)
      Bytes written to socket - i.e. after compression, chunking, etc.
      java.lang.String getCharacterEncoding()  
      java.nio.charset.Charset getCharset()  
      long getCommitTime()
      Return the time the response was committed (based on System.currentTimeMillis).
      long getCommitTimeNanos()
      Return the time the response was committed (based on System.nanoTime).
      java.lang.String getContentLanguage()
      Return the content language.
      int getContentLength()
      Get the content length as an int.
      long getContentLengthLong()
      Get the content length as a long.
      java.lang.String getContentType()
      Get the content type.
      long getContentWritten()
      Bytes written by application - i.e. before compression, chunking, etc.
      java.lang.Exception getErrorException()
      Get the Exception that occurred during the writing of the response.
      java.util.Locale getLocale()
      Get the locale for this response.
      java.lang.String getMessage()
      Get the status message.
      MimeHeaders getMimeHeaders()
      Get the response headers.
      java.lang.Object getNote​(int pos)
      Get the note at the given position.
      Request getRequest()
      Get the associated request.
      int getStatus()
      Get the response status code.
      java.util.function.Supplier<java.util.Map<java.lang.String,​java.lang.String>> getTrailerFields()
      Get the trailer fields supplier.
      WriteListener getWriteListener()
      Get the write listener.
      boolean isCommitted()
      Check if the response has been committed.
      boolean isError()
      Error flag accessor.
      boolean isErrorReportRequired()
      Check if error report is required.
      boolean isExceptionPresent()
      Check if an exception has occurred during response writing.
      boolean isReady()
      Check if the response is ready for writing.
      void onWritePossible()
      Called when writing is possible.
      void recycle()
      Recycle this response.
      void reset()
      Reset the response to its initial state.
      void resetError()
      Reset the error state.
      void sendHeaders()
      Deprecated.
      Unused.
      void setCharacterEncoding​(java.lang.String characterEncoding)
      Overrides the character encoding used in the body of the response.
      void setCommitted​(boolean v)
      Set the committed flag.
      void setContentLength​(long contentLength)
      Set the content length.
      void setContentType​(java.lang.String type)
      Sets the content type.
      void setContentTypeNoCharset​(java.lang.String type)
      Set the content type without charset.
      boolean setError()
      Deprecated.
      This method will be changed to return void in Tomcat 11 onwards
      void setErrorException​(java.lang.Exception ex)
      Set the error Exception that occurred during the writing of the response processing.
      boolean setErrorReported()
      Set the error as reported.
      void setHeader​(java.lang.String name, java.lang.String value)
      Set a response header.
      void setLocale​(java.util.Locale locale)
      Called explicitly by user to set the Content-Language and the default encoding.
      void setMessage​(java.lang.String message)
      Set the status message.
      void setNote​(int pos, java.lang.Object value)
      Set a note at the given position.
      void setOutputBuffer​(OutputBuffer outputBuffer)
      Set the output buffer.
      void setRequest​(Request req)
      Set the associated request.
      void setStatus​(int status)
      Set the response status.
      void setTrailerFields​(java.util.function.Supplier<java.util.Map<java.lang.String,​java.lang.String>> supplier)
      Set the trailer fields supplier.
      void setWriteListener​(WriteListener listener)
      Set the write listener.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Response

        public Response()
        Default constructor.
    • Method Detail

      • getRequest

        public Request getRequest()
        Get the associated request.
        Returns:
        The associated request
      • setRequest

        public void setRequest​(Request req)
        Set the associated request.
        Parameters:
        req - The associated request
      • setOutputBuffer

        public void setOutputBuffer​(OutputBuffer outputBuffer)
        Set the output buffer.
        Parameters:
        outputBuffer - The output buffer
      • getMimeHeaders

        public MimeHeaders getMimeHeaders()
        Get the response headers.
        Returns:
        The response headers
      • setNote

        public void setNote​(int pos,
                            java.lang.Object value)
        Set a note at the given position.
        Parameters:
        pos - The position
        value - The value to set
      • getNote

        public java.lang.Object getNote​(int pos)
        Get the note at the given position.
        Parameters:
        pos - The position
        Returns:
        The note value
      • action

        public void action​(ActionCode actionCode,
                           java.lang.Object param)
        Perform an action on this response.
        Parameters:
        actionCode - The action code
        param - The action parameter
      • getStatus

        public int getStatus()
        Get the response status code.
        Returns:
        The response status code
      • setStatus

        public void setStatus​(int status)
        Set the response status.
        Parameters:
        status - The status value to set
      • getMessage

        public java.lang.String getMessage()
        Get the status message.
        Returns:
        The message associated with the current status
      • setMessage

        public void setMessage​(java.lang.String message)
        Set the status message.
        Parameters:
        message - The status message to set
      • isCommitted

        public boolean isCommitted()
        Check if the response has been committed.
        Returns:
        true if the response has been committed
      • setCommitted

        public void setCommitted​(boolean v)
        Set the committed flag.
        Parameters:
        v - The committed flag value
      • getCommitTime

        public long getCommitTime()
        Return the time the response was committed (based on System.currentTimeMillis).
        Returns:
        the time the response was committed
      • getCommitTimeNanos

        public long getCommitTimeNanos()
        Return the time the response was committed (based on System.nanoTime).
        Returns:
        the time the response was committed
      • setErrorException

        public void setErrorException​(java.lang.Exception ex)
        Set the error Exception that occurred during the writing of the response processing.
        Parameters:
        ex - The exception that occurred
      • getErrorException

        public java.lang.Exception getErrorException()
        Get the Exception that occurred during the writing of the response.
        Returns:
        The exception that occurred
      • isExceptionPresent

        public boolean isExceptionPresent()
        Check if an exception has occurred during response writing.
        Returns:
        true if an exception has occurred
      • setError

        @Deprecated
        public boolean setError()
        Deprecated.
        This method will be changed to return void in Tomcat 11 onwards
        Set the error flag.
        Returns:
        false if the error flag was already set
      • isError

        public boolean isError()
        Error flag accessor.
        Returns:
        true if the response has encountered an error
      • isErrorReportRequired

        public boolean isErrorReportRequired()
        Check if error report is required.
        Returns:
        true if error report is required
      • setErrorReported

        public boolean setErrorReported()
        Set the error as reported.
        Returns:
        true if the error state was successfully transitioned to reported
      • resetError

        public void resetError()
        Reset the error state.
      • reset

        public void reset()
                   throws java.lang.IllegalStateException
        Reset the response to its initial state.
        Throws:
        java.lang.IllegalStateException - if the response has already been committed
      • containsHeader

        public boolean containsHeader​(java.lang.String name)
        Does the response contain the given header.
        Warning: This method always returns false for Content-Type and Content-Length.
        Parameters:
        name - The name of the header of interest
        Returns:
        true if the response contains the header.
      • setHeader

        public void setHeader​(java.lang.String name,
                              java.lang.String value)
        Set a response header.
        Parameters:
        name - The header name
        value - The header value
      • addHeader

        public void addHeader​(java.lang.String name,
                              java.lang.String value)
        Add a response header with the given name and value.
        Parameters:
        name - The header name
        value - The header value
      • addHeader

        public void addHeader​(java.lang.String name,
                              java.lang.String value,
                              java.nio.charset.Charset charset)
        Add a response header with the given name, value, and charset.
        Parameters:
        name - The header name
        value - The header value
        charset - The charset for the header value
      • setTrailerFields

        public void setTrailerFields​(java.util.function.Supplier<java.util.Map<java.lang.String,​java.lang.String>> supplier)
        Set the trailer fields supplier.
        Parameters:
        supplier - The trailer fields supplier
      • getTrailerFields

        public java.util.function.Supplier<java.util.Map<java.lang.String,​java.lang.String>> getTrailerFields()
        Get the trailer fields supplier.
        Returns:
        The trailer fields supplier
      • sendHeaders

        @Deprecated
        public void sendHeaders()
        Deprecated.
        Unused. Will be removed in Tomcat 11 onwards. Use commit().
        Signal that we're done with the headers, and body will follow.
      • commit

        public void commit()
        Signal that we're done with the headers, and body will follow.
      • getLocale

        public java.util.Locale getLocale()
        Get the locale for this response.
        Returns:
        The locale for this response
      • setLocale

        public void setLocale​(java.util.Locale locale)
        Called explicitly by user to set the Content-Language and the default encoding.
        Parameters:
        locale - The locale to use for this response
      • getContentLanguage

        public java.lang.String getContentLanguage()
        Return the content language.
        Returns:
        The language code for the language currently associated with this response
      • setCharacterEncoding

        public void setCharacterEncoding​(java.lang.String characterEncoding)
                                  throws java.io.UnsupportedEncodingException
        Overrides the character encoding used in the body of the response. This method must be called prior to writing output using getWriter().
        Parameters:
        characterEncoding - The name of character encoding.
        Throws:
        java.io.UnsupportedEncodingException - If the specified name is not recognised
      • getCharset

        public java.nio.charset.Charset getCharset()
      • getCharacterEncoding

        public java.lang.String getCharacterEncoding()
        Returns:
        The name of the current encoding
      • setContentType

        public void setContentType​(java.lang.String type)
        Sets the content type. This method must preserve any response charset that may already have been set via a call to response.setContentType(), response.setLocale(), or response.setCharacterEncoding().
        Parameters:
        type - the content type
      • setContentTypeNoCharset

        public void setContentTypeNoCharset​(java.lang.String type)
        Set the content type without charset.
        Parameters:
        type - The content type
      • getContentType

        public java.lang.String getContentType()
        Get the content type.
        Returns:
        The content type, including charset if set
      • setContentLength

        public void setContentLength​(long contentLength)
        Set the content length.
        Parameters:
        contentLength - The content length
      • getContentLength

        public int getContentLength()
        Get the content length as an int.
        Returns:
        The content length as an int, or -1 if the length exceeds Integer.MAX_VALUE
      • getContentLengthLong

        public long getContentLengthLong()
        Get the content length as a long.
        Returns:
        The content length
      • doWrite

        public void doWrite​(java.nio.ByteBuffer chunk)
                     throws java.io.IOException
        Write a chunk of bytes.
        Parameters:
        chunk - The ByteBuffer to write
        Throws:
        java.io.IOException - If an I/O error occurs during the write
      • recycle

        public void recycle()
        Recycle this response.
      • getContentWritten

        public long getContentWritten()
        Bytes written by application - i.e. before compression, chunking, etc.
        Returns:
        The total number of bytes written to the response by the application. This will not be the number of bytes written to the network which may be more or less than this value.
      • getBytesWritten

        public long getBytesWritten​(boolean flush)
        Bytes written to socket - i.e. after compression, chunking, etc.
        Parameters:
        flush - Should any remaining bytes be flushed before returning the total? If false bytes remaining in the buffer will not be included in the returned value
        Returns:
        The total number of bytes written to the socket for this response
      • getWriteListener

        public WriteListener getWriteListener()
        Get the write listener.
        Returns:
        The write listener
      • setWriteListener

        public void setWriteListener​(WriteListener listener)
        Set the write listener.
        Parameters:
        listener - The write listener
      • isReady

        public boolean isReady()
        Check if the response is ready for writing.
        Returns:
        true if ready for writing
      • checkRegisterForWrite

        public boolean checkRegisterForWrite()
        Check and register for write interest.
        Returns:
        true if ready for write
      • onWritePossible

        public void onWritePossible()
                             throws java.io.IOException
        Called when writing is possible.
        Throws:
        java.io.IOException - If an I/O error occurs