Class NonWrappingRequestContext
- All Implemented Interfaces:
RequestContext
- Direct Known Subclasses:
DefaultClientRequestContext
,DefaultServiceRequestContext
@UnstableApi public abstract class NonWrappingRequestContext extends Object implements RequestContext
RequestContext
implementation that helps to implement a non-wrapping
RequestContext
.-
Constructor Summary
Constructors Modifier Constructor Description protected
NonWrappingRequestContext(MeterRegistry meterRegistry, SessionProtocol sessionProtocol, RequestId id, HttpMethod method, String path, String query, HttpRequest req, RpcRequest rpcReq, RequestContext rootAttributeMap)
Creates a new instance. -
Method Summary
Modifier and Type Method Description <V> V
attr(AttributeKey<V> key)
Returns the value associated with the givenAttributeKey
ornull
if there's no value set byRequestContext.setAttr(AttributeKey, Object)
.Iterator<Map.Entry<AttributeKey<?>,Object>>
attrs()
protected abstract Channel
channel()
Returns theChannel
that is handling this request, ornull
if the connection is not established yet.String
decodedPath()
Returns the absolute path part of the currentRequest
URI, excluding the query part, decoded in UTF-8.RequestId
id()
<A extends SocketAddress>
AlocalAddress()
Returns the local address of this request, ornull
if the connection is not established yet.MeterRegistry
meterRegistry()
Returns theMeterRegistry
that collects various stats.HttpMethod
method()
Returns the HTTP method of the currentRequest
.<V> V
ownAttr(AttributeKey<V> key)
Returns the value associated with the givenAttributeKey
ornull
if there's no value set byRequestContext.setAttr(AttributeKey, Object)
.Iterator<Map.Entry<AttributeKey<?>,Object>>
ownAttrs()
String
path()
String
query()
<A extends SocketAddress>
AremoteAddress()
Returns the remote address of this request, ornull
if the connection is not established yet.HttpRequest
request()
Returns theHttpRequest
associated with this context, ornull
if there's noHttpRequest
associated with this context yet.RpcRequest
rpcRequest()
Returns theRpcRequest
associated with this context, ornull
if there's noRpcRequest
associated with this context.SessionProtocol
sessionProtocol()
Returns theSessionProtocol
of the currentRequest
.<V> V
setAttr(AttributeKey<V> key, V value)
Associates the specified value with the givenAttributeKey
in this context.protected void
unsafeUpdateRequest(HttpRequest req)
Replaces theHttpRequest
associated with this context with the specified one without any validation.void
updateRequest(HttpRequest req)
Replaces theHttpRequest
associated with this context with the specified one.void
updateRpcRequest(RpcRequest rpcReq)
Replaces theRpcRequest
associated with this context with the specified one.protected void
validateHeaders(RequestHeaders headers)
Validates the specifiedRequestHeaders
.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.linecorp.armeria.common.RequestContext
alloc, cancel, cancel, cancellationCause, eventLoop, hasAttr, hasOwnAttr, isCancelled, isTimedOut, log, logBuilder, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, push, replace, root, run, run, sslSession, timeoutNow
-
Constructor Details
-
NonWrappingRequestContext
protected NonWrappingRequestContext(MeterRegistry meterRegistry, SessionProtocol sessionProtocol, RequestId id, HttpMethod method, String path, @Nullable String query, @Nullable HttpRequest req, @Nullable RpcRequest rpcReq, @Nullable RequestContext rootAttributeMap)Creates a new instance.- Parameters:
sessionProtocol
- theSessionProtocol
of the invocationid
- theRequestId
associated with this contextreq
- theHttpRequest
associated with this contextrpcReq
- theRpcRequest
associated with this context
-
-
Method Details
-
request
Description copied from interface:RequestContext
Returns theHttpRequest
associated with this context, ornull
if there's noHttpRequest
associated with this context yet.- Specified by:
request
in interfaceRequestContext
-
rpcRequest
Description copied from interface:RequestContext
Returns theRpcRequest
associated with this context, ornull
if there's noRpcRequest
associated with this context.- Specified by:
rpcRequest
in interfaceRequestContext
-
updateRequest
Description copied from interface:RequestContext
Replaces theHttpRequest
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 anHttpRequest
, because the properties of this context, such asRequestContext.path()
, are unaffected by such an attempt.- Specified by:
updateRequest
in interfaceRequestContext
- See Also:
HttpRequest.withHeaders(RequestHeaders)
,HttpRequest.withHeaders(RequestHeadersBuilder)
-
updateRpcRequest
Description copied from interface:RequestContext
Replaces theRpcRequest
associated with this context with the specified one. This method is useful to a decorator that manipulates an RPC call.- Specified by:
updateRpcRequest
in interfaceRequestContext
-
validateHeaders
Validates the specifiedRequestHeaders
. By default, this method will raise anIllegalArgumentException
if it does not have":scheme"
or":authority"
header. -
unsafeUpdateRequest
Replaces theHttpRequest
associated with this context with the specified one without any validation. Internal use only. Use it at your own risk. -
sessionProtocol
Description copied from interface:RequestContext
Returns theSessionProtocol
of the currentRequest
.- Specified by:
sessionProtocol
in interfaceRequestContext
-
channel
Returns theChannel
that is handling this request, ornull
if the connection is not established yet. -
remoteAddress
Description copied from interface:RequestContext
Returns the remote address of this request, ornull
if the connection is not established yet.- Specified by:
remoteAddress
in interfaceRequestContext
-
localAddress
Description copied from interface:RequestContext
Returns the local address of this request, ornull
if the connection is not established yet.- Specified by:
localAddress
in interfaceRequestContext
-
id
Description copied from interface:RequestContext
- Specified by:
id
in interfaceRequestContext
-
method
Description copied from interface:RequestContext
Returns the HTTP method of the currentRequest
.- Specified by:
method
in interfaceRequestContext
-
path
Description copied from interface:RequestContext
Returns the absolute path part of the currentRequest
URI, excluding the query part, as defined in RFC3986.- Specified by:
path
in interfaceRequestContext
-
decodedPath
Description copied from interface:RequestContext
Returns the absolute path part of the currentRequest
URI, excluding the query part, decoded in UTF-8.- Specified by:
decodedPath
in interfaceRequestContext
-
query
Description copied from interface:RequestContext
- Specified by:
query
in interfaceRequestContext
-
meterRegistry
Description copied from interface:RequestContext
Returns theMeterRegistry
that collects various stats.- Specified by:
meterRegistry
in interfaceRequestContext
-
attr
Description copied from interface:RequestContext
Returns the value associated with the givenAttributeKey
ornull
if there's no value set byRequestContext.setAttr(AttributeKey, Object)
.Searching for attributes in a root context
Note: This section applies only to a
ClientRequestContext
. AServiceRequestContext
always has itself as aRequestContext.root()
.If the value does not exist in this context but only in
RequestContext.root()
, this method will return the value from theRequestContext.root()
.
If the value exists both in this context andClientRequestContext ctx = ...; assert ctx.root().attr(KEY).equals("root"); assert ctx.attr(KEY).equals("root"); assert ctx.ownAttr(KEY) == null;
RequestContext.root()
, this method will return the value from this context.ClientRequestContext ctx = ...; assert ctx.root().attr(KEY).equals("root"); assert ctx.ownAttr(KEY).equals("child"); assert ctx.attr(KEY).equals("child");
- Specified by:
attr
in interfaceRequestContext
- See Also:
RequestContext.ownAttr(AttributeKey)
-
ownAttr
Description copied from interface:RequestContext
Returns the value associated with the givenAttributeKey
ornull
if there's no value set byRequestContext.setAttr(AttributeKey, Object)
.Unlike
RequestContext.attr(AttributeKey)
, this does not search inRequestContext.root()
.- Specified by:
ownAttr
in interfaceRequestContext
- See Also:
RequestContext.attr(AttributeKey)
-
setAttr
Description copied from interface:RequestContext
Associates the specified value with the givenAttributeKey
in this context. If this context previously contained a mapping for theAttributeKey
, the old value is replaced by the specified value. Setnull
not to iterate the mapping fromRequestContext.attrs()
.- Specified by:
setAttr
in interfaceRequestContext
- Returns:
- the old value that has been replaced if there's a mapping for the specified key in this context
or its
RequestContext.root()
, ornull
otherwise.
-
attrs
Description copied from interface:RequestContext
Returns theIterator
of allMap.Entry
s this context contains.Searching for attributes in a root context
Note: This section applies only to a
ClientRequestContext
. AServiceRequestContext
always has itself as aRequestContext.root()
.The
Iterator
returned by this method will also yield theMap.Entry
s from theRequestContext.root()
except those whoseAttributeKey
exist already in this context, e.g.
Please note that any changes made to theClientRequestContext ctx = ...; assert ctx.ownAttr(KEY_A).equals("child_a"); assert ctx.root().attr(KEY_A).equals("root_a"); assert ctx.root().attr(KEY_B).equals("root_b"); Iterator<Entry<AttributeKey<?>, Object>> attrs = ctx.attrs(); assert attrs.next().getValue().equals("child_a"); // KEY_A // Skip KEY_A in the root. assert attrs.next().getValue().equals("root_b"); // KEY_B assert attrs.hasNext() == false;
Map.Entry
returned byIterator.next()
never affects theMap.Entry
owned byRequestContext.root()
. For example:
If you want to change the value from the root while iterating, please callClientRequestContext ctx = ...; assert ctx.root().attr(KEY).equals("root"); assert ctx.ownAttr(KEY) == null; Iterator<Entry<AttributeKey<?>, Object>> attrs = ctx.attrs(); Entry<AttributeKey<?>, Object> next = attrs.next(); assert next.getKey() == KEY; // Overriding the root entry creates the client context's own entry. next.setValue("child"); assert ctx.attr(KEY).equals("child"); assert ctx.ownAttr(KEY).equals("child"); // root attribute remains unaffected. assert ctx.root().attr(KEY).equals("root");
RequestContext.attrs()
fromRequestContext.root()
.ClientRequestContext ctx = ...; assert ctx.root().attr(KEY).equals("root"); assert ctx.ownAttr(KEY) == null; // Call attrs() from the root to set a value directly while iterating. Iterator<Entry<AttributeKey<?>, Object>> attrs = ctx.root().attrs(); Entry<AttributeKey<?>, Object> next = attrs.next(); assert next.getKey() == KEY; next.setValue("another_root"); // The ctx does not have its own attribute. assert ctx.ownAttr(KEY) == null; assert ctx.attr(KEY).equals("another_root");
- Specified by:
attrs
in interfaceRequestContext
- See Also:
RequestContext.ownAttrs()
-
ownAttrs
Description copied from interface:RequestContext
Returns theIterator
of allMap.Entry
s this context contains.Unlike
RequestContext.attrs()
, this does not iterateRequestContext.root()
.- Specified by:
ownAttrs
in interfaceRequestContext
- See Also:
RequestContext.attrs()
-