@UnstableApi public final class DefaultServiceRequestContext extends NonWrappingRequestContext implements ServiceRequestContext
ServiceRequestContext implementation.| Constructor and Description |
|---|
DefaultServiceRequestContext(ServiceConfig cfg,
io.netty.channel.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.
|
| 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. |
io.netty.buffer.ByteBufAllocator |
alloc()
Returns the
ByteBufAllocator for this RequestContext. |
ScheduledExecutorService |
blockingTaskExecutor()
Returns the
ScheduledExecutorService that could be used for executing a potentially
long-running task. |
protected io.netty.channel.Channel |
channel()
Returns the
Channel that is handling this request, or null 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.
|
String |
decodedMappedPath()
Returns the
RequestContext.decodedPath() with its context path removed. |
io.netty.channel.EventLoop |
eventLoop()
Returns the
EventLoop that is handling the current Request. |
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, 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. |
String |
mappedPath()
Returns the
RequestContext.path() with its context path removed. |
long |
maxRequestLength()
Returns the maximum length of the current
Request. |
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. |
Map<String,String> |
pathParams()
Returns the path parameters mapped by the
ServiceRequestContext.route() associated with the Service
that is handling the current Request. |
ProxiedAddresses |
proxiedAddresses()
Returns the proxied addresses of the current
Request. |
<A extends SocketAddress> |
remoteAddress()
Returns the remote address of this request, or
null if the connection is not established yet. |
boolean |
removeAdditionalResponseHeader(CharSequence name)
Removes all headers with the specified
name. |
boolean |
removeAdditionalResponseTrailer(CharSequence name)
Removes all trailers with the specified
name. |
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 ServiceRequestContext.requestTimeoutMillis(). |
long |
requestTimeoutMillis()
|
Route |
route()
|
RoutingContext |
routingContext()
Returns the
RoutingContext used to find the Service. |
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)
|
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. |
String |
toString() |
boolean |
verboseResponses()
Returns whether the verbose response mode is enabled.
|
VirtualHost |
virtualHost()
Returns the
VirtualHost that is handling the current Request. |
attr, attrs, computeAttrIfAbsent, decodedPath, id, meterRegistry, method, ownAttr, ownAttrs, path, query, request, rpcRequest, sessionProtocol, setAttr, setAttrIfAbsent, unsafeUpdateRequest, updateRequest, updateRpcRequest, validateHeadersclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitbuilder, current, currentOrNull, mapCurrent, negotiatedProduceType, of, pathParam, push, request, rpcRequestattr, attrs, computeAttrIfAbsent, contextAwareEventLoop, contextAwareExecutor, decodedPath, executor, id, 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, updateRequest, updateRpcRequestpublic DefaultServiceRequestContext(ServiceConfig cfg, io.netty.channel.Channel ch, MeterRegistry meterRegistry, SessionProtocol sessionProtocol, RequestId id, RoutingContext routingContext, RoutingResult routingResult, HttpRequest req, SSLSession sslSession, ProxiedAddresses proxiedAddresses, InetAddress clientAddress, long requestStartTimeNanos, long requestStartTimeMicros)
cfg - the ServiceConfigch - the Channel that handles the invocationmeterRegistry - the MeterRegistry that collects various statssessionProtocol - the SessionProtocol of the invocationid - the RequestId that represents the identifier of the current Request
and Response pair.routingContext - the parameters which are used when finding a matched RouteroutingResult - the result of finding a matched Routereq - the request associated with this contextsslSession - the SSLSession 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.public <A extends SocketAddress> A remoteAddress()
RequestContextnull if the connection is not established yet.remoteAddress in interface RequestContextremoteAddress in interface ServiceRequestContextremoteAddress in class NonWrappingRequestContextpublic <A extends SocketAddress> A localAddress()
RequestContextnull if the connection is not established yet.localAddress in interface RequestContextlocalAddress in interface ServiceRequestContextlocalAddress in class NonWrappingRequestContextpublic InetAddress clientAddress()
ServiceRequestContextclientAddress in interface ServiceRequestContextpublic ServiceRequestContext 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 RequestContextnewDerivedContext in interface ServiceRequestContextprotected io.netty.channel.Channel channel()
NonWrappingRequestContextChannel that is handling this request, or null if the connection is not
established yet.channel in class NonWrappingRequestContextpublic Server server()
ServiceRequestContextserver in interface ServiceRequestContextpublic VirtualHost virtualHost()
ServiceRequestContextVirtualHost that is handling the current Request.virtualHost in interface ServiceRequestContextpublic Route route()
ServiceRequestContextroute in interface ServiceRequestContextpublic RoutingContext routingContext()
ServiceRequestContextRoutingContext used to find the Service.routingContext in interface ServiceRequestContextpublic Map<String,String> pathParams()
ServiceRequestContextServiceRequestContext.route() associated with the Service
that is handling the current Request.pathParams in interface ServiceRequestContextpublic HttpService service()
ServiceRequestContextHttpService that is handling the current Request.service in interface ServiceRequestContextpublic ScheduledExecutorService blockingTaskExecutor()
ServiceRequestContextScheduledExecutorService 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.
blockingTaskExecutor in interface ServiceRequestContextpublic String mappedPath()
ServiceRequestContextRequestContext.path() with its context path removed. This method can be useful for a reusable
service bound at various path prefixes.mappedPath in interface ServiceRequestContextpublic String decodedMappedPath()
ServiceRequestContextRequestContext.decodedPath() with its context path removed. This method can be useful for
a reusable service bound at various path prefixes.decodedMappedPath in interface ServiceRequestContextpublic MediaType negotiatedResponseMediaType()
ServiceRequestContextService, null would be returned.negotiatedResponseMediaType in interface ServiceRequestContextpublic io.netty.channel.EventLoop eventLoop()
RequestContextEventLoop that is handling the current Request.eventLoop in interface RequestContextpublic SSLSession sslSession()
RequestContextSSLSession 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.sslSession in interface RequestContextpublic long requestTimeoutMillis()
ServiceRequestContextRequest until receiving
the current Request and sending the corresponding Response completely.
This value is initially set from ServiceConfig.requestTimeoutMillis().requestTimeoutMillis in interface ServiceRequestContextpublic void clearRequestTimeout()
ServiceRequestContextclearRequestTimeout in interface ServiceRequestContextpublic void setRequestTimeoutMillis(long requestTimeoutMillis)
ServiceRequestContextRequest 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;
setRequestTimeoutMillis in interface ServiceRequestContextrequestTimeoutMillis - the amount of time in milliseconds from the start time of the requestpublic void setRequestTimeout(Duration requestTimeout)
ServiceRequestContextRequest 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;
setRequestTimeout in interface ServiceRequestContextrequestTimeout - the amount of time from the start time of the requestpublic void extendRequestTimeoutMillis(long adjustmentMillis)
ServiceRequestContextadjustmentMillis.
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;
extendRequestTimeoutMillis in interface ServiceRequestContextadjustmentMillis - the amount of time in milliseconds to extend the current timeout bypublic void extendRequestTimeout(Duration adjustment)
ServiceRequestContextadjustment.
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;
extendRequestTimeout in interface ServiceRequestContextadjustment - the amount of time to extend the current timeout bypublic void setRequestTimeoutAfterMillis(long requestTimeoutMillis)
ServiceRequestContextRequest 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);
setRequestTimeoutAfterMillis in interface ServiceRequestContextrequestTimeoutMillis - the amount of time allowed in milliseconds from nowpublic void setRequestTimeoutAfter(Duration requestTimeout)
ServiceRequestContextRequest 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));
setRequestTimeoutAfter in interface ServiceRequestContextrequestTimeout - the amount of time allowed from nowpublic void setRequestTimeoutAtMillis(long requestTimeoutAtMillis)
ServiceRequestContext1970-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);
setRequestTimeoutAtMillis in interface ServiceRequestContextrequestTimeoutAtMillis - the request timeout represented as the number of milliseconds
since the epoch (1970-01-01T00:00:00Z)public void setRequestTimeoutAt(Instant requestTimeoutAt)
ServiceRequestContext1970-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));
setRequestTimeoutAt in interface ServiceRequestContextrequestTimeoutAt - the request timeout represented as the number of milliseconds
since the epoch (1970-01-01T00:00:00Z)public Runnable requestTimeoutHandler()
ServiceRequestContextRequest timeout handler which is executed when
receiving the current Request and sending the corresponding Response
is not completely received within the allowed ServiceRequestContext.requestTimeoutMillis().requestTimeoutHandler in interface ServiceRequestContextpublic void setRequestTimeoutHandler(Runnable requestTimeoutHandler)
ServiceRequestContextrequestTimeoutHandler 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();
});
...
setRequestTimeoutHandler in interface ServiceRequestContextpublic boolean isTimedOut()
ServiceRequestContextServiceRequestContext has been timed-out (e.g., when the
corresponding request passes a deadline).isTimedOut in interface ServiceRequestContextpublic long maxRequestLength()
ServiceRequestContextRequest.
This value is initially set from ServiceConfig.maxRequestLength().
If 0, there is no limit on the request size.maxRequestLength in interface ServiceRequestContextContentTooLargeExceptionpublic void setMaxRequestLength(long maxRequestLength)
ServiceRequestContextRequest.
This value is initially set from ServiceConfig.maxRequestLength().
If 0, there is no limit on the request size.setMaxRequestLength in interface ServiceRequestContextContentTooLargeExceptionpublic boolean verboseResponses()
ServiceRequestContextverboseResponses in interface ServiceRequestContextpublic AccessLogWriter accessLogWriter()
ServiceRequestContextAccessLogWriter.accessLogWriter in interface ServiceRequestContextpublic HttpHeaders additionalResponseHeaders()
ServiceRequestContextadditionalResponseHeaders in interface ServiceRequestContextpublic void setAdditionalResponseHeader(CharSequence name, Object value)
ServiceRequestContextname and value. This will remove all previous values
associated with the specified name.
The header will be included when a Service sends an HttpResponse.setAdditionalResponseHeader in interface ServiceRequestContextpublic void setAdditionalResponseHeaders(Iterable<? extends Map.Entry<? extends CharSequence,?>> headers)
ServiceRequestContextHttpHeaders which is included when a
Service sends an HttpResponse.setAdditionalResponseHeaders in interface ServiceRequestContextpublic void addAdditionalResponseHeader(CharSequence name, Object value)
ServiceRequestContextname and value. The header will be included when
a Service sends an HttpResponse.addAdditionalResponseHeader in interface ServiceRequestContextpublic void addAdditionalResponseHeaders(Iterable<? extends Map.Entry<? extends CharSequence,?>> headers)
ServiceRequestContextaddAdditionalResponseHeaders in interface ServiceRequestContextpublic boolean removeAdditionalResponseHeader(CharSequence name)
ServiceRequestContextname.removeAdditionalResponseHeader in interface ServiceRequestContexttrue if at least one entry has been removedpublic HttpHeaders additionalResponseTrailers()
ServiceRequestContextHttpHeaders which is returned along with any other trailers when a
Service completes an HttpResponse.additionalResponseTrailers in interface ServiceRequestContextpublic void setAdditionalResponseTrailer(CharSequence name, Object value)
ServiceRequestContextname and value. This will remove all previous values
associated with the specified name.
The trailer will be included when a Service completes an HttpResponse.setAdditionalResponseTrailer in interface ServiceRequestContextpublic void setAdditionalResponseTrailers(Iterable<? extends Map.Entry<? extends CharSequence,?>> headers)
ServiceRequestContextHttpHeaders which is included when a
Service completes an HttpResponse.setAdditionalResponseTrailers in interface ServiceRequestContextpublic void addAdditionalResponseTrailer(CharSequence name, Object value)
ServiceRequestContextname and value. The trailer will be included when
a Service completes an HttpResponse.addAdditionalResponseTrailer in interface ServiceRequestContextpublic void addAdditionalResponseTrailers(Iterable<? extends Map.Entry<? extends CharSequence,?>> headers)
ServiceRequestContextaddAdditionalResponseTrailers in interface ServiceRequestContextpublic boolean removeAdditionalResponseTrailer(CharSequence name)
ServiceRequestContextname.removeAdditionalResponseTrailer in interface ServiceRequestContexttrue if at least one entry has been removedpublic ProxiedAddresses proxiedAddresses()
ServiceRequestContextRequest.proxiedAddresses in interface ServiceRequestContextpublic RequestLogAccess log()
RequestContextRequestLogAccess that provides the access to the RequestLog, which
contains the information collected while processing the current Request.log in interface RequestContextpublic RequestLogBuilder logBuilder()
RequestContextRequestLogBuilder that collects the information about the current Request.logBuilder in interface RequestContextpublic io.netty.buffer.ByteBufAllocator alloc()
RequestContextByteBufAllocator 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.alloc in interface RequestContextCopyright © 2020 LeanCloud. All rights reserved.