public final class ClientBuilder extends Object
URI using the builder pattern. Use the factory
methods in Clients if you do not have many options to override. If you are creating an
WebClient, it is recommended to use the WebClientBuilder or
factory methods in WebClient.
Unlike other options, when a user calls option(ClientOption, Object) or options() with
a ClientOption.DECORATION or a ClientOption.HTTP_HEADERS, this builder will not simply
replace the old option but merge the specified option into the previous option value. For example:
ClientOptionsBuilder b = new ClientOptionsBuilder();
b.option(ClientOption.HTTP_HEADERS, headersA);
b.option(ClientOption.HTTP_HEADERS, headersB);
b.option(ClientOption.DECORATION, decorationA);
b.option(ClientOption.DECORATION, decorationB);
ClientOptions opts = b.build();
HttpHeaders httpHeaders = opts.httpHeaders();
ClientDecoration decorations = opts.decoration();
httpHeaders will contain all HTTP headers of headersA and headersB.
If headersA and headersB have the headers with the same name, the duplicate header in
headerB will replace the one with the same name in headerA.
Similarly, decorations will contain all decorators of decorationA and decorationB,
but there will be no replacement but only addition.| Constructor and Description |
|---|
ClientBuilder(Scheme scheme,
Endpoint endpoint)
Creates a new
ClientBuilder that builds the client that connects to the specified
Endpoint with the Scheme. |
ClientBuilder(SessionProtocol protocol,
Endpoint endpoint)
Creates a new
ClientBuilder that builds the client that connects to the specified
Endpoint with the SessionProtocol. |
ClientBuilder(String uri)
Creates a new
ClientBuilder that builds the client that connects to the specified uri. |
ClientBuilder(String scheme,
Endpoint endpoint)
Creates a new
ClientBuilder that builds the client that connects to the specified
Endpoint with the scheme. |
ClientBuilder(URI uri)
Creates a new
ClientBuilder that builds the client that connects to the specified URI. |
| Modifier and Type | Method and Description |
|---|---|
B |
addHttpHeader(CharSequence name,
Object value)
Adds the specified HTTP header.
|
B |
addHttpHeaders(Iterable<? extends Map.Entry<? extends CharSequence,?>> httpHeaders)
Adds the specified HTTP headers.
|
<T> T |
build(Class<T> clientType)
Returns a newly-created client which implements the specified
clientType, based on the
properties of this builder. |
B |
contentPreview(int length)
Sets the
ContentPreviewerFactory for creating a ContentPreviewer which produces the
preview with the maximum length limit for a request and a response. |
B |
contentPreview(int length,
Charset defaultCharset)
Sets the
ContentPreviewerFactory for creating a ContentPreviewer which produces the
preview with the maximum length limit for a request and a response. |
B |
contentPreviewerFactory(ContentPreviewerFactory factory)
Sets the
ContentPreviewerFactory for a request and a response. |
B |
decorator(DecoratingHttpClientFunction decorator)
Adds the specified HTTP-level
decorator. |
B |
decorator(Function<? super HttpClient,? extends HttpClient> decorator)
Adds the specified HTTP-level
decorator. |
B |
defaultMaxResponseLength(long maxResponseLength)
Deprecated.
|
B |
defaultResponseTimeout(Duration responseTimeout)
Deprecated.
|
B |
defaultResponseTimeoutMillis(long responseTimeoutMillis)
Deprecated.
|
B |
defaultWriteTimeout(Duration writeTimeout)
Deprecated.
|
B |
defaultWriteTimeoutMillis(long writeTimeoutMillis)
Deprecated.
|
ClientBuilder |
factory(ClientFactory factory)
Sets the
ClientFactory of the client. |
B |
maxResponseLength(long maxResponseLength)
Sets the maximum allowed length of a server response in bytes.
|
<T> B |
option(ClientOption<T> option,
T value)
Adds the specified
ClientOption and its value. |
<T> B |
option(ClientOptionValue<T> optionValue)
Adds the specified
ClientOptionValue. |
B |
options(ClientOptions options)
Adds the specified
ClientOptions. |
B |
options(ClientOptionValue<?>... options)
Adds the specified
ClientOptionValues. |
B |
options(Iterable<ClientOptionValue<?>> options)
Adds the specified
ClientOptionValues. |
ClientBuilder |
path(String path)
Sets the
path of the client. |
B |
requestContentPreviewerFactory(ContentPreviewerFactory factory)
Sets the
ContentPreviewerFactory for a request. |
B |
requestIdGenerator(Supplier<RequestId> requestIdGenerator)
|
B |
responseContentPreviewerFactory(ContentPreviewerFactory factory)
Sets the
ContentPreviewerFactory for a response. |
B |
responseTimeout(Duration responseTimeout)
Sets the timeout of a response.
|
B |
responseTimeoutMillis(long responseTimeoutMillis)
Sets the timeout of a response in milliseconds.
|
B |
rpcDecorator(DecoratingRpcClientFunction decorator)
Adds the specified RPC-level
decorator. |
B |
rpcDecorator(Function<? super RpcClient,? extends RpcClient> decorator)
Adds the specified RPC-level
decorator. |
ClientBuilder |
serializationFormat(SerializationFormat format)
Sets the
SerializationFormat of the client. |
B |
setHttpHeader(CharSequence name,
Object value)
Sets the specified HTTP header.
|
B |
setHttpHeaders(Iterable<? extends Map.Entry<? extends CharSequence,?>> httpHeaders)
Sets the specified HTTP headers.
|
B |
writeTimeout(Duration writeTimeout)
Sets the timeout of a socket write attempt.
|
B |
writeTimeoutMillis(long writeTimeoutMillis)
Sets the timeout of a socket write attempt in milliseconds.
|
public ClientBuilder(String uri)
ClientBuilder that builds the client that connects to the specified uri.public ClientBuilder(URI uri)
ClientBuilder that builds the client that connects to the specified URI.public ClientBuilder(String scheme, Endpoint endpoint)
ClientBuilder that builds the client that connects to the specified
Endpoint with the scheme.public ClientBuilder(Scheme scheme, Endpoint endpoint)
ClientBuilder that builds the client that connects to the specified
Endpoint with the Scheme.public ClientBuilder(SessionProtocol protocol, Endpoint endpoint)
ClientBuilder that builds the client that connects to the specified
Endpoint with the SessionProtocol.public ClientBuilder factory(ClientFactory factory)
ClientFactory of the client. The default is ClientFactory.ofDefault().public ClientBuilder path(String path)
path of the client.public ClientBuilder serializationFormat(SerializationFormat format)
SerializationFormat of the client. The default is SerializationFormat.NONE.public <T> T build(Class<T> clientType)
clientType, based on the
properties of this builder.IllegalArgumentException - if the scheme of the uri specified in
ClientBuilder(String) or the specified clientType is
unsupported for the schemepublic B options(ClientOptions options)
ClientOptions.public B options(ClientOptionValue<?>... options)
ClientOptionValues.public B options(Iterable<ClientOptionValue<?>> options)
ClientOptionValues.public <T> B option(ClientOption<T> option, T value)
ClientOption and its value.public <T> B option(ClientOptionValue<T> optionValue)
ClientOptionValue.@Deprecated public B defaultWriteTimeout(Duration writeTimeout)
writeTimeout(Duration).writeTimeout - the timeout. 0 disables the timeout.@Deprecated public B defaultWriteTimeoutMillis(long writeTimeoutMillis)
writeTimeoutMillis(long).writeTimeoutMillis - the timeout in milliseconds. 0 disables the timeout.public B writeTimeout(Duration writeTimeout)
writeTimeout - the timeout. 0 disables the timeout.public B writeTimeoutMillis(long writeTimeoutMillis)
writeTimeoutMillis - the timeout in milliseconds. 0 disables the timeout.@Deprecated public B defaultResponseTimeout(Duration responseTimeout)
responseTimeout(Duration).responseTimeout - the timeout. 0 disables the timeout.@Deprecated public B defaultResponseTimeoutMillis(long responseTimeoutMillis)
responseTimeoutMillis(long).responseTimeoutMillis - the timeout in milliseconds. 0 disables the timeout.public B responseTimeout(Duration responseTimeout)
responseTimeout - the timeout. 0 disables the timeout.public B responseTimeoutMillis(long responseTimeoutMillis)
responseTimeoutMillis - the timeout in milliseconds. 0 disables the timeout.@Deprecated public B defaultMaxResponseLength(long maxResponseLength)
maxResponseLength(long).maxResponseLength - the maximum length in bytes. 0 disables the limit.public B maxResponseLength(long maxResponseLength)
maxResponseLength - the maximum length in bytes. 0 disables the limit.public B requestContentPreviewerFactory(ContentPreviewerFactory factory)
ContentPreviewerFactory for a request.public B responseContentPreviewerFactory(ContentPreviewerFactory factory)
ContentPreviewerFactory for a response.public B contentPreviewerFactory(ContentPreviewerFactory factory)
ContentPreviewerFactory for a request and a response.public B contentPreview(int length,
Charset defaultCharset)
ContentPreviewerFactory for creating a ContentPreviewer which produces the
preview with the maximum length limit for a request and a response.
The previewer is enabled only if the content type of a request/response meets
any of the following conditions:
text/* or application/x-www-form-urlencoded"xml" or "json""+xml" or "+json"length - the maximum length of the previewdefaultCharset - the default charset used when a charset is not specified in the
"content-type" headerpublic B contentPreview(int length)
ContentPreviewerFactory for creating a ContentPreviewer which produces the
preview with the maximum length limit for a request and a response.
The previewer is enabled only if the content type of a request/response meets
any of the following conditions:
text/* or application/x-www-form-urlencoded"xml" or "json""+xml" or "+json"length - the maximum length of the preview.public B decorator(Function<? super HttpClient,? extends HttpClient> decorator)
decorator.decorator - the Function that transforms an HttpClient to anotherpublic B decorator(DecoratingHttpClientFunction decorator)
decorator.decorator - the DecoratingHttpClientFunction that intercepts an invocationpublic B rpcDecorator(Function<? super RpcClient,? extends RpcClient> decorator)
decorator.public B rpcDecorator(DecoratingRpcClientFunction decorator)
decorator.decorator - the DecoratingRpcClientFunction that intercepts an invocationpublic B addHttpHeader(CharSequence name, Object value)
public B addHttpHeaders(Iterable<? extends Map.Entry<? extends CharSequence,?>> httpHeaders)
public B setHttpHeader(CharSequence name, Object value)
public B setHttpHeaders(Iterable<? extends Map.Entry<? extends CharSequence,?>> httpHeaders)
Copyright © 2020 LeanCloud. All rights reserved.