Class HttpServerExchange
- All Implemented Interfaces:
io.undertow.httpcore.BufferAllocator,io.undertow.httpcore.CompletedListener,io.undertow.httpcore.InputChannel,io.undertow.httpcore.OutputChannel,io.undertow.httpcore.PreCommitListener,Attachable,AutoCloseable
- Author:
- David M. Lloyd
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final AttachmentKey<Map<String,String>> Attachment key that can be used to hold additional request attributesstatic final AttachmentKey<Boolean>Attachment key that can be used as a flag of secure attribute -
Constructor Summary
ConstructorsConstructorDescriptionHttpServerExchange(io.undertow.httpcore.HttpExchange delegate, long maxEntitySize) -
Method Summary
Modifier and TypeMethodDescriptionaddPathParam(String name, String param) addQueryParam(String name, String param) voidaddRequestHeader(String name, String value) voidAdds a listener that will be invoked on response commitvoidaddResponseHeader(String name, String value) voidaddWriteFunction(io.undertow.httpcore.WriteFunction function) io.netty.buffer.ByteBufio.netty.buffer.ByteBufallocateBuffer(boolean direct) io.netty.buffer.ByteBufallocateBuffer(boolean direct, int bufferSize) io.netty.buffer.ByteBufallocateBuffer(int bufferSize) voidvoidvoidvoidclose()voidcompleted(io.undertow.httpcore.HttpExchange exchange) booleancontainsRequestHeader(String name) booleancontainsResponseHeader(String name) voidUsed to terminate the request in an async manner, the actual mechanism will depend on the underlying getProtocol, for example HTTP/2 may send a RST_STREAM stream if there is more data coming.dispatch(HttpHandler handler) Dispatches this request to the XNIO worker thread pool.dispatch(Executor executor, HttpHandler handler) Dispatches this request to the given executor.Ends the exchange by fully draining the request channel, and flushing the response channel.voidintio.undertow.httpcore.HttpExchangeGet the destination address of the HTTP request.Gets the current executor that is used for dispatch operations.Return the host, and also the port if this request was sent to a non-standard port.Return the host that this request was sent to, in general this will be the value of the Host header, minus the port specifier.intReturn the port that this request was sent to.io.undertow.httpcore.InputChannelio.netty.util.concurrent.EventExecutorlongio.undertow.httpcore.OutputChannelReturns a mutable map of path parametersGet the request getProtocol string.Returns a mutable map of query parameters.Get the request relative path.longgetRequestHeader(String name) getRequestHeaders(String name) Get the HTTP request method.The request path.Get the request URI scheme.longThe original request URI.Reconstructs the complete URL as seen by the user.Get the resolved path.longlonggetResponseHeader(String name) getResponseHeaders(String name) Get the source address of the HTTP request.io.undertow.httpcore.SSLSessionInfointGet the status code.io.undertow.httpcore.UndertowOptionMapbooleanbooleanbooleanIf a request was submitted to the server with a full URI instead of just a path this will return true.booleanbooleanbooleanbooleanbooleanbooleanbooleanbooleanbooleanisSecure()voidpreCommit(io.undertow.httpcore.HttpExchange exchange) Sets the http getProtocolvoidio.netty.buffer.ByteBufio.netty.buffer.ByteBufReads some data.intvoidremoveRequestHeader(String name) voidremoveResponseHeader(String name) requestMethod(String requestMethod) Set the HTTP request method.voidvoidvoidsetDestinationAddress(InetSocketAddress destinationAddress) Sets the destination address of the HTTP request.setDispatchExecutor(Executor executor) Sets the executor that is used for dispatch operations where no executor is specified.setMaxEntitySize(long maxEntitySize) Sets the max entity size for this exchange.setPersistent(boolean persistent) setQueryString(String queryString) <T> voidsetReadHandler(BiConsumer<io.undertow.httpcore.InputChannel, T> handler, T context) setRelativePath(String relativePath) Set the request relative path.voidsetRequestHeader(String name, String value) setRequestPath(String requestPath) Set the request URI path.setRequestScheme(String requestScheme) Set the request URI scheme.setRequestURI(String requestURI) Sets the request URIsetRequestURI(String requestURI, boolean containsHost) Sets the request URIsetResolvedPath(String resolvedPath) Set the resolved path.setResponseContentLength(long length) Sets the response content lengthsetResponseCookie(Cookie cookie) Sets a response cookievoidsetResponseHeader(String name, String value) voidsetSecurityContext(SecurityContext securityContext) setSourceAddress(InetSocketAddress sourceAddress) Sets the source address of the HTTP request.voidsetSslSessionInfo(io.undertow.httpcore.SSLSessionInfo info) setStatusCode(int statusCode) Change the status code for this response.voidstartBlocking(io.undertow.httpcore.BlockingHttpExchange httpExchange) Calling this method puts the exchange in blocking mode, using the given blocking exchange as the source of the streams.toString()upgradeChannel(String productName, Consumer<Object> listener) Upgrade the channel to a raw socket.upgradeChannel(Consumer<Object> listener) Upgrade the channel to a raw socket.<T> voidwriteAsync(io.netty.buffer.ByteBuf data, boolean last, io.undertow.httpcore.IoCallback<T> callback, T context) voidwriteAsync(String data) Writes the given UTF-8 data and ends the exchangevoidwriteAsync(String data, Charset charset) Writes the given data in the provided charset and ends the exchange<T> voidwriteAsync(String data, Charset charset, boolean last, io.undertow.httpcore.IoCallback<T> callback, T context) Writes the given data in the provided charset and invokes the provided callback on completionvoidwriteBlocking(io.netty.buffer.ByteBuf data, boolean last) Methods inherited from class io.undertow.util.AbstractAttachable
addToAttachmentList, createAttachmentMap, getAttachment, getAttachmentList, putAttachment, removeAttachment
-
Field Details
-
REQUEST_ATTRIBUTES
Attachment key that can be used to hold additional request attributes -
SECURE_REQUEST
Attachment key that can be used as a flag of secure attribute
-
-
Constructor Details
-
HttpServerExchange
public HttpServerExchange(io.undertow.httpcore.HttpExchange delegate, long maxEntitySize)
-
-
Method Details
-
getProtocol
Get the request getProtocol string. Normally this is one of the strings listed inHttpProtocolNames.- Returns:
- the request getProtocol string
-
protocol
Sets the http getProtocol- Parameters:
protocol-
-
isSecure
public boolean isSecure() -
getRequestMethod
Get the HTTP request method. Normally this is one of the strings listed inHttpMethodNames.- Returns:
- the HTTP request method
-
requestMethod
Set the HTTP request method.- Parameters:
requestMethod- the HTTP request method
-
getRequestScheme
Get the request URI scheme. Normally this is one ofhttporhttps.- Returns:
- the request URI scheme
-
setRequestScheme
Set the request URI scheme.- Parameters:
requestScheme- the request URI scheme
-
getRequestId
-
getRequestURI
The original request URI. This will include the host name, getProtocol etc if it was specified by the client.This is not decoded in any way, and does not include the query string.
Examples: GET http://localhost:8080/myFile.jsf?foo=bar HTTP/1.1 -> 'http://localhost:8080/myFile.jsf' POST /my+File.jsf?foo=bar HTTP/1.1 -> '/my+File.jsf'
-
setRequestURI
Sets the request URI- Parameters:
requestURI- The new request URI
-
setRequestURI
Sets the request URI- Parameters:
requestURI- The new request URIcontainsHost- If this is true the request URI contains the host part
-
isHostIncludedInRequestURI
public boolean isHostIncludedInRequestURI()If a request was submitted to the server with a full URI instead of just a path this will return true. For example:GET http://localhost:8080/b/../my+File.jsf?foo=bar HTTP/1.1 -> true POST /my+File.jsf?foo=bar HTTP/1.1 -> false
- Returns:
trueIf the request URI contains the host part of the URI
-
getRequestPath
The request path. This will be decoded by the server, and does not include the query string.This path is not canonicalised, so care must be taken to ensure that escape attacks are not possible.
Examples: GET http://localhost:8080/b/../my+File.jsf?foo=bar HTTP/1.1 -> '/b/../my+File.jsf' POST /my+File.jsf?foo=bar HTTP/1.1 -> '/my File.jsf'
-
setRequestPath
Set the request URI path.- Parameters:
requestPath- the request URI path
-
getRelativePath
Get the request relative path. This is the path which should be evaluated by the current handler.If the
CanonicalPathHandleris installed in the current chain then this path with be canonicalized- Returns:
- the request relative path
-
setRelativePath
Set the request relative path.- Parameters:
relativePath- the request relative path
-
getResolvedPath
Get the resolved path.- Returns:
- the resolved path
-
setResolvedPath
Set the resolved path.- Parameters:
resolvedPath- the resolved path
-
getQueryString
- Returns:
- The query string, without the leading ?
-
setQueryString
-
getRequestURL
Reconstructs the complete URL as seen by the user. This includes scheme, host name etc, but does not include query string.This is not decoded.
-
getRequestContentLength
public long getRequestContentLength() -
getResponseContentLength
public long getResponseContentLength()- Returns:
- The content length of the response, or
-1if it has not been set
-
getHostName
Return the host that this request was sent to, in general this will be the value of the Host header, minus the port specifier.If this resolves to an IPv6 address it will not be enclosed by square brackets. Care must be taken when constructing URLs based on this method to ensure IPv6 URLs are handled correctly.
- Returns:
- The host part of the destination address
-
getHostAndPort
Return the host, and also the port if this request was sent to a non-standard port. In general this will just be the value of the Host header.If this resolves to an IPv6 address it *will* be enclosed by square brackets. The return value of this method is suitable for inclusion in a URL.
- Returns:
- The host and port part of the destination address
-
getDelegate
public io.undertow.httpcore.HttpExchange getDelegate() -
getHostPort
public int getHostPort()Return the port that this request was sent to. In general this will be the value of the Host header, minus the host name.- Returns:
- The port part of the destination address
-
isPersistent
public boolean isPersistent() -
isInIoThread
public boolean isInIoThread()- Returns:
trueIf the current thread in the IO thread for the exchange
-
getResponseBytesSent
public long getResponseBytesSent()- Returns:
- The number of bytes sent in the entity body
-
setPersistent
-
isDispatched
public boolean isDispatched() -
unDispatch
-
dispatch
Dispatches this request to the XNIO worker thread pool. Once the call stack returns the given runnable will be submitted to the executor.In general handlers should first check the value of
isInIoThread()before calling this method, and only dispatch if the request is actually running in the IO thread.- Parameters:
runnable- The task to run- Throws:
IllegalStateException- If this exchange has already been dispatched
-
dispatch
Dispatches this request to the given executor. Once the call stack returns the given runnable will be submitted to the executor.In general handlers should first check the value of
isInIoThread()before calling this method, and only dispatch if the request is actually running in the IO thread.- Parameters:
runnable- The task to run- Throws:
IllegalStateException- If this exchange has already been dispatched
-
dispatch
-
dispatch
-
setDispatchExecutor
Sets the executor that is used for dispatch operations where no executor is specified.- Parameters:
executor- The executor to use
-
getDispatchExecutor
Gets the current executor that is used for dispatch operations. This may be null- Returns:
- The current dispatch executor
-
addExchangeCompleteListener
-
addDefaultResponseListener
-
getSourceAddress
Get the source address of the HTTP request.- Returns:
- the source address of the HTTP request
-
isComplete
public boolean isComplete() -
isRequestComplete
public boolean isRequestComplete() -
isResponseComplete
public boolean isResponseComplete() -
setSourceAddress
Sets the source address of the HTTP request. If this is not explicitly set the actual source address of the channel is used.- Parameters:
sourceAddress- The address
-
getDestinationAddress
Get the destination address of the HTTP request.- Returns:
- the destination address of the HTTP request
-
setDestinationAddress
Sets the destination address of the HTTP request. If this is not explicitly set the actual destination address of the channel is used.- Parameters:
destinationAddress- The address
-
setResponseContentLength
Sets the response content length- Parameters:
length- The content length
-
getQueryParameters
Returns a mutable map of query parameters.- Returns:
- The query parameters
-
addQueryParam
-
getPathParameters
Returns a mutable map of path parameters- Returns:
- The path parameters
-
addPathParam
-
getRequestCookies
- Returns:
- A mutable map of request cookies
-
setResponseCookie
Sets a response cookie- Parameters:
cookie- The cookie
-
getResponseCookies
- Returns:
- A mutable map of response cookies
-
isResponseStarted
public boolean isResponseStarted()- Returns:
trueIf the response has already been started
-
readBlocking
Reads some data. If all data has been read it will return null.- Specified by:
readBlockingin interfaceio.undertow.httpcore.InputChannel- Returns:
- Throws:
IOException- on failure
-
send1ContinueIfRequired
public void send1ContinueIfRequired() -
writeAsync
Writes the given UTF-8 data and ends the exchange- Specified by:
writeAsyncin interfaceio.undertow.httpcore.OutputChannel- Parameters:
data- The data to write
-
writeAsync
Writes the given data in the provided charset and ends the exchange- Specified by:
writeAsyncin interfaceio.undertow.httpcore.OutputChannel- Parameters:
data- The data to write
-
writeAsync
public <T> void writeAsync(String data, Charset charset, boolean last, io.undertow.httpcore.IoCallback<T> callback, T context) Writes the given data in the provided charset and invokes the provided callback on completion- Specified by:
writeAsyncin interfaceio.undertow.httpcore.OutputChannel- Parameters:
data- The data to write
-
writeAsync
public <T> void writeAsync(io.netty.buffer.ByteBuf data, boolean last, io.undertow.httpcore.IoCallback<T> callback, T context) - Specified by:
writeAsyncin interfaceio.undertow.httpcore.OutputChannel
-
writeBlocking
- Specified by:
writeBlockingin interfaceio.undertow.httpcore.OutputChannel- Throws:
IOException
-
getStatusCode
public int getStatusCode()Get the status code.- Returns:
- the status code
-
getRequestHeader
-
getRequestHeaders
-
containsRequestHeader
-
removeRequestHeader
-
setRequestHeader
-
getRequestHeaderNames
-
addRequestHeader
-
clearRequestHeaders
public void clearRequestHeaders() -
clearResponseHeaders
public void clearResponseHeaders() -
getResponseHeader
-
getResponseHeaders
-
containsResponseHeader
-
removeResponseHeader
-
setResponseHeader
-
getResponseHeaderNames
-
addResponseHeader
-
setStatusCode
Change the status code for this response. If not specified, the code will be a200. Setting the status code after the response headers have been transmitted has no effect.- Parameters:
statusCode- the new code- Throws:
IllegalStateException- if a response or upgrade was already sent
-
startBlocking
public void startBlocking(io.undertow.httpcore.BlockingHttpExchange httpExchange) Calling this method puts the exchange in blocking mode, using the given blocking exchange as the source of the streams.When an exchange is in blocking mode the input stream methods become available, other than that there is presently no major difference between blocking an non-blocking modes.
Note that this method may be called multiple times with different exchange objects, to allow handlers to modify the streams that are being used.
-
getInputStream
- Returns:
- The input stream
-
getOutputStream
-
getRequestStartTime
public long getRequestStartTime()- Returns:
- The request start time, or -1 if this was not recorded
-
endExchange
Ends the exchange by fully draining the request channel, and flushing the response channel.This can result in handoff to an XNIO worker, so after this method is called the exchange should not be modified by the caller.
If the exchange is already complete this method is a noop
-
getIoThread
public io.netty.util.concurrent.EventExecutor getIoThread() -
getMaxEntitySize
public long getMaxEntitySize()- Returns:
- The maximum entity size for this exchange
-
setMaxEntitySize
Sets the max entity size for this exchange. This cannot be modified after the request channel has been obtained.- Parameters:
maxEntitySize- The max entity size
-
getSecurityContext
-
setSecurityContext
-
addResponseCommitListener
Adds a listener that will be invoked on response commit- Parameters:
listener- The response listener
-
readAsync
- Specified by:
readAsyncin interfaceio.undertow.httpcore.InputChannel- Throws:
IOException
-
isReadable
public boolean isReadable()- Specified by:
isReadablein interfaceio.undertow.httpcore.InputChannel
-
setReadHandler
- Specified by:
setReadHandlerin interfaceio.undertow.httpcore.InputChannel
-
readBytesAvailable
public int readBytesAvailable()- Specified by:
readBytesAvailablein interfaceio.undertow.httpcore.InputChannel
-
allocateBuffer
public io.netty.buffer.ByteBuf allocateBuffer()- Specified by:
allocateBufferin interfaceio.undertow.httpcore.BufferAllocator
-
allocateBuffer
public io.netty.buffer.ByteBuf allocateBuffer(boolean direct) - Specified by:
allocateBufferin interfaceio.undertow.httpcore.BufferAllocator
-
allocateBuffer
public io.netty.buffer.ByteBuf allocateBuffer(int bufferSize) - Specified by:
allocateBufferin interfaceio.undertow.httpcore.BufferAllocator
-
allocateBuffer
public io.netty.buffer.ByteBuf allocateBuffer(boolean direct, int bufferSize) - Specified by:
allocateBufferin interfaceio.undertow.httpcore.BufferAllocator
-
getBufferSize
public int getBufferSize()- Specified by:
getBufferSizein interfaceio.undertow.httpcore.BufferAllocator
-
discardRequest
public void discardRequest()Used to terminate the request in an async manner, the actual mechanism will depend on the underlying getProtocol, for example HTTP/2 may send a RST_STREAM stream if there is more data coming.This may result in an unclean close if it is called on a non multiplexed getProtocol
-
upgradeChannel
Upgrade the channel to a raw socket. This method set the response code to 101, and then marks both the request and response as terminated, which means that once the current request is completed the raw channel can be obtained- Throws:
IllegalStateException- if a response or upgrade was already sent, or if the request body is already being read
-
upgradeChannel
Upgrade the channel to a raw socket. This method set the response code to 101, and then marks both the request and response as terminated, which means that once the current request is completed the raw channel can be obtained- Parameters:
productName- the product name to report to the client- Throws:
IllegalStateException- if a response or upgrade was already sent, or if the request body is already being read
-
resetRequestChannel
public void resetRequestChannel() -
setSslSessionInfo
public void setSslSessionInfo(io.undertow.httpcore.SSLSessionInfo info) -
getSslSessionInfo
public io.undertow.httpcore.SSLSessionInfo getSslSessionInfo() -
close
public void close()- Specified by:
closein interfaceAutoCloseable
-
getWorker
-
getUndertowOptions
public io.undertow.httpcore.UndertowOptionMap getUndertowOptions() -
isPushSupported
public boolean isPushSupported() -
pushResource
-
isRequestTrailerFieldsSupported
public boolean isRequestTrailerFieldsSupported() -
completed
public void completed(io.undertow.httpcore.HttpExchange exchange) - Specified by:
completedin interfaceio.undertow.httpcore.CompletedListener
-
beginExecutingHandlerChain
public void beginExecutingHandlerChain() -
endExecutingHandlerChain
public void endExecutingHandlerChain() -
runResumeReadWrite
public void runResumeReadWrite() -
getOutputChannel
public io.undertow.httpcore.OutputChannel getOutputChannel() -
getInputChannel
public io.undertow.httpcore.InputChannel getInputChannel() -
toString
-
preCommit
public void preCommit(io.undertow.httpcore.HttpExchange exchange) - Specified by:
preCommitin interfaceio.undertow.httpcore.PreCommitListener
-
addWriteFunction
public void addWriteFunction(io.undertow.httpcore.WriteFunction function)
-