Top Description Fields Constructors Methods
org.apache.coyote

public final Class Response

extends Object
Class Inheritance
Imports
java.io.IOException, .StringReader, .UnsupportedEncodingException, java.nio.ByteBuffer, java.nio.charset.Charset, java.util.Locale, .Map, java.util.concurrent.TimeUnit, java.util.concurrent.atomic.AtomicBoolean, .AtomicInteger, java.util.function.Supplier, jakarta.servlet.WriteListener, org.apache.juli.logging.Log, .LogFactory, org.apache.tomcat.util.buf.B2CConverter, .MessageBytes, org.apache.tomcat.util.http.MimeHeaders, org.apache.tomcat.util.http.parser.MediaType, org.apache.tomcat.util.res.StringManager

Response object.
Authors
James Duncan Davidson [[email protected]], Jason Hunter [[email protected]], James Todd [[email protected]], Harish Prabandham, Hans Bergsten [[email protected]], Remy Maucherat

Field Summary

Modifier and TypeField and Description
pack-priv String
pack-priv Charset
pack-priv volatile boolean
committed

Committed flag.

private long
pack-priv String
pack-priv long
pack-priv String
contentType

HTTP specific fields.

private long
private static final Locale
DEFAULT_LOCALE

Default locale as mandated by the spec.

private Exception
errorException

Holds response writing error exception.

private final AtomicInteger
errorState

With the introduction of async processing and the possibility of non-container threads calling sendError() tracking the current error state and ensuring that the correct error page is called becomes more complicated.

private boolean
pack-priv final MimeHeaders
headers

Response headers.

pack-priv volatile ActionHook
hook

Action hook.

pack-priv volatile WriteListener
private Locale
private static final Log
pack-priv String
message

Status message.

private final Object
pack-priv final Object[]
notes

Notes.

pack-priv OutputBuffer
outputBuffer

Associated output buffer.

private boolean
pack-priv Request
private static final StringManager
pack-priv int
status

Status code.

private Supplier<Map<String, String>>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
action(ActionCode actionCode, Object param)

public void
addHeader(String name, String value)

public void
addHeader(String name, String value, Charset charset)

public boolean
private boolean
checkSpecialHeader(String name, String value)

Set internal fields for special header names.

public boolean

Returns:

true if the response contains the header.
containsHeader
(String
The name of the header of interest
name
)

Does the response contain the given header.

public void
doWrite(ByteBuffer
The ByteBuffer to write
chunk
)

Write a chunk of bytes.

public long

Returns:

The total number of bytes written to the socket for this response
getBytesWritten
(boolean
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
flush
)

Bytes written to socket - i.e. after compression, chunking, etc.

public String

Returns:

The name of the current encoding
getCharacterEncoding
()

public Charset
public long

Returns:

the time the response was committed
getCommitTime
()

Return the time the response was committed (based on System.currentTimeMillis).

public long

Returns:

the time the response was committed
getCommitTimeNanos
()

Return the time the response was committed (based on System.nanoTime).

public String

Returns:

The language code for the language currently associated with this response
getContentLanguage
()

Return the content language.

public int
public long
public String
public long

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.
getContentWritten
()

Bytes written by application - i.e. before compression, chunking, etc.

public Exception

Returns:

The exception that occurred
getErrorException
()

Get the Exception that occurred during the writing of the response.

public Locale
public String

Returns:

The message associated with the current status
getMessage
()

Get the status message.

public MimeHeaders
public Object
getNote(int pos)

public Request
public int
public Supplier<Map<String, String>>
public WriteListener
public boolean
public boolean

Returns:

true if the response has encountered an error
isError
()

Error flag accessor.

public boolean
public boolean
public boolean
public void
public void
public void
public void
sendHeaders()

Signal that we're done with the headers, and body will follow.

public void
setCharacterEncoding(String
The name of character encoding.
characterEncoding
)

Overrides the character encoding used in the body of the response.

public void
setCommitted(boolean v)

public void
setContentLength(long contentLength)

public void
setContentType(String
the content type
type
)

Sets the content type.

public void
public boolean

Returns:

false if the error flag was already set
setError
()
Deprecated This method will be changed to return void in Tomcat 11 onwards

Set the error flag.

public void
setErrorException(Exception
The exception that occurred
ex
)

Set the error Exception that occurred during the writing of the response processing.

public boolean
public void
setHeader(String name, String value)

protected void
public void
setLocale(Locale
The locale to use for this response
locale
)

Called explicitly by user to set the Content-Language and the default encoding.

public void
setMessage(String
The status message to set
message
)

Set the status message.

public void
setNote(int pos, Object value)

public void
public void
public void
setStatus(int
The status value to set
status
)

Set the response status.

public void
public void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

characterEncodingback to summary
pack-priv String characterEncoding
charsetback to summary
pack-priv Charset charset
committedback to summary
pack-priv volatile boolean committed

Committed flag.

commitTimeNanosback to summary
private long commitTimeNanos
contentLanguageback to summary
pack-priv String contentLanguage
contentLengthback to summary
pack-priv long contentLength
contentTypeback to summary
pack-priv String contentType

HTTP specific fields.

contentWrittenback to summary
private long contentWritten
DEFAULT_LOCALEback to summary
private static final Locale DEFAULT_LOCALE

Default locale as mandated by the spec.

errorExceptionback to summary
private Exception errorException

Holds response writing error exception.

errorStateback to summary
private final AtomicInteger errorState

With the introduction of async processing and the possibility of non-container threads calling sendError() tracking the current error state and ensuring that the correct error page is called becomes more complicated. This state attribute helps by tracking the current error state and informing callers that attempt to change state if the change was successful or if another thread got there first.

The state machine is very simple:

0 - NONE
1 - NOT_REPORTED
2 - REPORTED


  -->---->-- >NONE
  |   |        |
  |   |        | setError()
  ^   ^        |
  |   |       \|/
  |   |-<-NOT_REPORTED
  |            |
  ^            | report()
  |            |
  |           \|/
  |----<----REPORTED
fireListenerback to summary
private boolean fireListener
headersback to summary
pack-priv final MimeHeaders headers

Response headers.

hookback to summary
pack-priv volatile ActionHook hook

Action hook.

listenerback to summary
pack-priv volatile WriteListener listener
localeback to summary
private Locale locale
logback to summary
private static final Log log
messageback to summary
pack-priv String message

Status message.

nonBlockingStateLockback to summary
private final Object nonBlockingStateLock
notesback to summary
pack-priv final Object[] notes

Notes.

outputBufferback to summary
pack-priv OutputBuffer outputBuffer

Associated output buffer.

registeredForWriteback to summary
private boolean registeredForWrite
reqback to summary
pack-priv Request req
smback to summary
private static final StringManager sm
statusback to summary
pack-priv int status

Status code.

trailerFieldsSupplierback to summary
private Supplier<Map<String, String>> trailerFieldsSupplier

Constructor Detail

Responseback to summary
public Response()

Method Detail

actionback to summary
public void action(ActionCode actionCode, Object param)
addHeaderback to summary
public void addHeader(String name, String value)
addHeaderback to summary
public void addHeader(String name, String value, Charset charset)
checkRegisterForWriteback to summary
public boolean checkRegisterForWrite()
checkSpecialHeaderback to summary
private boolean checkSpecialHeader(String name, String value)

Set internal fields for special header names. Called from set/addHeader. Return true if the header is special, no need to set the header.

containsHeaderback to summary
public boolean containsHeader(String name)

Does the response contain the given header.

Warning

This method always returns false for Content-Type and Content-Length.

Parameters
name:String

The name of the header of interest

Returns:boolean

true if the response contains the header.

doWriteback to summary
public void doWrite(ByteBuffer chunk) throws IOException

Write a chunk of bytes.

Parameters
chunk:ByteBuffer

The ByteBuffer to write

Exceptions
IOException:
If an I/O error occurs during the write
getBytesWrittenback to summary
public long getBytesWritten(boolean flush)

Bytes written to socket - i.e. after compression, chunking, etc.

Parameters
flush:boolean

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:long

The total number of bytes written to the socket for this response

getCharacterEncodingback to summary
public String getCharacterEncoding()
Returns:String

The name of the current encoding

getCharsetback to summary
public Charset getCharset()
getCommitTimeback to summary
public long getCommitTime()

Return the time the response was committed (based on System.currentTimeMillis).

Returns:long

the time the response was committed

getCommitTimeNanosback to summary
public long getCommitTimeNanos()

Return the time the response was committed (based on System.nanoTime).

Returns:long

the time the response was committed

getContentLanguageback to summary
public String getContentLanguage()

Return the content language.

Returns:String

The language code for the language currently associated with this response

getContentLengthback to summary
public int getContentLength()
getContentLengthLongback to summary
public long getContentLengthLong()
getContentTypeback to summary
public String getContentType()
getContentWrittenback to summary
public long getContentWritten()

Bytes written by application - i.e. before compression, chunking, etc.

Returns:long

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.

getErrorExceptionback to summary
public Exception getErrorException()

Get the Exception that occurred during the writing of the response.

Returns:Exception

The exception that occurred

getLocaleback to summary
public Locale getLocale()
getMessageback to summary
public String getMessage()

Get the status message.

Returns:String

The message associated with the current status

getMimeHeadersback to summary
public MimeHeaders getMimeHeaders()
getNoteback to summary
public Object getNote(int pos)
getRequestback to summary
public Request getRequest()
getStatusback to summary
public int getStatus()
getTrailerFieldsback to summary
public Supplier<Map<String, String>> getTrailerFields()
getWriteListenerback to summary
public WriteListener getWriteListener()
isCommittedback to summary
public boolean isCommitted()
isErrorback to summary
public boolean isError()

Error flag accessor.

Returns:boolean

true if the response has encountered an error

isErrorReportRequiredback to summary
public boolean isErrorReportRequired()
isExceptionPresentback to summary
public boolean isExceptionPresent()
isReadyback to summary
public boolean isReady()
onWritePossibleback to summary
public void onWritePossible() throws IOException
recycleback to summary
public void recycle()
resetback to summary
public void reset() throws IllegalStateException
sendHeadersback to summary
public void sendHeaders()

Signal that we're done with the headers, and body will follow. Any implementation needs to notify ContextManager, to allow interceptors to fix headers.

setCharacterEncodingback to summary
public void setCharacterEncoding(String characterEncoding) throws 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:String

The name of character encoding.

Exceptions
UnsupportedEncodingException:
If the specified name is not recognised
setCommittedback to summary
public void setCommitted(boolean v)
setContentLengthback to summary
public void setContentLength(long contentLength)
setContentTypeback to summary
public void setContentType(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:String

the content type

setContentTypeNoCharsetback to summary
public void setContentTypeNoCharset(String type)
setErrorback to summary
public boolean setError()

Deprecated

This method will be changed to return void in Tomcat 11 onwards

Set the error flag.

Returns:boolean

false if the error flag was already set

Annotations
@Deprecated
setErrorExceptionback to summary
public void setErrorException(Exception ex)

Set the error Exception that occurred during the writing of the response processing.

Parameters
ex:Exception

The exception that occurred

setErrorReportedback to summary
public boolean setErrorReported()
setHeaderback to summary
public void setHeader(String name, String value)
setHookback to summary
protected void setHook(ActionHook hook)
setLocaleback to summary
public void setLocale(Locale locale)

Called explicitly by user to set the Content-Language and the default encoding.

Parameters
locale:Locale

The locale to use for this response

setMessageback to summary
public void setMessage(String message)

Set the status message.

Parameters
message:String

The status message to set

setNoteback to summary
public void setNote(int pos, Object value)
setOutputBufferback to summary
public void setOutputBuffer(OutputBuffer outputBuffer)
setRequestback to summary
public void setRequest(Request req)
setStatusback to summary
public void setStatus(int status)

Set the response status.

Parameters
status:int

The status value to set

setTrailerFieldsback to summary
public void setTrailerFields(Supplier<Map<String, String>> supplier)
setWriteListenerback to summary
public void setWriteListener(WriteListener listener)