Package com.linecorp.armeria.server
Class DefaultServiceRequestContext
- java.lang.Object
-
- com.linecorp.armeria.common.AbstractRequestContext
-
- com.linecorp.armeria.common.NonWrappingRequestContext
-
- com.linecorp.armeria.server.DefaultServiceRequestContext
-
- All Implemented Interfaces:
RequestContext
,ServiceRequestContext
,AttributeMap
public class DefaultServiceRequestContext extends NonWrappingRequestContext implements ServiceRequestContext
DefaultServiceRequestContext
implementation.
-
-
Constructor Summary
Constructors Constructor Description DefaultServiceRequestContext(ServiceConfig cfg, Channel ch, MeterRegistry meterRegistry, SessionProtocol sessionProtocol, PathMappingContext pathMappingContext, PathMappingResult pathMappingResult, Request request, SSLSession sslSession, ProxiedAddresses proxiedAddresses)
Creates a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addAdditionalResponseHeader(AsciiString name, String value)
Adds a header with the specifiedname
andvalue
.void
addAdditionalResponseHeaders(Headers<? extends AsciiString,? extends String,?> headers)
HttpHeaders
additionalResponseHeaders()
ByteBufAllocator
alloc()
Returns theByteBufAllocator
for thisRequestContext
.ExecutorService
blockingTaskExecutor()
Returns theExecutorService
that could be used for executing a potentially long-running task.protected Channel
channel()
Returns theChannel
that is handling this request, ornull
if the connection is not established yet.EventLoop
eventLoop()
RequestLog
log()
Returns theRequestLog
that contains the information about the currentRequest
.RequestLogBuilder
logBuilder()
Returns theRequestLogBuilder
that collects the information about the currentRequest
.Logger
logger()
String
mappedPath()
Returns the path with its context path removed.long
maxRequestLength()
Returns the maximum length of the currentRequest
.MediaType
negotiatedResponseMediaType()
Returns the negotiated producible media type.ServiceRequestContext
newDerivedContext()
Creates a new derivedRequestContext
which only theRequestLog
is different from the deriving context.ServiceRequestContext
newDerivedContext(Request request)
Creates a new derivedRequestContext
with the specifiedRequest
which theRequestLog
is different from the deriving context.PathMapping
pathMapping()
PathMappingContext
pathMappingContext()
Returns thePathMappingContext
used to find theService
.Map<String,String>
pathParams()
Returns the path parameters mapped by thePathMapping
associated with theService
that is handling the currentRequest
.ProxiedAddresses
proxiedAddresses()
Returns the proxied addresses if the currentRequest
is received through a proxy.boolean
removeAdditionalResponseHeader(AsciiString name)
Removes all headers with the specifiedname
.Runnable
requestTimeoutHandler()
long
requestTimeoutMillis()
Returns the amount of time allowed until receiving the currentRequest
completely.Server
server()
<T extends Service<HttpRequest,HttpResponse>>
Tservice()
void
setAdditionalResponseHeader(AsciiString name, String value)
Sets a header with the specifiedname
andvalue
.void
setAdditionalResponseHeaders(Headers<? extends AsciiString,? extends String,?> headers)
Clears the current header and sets the specifiedHeaders
which is included when aService
sends anHttpResponse
.void
setMaxRequestLength(long maxRequestLength)
Sets the maximum length of the currentRequest
.void
setRequestTimeout(Duration requestTimeout)
Sets the amount of time allowed until receiving the currentRequest
completely.void
setRequestTimeoutChangeListener(RequestTimeoutChangeListener listener)
Sets the listener that is notified when the requestTimeoutMillis() request timeout} of the request is changed.void
setRequestTimeoutHandler(Runnable requestTimeoutHandler)
Sets a handler to run when the request times out.void
setRequestTimeoutMillis(long requestTimeoutMillis)
Sets the amount of time allowed until receiving the currentRequest
completely.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()
VirtualHost
virtualHost()
Returns theVirtualHost
that is handling the currentRequest
.-
Methods inherited from class com.linecorp.armeria.common.AbstractRequestContext
contextAwareEventLoop, equals, hashCode, isTimedOut, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, onEnter, onExit, rejectPromise, resolvePromise, setTimedOut
-
Methods inherited from interface io.netty.util.AttributeMap
attr, hasAttr
-
Methods inherited from class com.linecorp.armeria.common.NonWrappingRequestContext
attr, attrs, hasAttr, invokeOnChildCallbacks, invokeOnEnterCallbacks, invokeOnExitCallbacks, localAddress, meterRegistry, method, onChild, onEnter, onExit, path, query, remoteAddress, request, sessionProtocol
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface com.linecorp.armeria.common.RequestContext
attrs, contextAwareEventLoop, contextAwareExecutor, executor, invokeOnChildCallbacks, invokeOnEnterCallbacks, invokeOnExitCallbacks, isTimedOut, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, meterRegistry, method, onChild, onEnter, onEnter, onExit, onExit, path, push, push, pushIfAbsent, query, rejectPromise, request, resolvePromise, sessionProtocol
-
Methods inherited from interface com.linecorp.armeria.server.ServiceRequestContext
localAddress, negotiatedProduceType, pathParam, remoteAddress
-
-
-
-
Constructor Detail
-
DefaultServiceRequestContext
public DefaultServiceRequestContext(ServiceConfig cfg, Channel ch, MeterRegistry meterRegistry, SessionProtocol sessionProtocol, PathMappingContext pathMappingContext, PathMappingResult pathMappingResult, Request request, @Nullable SSLSession sslSession, @Nullable ProxiedAddresses proxiedAddresses)
Creates a new instance.- Parameters:
ch
- theChannel
that handles the invocationmeterRegistry
- theMeterRegistry
that collects various statssessionProtocol
- theSessionProtocol
of the invocationrequest
- the request associated with this contextsslSession
- theSSLSession
for this invocation if it is over TLS
-
-
Method Detail
-
newDerivedContext
public ServiceRequestContext newDerivedContext()
Description copied from interface:RequestContext
Creates a new derivedRequestContext
which only theRequestLog
is different from the deriving context. Note that the references ofAttribute
s in theRequestContext.attrs()
are copied as well.- Specified by:
newDerivedContext
in interfaceRequestContext
- Specified by:
newDerivedContext
in interfaceServiceRequestContext
-
newDerivedContext
public ServiceRequestContext newDerivedContext(Request request)
Description copied from interface:RequestContext
Creates a new derivedRequestContext
with the specifiedRequest
which theRequestLog
is different from the deriving context. Note that the references ofAttribute
s in theRequestContext.attrs()
are copied as well.- Specified by:
newDerivedContext
in interfaceRequestContext
- Specified by:
newDerivedContext
in interfaceServiceRequestContext
-
channel
protected Channel 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
-
server
public Server server()
Description copied from interface:ServiceRequestContext
- Specified by:
server
in interfaceServiceRequestContext
-
virtualHost
public VirtualHost virtualHost()
Description copied from interface:ServiceRequestContext
Returns theVirtualHost
that is handling the currentRequest
.- Specified by:
virtualHost
in interfaceServiceRequestContext
-
pathMapping
public PathMapping pathMapping()
Description copied from interface:ServiceRequestContext
- Specified by:
pathMapping
in interfaceServiceRequestContext
-
pathMappingContext
public PathMappingContext pathMappingContext()
Description copied from interface:ServiceRequestContext
Returns thePathMappingContext
used to find theService
.- Specified by:
pathMappingContext
in interfaceServiceRequestContext
-
pathParams
public Map<String,String> pathParams()
Description copied from interface:ServiceRequestContext
Returns the path parameters mapped by thePathMapping
associated with theService
that is handling the currentRequest
.- Specified by:
pathParams
in interfaceServiceRequestContext
-
service
public <T extends Service<HttpRequest,HttpResponse>> T service()
Description copied from interface:ServiceRequestContext
- Specified by:
service
in interfaceServiceRequestContext
-
blockingTaskExecutor
public ExecutorService blockingTaskExecutor()
Description copied from interface:ServiceRequestContext
Returns theExecutorService
that could be used for executing a potentially long-running task. TheExecutorService
will propagate theServiceRequestContext
automatically when running a task.Note that performing a long-running task in
Service.serve(ServiceRequestContext, Request)
may block theServer
's I/O event loop and thus should be executed in other threads.- Specified by:
blockingTaskExecutor
in interfaceServiceRequestContext
-
mappedPath
public String mappedPath()
Description copied from interface:ServiceRequestContext
Returns the 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
-
negotiatedResponseMediaType
@Nullable public MediaType 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
public EventLoop eventLoop()
Description copied from interface:RequestContext
- Specified by:
eventLoop
in interfaceRequestContext
-
logger
public Logger logger()
Description copied from interface:ServiceRequestContext
- Specified by:
logger
in interfaceServiceRequestContext
-
sslSession
@Nullable public SSLSession 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
- Overrides:
sslSession
in classNonWrappingRequestContext
-
requestTimeoutMillis
public long requestTimeoutMillis()
Description copied from interface:ServiceRequestContext
Returns the amount of time allowed until receiving the currentRequest
completely. This value is initially set fromServerConfig.defaultRequestTimeoutMillis()
.- Specified by:
requestTimeoutMillis
in interfaceServiceRequestContext
-
setRequestTimeoutMillis
public void setRequestTimeoutMillis(long requestTimeoutMillis)
Description copied from interface:ServiceRequestContext
Sets the amount of time allowed until receiving the currentRequest
completely. This value is initially set fromServerConfig.defaultRequestTimeoutMillis()
.- Specified by:
setRequestTimeoutMillis
in interfaceServiceRequestContext
-
setRequestTimeout
public void setRequestTimeout(Duration requestTimeout)
Description copied from interface:ServiceRequestContext
Sets the amount of time allowed until receiving the currentRequest
completely. This value is initially set fromServerConfig.defaultRequestTimeoutMillis()
.- Specified by:
setRequestTimeout
in interfaceServiceRequestContext
-
setRequestTimeoutHandler
public void setRequestTimeoutHandler(Runnable requestTimeoutHandler)
Description copied from interface:ServiceRequestContext
Sets a handler to run when the request times out.requestTimeoutHandler
must close the response, e.g., by callingStreamWriter.close()
. If not set, the response will be closed withHttpStatus.SERVICE_UNAVAILABLE
.For example,
HttpResponseWriter res = HttpResponse.streaming(); ctx.setRequestTimeoutHandler(() -> { res.write(HttpHeaders.of(HttpStatus.OK, MediaType.PLAIN_TEXT_UTF_8, "Request timed out.")); res.close(); }); ...
- Specified by:
setRequestTimeoutHandler
in interfaceServiceRequestContext
-
maxRequestLength
public long maxRequestLength()
Description copied from interface:ServiceRequestContext
Returns the maximum length of the currentRequest
. This value is initially set fromServerConfig.defaultMaxRequestLength()
. 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 fromServerConfig.defaultMaxRequestLength()
. If 0, there is no limit on the request size.- Specified by:
setMaxRequestLength
in interfaceServiceRequestContext
- See Also:
ContentTooLargeException
-
additionalResponseHeaders
public HttpHeaders additionalResponseHeaders()
Description copied from interface:ServiceRequestContext
- Specified by:
additionalResponseHeaders
in interfaceServiceRequestContext
-
setAdditionalResponseHeader
public void setAdditionalResponseHeader(AsciiString name, String value)
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
-
setAdditionalResponseHeaders
public void setAdditionalResponseHeaders(Headers<? extends AsciiString,? extends String,?> headers)
Description copied from interface:ServiceRequestContext
Clears the current header and sets the specifiedHeaders
which is included when aService
sends anHttpResponse
.- Specified by:
setAdditionalResponseHeaders
in interfaceServiceRequestContext
-
addAdditionalResponseHeader
public void addAdditionalResponseHeader(AsciiString name, String value)
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
-
addAdditionalResponseHeaders
public void addAdditionalResponseHeaders(Headers<? extends AsciiString,? extends String,?> headers)
Description copied from interface:ServiceRequestContext
- Specified by:
addAdditionalResponseHeaders
in interfaceServiceRequestContext
-
removeAdditionalResponseHeader
public boolean removeAdditionalResponseHeader(AsciiString name)
Description copied from interface:ServiceRequestContext
Removes all headers with the specifiedname
.- Specified by:
removeAdditionalResponseHeader
in interfaceServiceRequestContext
- Returns:
true
if at least one entry has been removed
-
proxiedAddresses
@Nullable public ProxiedAddresses proxiedAddresses()
Description copied from interface:ServiceRequestContext
Returns the proxied addresses if the currentRequest
is received through a proxy.- Specified by:
proxiedAddresses
in interfaceServiceRequestContext
-
log
public RequestLog log()
Description copied from interface:RequestContext
Returns theRequestLog
that contains the information about the currentRequest
.- Specified by:
log
in interfaceRequestContext
-
logBuilder
public RequestLogBuilder logBuilder()
Description copied from interface:RequestContext
Returns theRequestLogBuilder
that collects the information about the currentRequest
.- Specified by:
logBuilder
in interfaceRequestContext
-
alloc
public ByteBufAllocator 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
-
setRequestTimeoutChangeListener
public void setRequestTimeoutChangeListener(RequestTimeoutChangeListener listener)
Sets the listener that is notified when the requestTimeoutMillis() request timeout} of the request is changed.Note: This method is meant for internal use by server-side protocol implementation to reschedule a timeout task when a user updates the request timeout configuration.
-
-