public class ClientBuilder
extends java.lang.Object
CloudantClient
instances.
CloudantClient client = ClientBuilder.account("yourCloudantAccount")
.username("yourUsername")
.password("yourPassword")
.build();
CloudantClient client = ClientBuilder.bluemix(System.getenv("VCAP_SERVICES")).build();
CloudantClient client = ClientBuilder.url(new URL("https://yourCloudantLocalAddress.example"))
.username("yourUsername")
.password("yourPassword")
.build();
CloudantClient client = ClientBuilder.account("yourCloudantAccount")
.username("yourUsername")
.password("yourPassword")
.proxyURL(new URL("https://yourProxyServerAddress.example"))
.proxyUser(yourProxyUser)
.proxyPassword(yourProxyPass)
.build();
CloudantClient client = ClientBuilder.account("yourCloudantAccount")
.username("yourUsername")
.password("yourPassword")
.customSSLSocketFactory(...)
.build();
CloudantClient client = ClientBuilder.account("yourCloudantAccount")
.username("yourUsername")
.password("yourPassword")
.connectTimeout(1, TimeUnit.MINUTES)
.readTimeout(1, TimeUnit.MINUTES)
.build();
Modifier and Type | Field and Description |
---|---|
static long |
DEFAULT_CONNECTION_TIMEOUT
Connection timeout defaults to 5 minutes
|
static int |
DEFAULT_MAX_CONNECTIONS
Default max of 6 connections
|
static long |
DEFAULT_READ_TIMEOUT
Read timeout defaults to 5 minutes
|
Modifier and Type | Method and Description |
---|---|
static ClientBuilder |
account(java.lang.String account)
Constructs a new ClientBuilder for building a CloudantClient instance to connect to the
Cloudant server with the specified account.
|
static ClientBuilder |
bluemix(java.lang.String vcapServices)
|
static ClientBuilder |
bluemix(java.lang.String vcapServices,
java.lang.String instanceName)
Sets Cloudant client credentials by inspecting a service information JSON string.
|
static ClientBuilder |
bluemix(java.lang.String vcapServices,
java.lang.String serviceName,
java.lang.String instanceName)
Sets Cloudant client credentials by inspecting a service information JSON string.
|
CloudantClient |
build()
Build the
CloudantClient instance based on the endpoint used to construct this
client builder and the options that have been set on it before calling this method. |
ClientBuilder |
connectTimeout(long connectTimeout,
java.util.concurrent.TimeUnit connectTimeoutUnit)
Sets the specified timeout value when opening the client connection.
|
ClientBuilder |
customSSLSocketFactory(javax.net.ssl.SSLSocketFactory factory)
Specifies the custom SSLSocketFactory to use when connecting to Cloudant over a
https URL, when SSL authentication is enabled. |
ClientBuilder |
disableSSLAuthentication()
Flag to disable hostname verification and certificate chain validation.
|
ClientBuilder |
gsonBuilder(com.google.gson.GsonBuilder gsonBuilder)
Set a custom GsonBuilder to use when serializing and de-serializing JSON in requests and
responses between the CloudantClient and the server.
|
ClientBuilder |
iamApiKey(java.lang.String iamApiKey)
Sets the
IAM API key for the client connection.
|
ClientBuilder |
iamApiKey(java.lang.String iamApiKey,
java.lang.String iamServerClientId,
java.lang.String iamServerClientSecret)
Sets the
IAM API key for the client connection.
|
ClientBuilder |
interceptors(HttpConnectionInterceptor... interceptors)
This method adds
HttpConnectionInterceptor s to be used on the CloudantClient
connection. |
ClientBuilder |
maxConnections(int maxConnections)
Set the maximum number of connections to maintain in the connection pool.
|
ClientBuilder |
password(java.lang.String password)
Sets the password for the client connection.
|
ClientBuilder |
proxyPassword(java.lang.String proxyPassword)
Sets an optional proxy password for the proxy user specified by
proxyUser(String) . |
ClientBuilder |
proxyURL(java.net.URL proxyURL)
Sets a proxy url for the client connection.
|
ClientBuilder |
proxyUser(java.lang.String proxyUser)
Sets an optional proxy username for the client connection.
|
ClientBuilder |
readTimeout(long readTimeout,
java.util.concurrent.TimeUnit readTimeoutUnit)
Sets the specified timeout value when reading from a
InputStream with an
established client connection. |
static ClientBuilder |
url(java.net.URL url)
Constructs a new ClientBuilder for building a CloudantClient instance to connect to the
Cloudant server with the specified URL.
|
ClientBuilder |
username(java.lang.String username)
Sets a username or API key for the client connection.
|
public static final int DEFAULT_MAX_CONNECTIONS
public static final long DEFAULT_CONNECTION_TIMEOUT
public static final long DEFAULT_READ_TIMEOUT
public static ClientBuilder account(java.lang.String account)
account
- the Cloudant account name to connect to e.g. "example" is the account name
for the "example.cloudant.com" endpointjava.lang.IllegalArgumentException
- if the specified account name forms an invalid endpoint URLpublic static ClientBuilder url(java.net.URL url)
url
- server URL e.g. "https://yourCloudantLocalAddress.example"public CloudantClient build()
CloudantClient
instance based on the endpoint used to construct this
client builder and the options that have been set on it before calling this method.CloudantClient
instance for the specified end point and optionspublic ClientBuilder username(java.lang.String username)
username
- the user or API key for the sessionpublic ClientBuilder password(java.lang.String password)
username(String)
method.password
- user password or API key passphrasepublic ClientBuilder gsonBuilder(com.google.gson.GsonBuilder gsonBuilder)
Note: the supplied GsonBuilder will be augmented with some internal TypeAdapters.
gsonBuilder
- the custom GsonBuilder to usepublic ClientBuilder maxConnections(int maxConnections)
Note: this setting only applies if using the optional OkHttp dependency. If OkHttp is not
present then the JVM configuration is used for pooling. Consult the JVM documentation for
the http.maxConnections
property for further details.
DEFAULT_MAX_CONNECTIONS
maxConnections
- the maximum number of simultaneous connections to open to the serverpublic ClientBuilder proxyURL(java.net.URL proxyURL)
Sets a proxy url for the client connection.
Note that this method can only set the configuration for an unencrypted HTTP proxy. Even when using this type of proxy communication from the client to a https database server is encrypted via a SSL tunnel.
proxyURL
- the URL of the proxy serverpublic ClientBuilder proxyUser(java.lang.String proxyUser)
Sets an optional proxy username for the client connection.
Note: Use Authenticator.setDefault(Authenticator)
to configure proxy
authentication when using the JVM default HttpURLConnection (i.e. not using the optional
okhttp dependency) in combination with a HTTPS database server.
proxyUser
- username for the proxy serverpublic ClientBuilder proxyPassword(java.lang.String proxyPassword)
Sets an optional proxy password for the proxy user specified by
proxyUser(String)
.
Note: Use Authenticator.setDefault(Authenticator)
to configure proxy
authentication when using the JVM default HttpURLConnection (i.e. not using the optional
okhttp dependency) in combination with a HTTPS database server.
proxyPassword
- password for the proxy server userpublic ClientBuilder disableSSLAuthentication()
The SSL authentication is enabled by default meaning that hostname verification and certificate chain validation is done using the JVM default settings.
java.lang.IllegalStateException
- if customSSLSocketFactory(SSLSocketFactory)
has been called on this ClientBuildercustomSSLSocketFactory(SSLSocketFactory)
public ClientBuilder customSSLSocketFactory(javax.net.ssl.SSLSocketFactory factory)
https
URL, when SSL authentication is enabled.factory
- An SSLSocketFactory, or null
for the
default SSLSocketFactory of the JRE.java.lang.IllegalStateException
- if disableSSLAuthentication()
has been called on this ClientBuilderdisableSSLAuthentication()
public ClientBuilder interceptors(HttpConnectionInterceptor... interceptors)
HttpConnectionInterceptor
s to be used on the CloudantClient
connection. Interceptors can be used to modify the HTTP requests and responses between the
CloudantClient and the server.
An example interceptor use might be to apply a custom authorization mechanism. For
instance to use BasicAuth instead of CookieAuth it is possible to use a
BasicAuthInterceptor
that adds the BasicAuth
Authorization
header to the request:
CloudantClient client = ClientBuilder.account("yourCloudantAccount")
.interceptors(new BasicAuthInterceptor("yourUsername:yourPassword"))
.build()
interceptors
- one or more HttpConnectionInterceptor objectsHttpConnectionInterceptor
public ClientBuilder connectTimeout(long connectTimeout, java.util.concurrent.TimeUnit connectTimeoutUnit)
SocketTimeoutException
is raised.
Example creating a CloudantClient
with a connection timeout of 2 seconds:
CloudantClient client = ClientBuilder.account("yourCloudantAccount")
.username("yourUsername")
.password("yourPassword")
.connectTimeout(2, TimeUnit.SECONDS)
.build();
Defaults to DEFAULT_CONNECTION_TIMEOUT
with TimeUnit.MINUTES
.connectTimeout
- duration of the read timeoutconnectTimeoutUnit
- unit of measurement of the read timeout parameterURLConnection.setConnectTimeout(int)
public ClientBuilder readTimeout(long readTimeout, java.util.concurrent.TimeUnit readTimeoutUnit)
InputStream
with an
established client connection. If the timeout expires before there is data available for
read, a SocketTimeoutException
is raised.
Example creating a CloudantClient
with a read timeout of 2 seconds:
CloudantClient client = ClientBuilder.account("yourCloudantAccount")
.username("yourUsername")
.password("yourPassword")
.readTimeout(2, TimeUnit.SECONDS)
.build();
Defaults to DEFAULT_READ_TIMEOUT
with TimeUnit.MINUTES
.readTimeout
- duration of the read timeoutreadTimeoutUnit
- unit of measurement of the read timeout parameterURLConnection.setReadTimeout(int)
public static ClientBuilder bluemix(java.lang.String vcapServices)
vcapServices
- service information JSON string, for example the contents of
VCAP_SERVICES
environment variablejava.lang.IllegalArgumentException
- see bluemix(String, String)
for conditions that
cause IllegalArgumentException
to be thrown.public static ClientBuilder bluemix(java.lang.String vcapServices, java.lang.String instanceName)
VCAP_SERVICES
environment variable which is a JSON object that
contains information that you can use to interact with a service instance in Bluemix.
Note: Specifying an instance name is only required when multiple Cloudant service instances
are present. If there is only a single Cloudant service instance then
bluemix(String)
can also be used.
vcapServices
- service information JSON string, for example the contents of
VCAP_SERVICES
environment variableinstanceName
- name of Bluemix service instance or null
to try to use the only
available Cloudant servicejava.lang.IllegalArgumentException
- if any of the following conditions are true:
vcapServices
is null
.vcapServices
is not valid.vcapServices
.instanceName
could not be found in
vcapServices
.instanceName
is null
and multiple Cloudant service instances
exist in vcapServices
.public static ClientBuilder bluemix(java.lang.String vcapServices, java.lang.String serviceName, java.lang.String instanceName)
VCAP_SERVICES
environment variable which is a JSON object that
contains information that you can use to interact with a service instance in Bluemix.vcapServices
- service information JSON string, for example the contents of
VCAP_SERVICES
environment variableserviceName
- name of Bluemix service to useinstanceName
- name of Bluemix service instance or null
to try to use the only
available Cloudant servicejava.lang.IllegalArgumentException
- if any of the following conditions are true:
vcapServices
is null
.serviceName
is null
.vcapServices
is not valid.serviceName
could not be found in
vcapServices
.instanceName
could not be found in
vcapServices
.instanceName
is null
and multiple Cloudant service instances
exist in vcapServices
.public ClientBuilder iamApiKey(java.lang.String iamApiKey)
Example creating a CloudantClient
using IAM authentication:
CloudantClient client = ClientBuilder.account("yourCloudantAccount")
.iamApiKey("yourIamApiKey")
.build();
iamApiKey
- the IAM API key for the sessionpublic ClientBuilder iamApiKey(java.lang.String iamApiKey, java.lang.String iamServerClientId, java.lang.String iamServerClientSecret)
Example creating a CloudantClient
using IAM authentication:
CloudantClient client = ClientBuilder.account("yourCloudantAccount")
.iamApiKey("yourIamApiKey", "yourClientId", "yourClientSecret")
.build();
iamApiKey
- the IAM API key for the sessioniamServerClientId
- Client ID used to authenticate with IAM token serveriamServerClientSecret
- Client secret used to authenticate with IAM token server