public interface RequestContext
Request, its Response and related utilities.
A server-side Request has a ServiceRequestContext and
a client-side Request has a ClientRequestContext.| Modifier and Type | Method and Description |
|---|---|
default io.netty.buffer.ByteBufAllocator |
alloc()
Returns the
ByteBufAllocator for this RequestContext. |
<V> V |
attr(io.netty.util.AttributeKey<V> key)
Returns the value mapped to the given
AttributeKey or null if there's no value set by
setAttr(AttributeKey, Object) or setAttrIfAbsent(AttributeKey, Object). |
Iterator<Map.Entry<io.netty.util.AttributeKey<?>,Object>> |
attrs()
|
<V> V |
computeAttrIfAbsent(io.netty.util.AttributeKey<V> key,
Function<? super io.netty.util.AttributeKey<V>,? extends V> mappingFunction)
If the specified
AttributeKey is not already associated with a value (or is mapped
to null), attempts to compute its value using the given mapping
function and stores it into this context. |
default io.netty.channel.EventLoop |
contextAwareEventLoop()
Returns an
EventLoop that will make sure this RequestContext is set as the current
context before executing any callback. |
default Executor |
contextAwareExecutor()
Returns an
Executor that will make sure this RequestContext is set as the current
context before executing any callback. |
static <T extends RequestContext> |
current()
Returns the context of the
Request that is being handled in the current thread. |
static <T extends RequestContext> |
currentOrNull()
Returns the context of the
Request that is being handled in the current thread. |
String |
decodedPath()
Returns the absolute path part of the current
Request URI, excluding the query part,
decoded in UTF-8. |
io.netty.channel.EventLoop |
eventLoop()
Returns the
EventLoop that is handling the current Request. |
default Executor |
executor()
|
RequestId |
id()
|
<A extends SocketAddress> |
localAddress()
Returns the local address of this request, or
null if the connection is not established yet. |
RequestLogAccess |
log()
Returns the
RequestLogAccess that provides the access to the RequestLog, which
contains the information collected while processing the current Request. |
RequestLogBuilder |
logBuilder()
Returns the
RequestLogBuilder that collects the information about the current Request. |
default <T,U> BiConsumer<T,U> |
makeContextAware(BiConsumer<T,U> action)
Returns a
BiConsumer that makes sure the current RequestContext is set and then invokes
the input action. |
default <T,U,V> BiFunction<T,U,V> |
makeContextAware(BiFunction<T,U,V> function)
Returns a
BiFunction that makes sure the current RequestContext is set and then invokes
the input function. |
default <T> Callable<T> |
makeContextAware(Callable<T> callable)
Returns a
Callable that makes sure the current RequestContext is set and then invokes
the input callable. |
default io.netty.channel.ChannelFutureListener |
makeContextAware(io.netty.channel.ChannelFutureListener listener)
Deprecated.
Use
CompletableFuture instead. |
default <T> CompletableFuture<T> |
makeContextAware(CompletableFuture<T> future)
Returns a
CompletableFuture that makes sure the current RequestContext is set and
then invokes the input future. |
default <T> CompletionStage<T> |
makeContextAware(CompletionStage<T> stage)
Returns a
CompletionStage that makes sure the current RequestContext is set and
then invokes the input stage. |
default <T> Consumer<T> |
makeContextAware(Consumer<T> action)
Returns a
Consumer that makes sure the current RequestContext is set and then invokes
the input action. |
default Executor |
makeContextAware(Executor executor)
Returns an
Executor that will execute callbacks in the given executor, making sure to
propagate the current RequestContext into the callback execution. |
default ExecutorService |
makeContextAware(ExecutorService executor)
Returns an
ExecutorService that will execute callbacks in the given executor, making
sure to propagate the current RequestContext into the callback execution. |
default <T,R> Function<T,R> |
makeContextAware(Function<T,R> function)
Returns a
Function that makes sure the current RequestContext is set and then invokes
the input function. |
default <T> io.netty.util.concurrent.FutureListener<T> |
makeContextAware(io.netty.util.concurrent.FutureListener<T> listener)
Deprecated.
Use
CompletableFuture instead. |
default <T extends io.netty.util.concurrent.Future<?>> |
makeContextAware(io.netty.util.concurrent.GenericFutureListener<T> listener)
Deprecated.
Use
CompletableFuture instead. |
default org.slf4j.Logger |
makeContextAware(org.slf4j.Logger logger)
Returns a
Logger which prepends this RequestContext to the log message. |
default Runnable |
makeContextAware(Runnable runnable)
Returns a
Runnable that makes sure the current RequestContext is set and then invokes
the input runnable. |
default ScheduledExecutorService |
makeContextAware(ScheduledExecutorService executor)
Returns a
ScheduledExecutorService that will execute callbacks in the given executor,
making sure to propagate the current RequestContext into the callback execution. |
static <T> T |
mapCurrent(Function<? super RequestContext,T> mapper,
Supplier<T> defaultValueSupplier)
Maps the context of the
Request that is being handled in the current thread. |
MeterRegistry |
meterRegistry()
Returns the
MeterRegistry that collects various stats. |
HttpMethod |
method()
Returns the HTTP method of the current
Request. |
RequestContext |
newDerivedContext(RequestId id,
HttpRequest req,
RpcRequest rpcReq)
Creates a new
RequestContext whose properties and attrs() are copied from this
RequestContext, except having a different pair of HttpRequest and RpcRequest
and its own RequestLog. |
String |
path()
|
SafeCloseable |
push()
Pushes the specified context to the thread-local stack.
|
default SafeCloseable |
push(boolean runCallbacks)
Deprecated.
Use
push(). |
static SafeCloseable |
push(RequestContext ctx)
Deprecated.
Use
push(). |
static SafeCloseable |
push(RequestContext ctx,
boolean runCallbacks)
Deprecated.
Use
push(). |
default SafeCloseable |
pushIfAbsent()
Deprecated.
Use
push(). |
String |
query()
Returns the query part of the current
Request URI, without the leading '?' |
default void |
rejectPromise(io.netty.util.concurrent.Promise<?> promise,
Throwable cause)
Deprecated.
Use
CompletableFuture instead. |
<A extends SocketAddress> |
remoteAddress()
Returns the remote address of this request, or
null if the connection is not established yet. |
default SafeCloseable |
replace()
Replaces the current
RequestContext in the thread-local with this context without any validation. |
HttpRequest |
request()
Returns the
HttpRequest associated with this context, or null if there's no
HttpRequest associated with this context yet. |
default void |
resolvePromise(io.netty.util.concurrent.Promise<?> promise,
Object result)
Deprecated.
Use
CompletableFuture instead. |
RpcRequest |
rpcRequest()
Returns the
RpcRequest associated with this context, or null if there's no
RpcRequest associated with this context. |
SessionProtocol |
sessionProtocol()
Returns the
SessionProtocol of the current Request. |
<V> void |
setAttr(io.netty.util.AttributeKey<V> key,
V value)
Associates the specified value with the given
AttributeKey in this context. |
<V> V |
setAttrIfAbsent(io.netty.util.AttributeKey<V> key,
V value)
Associates the specified value with the given
AttributeKey in this context only
if this context does not contain a mapping for the AttributeKey. |
SSLSession |
sslSession()
The
SSLSession for this request if the connection is made over TLS, or null if
the connection is not established yet or the connection is not a TLS connection. |
void |
updateRequest(HttpRequest req)
Replaces the
HttpRequest associated with this context with the specified one. |
void |
updateRpcRequest(RpcRequest rpcReq)
Replaces the
RpcRequest associated with this context with the specified one. |
static <T extends RequestContext> T current()
Request that is being handled in the current thread.IllegalStateException - if the context is unavailable in the current threadstatic <T extends RequestContext> T currentOrNull()
Request that is being handled in the current thread.RequestContext available in the current thread, or null if unavailable.static <T> T mapCurrent(Function<? super RequestContext,T> mapper, Supplier<T> defaultValueSupplier)
Request that is being handled in the current thread.mapper - the Function that maps the RequestContextdefaultValueSupplier - the Supplier that provides the value when the context is unavailable
in the current thread. If null, the null will be returned
when the context is unavailable in the current thread.<V> V attr(io.netty.util.AttributeKey<V> key)
AttributeKey or null if there's no value set by
setAttr(AttributeKey, Object) or setAttrIfAbsent(AttributeKey, Object).<V> void setAttr(io.netty.util.AttributeKey<V> key,
V value)
AttributeKey in this context.
If this context previously contained a mapping for the AttributeKey,
the old value is replaced by the specified value. Set null not to iterate the mapping from
attrs().<V> V setAttrIfAbsent(io.netty.util.AttributeKey<V> key,
V value)
AttributeKey in this context only
if this context does not contain a mapping for the AttributeKey.null if there was no mapping for the AttributeKey or the old value if there's
a mapping for the AttributeKey.<V> V computeAttrIfAbsent(io.netty.util.AttributeKey<V> key,
Function<? super io.netty.util.AttributeKey<V>,? extends V> mappingFunction)
AttributeKey is not already associated with a value (or is mapped
to null), attempts to compute its value using the given mapping
function and stores it into this context.
If the mapping function returns null, no mapping is recorded.
AttributeKey, or null if the computed value is nullHttpRequest request()
HttpRequest associated with this context, or null if there's no
HttpRequest associated with this context yet.RpcRequest rpcRequest()
RpcRequest associated with this context, or null if there's no
RpcRequest associated with this context.void updateRequest(HttpRequest req)
HttpRequest associated with this context with the specified one.
This method is useful to a decorator that manipulates HTTP request headers.
Note that it is a bad idea to change the values of the pseudo headers (":method",
":path", ":scheme" and ":authority") when replacing an HttpRequest,
because the properties of this context, such as path(), are unaffected by such an attempt.
void updateRpcRequest(RpcRequest rpcReq)
RpcRequest associated with this context with the specified one.
This method is useful to a decorator that manipulates an RPC call.SessionProtocol sessionProtocol()
SessionProtocol of the current Request.<A extends SocketAddress> A remoteAddress()
null if the connection is not established yet.<A extends SocketAddress> A localAddress()
null if the connection is not established yet.SSLSession sslSession()
SSLSession for this request if the connection is made over TLS, or null if
the connection is not established yet or the connection is not a TLS connection.RequestId id()
HttpMethod method()
Request.String path()
String decodedPath()
Request URI, excluding the query part,
decoded in UTF-8.String query()
RequestLogAccess log()
RequestLogAccess that provides the access to the RequestLog, which
contains the information collected while processing the current Request.RequestLogBuilder logBuilder()
RequestLogBuilder that collects the information about the current Request.MeterRegistry meterRegistry()
MeterRegistry that collects various stats.default Executor executor()
io.netty.channel.EventLoop eventLoop()
EventLoop that is handling the current Request.default io.netty.buffer.ByteBufAllocator alloc()
ByteBufAllocator for this RequestContext. Any buffers created by this
ByteBufAllocator must be
reference-counted. If you don't know
what this means, you should probably use byte[] or ByteBuffer directly instead
of calling this method.default Executor contextAwareExecutor()
Executor that will make sure this RequestContext is set as the current
context before executing any callback. This should almost always be used for executing asynchronous
callbacks in service code to make sure features that require the RequestContext work properly.
Most asynchronous libraries like CompletableFuture provide methods that accept an
Executor to run callbacks on.default io.netty.channel.EventLoop contextAwareEventLoop()
EventLoop that will make sure this RequestContext is set as the current
context before executing any callback.@Deprecated static SafeCloseable push(RequestContext ctx)
push().SafeCloseable.close(), which can be done using a try-with-resources block.
This method may throw an IllegalStateException according to the status of the current
thread-local. Please see ServiceRequestContext.push() and
ClientRequestContext.push() to find out the satisfying conditions.
@Deprecated static SafeCloseable push(RequestContext ctx, boolean runCallbacks)
push().SafeCloseable.close(), which can be done using a try-with-resources block.
This method may throw an IllegalStateException according to the status of the current
thread-local. Please see ServiceRequestContext.push() and
ClientRequestContext.push() to find out the satisfying conditions.
SafeCloseable push()
SafeCloseable.close(), which can be done using a try-with-resources block:
try (SafeCloseable ignored = ctx.push()) {
...
}
This method may throw an IllegalStateException according to the status of the current
thread-local. Please see ServiceRequestContext.push() and
ClientRequestContext.push() to find out the satisfying conditions.
@Deprecated default SafeCloseable push(boolean runCallbacks)
push().SafeCloseable.close(), which can be done using a try-with-resources block:
This method may throw an IllegalStateException according to the status of the current
thread-local. Please see ServiceRequestContext.push() and
ClientRequestContext.push() to find out the satisfying conditions.
runCallbacks - This is not used.@Deprecated default SafeCloseable pushIfAbsent()
push().SafeCloseable.close(), which can be done using a try-with-resources block.
This method may throw an IllegalStateException according to the status of the current
thread-local. Please see ServiceRequestContext.push() and
ClientRequestContext.push() to find out the satisfying conditions.
default SafeCloseable replace()
RequestContext in the thread-local with this context without any validation.
This method also does not run any callbacks.
Note: Do not use this if you don't know what you are doing. This method does not
prevent the situation where a wrong RequestContext is pushed into the thread-local.
Use push() instead.
default Executor makeContextAware(Executor executor)
Executor that will execute callbacks in the given executor, making sure to
propagate the current RequestContext into the callback execution. It is generally preferred to
use contextAwareEventLoop() to ensure the callback stays on the same thread as well.default ExecutorService makeContextAware(ExecutorService executor)
ExecutorService that will execute callbacks in the given executor, making
sure to propagate the current RequestContext into the callback execution.default ScheduledExecutorService makeContextAware(ScheduledExecutorService executor)
ScheduledExecutorService that will execute callbacks in the given executor,
making sure to propagate the current RequestContext into the callback execution.default <T> Callable<T> makeContextAware(Callable<T> callable)
Callable that makes sure the current RequestContext is set and then invokes
the input callable.default Runnable makeContextAware(Runnable runnable)
Runnable that makes sure the current RequestContext is set and then invokes
the input runnable.default <T,R> Function<T,R> makeContextAware(Function<T,R> function)
Function that makes sure the current RequestContext is set and then invokes
the input function.default <T,U,V> BiFunction<T,U,V> makeContextAware(BiFunction<T,U,V> function)
BiFunction that makes sure the current RequestContext is set and then invokes
the input function.default <T> Consumer<T> makeContextAware(Consumer<T> action)
Consumer that makes sure the current RequestContext is set and then invokes
the input action.default <T,U> BiConsumer<T,U> makeContextAware(BiConsumer<T,U> action)
BiConsumer that makes sure the current RequestContext is set and then invokes
the input action.@Deprecated default <T> io.netty.util.concurrent.FutureListener<T> makeContextAware(io.netty.util.concurrent.FutureListener<T> listener)
CompletableFuture instead.FutureListener that makes sure the current RequestContext is set and then
invokes the input listener.@Deprecated default io.netty.channel.ChannelFutureListener makeContextAware(io.netty.channel.ChannelFutureListener listener)
CompletableFuture instead.ChannelFutureListener that makes sure the current RequestContext is set and
then invokes the input listener.@Deprecated default <T extends io.netty.util.concurrent.Future<?>> io.netty.util.concurrent.GenericFutureListener<T> makeContextAware(io.netty.util.concurrent.GenericFutureListener<T> listener)
CompletableFuture instead.GenericFutureListener that makes sure the current RequestContext is set and
then invokes the input listener. Unlike other versions of makeContextAware, this one will
invoke the listener with the future's result even if the context has already been timed out.default <T> CompletionStage<T> makeContextAware(CompletionStage<T> stage)
CompletionStage that makes sure the current RequestContext is set and
then invokes the input stage.default <T> CompletableFuture<T> makeContextAware(CompletableFuture<T> future)
CompletableFuture that makes sure the current RequestContext is set and
then invokes the input future.default org.slf4j.Logger makeContextAware(org.slf4j.Logger logger)
Logger which prepends this RequestContext to the log message.logger - the Logger to decorate.@Deprecated default void resolvePromise(io.netty.util.concurrent.Promise<?> promise, Object result)
CompletableFuture instead.promise with the specified result so that the promise is
marked as 'done'. If promise is done already, this method does the following:
result if it is a reference-counted object,
such as ByteBuf and FullHttpResponse.Promise can be done already even if you did not call this method in the following
cases:
Promise has been timed out.resolvePromise(Promise, Object)rejectPromise(Promise, Throwable)Promise.setSuccess(Object)Promise.setFailure(Throwable)Future.cancel(boolean)@Deprecated default void rejectPromise(io.netty.util.concurrent.Promise<?> promise, Throwable cause)
CompletableFuture instead.promise with the specified cause. If promise is done
already, this method logs a warning about the failure. Note that a Promise can be done already
even if you did not call this method in the following cases:
Promise has been timed out.resolvePromise(Promise, Object)rejectPromise(Promise, Throwable)Promise.setSuccess(Object)Promise.setFailure(Throwable)Future.cancel(boolean)RequestContext newDerivedContext(RequestId id, HttpRequest req, RpcRequest rpcReq)
RequestContext whose properties and attrs() are copied from this
RequestContext, except having a different pair of HttpRequest and RpcRequest
and its own RequestLog.Copyright © 2020 LeanCloud. All rights reserved.