public interface ClientRequestContext extends RequestContext
Request, its Response and its related utilities.
Every client request has its own ClientRequestContext instance.| Modifier and Type | Method and Description |
|---|---|
void |
addAdditionalRequestHeader(CharSequence name,
Object value)
Adds a header with the specified
name and value. |
void |
addAdditionalRequestHeaders(Iterable<? extends Map.Entry<? extends CharSequence,?>> headers)
|
HttpHeaders |
additionalRequestHeaders()
|
<V> V |
attr(io.netty.util.AttributeKey<V> key)
Returns the value mapped to the given
AttributeKey or null if there's no value set by
RequestContext.setAttr(AttributeKey, Object) or RequestContext.setAttrIfAbsent(AttributeKey, Object). |
Iterator<Map.Entry<io.netty.util.AttributeKey<?>,Object>> |
attrs()
|
static ClientRequestContextBuilder |
builder(HttpRequest request)
Returns a new
ClientRequestContextBuilder created from the specified HttpRequest. |
static ClientRequestContextBuilder |
builder(RpcRequest request,
String uri)
|
static ClientRequestContextBuilder |
builder(RpcRequest request,
URI uri)
|
void |
clearResponseTimeout()
Clears the previously scheduled response timeout, if any.
|
static ClientRequestContext |
current()
Returns the client-side context of the
Request that is being handled in the current thread. |
static ClientRequestContext |
currentOrNull()
Returns the client-side context of the
Request that is being handled in the current thread. |
Endpoint |
endpoint()
|
EndpointGroup |
endpointGroup()
Returns the
EndpointGroup used for the current Request. |
void |
extendResponseTimeout(Duration adjustment)
Extends the previously scheduled response timeout by the specified amount of
adjustment. |
void |
extendResponseTimeoutMillis(long adjustmentMillis)
Extends the previously scheduled response timeout by
the specified amount of
adjustmentMillis. |
String |
fragment()
Returns the fragment part of the URI of the current
Request, as defined in
the section 3.5 of RFC3986. |
static <T> T |
mapCurrent(Function<? super ClientRequestContext,T> mapper,
Supplier<T> defaultValueSupplier)
Maps the client-side context of the
Request that is being handled in the current thread. |
long |
maxResponseLength()
Returns the maximum length of the received
Response. |
default ClientRequestContext |
newDerivedContext(RequestId id,
HttpRequest req,
RpcRequest rpcReq)
Creates a new
ClientRequestContext whose properties and Attributes are copied from this
ClientRequestContext, except having a different Request and its own RequestLog. |
ClientRequestContext |
newDerivedContext(RequestId id,
HttpRequest req,
RpcRequest rpcReq,
Endpoint endpoint)
Creates a new
ClientRequestContext whose properties and Attributes are copied from this
ClientRequestContext, except having different Request, Endpoint and its own
RequestLog. |
static ClientRequestContext |
of(HttpRequest request)
Returns a new
ClientRequestContext created from the specified HttpRequest. |
static ClientRequestContext |
of(RpcRequest request,
String uri)
Returns a new
ClientRequestContext created from the specified RpcRequest and URI. |
static ClientRequestContext |
of(RpcRequest request,
URI uri)
|
ClientOptions |
options()
Returns the
ClientOptions of the current Request. |
<V> V |
ownAttr(io.netty.util.AttributeKey<V> key)
Returns the value mapped to the given
AttributeKey or null if there's no value set by
RequestContext.setAttr(AttributeKey, Object) or RequestContext.setAttrIfAbsent(AttributeKey, Object). |
Iterator<Map.Entry<io.netty.util.AttributeKey<?>,Object>> |
ownAttrs()
|
default SafeCloseable |
push()
Pushes this context to the thread-local stack.
|
boolean |
removeAdditionalRequestHeader(CharSequence name)
Removes all headers with the specified
name. |
HttpRequest |
request()
Returns the
HttpRequest associated with this context, or null if there's no
HttpRequest associated with this context yet. |
Runnable |
responseTimeoutHandler()
Returns
Response timeout handler which is executed when
the Response is not completely received within the allowed responseTimeoutMillis()
or the default ClientOption.RESPONSE_TIMEOUT_MILLIS. |
long |
responseTimeoutMillis()
|
ServiceRequestContext |
root()
Returns the
ServiceRequestContext whose Service invokes the Client
Request which created this ClientRequestContext, or null if this client request
was not made in the context of a server request. |
RpcRequest |
rpcRequest()
Returns the
RpcRequest associated with this context, or null if there's no
RpcRequest associated with this context. |
void |
setAdditionalRequestHeader(CharSequence name,
Object value)
Sets a header with the specified
name and value. |
void |
setAdditionalRequestHeaders(Iterable<? extends Map.Entry<? extends CharSequence,?>> headers)
Clears the current header and sets the specified
HttpHeaders which is included when a
Client sends an HttpRequest. |
void |
setMaxResponseLength(long maxResponseLength)
Sets the maximum length of the received
Response. |
void |
setResponseTimeout(Duration responseTimeout)
|
void |
setResponseTimeoutAfter(Duration responseTimeout)
Schedules the response timeout that is triggered when the
Response is not
fully received within the specified amount of time from now. |
void |
setResponseTimeoutAfterMillis(long responseTimeoutMillis)
Schedules the response timeout that is triggered when the
Response is not
fully received within the specified amount of time from now. |
void |
setResponseTimeoutAt(Instant responseTimeoutAt)
Schedules the response timeout that is triggered at the specified time represented
as the number of milliseconds since the epoch (
1970-01-01T00:00:00Z). |
void |
setResponseTimeoutAtMillis(long responseTimeoutAtMillis)
Schedules the response timeout that is triggered at the specified time represented
as the number since the epoch (
1970-01-01T00:00:00Z). |
void |
setResponseTimeoutHandler(Runnable responseTimeoutHandler)
Sets a handler to run when the response times out.
|
void |
setResponseTimeoutMillis(long responseTimeoutMillis)
|
void |
setWriteTimeout(Duration writeTimeout)
Returns the amount of time allowed until the initial write attempt of the current
Request
succeeds. |
void |
setWriteTimeoutMillis(long writeTimeoutMillis)
Returns the amount of time allowed until the initial write attempt of the current
Request
succeeds. |
long |
writeTimeoutMillis()
Returns the amount of time allowed until the initial write attempt of the current
Request
succeeds. |
alloc, computeAttrIfAbsent, contextAwareEventLoop, contextAwareExecutor, decodedPath, eventLoop, executor, id, localAddress, 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, remoteAddress, replace, resolvePromise, sessionProtocol, setAttr, setAttrIfAbsent, sslSession, updateRequest, updateRpcRequeststatic ClientRequestContext current()
Request that is being handled in the current thread.current in interface RequestContextIllegalStateException - if the context is unavailable in the current thread or
the current context is not a ClientRequestContext.static ClientRequestContext currentOrNull()
Request that is being handled in the current thread.currentOrNull in interface RequestContextClientRequestContext available in the current thread, or null if unavailable.IllegalStateException - if the current context is not a ClientRequestContext.static <T> T mapCurrent(Function<? super ClientRequestContext,T> mapper, Supplier<T> defaultValueSupplier)
Request that is being handled in the current thread.mapper - the Function that maps the ClientRequestContextdefaultValueSupplier - 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 ClientRequestContext.static ClientRequestContext of(HttpRequest request)
ClientRequestContext 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.ClientRequestContextBuilderstatic ClientRequestContext of(RpcRequest request, String uri)
ClientRequestContext created from the specified RpcRequest and URI.
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.ClientRequestContextBuilderstatic ClientRequestContext of(RpcRequest request, URI uri)
ClientRequestContext created from the specified RpcRequest and URI.
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.ClientRequestContextBuilderstatic ClientRequestContextBuilder builder(HttpRequest request)
ClientRequestContextBuilder created from the specified HttpRequest.static ClientRequestContextBuilder builder(RpcRequest request, String uri)
static ClientRequestContextBuilder builder(RpcRequest request, URI uri)
ServiceRequestContext root()
ServiceRequestContext whose Service invokes the Client
Request which created this ClientRequestContext, or null if this client request
was not made in the context of a server request.<V> V attr(io.netty.util.AttributeKey<V> key)
AttributeKey or null if there's no value set by
RequestContext.setAttr(AttributeKey, Object) or RequestContext.setAttrIfAbsent(AttributeKey, Object).
If the value does not exist in this context but only in root(),
this method will return the value from the root().
ClientRequestContext ctx = ...;
assert ctx.root().attr(KEY).equals("root");
assert ctx.attr(KEY).equals("root");
assert ctx.ownAttr(KEY) == null;
If the value exists both in this context and 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");
attr in interface RequestContextownAttr(AttributeKey)<V> V ownAttr(io.netty.util.AttributeKey<V> key)
AttributeKey or null if there's no value set by
RequestContext.setAttr(AttributeKey, Object) or RequestContext.setAttrIfAbsent(AttributeKey, Object).
Unlike attr(AttributeKey), this does not search in root().attr(AttributeKey)Iterator<Map.Entry<io.netty.util.AttributeKey<?>,Object>> attrs()
Iterator of all Map.Entrys this context contains.
The Iterator returned by this method will also yield the Map.Entrys from the
root() except those whose AttributeKey exist already in this context, e.g.
ClientRequestContext 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;
Please note that any changes made to the Map.Entry returned by Iterator.next() never
affects the Map.Entry owned by root(). For example:
ClientRequestContext 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");
If you want to change the value from the root while iterating, please call
attrs() from 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");
attrs in interface RequestContextownAttrs()Iterator<Map.Entry<io.netty.util.AttributeKey<?>,Object>> ownAttrs()
Iterator of all Map.Entrys this context contains.
Unlike attrs(), this does not iterate root().attrs()HttpRequest request()
HttpRequest associated with this context, or null if there's no
HttpRequest associated with this context yet. For example, when you send an RPC request, this method will return null until
the RPC request is translated into an HTTP request.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 you are not sending an RPC request
but just a plain HTTP request.rpcRequest in interface RequestContextdefault 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 itClientRequestContext as this - reentranceServiceRequestContext which is the same as root()ClientRequestContext whose root()
is the same root()ClientRequestContext whose root() is null
and this root() is nullIllegalStateException.push in interface RequestContextdefault ClientRequestContext newDerivedContext(RequestId id, HttpRequest req, RpcRequest rpcReq)
ClientRequestContext whose properties and Attributes are copied from this
ClientRequestContext, except having a different Request and its own RequestLog.newDerivedContext in interface RequestContextClientRequestContext newDerivedContext(RequestId id, HttpRequest req, RpcRequest rpcReq, Endpoint endpoint)
ClientRequestContext whose properties and Attributes are copied from this
ClientRequestContext, except having different Request, Endpoint and its own
RequestLog.EndpointGroup endpointGroup()
EndpointGroup used for the current Request.EndpointGroup if a user specified an EndpointGroup when initiating
a Request. null if a user specified an Endpoint.Endpoint endpoint()
ClientOptions options()
ClientOptions of the current Request.String fragment()
Request, as defined in
the section 3.5 of RFC3986.null if no fragment was specifiedlong writeTimeoutMillis()
Request
succeeds. This value is initially set from ClientOption.WRITE_TIMEOUT_MILLIS.void setWriteTimeoutMillis(long writeTimeoutMillis)
Request
succeeds. This value is initially set from ClientOption.WRITE_TIMEOUT_MILLIS.void setWriteTimeout(Duration writeTimeout)
Request
succeeds. This value is initially set from ClientOption.WRITE_TIMEOUT_MILLIS.long responseTimeoutMillis()
Response completely
since the transfer of the Response started. This value is initially set from
ClientOption.RESPONSE_TIMEOUT_MILLIS.void clearResponseTimeout()
@Deprecated void setResponseTimeoutMillis(long responseTimeoutMillis)
extendResponseTimeoutMillis(long), setResponseTimeoutAfterMillis(long),
setResponseTimeoutAtMillis(long) or clearResponseTimeout()Response is not
fully received within the specified amount of time since the Response started
or Request was fully sent.
This value is initially set from ClientOption.RESPONSE_TIMEOUT_MILLIS.
For example:
ClientRequestContext ctx = ...;
ctx.setResponseTimeoutMillis(1000);
assert ctx.responseTimeoutMillis() == 1000;
ctx.setResponseTimeoutMillis(2000);
assert ctx.responseTimeoutMillis() == 2000;
responseTimeoutMillis - the amount of time allowed in milliseconds from
the beginning of the response@Deprecated void setResponseTimeout(Duration responseTimeout)
extendResponseTimeout(Duration), setResponseTimeoutAfter(Duration),
setResponseTimeoutAt(Instant) or clearResponseTimeout()Response is not
fully received within the specified amount of time since the Response started
or Request was fully sent.
This value is initially set from ClientOption.RESPONSE_TIMEOUT_MILLIS.
For example:
ClientRequestContext ctx = ...;
ctx.setResponseTimeout(Duration.ofSeconds(1));
assert ctx.responseTimeoutMillis() == 1000;
ctx.setResponseTimeout(Duration.ofSeconds(2));
assert ctx.responseTimeoutMillis() == 2000;
responseTimeout - the amount of time allowed from the beginning of the responsevoid extendResponseTimeoutMillis(long adjustmentMillis)
adjustmentMillis.
This method does nothing if no response timeout was scheduled previously.
Note that a negative adjustmentMillis reduces the current timeout.
The initial timeout is set from ClientOption.RESPONSE_TIMEOUT_MILLIS.
For example:
ClientRequestContext ctx = ...;
long oldResponseTimeoutMillis = ctx.responseTimeoutMillis();
ctx.extendResponseTimeoutMillis(1000);
assert ctx.responseTimeoutMillis() == oldResponseTimeoutMillis + 1000;
ctx.extendResponseTimeoutMillis(-500);
assert ctx.responseTimeoutMillis() == oldResponseTimeoutMillis + 500;
adjustmentMillis - the amount of time in milliseconds to extend the current timeout byvoid extendResponseTimeout(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 ClientOption.RESPONSE_TIMEOUT_MILLIS.
For example:
ClientRequestContext ctx = ...;
long oldResponseTimeoutMillis = ctx.responseTimeoutMillis();
ctx.extendResponseTimeout(Duration.ofSeconds(1));
assert ctx.responseTimeoutMillis() == oldResponseTimeoutMillis + 1000;
ctx.extendResponseTimeout(Duration.ofMillis(-500));
assert ctx.responseTimeoutMillis() == oldResponseTimeoutMillis + 500;
adjustment - the amount of time to extend the current timeout byvoid setResponseTimeoutAfterMillis(long responseTimeoutMillis)
Response is not
fully received within the specified amount of time from now.
Note that the specified responseTimeoutMillis must be positive.
The initial timeout is set from ClientOption.RESPONSE_TIMEOUT_MILLIS.
For example:
ClientRequestContext ctx = ...;
// Schedules timeout after 1 seconds from now.
ctx.setResponseTimeoutAfterMillis(1000);
responseTimeoutMillis - the amount of time allowed in milliseconds from nowvoid setResponseTimeoutAfter(Duration responseTimeout)
Response is not
fully received within the specified amount of time from now.
Note that the specified responseTimeout must be positive.
The initial timeout is set from ClientOption.RESPONSE_TIMEOUT_MILLIS.
For example:
ClientRequestContext ctx = ...;
// Schedules timeout after 1 seconds from now.
ctx.setResponseTimeoutAfter(Duration.ofSeconds(1));
responseTimeout - the amount of time allowed from nowvoid setResponseTimeoutAtMillis(long responseTimeoutAtMillis)
1970-01-01T00:00:00Z).
Note that the response will be timed out immediately if the specified time is before now.
The initial timeout is set from ClientOption.RESPONSE_TIMEOUT_MILLIS.
For example:
ClientRequestContext ctx = ...;
// Schedules timeout after 1 seconds from now.
long responseTimeoutAt = Instant.now().plus(1, ChronoUnit.SECONDS).toEpochMilli();
ctx.setResponseTimeoutAtMillis(responseTimeoutAt);
responseTimeoutAtMillis - the response timeout represented as the number of milliseconds
since the epoch (1970-01-01T00:00:00Z)void setResponseTimeoutAt(Instant responseTimeoutAt)
1970-01-01T00:00:00Z).
Note that the response will be timed out immediately if the specified time is before now.
The initial timeout is set from ClientOption.RESPONSE_TIMEOUT_MILLIS.
For example:
ClientRequestContext ctx = ...;
// Schedules timeout after 1 seconds from now.
ctx.setResponseTimeoutAt(Instant.now().plus(1, ChronoUnit.SECONDS));
responseTimeoutAt - the response timeout represented as the number of milliseconds
since the epoch (1970-01-01T00:00:00Z)Runnable responseTimeoutHandler()
Response timeout handler which is executed when
the Response is not completely received within the allowed responseTimeoutMillis()
or the default ClientOption.RESPONSE_TIMEOUT_MILLIS.void setResponseTimeoutHandler(Runnable responseTimeoutHandler)
responseTimeoutHandler must abort
the response, e.g., by calling StreamMessage.abort(Throwable).
If not set, the response will be closed with ResponseTimeoutException.
For example,
HttpResponseWriter res = HttpResponse.streaming();
ctx.setResponseTimeoutHandler(() -> {
res.abort(new IllegalStateException("Server is in a bad state."));
});
...
long maxResponseLength()
Response.
This value is initially set from ClientOption.MAX_RESPONSE_LENGTH.0 if unlimited.ContentTooLargeExceptionvoid setMaxResponseLength(long maxResponseLength)
Response.
This value is initially set from ClientOption.MAX_RESPONSE_LENGTH.
Specify 0 to disable the limit of the length of a response.ContentTooLargeExceptionHttpHeaders additionalRequestHeaders()
void setAdditionalRequestHeader(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 Client sends an HttpRequest.void setAdditionalRequestHeaders(Iterable<? extends Map.Entry<? extends CharSequence,?>> headers)
HttpHeaders which is included when a
Client sends an HttpRequest.void addAdditionalRequestHeader(CharSequence name, Object value)
name and value. The header will be included when
a Client sends an HttpRequest.void addAdditionalRequestHeaders(Iterable<? extends Map.Entry<? extends CharSequence,?>> headers)
boolean removeAdditionalRequestHeader(CharSequence name)
name.true if at least one entry has been removedCopyright © 2020 LeanCloud. All rights reserved.