public interface ServiceRequestContext extends RequestContext
ServiceRequestContext instance.| Modifier and Type | Method and Description |
|---|---|
AccessLogWriter |
accessLogWriter()
Returns the
AccessLogWriter. |
void |
addAdditionalResponseHeader(CharSequence name,
Object value)
Adds a header with the specified
name and value. |
void |
addAdditionalResponseHeaders(Iterable<? extends Map.Entry<? extends CharSequence,?>> headers)
|
void |
addAdditionalResponseTrailer(CharSequence name,
Object value)
Adds a trailer with the specified
name and value. |
void |
addAdditionalResponseTrailers(Iterable<? extends Map.Entry<? extends CharSequence,?>> headers)
|
HttpHeaders |
additionalResponseHeaders()
|
HttpHeaders |
additionalResponseTrailers()
Returns the
HttpHeaders which is returned along with any other trailers when a
Service completes an HttpResponse. |
ScheduledExecutorService |
blockingTaskExecutor()
Returns the
ScheduledExecutorService that could be used for executing a potentially
long-running task. |
static ServiceRequestContextBuilder |
builder(HttpRequest request)
Returns a new
ServiceRequestContextBuilder created from the specified HttpRequest. |
void |
clearRequestTimeout()
Clears the previously scheduled request timeout, if any.
|
default InetAddress |
clientAddress()
Returns the address of the client who initiated this request.
|
static ServiceRequestContext |
current()
Returns the server-side context of the
Request that is being handled in the current thread. |
static ServiceRequestContext |
currentOrNull()
Returns the server-side context of the
Request that is being handled in the current thread. |
String |
decodedMappedPath()
Returns the
RequestContext.decodedPath() with its context path removed. |
void |
extendRequestTimeout(Duration adjustment)
Extends the previously scheduled request timeout by the specified amount of
adjustment. |
void |
extendRequestTimeoutMillis(long adjustmentMillis)
Extends the previously scheduled request timeout by the specified amount of
adjustmentMillis. |
boolean |
isTimedOut()
Returns whether this
ServiceRequestContext has been timed-out (e.g., when the
corresponding request passes a deadline). |
<A extends SocketAddress> |
localAddress()
Returns the local address of this request.
|
static <T> T |
mapCurrent(Function<? super ServiceRequestContext,T> mapper,
Supplier<T> defaultValueSupplier)
Maps the server-side context of the
Request that is being handled in the current thread. |
String |
mappedPath()
Returns the
RequestContext.path() with its context path removed. |
long |
maxRequestLength()
Returns the maximum length of the current
Request. |
default MediaType |
negotiatedProduceType()
Deprecated.
|
MediaType |
negotiatedResponseMediaType()
Returns the negotiated producible media type.
|
ServiceRequestContext |
newDerivedContext(RequestId id,
HttpRequest req,
RpcRequest rpcReq)
Creates a new
RequestContext whose properties and RequestContext.attrs() are copied from this
RequestContext, except having a different pair of HttpRequest and RpcRequest
and its own RequestLog. |
static ServiceRequestContext |
of(HttpRequest request)
Returns a new
ServiceRequestContext created from the specified HttpRequest. |
default String |
pathParam(String name)
Returns the value of the specified path parameter.
|
Map<String,String> |
pathParams()
|
ProxiedAddresses |
proxiedAddresses()
Returns the proxied addresses of the current
Request. |
default SafeCloseable |
push()
Pushes this context to the thread-local stack.
|
<A extends SocketAddress> |
remoteAddress()
Returns the remote address of this request.
|
boolean |
removeAdditionalResponseHeader(CharSequence name)
Removes all headers with the specified
name. |
boolean |
removeAdditionalResponseTrailer(CharSequence name)
Removes all trailers with the specified
name. |
HttpRequest |
request()
Returns the
HttpRequest associated with this context. |
Runnable |
requestTimeoutHandler()
Returns
Request timeout handler which is executed when
receiving the current Request and sending the corresponding Response
is not completely received within the allowed requestTimeoutMillis(). |
long |
requestTimeoutMillis()
|
Route |
route()
|
RoutingContext |
routingContext()
Returns the
RoutingContext used to find the Service. |
RpcRequest |
rpcRequest()
Returns the
RpcRequest associated with this context, or null if there's no
RpcRequest associated with this context. |
Server |
server()
|
HttpService |
service()
Returns the
HttpService that is handling the current Request. |
void |
setAdditionalResponseHeader(CharSequence name,
Object value)
Sets a header with the specified
name and value. |
void |
setAdditionalResponseHeaders(Iterable<? extends Map.Entry<? extends CharSequence,?>> headers)
Clears the current header and sets the specified
HttpHeaders which is included when a
Service sends an HttpResponse. |
void |
setAdditionalResponseTrailer(CharSequence name,
Object value)
Sets a trailer with the specified
name and value. |
void |
setAdditionalResponseTrailers(Iterable<? extends Map.Entry<? extends CharSequence,?>> headers)
Clears the current trailer and sets the specified
HttpHeaders which is included when a
Service completes an HttpResponse. |
void |
setMaxRequestLength(long maxRequestLength)
Sets the maximum length of the current
Request. |
void |
setRequestTimeout(Duration requestTimeout)
|
void |
setRequestTimeoutAfter(Duration requestTimeout)
|
void |
setRequestTimeoutAfterMillis(long requestTimeoutMillis)
|
void |
setRequestTimeoutAt(Instant requestTimeoutAt)
Schedules the request timeout that is triggered at the specified time represented
as the number since the epoch (
1970-01-01T00:00:00Z). |
void |
setRequestTimeoutAtMillis(long requestTimeoutAtMillis)
Schedules the request timeout that is triggered at the specified time represented
as the number since the epoch (
1970-01-01T00:00:00Z). |
void |
setRequestTimeoutHandler(Runnable requestTimeoutHandler)
Sets a handler to run when the request times out.
|
void |
setRequestTimeoutMillis(long requestTimeoutMillis)
|
boolean |
verboseResponses()
Returns whether the verbose response mode is enabled.
|
VirtualHost |
virtualHost()
Returns the
VirtualHost that is handling the current Request. |
alloc, attr, attrs, computeAttrIfAbsent, contextAwareEventLoop, contextAwareExecutor, decodedPath, eventLoop, executor, id, log, logBuilder, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, makeContextAware, mapCurrent, meterRegistry, method, path, push, push, push, pushIfAbsent, query, rejectPromise, replace, resolvePromise, sessionProtocol, setAttr, setAttrIfAbsent, sslSession, updateRequest, updateRpcRequeststatic ServiceRequestContext current()
Request that is being handled in the current thread.
If the context is a ClientRequestContext, ClientRequestContext.root() is returned.current in interface RequestContextIllegalStateException - if the context is unavailable in the current thread or
the current context is a ClientRequestContext and
ClientRequestContext.root() is nullstatic ServiceRequestContext currentOrNull()
Request that is being handled in the current thread.
If the context is a ClientRequestContext, ClientRequestContext.root() is returned.currentOrNull in interface RequestContextServiceRequestContext available in the current thread,
or null if unavailable.static <T> T mapCurrent(Function<? super ServiceRequestContext,T> mapper, Supplier<T> defaultValueSupplier)
Request that is being handled in the current thread.mapper - the Function that maps the ServiceRequestContextdefaultValueSupplier - 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.IllegalStateException - if the current context is not a ServiceRequestContext.static ServiceRequestContext of(HttpRequest request)
ServiceRequestContext created from the specified HttpRequest.
Note that it is not usually required to create a new context by yourself, because Armeria
will always provide a context object for you. However, it may be useful in some cases such as
unit testing.ServiceRequestContextBuilderstatic ServiceRequestContextBuilder builder(HttpRequest request)
ServiceRequestContextBuilder created from the specified HttpRequest.HttpRequest request()
HttpRequest associated with this context.request in interface RequestContextRpcRequest rpcRequest()
RpcRequest associated with this context, or null if there's no
RpcRequest associated with this context. For example, this method will return null when the request being handled is
1) not an RPC request or 2) not decoded into an RPC request yet.rpcRequest in interface RequestContext<A extends SocketAddress> A remoteAddress()
remoteAddress in interface RequestContext<A extends SocketAddress> A localAddress()
localAddress in interface RequestContextdefault InetAddress clientAddress()
default SafeCloseable push()
SafeCloseable.close(), which can be done using a try-with-resources block:
try (SafeCloseable ignored = ctx.push()) {
...
}
In order to call this method, the current thread-local state must meet one of the following conditions:
RequestContext in itServiceRequestContext as this - reentranceClientRequestContext whose ClientRequestContext.root()
is the same ServiceRequestContext as thisIllegalStateException.push in interface RequestContextServiceRequestContext newDerivedContext(RequestId id, HttpRequest req, RpcRequest rpcReq)
RequestContextRequestContext whose properties and RequestContext.attrs() are copied from this
RequestContext, except having a different pair of HttpRequest and RpcRequest
and its own RequestLog.newDerivedContext in interface RequestContextServer server()
VirtualHost virtualHost()
VirtualHost that is handling the current Request.Route route()
RoutingContext routingContext()
RoutingContext used to find the Service.HttpService service()
HttpService that is handling the current Request.ScheduledExecutorService blockingTaskExecutor()
ScheduledExecutorService that could be used for executing a potentially
long-running task. The ScheduledExecutorService will propagate the ServiceRequestContext
automatically when running a task.
Note that performing a long-running task in Service.serve(ServiceRequestContext, Request)
may block the Server's I/O event loop and thus should be executed in other threads.
String mappedPath()
RequestContext.path() with its context path removed. This method can be useful for a reusable
service bound at various path prefixes.String decodedMappedPath()
RequestContext.decodedPath() with its context path removed. This method can be useful for
a reusable service bound at various path prefixes.MediaType negotiatedResponseMediaType()
Service, null would be returned.@Deprecated default MediaType negotiatedProduceType()
negotiatedResponseMediaType().Service, null would be returned.long requestTimeoutMillis()
Request until receiving
the current Request and sending the corresponding Response completely.
This value is initially set from ServiceConfig.requestTimeoutMillis().void clearRequestTimeout()
@Deprecated void setRequestTimeoutMillis(long requestTimeoutMillis)
extendRequestTimeoutMillis(long))}, setRequestTimeoutAfterMillis(long),
setRequestTimeoutAtMillis(long) or clearRequestTimeout()Request is not fully received or
the corresponding Response is not sent completely since the Request started.
This value is initially set from ServiceConfig.requestTimeoutMillis().
For example:
ServiceRequestContext ctx = ...;
ctx.setRequestTimeoutMillis(1000);
assert ctx.requestTimeoutMillis() == 1000;
ctx.setRequestTimeoutMillis(2000);
assert ctx.requestTimeoutMillis() == 2000;
requestTimeoutMillis - the amount of time in milliseconds from the start time of the request@Deprecated void setRequestTimeout(Duration requestTimeout)
extendRequestTimeout(Duration), setRequestTimeoutAfter(Duration),
setRequestTimeoutAt(Instant) or clearRequestTimeout()Request is not fully received or
the corresponding Response is not sent completely since the Request started.
This value is initially set from ServiceConfig.requestTimeoutMillis().
For example:
ServiceRequestContext ctx = ...;
ctx.setRequestTimeout(Duration.ofSeconds(1));
assert ctx.requestTimeoutMillis() == 1000;
ctx.setRequestTimeout(Duration.ofSeconds(2));
assert ctx.requestTimeoutMillis() == 2000;
requestTimeout - the amount of time from the start time of the requestvoid extendRequestTimeoutMillis(long adjustmentMillis)
adjustmentMillis.
This method does nothing if no request timeout was scheduled previously.
Note that a negative adjustment reduces the current timeout.
The initial timeout is set from ServiceConfig.requestTimeoutMillis().
For example:
ServiceRequestContext ctx = ...;
long oldRequestTimeoutMillis = ctx.requestTimeoutMillis();
ctx.extendRequestTimeoutMillis(1000);
assert ctx.requestTimeoutMillis() == oldRequestTimeoutMillis + 1000;
ctx.extendRequestTimeoutMillis(-500);
assert ctx.requestTimeoutMillis() == oldRequestTimeoutMillis + 500;
adjustmentMillis - the amount of time in milliseconds to extend the current timeout byvoid extendRequestTimeout(Duration adjustment)
adjustment.
This method does nothing if no response timeout was scheduled previously.
Note that a negative adjustment reduces the current timeout.
The initial timeout is set from ServiceConfig.requestTimeoutMillis().
For example:
ServiceRequestContext ctx = ...;
long oldRequestTimeoutMillis = ctx.requestTimeoutMillis();
ctx.extendRequestTimeout(Duration.ofSeconds(1));
assert ctx.requestTimeoutMillis() == oldRequestTimeoutMillis + 1000;
ctx.extendRequestTimeout(Duration.ofMillis(-500));
assert ctx.requestTimeoutMillis() == oldRequestTimeoutMillis + 500;
adjustment - the amount of time to extend the current timeout byvoid setRequestTimeoutAfterMillis(long requestTimeoutMillis)
Request is not fully received or
the corresponding Response is not sent completely within the specified amount of time from now.
Note that the specified requestTimeoutMillis must be positive.
The initial timeout is set from ServiceConfig.requestTimeoutMillis().
For example:
ServiceRequestContext ctx = ...;
// Schedules timeout after 1 seconds from now.
ctx.setRequestTimeoutAfterMillis(1000);
requestTimeoutMillis - the amount of time allowed in milliseconds from nowvoid setRequestTimeoutAfter(Duration requestTimeout)
Request is not fully received or
the corresponding Response is not sent completely within the specified amount time from now.
Note that the specified requestTimeout must be positive.
The initial timeout is set from ServiceConfig.requestTimeoutMillis().
For example:
ServiceRequestContext ctx = ...;
// Schedules timeout after 1 seconds from now.
ctx.setRequestTimeoutAfter(Duration.ofSeconds(1));
requestTimeout - the amount of time allowed from nowvoid setRequestTimeoutAtMillis(long requestTimeoutAtMillis)
1970-01-01T00:00:00Z).
Note that the request will be timed out immediately if the specified time is before now.
The initial timeout is set from ServiceConfig.requestTimeoutMillis().
For example:
ServiceRequestContext ctx = ...;
// Schedules timeout after 1 seconds from now.
long responseTimeoutAt = Instant.now().plus(1, ChronoUnit.SECONDS).toEpochMilli();
ctx.setRequestTimeoutAtMillis(responseTimeoutAt);
requestTimeoutAtMillis - the request timeout represented as the number of milliseconds
since the epoch (1970-01-01T00:00:00Z)void setRequestTimeoutAt(Instant requestTimeoutAt)
1970-01-01T00:00:00Z).
Note that the request will be timed out immediately if the specified time is before now.
The initial timeout is set from ServiceConfig.requestTimeoutMillis().
For example:
ServiceRequestContext ctx = ...;
// Schedules timeout after 1 seconds from now.
ctx.setRequestTimeoutAt(Instant.now().plus(1, ChronoUnit.SECONDS));
requestTimeoutAt - the request timeout represented as the number of milliseconds
since the epoch (1970-01-01T00:00:00Z)Runnable requestTimeoutHandler()
Request timeout handler which is executed when
receiving the current Request and sending the corresponding Response
is not completely received within the allowed requestTimeoutMillis().void setRequestTimeoutHandler(Runnable requestTimeoutHandler)
requestTimeoutHandler must close the response,
e.g., by calling StreamWriter.close(). If not set, the response will be closed with
HttpStatus.SERVICE_UNAVAILABLE.
For example,
HttpResponseWriter res = HttpResponse.streaming();
ctx.setRequestTimeoutHandler(() -> {
res.write(ResponseHeaders.of(HttpStatus.OK,
HttpHeaderNames.CONTENT_TYPE, MediaType.PLAIN_TEXT_UTF_8));
res.write(HttpData.ofUtf8("Request timed out."));
res.close();
});
...
boolean isTimedOut()
ServiceRequestContext has been timed-out (e.g., when the
corresponding request passes a deadline).long maxRequestLength()
Request.
This value is initially set from ServiceConfig.maxRequestLength().
If 0, there is no limit on the request size.ContentTooLargeExceptionvoid setMaxRequestLength(long maxRequestLength)
Request.
This value is initially set from ServiceConfig.maxRequestLength().
If 0, there is no limit on the request size.ContentTooLargeExceptionboolean verboseResponses()
AccessLogWriter accessLogWriter()
AccessLogWriter.HttpHeaders additionalResponseHeaders()
void setAdditionalResponseHeader(CharSequence name, Object value)
name and value. This will remove all previous values
associated with the specified name.
The header will be included when a Service sends an HttpResponse.void setAdditionalResponseHeaders(Iterable<? extends Map.Entry<? extends CharSequence,?>> headers)
HttpHeaders which is included when a
Service sends an HttpResponse.void addAdditionalResponseHeader(CharSequence name, Object value)
name and value. The header will be included when
a Service sends an HttpResponse.void addAdditionalResponseHeaders(Iterable<? extends Map.Entry<? extends CharSequence,?>> headers)
boolean removeAdditionalResponseHeader(CharSequence name)
name.true if at least one entry has been removedHttpHeaders additionalResponseTrailers()
HttpHeaders which is returned along with any other trailers when a
Service completes an HttpResponse.void setAdditionalResponseTrailer(CharSequence name, Object value)
name and value. This will remove all previous values
associated with the specified name.
The trailer will be included when a Service completes an HttpResponse.void setAdditionalResponseTrailers(Iterable<? extends Map.Entry<? extends CharSequence,?>> headers)
HttpHeaders which is included when a
Service completes an HttpResponse.void addAdditionalResponseTrailer(CharSequence name, Object value)
name and value. The trailer will be included when
a Service completes an HttpResponse.void addAdditionalResponseTrailers(Iterable<? extends Map.Entry<? extends CharSequence,?>> headers)
boolean removeAdditionalResponseTrailer(CharSequence name)
name.true if at least one entry has been removedProxiedAddresses proxiedAddresses()
Request.Copyright © 2020 LeanCloud. All rights reserved.