Package com.linecorp.armeria.server
Class DefaultServiceRequestContext
java.lang.Object
com.linecorp.armeria.common.NonWrappingRequestContext
com.linecorp.armeria.server.DefaultServiceRequestContext
- All Implemented Interfaces:
RequestContext
,ServiceRequestContext
@UnstableApi public final class DefaultServiceRequestContext extends NonWrappingRequestContext implements ServiceRequestContext
Default
ServiceRequestContext
implementation.-
Constructor Summary
Constructors Constructor Description DefaultServiceRequestContext(ServiceConfig cfg, Channel ch, MeterRegistry meterRegistry, SessionProtocol sessionProtocol, RequestId id, RoutingContext routingContext, RoutingResult routingResult, HttpRequest req, SSLSession sslSession, ProxiedAddresses proxiedAddresses, InetAddress clientAddress, long requestStartTimeNanos, long requestStartTimeMicros)
Creates a new instance. -
Method Summary
Modifier and Type Method Description void
addAdditionalResponseHeader(CharSequence name, Object value)
Adds a header with the specifiedname
andvalue
.void
addAdditionalResponseTrailer(CharSequence name, Object value)
Adds a trailer with the specifiedname
andvalue
.HttpHeaders
additionalResponseHeaders()
HttpHeaders
additionalResponseTrailers()
Returns theHttpHeaders
which is included along with any other trailers when aService
completes anHttpResponse
.ByteBufAllocator
alloc()
Returns theByteBufAllocator
for thisRequestContext
.<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()
ContextAwareScheduledExecutorService
blockingTaskExecutor()
Returns theContextAwareScheduledExecutorService
that could be used for executing a potentially long-running task.void
cancel(Throwable cause)
Throwable
cancellationCause()
Returns the cause of cancellation,null
if the request has not been cancelled.protected Channel
channel()
Returns theChannel
that is handling this request, ornull
if the connection is not established yet.void
clearRequestTimeout()
Clears the previously scheduled request timeout, if any.InetAddress
clientAddress()
Returns the address of the client who initiated this request.ServiceConfig
config()
String
decodedMappedPath()
Returns theRequestContext.decodedPath()
with its context path removed.ContextAwareEventLoop
eventLoop()
Returns theContextAwareEventLoop
that is handling the currentRequest
.<A extends SocketAddress>
AlocalAddress()
Returns the local address of this request, ornull
if the connection is not established yet.RequestLogAccess
log()
Returns theRequestLogAccess
that provides the access to theRequestLog
, which contains the information collected while processing the currentRequest
.RequestLogBuilder
logBuilder()
Returns theRequestLogBuilder
that collects the information about the currentRequest
.String
mappedPath()
Returns theRequestContext.path()
with its context path removed.long
maxRequestLength()
Returns the maximum length of the currentRequest
.void
mutateAdditionalResponseHeaders(Consumer<HttpHeadersBuilder> mutator)
void
mutateAdditionalResponseTrailers(Consumer<HttpHeadersBuilder> mutator)
Mutates theHttpHeaders
which is included along with any other trailers when aService
completes anHttpResponse
.MediaType
negotiatedResponseMediaType()
Returns the negotiated producible media type.Map<String,String>
pathParams()
ProxiedAddresses
proxiedAddresses()
Returns the proxied addresses of the currentRequest
.<A extends SocketAddress>
AremoteAddress()
Returns the remote address of this request, ornull
if the connection is not established yet.long
requestTimeoutMillis()
RoutingContext
routingContext()
Returns theRoutingContext
used to find theService
.void
setAdditionalResponseHeader(CharSequence name, Object value)
Sets a header with the specifiedname
andvalue
.void
setAdditionalResponseTrailer(CharSequence name, Object value)
Sets a trailer with the specifiedname
andvalue
.void
setMaxRequestLength(long maxRequestLength)
Sets the maximum length of the currentRequest
.void
setRequestTimeout(TimeoutMode mode, Duration requestTimeout)
Schedules the request timeout that is triggered when theRequest
is not fully received or the correspondingResponse
is not sent completely within the specifiedTimeoutMode
and the specifiedrequestTimeout
.void
setRequestTimeoutMillis(TimeoutMode mode, long requestTimeoutMillis)
Schedules the request timeout that is triggered when theRequest
is not fully received or the correspondingResponse
is not sent completely within the specifiedTimeoutMode
and the specifiedrequestTimeoutMillis
.SSLSession
sslSession()
TheSSLSession
for this request if the connection is made over TLS, ornull
if the connection is not established yet or the connection is not a TLS connection.String
toString()
CompletableFuture<Throwable>
whenRequestCancelled()
Returns aCompletableFuture
which is completed with aThrowable
cancellation cause after theServiceRequestContext
has been cancelled.CompletableFuture<Throwable>
whenRequestCancelling()
Returns aCompletableFuture
which is completed with aThrowable
cancellation cause when theServiceRequestContext
is about to get cancelled.CompletableFuture<Void>
whenRequestTimedOut()
Deprecated.CompletableFuture<Void>
whenRequestTimingOut()
Deprecated.Methods inherited from class com.linecorp.armeria.common.NonWrappingRequestContext
decodedPath, id, meterRegistry, method, ownAttr, ownAttrs, path, query, request, rpcRequest, sessionProtocol, setAttr, unsafeUpdateRequest, updateRequest, updateRpcRequest, validateHeaders
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface com.linecorp.armeria.common.RequestContext
decodedPath, hasAttr, hasOwnAttr, id, isCancelled, isTimedOut, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, meterRegistry, method, ownAttr, ownAttrs, path, query, replace, sessionProtocol, setAttr, updateRequest, updateRpcRequest
Methods inherited from interface com.linecorp.armeria.server.ServiceRequestContext
cancel, pathParam, push, request, root, rpcRequest, setRequestTimeout, setRequestTimeoutMillis, timeoutNow
-
Constructor Details
-
DefaultServiceRequestContext
public DefaultServiceRequestContext(ServiceConfig cfg, Channel ch, MeterRegistry meterRegistry, SessionProtocol sessionProtocol, RequestId id, RoutingContext routingContext, RoutingResult routingResult, HttpRequest req, @Nullable SSLSession sslSession, ProxiedAddresses proxiedAddresses, InetAddress clientAddress, long requestStartTimeNanos, long requestStartTimeMicros)Creates a new instance.- Parameters:
cfg
- theServiceConfig
ch
- theChannel
that handles the invocationmeterRegistry
- theMeterRegistry
that collects various statssessionProtocol
- theSessionProtocol
of the invocationid
- theRequestId
that represents the identifier of the currentRequest
andResponse
pair.routingContext
- the parameters which are used when finding a matchedRoute
routingResult
- the result of finding a matchedRoute
req
- the request associated with this contextsslSession
- theSSLSession
for this invocation if it is over TLSproxiedAddresses
- source and destination addresses delivered through proxy serversclientAddress
- the address of a client who initiated the requestrequestStartTimeNanos
-System.nanoTime()
value when the request started.requestStartTimeMicros
- the number of microseconds since the epoch, e.g.System.currentTimeMillis() * 1000
.
-
-
Method Details
-
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
- Overrides:
attr
in classNonWrappingRequestContext
- See Also:
RequestContext.ownAttr(AttributeKey)
-
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
- Overrides:
attrs
in classNonWrappingRequestContext
- See Also:
RequestContext.ownAttrs()
-
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
- Specified by:
remoteAddress
in interfaceServiceRequestContext
- Overrides:
remoteAddress
in classNonWrappingRequestContext
-
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
- Specified by:
localAddress
in interfaceServiceRequestContext
- Overrides:
localAddress
in classNonWrappingRequestContext
-
clientAddress
Description copied from interface:ServiceRequestContext
Returns the address of the client who initiated this request.- Specified by:
clientAddress
in interfaceServiceRequestContext
-
channel
Description copied from class:NonWrappingRequestContext
Returns theChannel
that is handling this request, ornull
if the connection is not established yet.- Specified by:
channel
in classNonWrappingRequestContext
-
config
Description copied from interface:ServiceRequestContext
- Specified by:
config
in interfaceServiceRequestContext
-
routingContext
Description copied from interface:ServiceRequestContext
Returns theRoutingContext
used to find theService
.- Specified by:
routingContext
in interfaceServiceRequestContext
-
pathParams
Description copied from interface:ServiceRequestContext
Returns the path parameters mapped by theRoute
associated with theService
that is handling the currentRequest
.- Specified by:
pathParams
in interfaceServiceRequestContext
-
blockingTaskExecutor
Description copied from interface:ServiceRequestContext
Returns theContextAwareScheduledExecutorService
that could be used for executing a potentially long-running task. TheContextAwareScheduledExecutorService
sets thisServiceRequestContext
as the current context before executing any submitted tasks. If you want to useScheduledExecutorService
without setting this context, callContextAwareScheduledExecutorService.withoutContext()
and use the returnedScheduledExecutorService
.- Specified by:
blockingTaskExecutor
in interfaceServiceRequestContext
-
mappedPath
Description copied from interface:ServiceRequestContext
Returns theRequestContext.path()
with its context path removed. This method can be useful for a reusable service bound at various path prefixes.- Specified by:
mappedPath
in interfaceServiceRequestContext
-
decodedMappedPath
Description copied from interface:ServiceRequestContext
Returns theRequestContext.decodedPath()
with its context path removed. This method can be useful for a reusable service bound at various path prefixes.- Specified by:
decodedMappedPath
in interfaceServiceRequestContext
-
negotiatedResponseMediaType
Description copied from interface:ServiceRequestContext
Returns the negotiated producible media type. If the media type negotiation is not used for theService
,null
would be returned.- Specified by:
negotiatedResponseMediaType
in interfaceServiceRequestContext
-
eventLoop
Description copied from interface:RequestContext
Returns theContextAwareEventLoop
that is handling the currentRequest
. TheContextAwareEventLoop
sets thisRequestContext
as the current context before executing any submitted tasks. If you want to useEventLoop
without setting this context, callContextAwareEventLoop.withoutContext()
and use the returnedEventLoop
.- Specified by:
eventLoop
in interfaceRequestContext
-
alloc
Description copied from interface:RequestContext
Returns theByteBufAllocator
for thisRequestContext
. Any buffers created by thisByteBufAllocator
must be reference-counted. If you don't know what this means, you should probably usebyte[]
orByteBuffer
directly instead of calling this method.- Specified by:
alloc
in interfaceRequestContext
-
sslSession
Description copied from interface:RequestContext
TheSSLSession
for this request if the connection is made over TLS, ornull
if the connection is not established yet or the connection is not a TLS connection.- Specified by:
sslSession
in interfaceRequestContext
-
requestTimeoutMillis
public long requestTimeoutMillis()Description copied from interface:ServiceRequestContext
Returns the amount of time allowed from the start time of theRequest
until receiving the currentRequest
and sending the correspondingResponse
completely. This value is initially set fromServiceConfig.requestTimeoutMillis()
.- Specified by:
requestTimeoutMillis
in interfaceServiceRequestContext
-
clearRequestTimeout
public void clearRequestTimeout()Description copied from interface:ServiceRequestContext
Clears the previously scheduled request timeout, if any. Note that calling this will prevent the request from ever being timed out.- Specified by:
clearRequestTimeout
in interfaceServiceRequestContext
-
setRequestTimeoutMillis
Description copied from interface:ServiceRequestContext
Schedules the request timeout that is triggered when theRequest
is not fully received or the correspondingResponse
is not sent completely within the specifiedTimeoutMode
and the specifiedrequestTimeoutMillis
.timeout mode description Timeout mode description TimeoutMode.SET_FROM_NOW
Sets a given amount of timeout from the current time. TimeoutMode.SET_FROM_START
Sets a given amount of timeout since the current Request
began processing.TimeoutMode.EXTEND
Extends the previously scheduled timeout by the given amount of timeout. For example:
ServiceRequestContext ctx = ...; // Schedules a timeout from the start time of the request ctx.setRequestTimeoutMillis(TimeoutMode.SET_FROM_START, 2000); assert ctx.requestTimeoutMillis() == 2000; ctx.setRequestTimeoutMillis(TimeoutMode.SET_FROM_START, 1000); assert ctx.requestTimeoutMillis() == 1000; // Schedules timeout after 3 seconds from now. ctx.setRequestTimeoutMillis(TimeoutMode.SET_FROM_NOW, 3000); // Extends the previously scheduled timeout. long oldRequestTimeoutMillis = ctx.requestTimeoutMillis(); ctx.setRequestTimeoutMillis(TimeoutMode.EXTEND, 1000); assert ctx.requestTimeoutMillis() == oldRequestTimeoutMillis + 1000; ctx.extendRequestTimeoutMillis(TimeoutMode.EXTEND, -500); assert ctx.requestTimeoutMillis() == oldRequestTimeoutMillis + 500;
- Specified by:
setRequestTimeoutMillis
in interfaceServiceRequestContext
-
setRequestTimeout
Description copied from interface:ServiceRequestContext
Schedules the request timeout that is triggered when theRequest
is not fully received or the correspondingResponse
is not sent completely within the specifiedTimeoutMode
and the specifiedrequestTimeout
.timeout mode description Timeout mode description TimeoutMode.SET_FROM_NOW
Sets a given amount of timeout from the current time. TimeoutMode.SET_FROM_START
Sets a given amount of timeout since the current Request
began processing.TimeoutMode.EXTEND
Extends the previously scheduled timeout by the given amount of timeout. For example:
ServiceRequestContext ctx = ...; // Schedules a timeout from the start time of the request ctx.setRequestTimeout(TimeoutMode.SET_FROM_START, Duration.ofSeconds(2)); assert ctx.requestTimeoutMillis() == 2000; ctx.setRequestTimeout(TimeoutMode.SET_FROM_START, Duration.ofSeconds(1)); assert ctx.requestTimeoutMillis() == 1000; // Schedules timeout after 3 seconds from now. ctx.setRequestTimeout(TimeoutMode.SET_FROM_NOW, Duration.ofSeconds(3)); // Extends the previously scheduled timeout. long oldRequestTimeoutMillis = ctx.requestTimeoutMillis(); ctx.setRequestTimeout(TimeoutMode.EXTEND, Duration.ofSeconds(1)); assert ctx.requestTimeoutMillis() == oldRequestTimeoutMillis + 1000; ctx.setRequestTimeout(TimeoutMode.EXTEND, Duration.ofMillis(-500)); assert ctx.requestTimeoutMillis() == oldRequestTimeoutMillis + 500;
- Specified by:
setRequestTimeout
in interfaceServiceRequestContext
-
cancel
Description copied from interface:RequestContext
- Specified by:
cancel
in interfaceRequestContext
-
cancellationCause
Description copied from interface:RequestContext
Returns the cause of cancellation,null
if the request has not been cancelled.- Specified by:
cancellationCause
in interfaceRequestContext
-
whenRequestCancelling
Description copied from interface:ServiceRequestContext
Returns aCompletableFuture
which is completed with aThrowable
cancellation cause when theServiceRequestContext
is about to get cancelled. If the request is handled successfully without cancellation, theCompletableFuture
won't complete.- Specified by:
whenRequestCancelling
in interfaceServiceRequestContext
-
whenRequestCancelled
Description copied from interface:ServiceRequestContext
Returns aCompletableFuture
which is completed with aThrowable
cancellation cause after theServiceRequestContext
has been cancelled.RequestContext.isCancelled()
will always returntrue
when the returnedCompletableFuture
is completed. If the request is handled successfully without cancellation, theCompletableFuture
won't complete.- Specified by:
whenRequestCancelled
in interfaceServiceRequestContext
-
whenRequestTimingOut
Deprecated.Description copied from interface:ServiceRequestContext
Returns aCompletableFuture
which is completed when theServiceRequestContext
is about to get timed out. If the request is handled successfully or not cancelled by timeout, theCompletableFuture
won't complete.- Specified by:
whenRequestTimingOut
in interfaceServiceRequestContext
-
whenRequestTimedOut
Deprecated.Description copied from interface:ServiceRequestContext
Returns aCompletableFuture
which is completed after theServiceRequestContext
has been timed out.RequestContext.isTimedOut()
will always returntrue
when the returnedCompletableFuture
is completed. If the request is handled successfully or not cancelled by timeout, theCompletableFuture
won't complete.- Specified by:
whenRequestTimedOut
in interfaceServiceRequestContext
-
maxRequestLength
public long maxRequestLength()Description copied from interface:ServiceRequestContext
Returns the maximum length of the currentRequest
. This value is initially set fromServiceConfig.maxRequestLength()
. If 0, there is no limit on the request size.- Specified by:
maxRequestLength
in interfaceServiceRequestContext
- See Also:
ContentTooLargeException
-
setMaxRequestLength
public void setMaxRequestLength(long maxRequestLength)Description copied from interface:ServiceRequestContext
Sets the maximum length of the currentRequest
. This value is initially set fromServiceConfig.maxRequestLength()
. If 0, there is no limit on the request size.- Specified by:
setMaxRequestLength
in interfaceServiceRequestContext
- See Also:
ContentTooLargeException
-
additionalResponseHeaders
Description copied from interface:ServiceRequestContext
- Specified by:
additionalResponseHeaders
in interfaceServiceRequestContext
-
setAdditionalResponseHeader
Description copied from interface:ServiceRequestContext
Sets a header with the specifiedname
andvalue
. This will remove all previous values associated with the specifiedname
. The header will be included when aService
sends anHttpResponse
.- Specified by:
setAdditionalResponseHeader
in interfaceServiceRequestContext
-
addAdditionalResponseHeader
Description copied from interface:ServiceRequestContext
Adds a header with the specifiedname
andvalue
. The header will be included when aService
sends anHttpResponse
.- Specified by:
addAdditionalResponseHeader
in interfaceServiceRequestContext
-
mutateAdditionalResponseHeaders
Description copied from interface:ServiceRequestContext
- Specified by:
mutateAdditionalResponseHeaders
in interfaceServiceRequestContext
- Parameters:
mutator
- theConsumer
that mutates the additional response headers
-
additionalResponseTrailers
Description copied from interface:ServiceRequestContext
Returns theHttpHeaders
which is included along with any other trailers when aService
completes anHttpResponse
.- Specified by:
additionalResponseTrailers
in interfaceServiceRequestContext
-
setAdditionalResponseTrailer
Description copied from interface:ServiceRequestContext
Sets a trailer with the specifiedname
andvalue
. This will remove all previous values associated with the specifiedname
. The trailer will be included when aService
completes anHttpResponse
.- Specified by:
setAdditionalResponseTrailer
in interfaceServiceRequestContext
-
addAdditionalResponseTrailer
Description copied from interface:ServiceRequestContext
Adds a trailer with the specifiedname
andvalue
. The trailer will be included when aService
completes anHttpResponse
.- Specified by:
addAdditionalResponseTrailer
in interfaceServiceRequestContext
-
mutateAdditionalResponseTrailers
Description copied from interface:ServiceRequestContext
Mutates theHttpHeaders
which is included along with any other trailers when aService
completes anHttpResponse
.- Specified by:
mutateAdditionalResponseTrailers
in interfaceServiceRequestContext
- Parameters:
mutator
- theConsumer
that mutates the additional trailers
-
proxiedAddresses
Description copied from interface:ServiceRequestContext
Returns the proxied addresses of the currentRequest
.- Specified by:
proxiedAddresses
in interfaceServiceRequestContext
-
log
Description copied from interface:RequestContext
Returns theRequestLogAccess
that provides the access to theRequestLog
, which contains the information collected while processing the currentRequest
.- Specified by:
log
in interfaceRequestContext
-
logBuilder
Description copied from interface:RequestContext
Returns theRequestLogBuilder
that collects the information about the currentRequest
.- Specified by:
logBuilder
in interfaceRequestContext
-
toString
-