Package com.linecorp.armeria.client
Class ClientBuilder
- java.lang.Object
-
- com.linecorp.armeria.client.ClientBuilder
-
public final class ClientBuilder extends Object
Creates a new client that connects to the specifiedURI
using the builder pattern. Use the factory methods inClients
if you do not have many options to override. If you are creating anHttpClient
, it is recommended to use theHttpClientBuilder
or factory methods inHttpClient
.How are decorators and HTTP headers configured?
Unlike other options, when a user calls
option(ClientOption, Object)
oroptions()
with aClientOption.DECORATION
or aClientOption.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 ofheadersA
andheadersB
. IfheadersA
andheadersB
have the headers with the same name, the duplicate header inheaderB
will replace the one with the same name inheaderA
. Similarly,decorations
will contain all decorators ofdecorationA
anddecorationB
, but there will be no replacement but only addition.
-
-
Constructor Summary
Constructors Constructor Description ClientBuilder(Scheme scheme, Endpoint endpoint)
Creates a newClientBuilder
that builds the client that connects to the specifiedEndpoint
with theScheme
.ClientBuilder(SessionProtocol protocol, Endpoint endpoint)
Creates a newClientBuilder
that builds the client that connects to the specifiedEndpoint
with theSessionProtocol
.ClientBuilder(String uri)
Creates a newClientBuilder
that builds the client that connects to the specifieduri
.ClientBuilder(String scheme, Endpoint endpoint)
Creates a newClientBuilder
that builds the client that connects to the specifiedEndpoint
with thescheme
.ClientBuilder(URI uri)
Creates a newClientBuilder
that builds the client that connects to the specifiedURI
.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method 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 specifiedclientType
, based on the properties of this builder.B
contentPreview(int length)
Sets theContentPreviewerFactory
for creating aContentPreviewer
which produces the preview with the maximumlength
limit for a request and a response.B
contentPreview(int length, Charset defaultCharset)
Sets theContentPreviewerFactory
for creating aContentPreviewer
which produces the preview with the maximumlength
limit for a request and a response.B
contentPreviewerFactory(ContentPreviewerFactory factory)
Sets theContentPreviewerFactory
for a request and a response.<I extends HttpRequest,O extends HttpResponse>
Bdecorator(DecoratingClientFunction<I,O> decorator)
Adds the specified HTTP-leveldecorator
.<I extends Request,O extends Response>
Bdecorator(Class<I> requestType, Class<O> responseType, DecoratingClientFunction<I,O> decorator)
Deprecated.<T extends Client<I,O>,R extends Client<I,O>,I extends Request,O extends Response>
Bdecorator(Class<I> requestType, Class<O> responseType, Function<T,R> decorator)
Deprecated.<T extends Client<I,O>,R extends Client<I,O>,I extends HttpRequest,O extends HttpResponse>
Bdecorator(Function<T,R> decorator)
Adds the specified HTTP-leveldecorator
.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 theClientFactory
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 specifiedClientOption
and itsvalue
.<T> B
option(ClientOptionValue<T> optionValue)
Adds the specifiedClientOptionValue
.B
options(ClientOptions options)
Adds the specifiedClientOptions
.B
options(ClientOptionValue<?>... options)
Adds the specifiedClientOptionValue
s.B
options(Iterable<ClientOptionValue<?>> options)
Adds the specifiedClientOptionValue
s.ClientBuilder
path(String path)
Sets thepath
of the client.B
requestContentPreviewerFactory(ContentPreviewerFactory factory)
Sets theContentPreviewerFactory
for a request.B
responseContentPreviewerFactory(ContentPreviewerFactory factory)
Sets theContentPreviewerFactory
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.<I extends RpcRequest,O extends RpcResponse>
BrpcDecorator(DecoratingClientFunction<I,O> decorator)
Adds the specified RPC-leveldecorator
.<T extends Client<I,O>,R extends Client<I,O>,I extends RpcRequest,O extends RpcResponse>
BrpcDecorator(Function<T,R> decorator)
Adds the specified RPC-leveldecorator
.ClientBuilder
serializationFormat(SerializationFormat format)
Sets theSerializationFormat
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.
-
-
-
Constructor Detail
-
ClientBuilder
public ClientBuilder(String uri)
Creates a newClientBuilder
that builds the client that connects to the specifieduri
.
-
ClientBuilder
public ClientBuilder(URI uri)
Creates a newClientBuilder
that builds the client that connects to the specifiedURI
.
-
ClientBuilder
public ClientBuilder(String scheme, Endpoint endpoint)
Creates a newClientBuilder
that builds the client that connects to the specifiedEndpoint
with thescheme
.
-
ClientBuilder
public ClientBuilder(Scheme scheme, Endpoint endpoint)
Creates a newClientBuilder
that builds the client that connects to the specifiedEndpoint
with theScheme
.
-
ClientBuilder
public ClientBuilder(SessionProtocol protocol, Endpoint endpoint)
Creates a newClientBuilder
that builds the client that connects to the specifiedEndpoint
with theSessionProtocol
.
-
-
Method Detail
-
factory
public ClientBuilder factory(ClientFactory factory)
Sets theClientFactory
of the client. The default isClientFactory.DEFAULT
.
-
path
public ClientBuilder path(String path)
Sets thepath
of the client.
-
serializationFormat
public ClientBuilder serializationFormat(SerializationFormat format)
Sets theSerializationFormat
of the client. The default isSerializationFormat.NONE
.
-
build
public <T> T build(Class<T> clientType)
Returns a newly-created client which implements the specifiedclientType
, based on the properties of this builder.- Throws:
IllegalArgumentException
- if the scheme of theuri
specified inClientBuilder(String)
or the specifiedclientType
is unsupported for the scheme
-
options
public B options(ClientOptions options)
Adds the specifiedClientOptions
.
-
options
public B options(ClientOptionValue<?>... options)
Adds the specifiedClientOptionValue
s.
-
options
public B options(Iterable<ClientOptionValue<?>> options)
Adds the specifiedClientOptionValue
s.
-
option
public <T> B option(ClientOption<T> option, T value)
Adds the specifiedClientOption
and itsvalue
.
-
option
public <T> B option(ClientOptionValue<T> optionValue)
Adds the specifiedClientOptionValue
.
-
defaultWriteTimeout
@Deprecated public B defaultWriteTimeout(Duration writeTimeout)
Deprecated.Sets the timeout of a socket write attempt.- Parameters:
writeTimeout
- the timeout.0
disables the timeout.
-
defaultWriteTimeoutMillis
@Deprecated public B defaultWriteTimeoutMillis(long writeTimeoutMillis)
Deprecated.Sets the timeout of a socket write attempt in milliseconds.- Parameters:
writeTimeoutMillis
- the timeout in milliseconds.0
disables the timeout.
-
writeTimeout
public B writeTimeout(Duration writeTimeout)
Sets the timeout of a socket write attempt.- Parameters:
writeTimeout
- the timeout.0
disables the timeout.
-
writeTimeoutMillis
public B writeTimeoutMillis(long writeTimeoutMillis)
Sets the timeout of a socket write attempt in milliseconds.- Parameters:
writeTimeoutMillis
- the timeout in milliseconds.0
disables the timeout.
-
defaultResponseTimeout
@Deprecated public B defaultResponseTimeout(Duration responseTimeout)
Deprecated.Sets the timeout of a response.- Parameters:
responseTimeout
- the timeout.0
disables the timeout.
-
defaultResponseTimeoutMillis
@Deprecated public B defaultResponseTimeoutMillis(long responseTimeoutMillis)
Deprecated.Sets the timeout of a response in milliseconds.- Parameters:
responseTimeoutMillis
- the timeout in milliseconds.0
disables the timeout.
-
responseTimeout
public B responseTimeout(Duration responseTimeout)
Sets the timeout of a response.- Parameters:
responseTimeout
- the timeout.0
disables the timeout.
-
responseTimeoutMillis
public B responseTimeoutMillis(long responseTimeoutMillis)
Sets the timeout of a response in milliseconds.- Parameters:
responseTimeoutMillis
- the timeout in milliseconds.0
disables the timeout.
-
defaultMaxResponseLength
@Deprecated public B defaultMaxResponseLength(long maxResponseLength)
Deprecated.Sets the maximum allowed length of a server response in bytes.- Parameters:
maxResponseLength
- the maximum length in bytes.0
disables the limit.
-
maxResponseLength
public B maxResponseLength(long maxResponseLength)
Sets the maximum allowed length of a server response in bytes.- Parameters:
maxResponseLength
- the maximum length in bytes.0
disables the limit.
-
requestContentPreviewerFactory
public B requestContentPreviewerFactory(ContentPreviewerFactory factory)
Sets theContentPreviewerFactory
for a request.
-
responseContentPreviewerFactory
public B responseContentPreviewerFactory(ContentPreviewerFactory factory)
Sets theContentPreviewerFactory
for a response.
-
contentPreviewerFactory
public B contentPreviewerFactory(ContentPreviewerFactory factory)
Sets theContentPreviewerFactory
for a request and a response.
-
contentPreview
public B contentPreview(int length, Charset defaultCharset)
Sets theContentPreviewerFactory
for creating aContentPreviewer
which produces the preview with the maximumlength
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:- when it matches
text/*
orapplication/x-www-form-urlencoded
- when its charset has been specified
- when its subtype is
"xml"
or"json"
- when its subtype ends with
"+xml"
or"+json"
- Parameters:
length
- the maximum length of the previewdefaultCharset
- the default charset used when a charset is not specified in the"content-type"
header
- when it matches
-
contentPreview
public B contentPreview(int length)
Sets theContentPreviewerFactory
for creating aContentPreviewer
which produces the preview with the maximumlength
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:- when it matches
text/*
orapplication/x-www-form-urlencoded
- when its charset has been specified
- when its subtype is
"xml"
or"json"
- when its subtype ends with
"+xml"
or"+json"
- Parameters:
length
- the maximum length of the preview.
- when it matches
-
decorator
@Deprecated 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)
Deprecated.Adds the specifieddecorator
.- Type Parameters:
T
- the type of theClient
being decoratedR
- the type of theClient
produced by thedecorator
I
- theRequest
type of theClient
being decoratedO
- theResponse
type of theClient
being decorated- Parameters:
requestType
- the type of theRequest
that thedecorator
is interested inresponseType
- the type of theResponse
that thedecorator
is interested indecorator
- theFunction
that transforms aClient
to another
-
decorator
@Deprecated public <I extends Request,O extends Response> B decorator(Class<I> requestType, Class<O> responseType, DecoratingClientFunction<I,O> decorator)
Deprecated.Adds the specifieddecorator
.- Type Parameters:
I
- theRequest
type of theClient
being decoratedO
- theResponse
type of theClient
being decorated- Parameters:
requestType
- the type of theRequest
that thedecorator
is interested inresponseType
- the type of theResponse
that thedecorator
is interested indecorator
- theDecoratingClientFunction
that intercepts an invocation
-
decorator
public <T extends Client<I,O>,R extends Client<I,O>,I extends HttpRequest,O extends HttpResponse> B decorator(Function<T,R> decorator)
Adds the specified HTTP-leveldecorator
.
-
decorator
public <I extends HttpRequest,O extends HttpResponse> B decorator(DecoratingClientFunction<I,O> decorator)
Adds the specified HTTP-leveldecorator
.- Type Parameters:
I
- theRequest
type of theClient
being decoratedO
- theResponse
type of theClient
being decorated- Parameters:
decorator
- theDecoratingClientFunction
that intercepts an invocation
-
rpcDecorator
public <T extends Client<I,O>,R extends Client<I,O>,I extends RpcRequest,O extends RpcResponse> B rpcDecorator(Function<T,R> decorator)
Adds the specified RPC-leveldecorator
.
-
rpcDecorator
public <I extends RpcRequest,O extends RpcResponse> B rpcDecorator(DecoratingClientFunction<I,O> decorator)
Adds the specified RPC-leveldecorator
.- Type Parameters:
I
- theRequest
type of theClient
being decoratedO
- theResponse
type of theClient
being decorated- Parameters:
decorator
- theDecoratingClientFunction
that intercepts an invocation
-
addHttpHeader
public B addHttpHeader(CharSequence name, Object value)
Adds the specified HTTP header.
-
addHttpHeaders
public B addHttpHeaders(Iterable<? extends Map.Entry<? extends CharSequence,?>> httpHeaders)
Adds the specified HTTP headers.
-
setHttpHeader
public B setHttpHeader(CharSequence name, Object value)
Sets the specified HTTP header.
-
setHttpHeaders
public B setHttpHeaders(Iterable<? extends Map.Entry<? extends CharSequence,?>> httpHeaders)
Sets the specified HTTP headers.
-
-