Package org.apache.coyote
Class Request
- java.lang.Object
-
- org.apache.coyote.Request
-
public final class Request extends java.lang.ObjectThis is a low-level, efficient representation of a server request. Most fields are GC-free, expensive operations are delayed until the user code needs the information. Processing is delegated to modules, using a hook mechanism. This class is not intended for user code - it is used internally by tomcat for processing the request in the most efficient way. Users ( servlets ) can access the information using a facade, which provides the high-level view of the request. Tomcat defines a number of attributes:- "org.apache.tomcat.request" - allows access to the low-level request object in trusted applications
-
-
Field Summary
Fields Modifier and Type Field Description static intNOTE_BAD_REQUESTNote key for bad request.
-
Constructor Summary
Constructors Constructor Description Request()Default constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidaction(ActionCode actionCode, java.lang.Object param)Perform an action on this request.voidaddPathParameter(java.lang.String name, java.lang.String value)Add a path parameter.voidclearRequestThread()Clear the request thread ID.MessageBytescontentType()Return the content type.MessageBytesdecodedURI()Return the decoded request URI.intdoRead(ApplicationBufferHandler handler)Read data from the input buffer and put it into ApplicationBufferHandler.java.lang.ObjectgetAttribute(java.lang.String name)Return the value of the specified attribute.java.util.HashMap<java.lang.String,java.lang.Object>getAttributes()Return the request attributes map.MessageBytesgetAuthType()Return the authentication type.intgetAvailable()Return the number of available bytes.longgetBytesRead()Return the number of bytes read.java.lang.StringgetCharacterEncoding()Get the character encoding used for this request.java.nio.charset.CharsetgetCharset()Get the character encoding used for this request.intgetContentLength()Return the content length.longgetContentLengthLong()Return the content length as a long.java.lang.StringgetContentType()Return the content type as a string.ServerCookiesgetCookies()Return the server cookies.java.lang.ExceptiongetErrorException()Get the Exception that occurred during the writing of the response.java.lang.StringgetHeader(java.lang.String name)Return the value of the specified header.InputBuffergetInputBuffer()Return the input buffer.intgetLocalPort()Return the local port.java.lang.StringgetMethod()Return the HTTP method.MimeHeadersgetMimeHeaders()Return the MIME headers.MimeHeadersgetMimeTrailerFields()Return the MIME trailer fields.java.lang.ObjectgetNote(int pos)Return the note at the specified position.ParametersgetParameters()Return the request parameters.java.lang.StringgetPathParameter(java.lang.String name)Return the value of the specified path parameter.java.lang.StringgetProtocolRequestId()Return the protocol request ID.ReadListenergetReadListener()Return the read listener.intgetRemotePort()Return the remote port.MessageBytesgetRemoteUser()Return the remote user.booleangetRemoteUserNeedsAuthorization()Check if the remote user needs authorization.java.lang.StringgetRequestId()Return the request ID.RequestInfogetRequestProcessor()Return the request processor info.ResponsegetResponse()Return the associated response.booleangetSendfile()Check if sendfile is enabled.intgetServerPort()Return the server port.ServletConnectiongetServletConnection()Return the servlet connection.java.time.InstantgetStartInstant()Return the request start instant.longgetStartTime()Return the request start time in milliseconds.longgetStartTimeNanos()Return the request start time in nanoseconds.booleangetSupportsRelativeRedirects()Check if relative redirects are supported.longgetThreadId()Return the thread ID.java.util.Map<java.lang.String,java.lang.String>getTrailerFields()Return the trailer fields as a map.UDecodergetURLDecoder()Return the URL decoder.booleanhasExpectation()Check if there is an expectation.booleanisExceptionPresent()Check if an error exception is present.booleanisFinished()Check if the request body has been fully read.booleanisProcessing()Check if the request is currently being processed.booleanisReady()Check if the request is ready for reading.booleanisRequestThread()Check if the current thread is the request thread.booleanisTrailerFieldsReady()Check if trailer fields are ready.MessageByteslocalAddr()Return the local address.MessageByteslocalName()Return the local name.voidmarkStartTime()Mark the request start time.MessageBytesmethod()Deprecated.voidonDataAvailable()Called when data is available for reading.MessageBytespeerAddr()Return the peer address.MessageBytesprotocol()Return the protocol.MessageBytesqueryString()Return the query string.voidrecycle()Recycle this request for reuse.MessageBytesremoteAddr()Return the remote address.MessageBytesremoteHost()Return the remote host.MessageBytesrequestURI()Return the request URI.MessageBytesscheme()Return the scheme (e.g.booleansendAllDataReadEvent()Send the all-data-read event.MessageBytesserverName()Get the "virtual host", derived from the Host: header associated with this request.voidsetAttribute(java.lang.String name, java.lang.Object o)Set an attribute.voidsetAvailable(int available)Set the number of available bytes.voidsetCharset(java.nio.charset.Charset charset)voidsetContentLength(long len)Set the content length.voidsetContentType(java.lang.String type)Set the content type.voidsetContentType(MessageBytes mb)Set the content type.voidsetErrorException(java.lang.Exception ex)Set the error Exception that occurred during the writing of the response processing.voidsetExpectation(boolean expectation)Set the expectation flag.voidsetInputBuffer(InputBuffer inputBuffer)Set the input buffer.voidsetLocalPort(int port)Set the local port.voidsetMethod(byte[] buf, int start, int len)Set the HTTP method from a byte buffer.voidsetMethod(java.lang.String method)Set the HTTP method.voidsetNote(int pos, java.lang.Object value)Used to store private data.voidsetReadListener(ReadListener listener)Set the read listener for non-blocking reads.voidsetRemotePort(int port)Set the remote port.voidsetRemoteUserNeedsAuthorization(boolean remoteUserNeedsAuthorization)Set whether the remote user needs authorization.voidsetRequestThread()Set the request thread ID.voidsetResponse(Response response)Set the associated response.voidsetSendfile(boolean sendfile)Set whether sendfile is enabled.voidsetServerPort(int serverPort)Set the server port.voidsetStartTime(long startTime)Deprecated.This setter will be removed in Tomcat 11.voidsetStartTimeNanos(long startTimeNanos)Deprecated.Unused.java.lang.StringtoString()voidupdateCounters()Update request counters.
-
-
-
Field Detail
-
NOTE_BAD_REQUEST
public static final int NOTE_BAD_REQUEST
Note key for bad request.- See Also:
- Constant Field Values
-
-
Method Detail
-
getReadListener
public ReadListener getReadListener()
Return the read listener.- Returns:
- the read listener
-
setReadListener
public void setReadListener(ReadListener listener)
Set the read listener for non-blocking reads.- Parameters:
listener- the read listener
-
isReady
public boolean isReady()
Check if the request is ready for reading.- Returns:
trueif ready for reading
-
onDataAvailable
public void onDataAvailable() throws java.io.IOExceptionCalled when data is available for reading.- Throws:
java.io.IOException- if an I/O error occurs
-
sendAllDataReadEvent
public boolean sendAllDataReadEvent()
Send the all-data-read event.- Returns:
trueif the event was sent
-
getMimeHeaders
public MimeHeaders getMimeHeaders()
Return the MIME headers.- Returns:
- the MIME headers
-
isTrailerFieldsReady
public boolean isTrailerFieldsReady()
Check if trailer fields are ready.- Returns:
trueif trailer fields are ready
-
getTrailerFields
public java.util.Map<java.lang.String,java.lang.String> getTrailerFields()
Return the trailer fields as a map.- Returns:
- the trailer fields
-
getMimeTrailerFields
public MimeHeaders getMimeTrailerFields()
Return the MIME trailer fields.- Returns:
- the MIME trailer fields
-
getURLDecoder
public UDecoder getURLDecoder()
Return the URL decoder.- Returns:
- the URL decoder
-
scheme
public MessageBytes scheme()
Return the scheme (e.g. "http" or "https").- Returns:
- the scheme
-
method
@Deprecated public MessageBytes method()
Deprecated.Get a MessageBytes instance that holds the current request's HTTP method.- Returns:
- a MessageBytes instance that holds the current request's HTTP method.
-
setMethod
public void setMethod(java.lang.String method)
Set the HTTP method.- Parameters:
method- the HTTP method
-
setMethod
public void setMethod(byte[] buf, int start, int len)Set the HTTP method from a byte buffer.- Parameters:
buf- the byte bufferstart- the start offsetlen- the length
-
getMethod
public java.lang.String getMethod()
Return the HTTP method.- Returns:
- the HTTP method
-
requestURI
public MessageBytes requestURI()
Return the request URI.- Returns:
- the request URI
-
decodedURI
public MessageBytes decodedURI()
Return the decoded request URI.- Returns:
- the decoded URI
-
queryString
public MessageBytes queryString()
Return the query string.- Returns:
- the query string
-
protocol
public MessageBytes protocol()
Return the protocol.- Returns:
- the protocol
-
serverName
public MessageBytes serverName()
Get the "virtual host", derived from the Host: header associated with this request.- Returns:
- The buffer holding the server name, if any. Use isNull() to check if there is no value set.
-
getServerPort
public int getServerPort()
Return the server port.- Returns:
- the server port
-
setServerPort
public void setServerPort(int serverPort)
Set the server port.- Parameters:
serverPort- the server port
-
remoteAddr
public MessageBytes remoteAddr()
Return the remote address.- Returns:
- the remote address
-
peerAddr
public MessageBytes peerAddr()
Return the peer address.- Returns:
- the peer address
-
remoteHost
public MessageBytes remoteHost()
Return the remote host.- Returns:
- the remote host
-
localName
public MessageBytes localName()
Return the local name.- Returns:
- the local name
-
localAddr
public MessageBytes localAddr()
Return the local address.- Returns:
- the local address
-
getRemotePort
public int getRemotePort()
Return the remote port.- Returns:
- the remote port
-
setRemotePort
public void setRemotePort(int port)
Set the remote port.- Parameters:
port- the remote port
-
getLocalPort
public int getLocalPort()
Return the local port.- Returns:
- the local port
-
setLocalPort
public void setLocalPort(int port)
Set the local port.- Parameters:
port- the local port
-
getCharacterEncoding
public java.lang.String getCharacterEncoding()
Get the character encoding used for this request.- Returns:
- The value set via
setCharset(Charset)or if no call has been made to that method try to obtain if from the content type.
-
getCharset
public java.nio.charset.Charset getCharset() throws java.io.UnsupportedEncodingExceptionGet the character encoding used for this request.- Returns:
- The value set via
setCharset(Charset)or if no call has been made to that method try to obtain if from the content type. - Throws:
java.io.UnsupportedEncodingException- If the user agent has specified an invalid character encoding
-
setCharset
public void setCharset(java.nio.charset.Charset charset)
-
setContentLength
public void setContentLength(long len)
Set the content length.- Parameters:
len- the content length
-
getContentLength
public int getContentLength()
Return the content length.- Returns:
- the content length
-
getContentLengthLong
public long getContentLengthLong()
Return the content length as a long.- Returns:
- the content length
-
getContentType
public java.lang.String getContentType()
Return the content type as a string.- Returns:
- the content type
-
setContentType
public void setContentType(java.lang.String type)
Set the content type.- Parameters:
type- the content type
-
contentType
public MessageBytes contentType()
Return the content type.- Returns:
- the content type
-
setContentType
public void setContentType(MessageBytes mb)
Set the content type.- Parameters:
mb- the content type
-
getHeader
public java.lang.String getHeader(java.lang.String name)
Return the value of the specified header.- Parameters:
name- the header name- Returns:
- the header value
-
setExpectation
public void setExpectation(boolean expectation)
Set the expectation flag.- Parameters:
expectation- the expectation flag
-
hasExpectation
public boolean hasExpectation()
Check if there is an expectation.- Returns:
trueif there is an expectation
-
getResponse
public Response getResponse()
Return the associated response.- Returns:
- the response
-
setResponse
public void setResponse(Response response)
Set the associated response.- Parameters:
response- the response
-
action
public void action(ActionCode actionCode, java.lang.Object param)
Perform an action on this request.- Parameters:
actionCode- the action codeparam- the action parameter
-
getCookies
public ServerCookies getCookies()
Return the server cookies.- Returns:
- the server cookies
-
getParameters
public Parameters getParameters()
Return the request parameters.- Returns:
- the parameters
-
addPathParameter
public void addPathParameter(java.lang.String name, java.lang.String value)Add a path parameter.- Parameters:
name- the parameter namevalue- the parameter value
-
getPathParameter
public java.lang.String getPathParameter(java.lang.String name)
Return the value of the specified path parameter.- Parameters:
name- the parameter name- Returns:
- the parameter value
-
setAttribute
public void setAttribute(java.lang.String name, java.lang.Object o)Set an attribute.- Parameters:
name- the attribute nameo- the attribute value
-
getAttributes
public java.util.HashMap<java.lang.String,java.lang.Object> getAttributes()
Return the request attributes map.- Returns:
- the attributes map
-
getAttribute
public java.lang.Object getAttribute(java.lang.String name)
Return the value of the specified attribute.- Parameters:
name- the attribute name- Returns:
- the attribute value
-
getRemoteUser
public MessageBytes getRemoteUser()
Return the remote user.- Returns:
- the remote user
-
getRemoteUserNeedsAuthorization
public boolean getRemoteUserNeedsAuthorization()
Check if the remote user needs authorization.- Returns:
trueif the remote user needs authorization
-
setRemoteUserNeedsAuthorization
public void setRemoteUserNeedsAuthorization(boolean remoteUserNeedsAuthorization)
Set whether the remote user needs authorization.- Parameters:
remoteUserNeedsAuthorization-trueif authorization is needed
-
getAuthType
public MessageBytes getAuthType()
Return the authentication type.- Returns:
- the authentication type
-
getAvailable
public int getAvailable()
Return the number of available bytes.- Returns:
- the available bytes
-
setAvailable
public void setAvailable(int available)
Set the number of available bytes.- Parameters:
available- the available bytes
-
getSendfile
public boolean getSendfile()
Check if sendfile is enabled.- Returns:
trueif sendfile is enabled
-
setSendfile
public void setSendfile(boolean sendfile)
Set whether sendfile is enabled.- Parameters:
sendfile-trueto enable sendfile
-
isFinished
public boolean isFinished()
Check if the request body has been fully read.- Returns:
trueif finished
-
getSupportsRelativeRedirects
public boolean getSupportsRelativeRedirects()
Check if relative redirects are supported.- Returns:
trueif relative redirects are supported
-
getInputBuffer
public InputBuffer getInputBuffer()
Return the input buffer.- Returns:
- the input buffer
-
setInputBuffer
public void setInputBuffer(InputBuffer inputBuffer)
Set the input buffer.- Parameters:
inputBuffer- the input buffer
-
doRead
public int doRead(ApplicationBufferHandler handler) throws java.io.IOException
Read data from the input buffer and put it into ApplicationBufferHandler. The buffer is owned by the protocol implementation - it will be reused on the next read. The Adapter must either process the data in place or copy it to a separate buffer if it needs to hold it. In most cases this is done during byte->char conversions or via InputStream. Unlike InputStream, this interface allows the app to process data in place, without copy.- Parameters:
handler- The destination to which to copy the data- Returns:
- The number of bytes copied
- Throws:
java.io.IOException- If an I/O error occurs during the copy
-
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 error exception is present.- Returns:
trueif an exception is present
-
getRequestId
public java.lang.String getRequestId()
Return the request ID.- Returns:
- the request ID
-
getProtocolRequestId
public java.lang.String getProtocolRequestId()
Return the protocol request ID.- Returns:
- the protocol request ID
-
getServletConnection
public ServletConnection getServletConnection()
Return the servlet connection.- Returns:
- the servlet connection
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
getStartTime
public long getStartTime()
Return the request start time in milliseconds.- Returns:
- the start time
-
getStartTimeNanos
public long getStartTimeNanos()
Return the request start time in nanoseconds.- Returns:
- the start time in nanoseconds
-
setStartTime
@Deprecated public void setStartTime(long startTime)
Deprecated.This setter will be removed in Tomcat 11. UsemarkStartTime().- Parameters:
startTime- time
-
setStartTimeNanos
@Deprecated public void setStartTimeNanos(long startTimeNanos)
Deprecated.Unused. Will be removed in Tomcat 12 onwards. UsemarkStartTime().Set the start time using the value provided bySystem.nanoTime().- Parameters:
startTimeNanos- The value returned fromSystem.nanoTime()at the point the requests started.
-
markStartTime
public void markStartTime()
Mark the request start time.
-
getStartInstant
public java.time.Instant getStartInstant()
Return the request start instant.- Returns:
- the start instant
-
getThreadId
public long getThreadId()
Return the thread ID.- Returns:
- the thread ID
-
clearRequestThread
public void clearRequestThread()
Clear the request thread ID.
-
setRequestThread
public void setRequestThread()
Set the request thread ID.
-
isRequestThread
public boolean isRequestThread()
Check if the current thread is the request thread.- Returns:
trueif the current thread is the request thread
-
setNote
public void setNote(int pos, java.lang.Object value)Used to store private data. Thread data could be used instead - but if you have the req, getting/setting a note is just an array access, may be faster than ThreadLocal for very frequent operations. Example use: Catalina CoyoteAdapter: ADAPTER_NOTES = 1 - stores the HttpServletRequest object ( req/res) To avoid conflicts, note in the range 0 - 8 are reserved for the servlet container ( catalina connector, etc ), and values in 9 - 16 for connector use. 17-31 range is not allocated or used.- Parameters:
pos- Index to use to store the notevalue- The value to store at that index
-
getNote
public java.lang.Object getNote(int pos)
Return the note at the specified position.- Parameters:
pos- the position- Returns:
- the note
-
recycle
public void recycle()
Recycle this request for reuse.
-
updateCounters
public void updateCounters()
Update request counters.
-
getRequestProcessor
public RequestInfo getRequestProcessor()
Return the request processor info.- Returns:
- the request processor info
-
getBytesRead
public long getBytesRead()
Return the number of bytes read.- Returns:
- the bytes read
-
isProcessing
public boolean isProcessing()
Check if the request is currently being processed.- Returns:
trueif processing
-
-