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
HttpClient
, it is recommended to use the HttpClientBuilder
or
factory methods in HttpClient
.
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(String uri)
Creates a new
ClientBuilder that builds the client that connects to the specified uri . |
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(AsciiString name,
Object value)
Adds the specified HTTP header.
|
B |
addHttpHeaders(Headers<AsciiString,String,?> 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. |
<I extends Request,O extends Response> |
decorator(Class<I> requestType,
Class<O> responseType,
DecoratingClientFunction<I,O> decorator)
Adds the specified
decorator . |
<T extends Client<I,O>,R extends Client<I,O>,I extends Request,O extends Response> |
decorator(Class<I> requestType,
Class<O> responseType,
Function<T,R> decorator)
Adds the specified
decorator . |
B |
defaultMaxResponseLength(long defaultMaxResponseLength)
Sets the default maximum allowed length of a server response in bytes.
|
B |
defaultResponseTimeout(Duration defaultResponseTimeout)
Sets the default timeout of a response.
|
B |
defaultResponseTimeoutMillis(long defaultResponseTimeoutMillis)
Sets the default timeout of a response in milliseconds.
|
B |
defaultWriteTimeout(Duration defaultWriteTimeout)
Sets the default timeout of a socket write attempt.
|
B |
defaultWriteTimeoutMillis(long defaultWriteTimeoutMillis)
Sets the default timeout of a socket write attempt in milliseconds.
|
ClientBuilder |
factory(ClientFactory factory)
Sets the
ClientFactory of the client. |
<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
ClientOptionValue s. |
B |
options(Iterable<ClientOptionValue<?>> options)
Adds the specified
ClientOptionValue s. |
B |
setHttpHeader(AsciiString name,
Object value)
Sets the specified HTTP header.
|
B |
setHttpHeaders(Headers<AsciiString,String,?> httpHeaders)
Sets the specified HTTP headers.
|
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 factory(ClientFactory factory)
ClientFactory
of the client. The default is ClientFactory.DEFAULT
.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)
ClientOptionValue
s.public B options(Iterable<ClientOptionValue<?>> options)
ClientOptionValue
s.public <T> B option(ClientOption<T> option, T value)
ClientOption
and its value
.public <T> B option(ClientOptionValue<T> optionValue)
ClientOptionValue
.public B defaultWriteTimeoutMillis(long defaultWriteTimeoutMillis)
defaultWriteTimeoutMillis
- the timeout in milliseconds. 0
disables the timeout.public B defaultWriteTimeout(Duration defaultWriteTimeout)
defaultWriteTimeout
- the timeout. 0
disables the timeout.public B defaultResponseTimeoutMillis(long defaultResponseTimeoutMillis)
defaultResponseTimeoutMillis
- the timeout in milliseconds. 0
disables the timeout.public B defaultResponseTimeout(Duration defaultResponseTimeout)
defaultResponseTimeout
- the timeout. 0
disables the timeout.public B defaultMaxResponseLength(long defaultMaxResponseLength)
defaultMaxResponseLength
- the maximum length in bytes. 0
disables the limit.public <T extends Client<I,O>,R extends Client<I,O>,I extends Request,O extends Response> B decorator(Class<I> requestType, Class<O> responseType, Function<T,R> decorator)
decorator
.T
- the type of the Client
being decoratedR
- the type of the Client
produced by the decorator
I
- the Request
type of the Client
being decoratedO
- the Response
type of the Client
being decoratedrequestType
- the type of the Request
that the decorator
is interested inresponseType
- the type of the Response
that the decorator
is interested indecorator
- the Function
that transforms a Client
to anotherpublic <I extends Request,O extends Response> B decorator(Class<I> requestType, Class<O> responseType, DecoratingClientFunction<I,O> decorator)
decorator
.I
- the Request
type of the Client
being decoratedO
- the Response
type of the Client
being decoratedrequestType
- the type of the Request
that the decorator
is interested inresponseType
- the type of the Response
that the decorator
is interested indecorator
- the DecoratingClientFunction
that intercepts an invocationpublic B addHttpHeader(AsciiString name, Object value)
public B addHttpHeaders(Headers<AsciiString,String,?> httpHeaders)
public B setHttpHeader(AsciiString name, Object value)
public B setHttpHeaders(Headers<AsciiString,String,?> httpHeaders)
© Copyright 2015–2017 LINE Corporation. All rights reserved.