Class Config
- Direct Known Subclasses:
MockConfig
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate CacheManager
private String[]
private int
private boolean
private String
private Executor
static final int
private String
private String
private boolean
private Headers
private CompoundInterceptor
static final String
private KeyStore
private UniMetric
private Supplier
<ObjectMapper> private String[]
private Proxy
private boolean
private Integer
private RetryStrategy
private SSLContext
private boolean
private boolean
private HttpClient.Version
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddDefaultCookie
(String name, String value) Adds a default cookie to be added to all requests with this configaddDefaultCookie
(Cookie cookie) Adds a default cookie to be added to all requests with this configaddDefaultHeader
(String name, String value) Add default header to appear on all requestsprivate void
cacheResponses
(boolean value) Enable Response Caching with default optionscacheResponses
(Cache.Builder value) Enable Response Caching with custom optionsSet a custom array of ciphersClear default headersclientCertificateStore
(String fileLocation, String password) Set a custom keystore via a file path.clientCertificateStore
(KeyStore store, String password) Set a custom keystoreconnectionTTL
(long duration, TimeUnit unit) Sets the jdk.httpclient.keepalive.timeout setting https://docs.oracle.com/en/java/javase/20/docs/api/java.net.http/module-summary.html The number of seconds to keep idle HTTP connections alive in the keep alive cache.connectionTTL
(Duration duration) Sets the jdk.httpclient.keepalive.timeout setting https://docs.oracle.com/en/java/javase/20/docs/api/java.net.http/module-summary.html The number of seconds to keep idle HTTP connections alive in the keep alive cache.connectTimeout
(int inMillies) Sets the connect timeout duration for this client.cookieSpec
(String policy) Sets a cookie policy Acceptable values: 'default' (same as Netscape), 'netscape', 'ignoreCookies', 'standard' (RFC 6265 interoprability profile) , 'standard-strict' (RFC 6265 strict profile)defaultBaseUrl
(String value) set a default base url for all routes.enableCookieManagement
(boolean enable) Allow the client to manage cookies.Sets a custom executor for requestsfollowRedirects
(boolean enable) Allow the client to follow redirects.String[]
Return the current Client.int
Return default headers that are added to every requestboolean
private Client
boolean
String[]
getProxy()
long
getTTL()
httpClient
(Function<Config, Client> httpClient) Provide a builder for a clienthttpClient
(Client httpClient) Set the HttpClient implementation to use for every synchronous requestinstrumentWith
(UniMetric metric) Add a metric object for instrumentationinterceptor
(Interceptor value) Add a Interceptor which will be called before and after the request;boolean
boolean
boolean
Does the config have currently running clients? Find out here.boolean
Will unirest verify the SSL? You should only do this in non-prod environments.int
Set a custom array of protocolsSet a proxySet an authenticated proxySet a proxyrequestCompression
(boolean value) Turn on or off requesting all content as compressed.requestTimeout
(Integer inMillies) Sets a default timeout for all requests.reset()
Shutdown the current config and re-init.void
reset
(boolean clearOptions) Shut down the configuration and its clients.retryAfter
(boolean value) Automatically retry synchronous requests on 429/529 responses with the Retry-After response header Default is falseretryAfter
(boolean value, int maxRetryAttempts) Automatically retry synchronous requests on 429/529 responses with the Retry-After response header Default is falseretryAfter
(RetryStrategy strategy) Automatically retry synchronous requests on 429/529 responses with the Retry-After response header Default is falsesetDefaultBasicAuth
(String username, String password) Default basic auth credentialssetDefaultHeader
(String name, String value) Set default header to appear on all requestssetDefaultHeader
(String name, Supplier<String> value) Set default header to appear on all requests, value is through a Supplier This is useful for adding tracing elements to requests.setDefaultResponseEncoding
(String value) Set the default encoding that will be used for serialization into Strings.private void
Set the ObjectMapper implementation to use for Response to Object bindingsslContext
(SSLContext ssl) Set a custom SSLContext.boolean
useSystemProperties
(boolean value) Tell the HttpClients to use the system properties for things like proxiesprivate void
private void
verifySecurityConfig
(Object thing) verifySsl
(boolean value) Toggle verifying SSL/TLS certificates.version
(HttpClient.Version value) Requests a specific HTTP protocol version where possible.
-
Field Details
-
DEFAULT_CONNECT_TIMEOUT
public static final int DEFAULT_CONNECT_TIMEOUT- See Also:
-
JDK_HTTPCLIENT_KEEPALIVE_TIMEOUT
- See Also:
-
client
-
objectMapper
-
customExecutor
-
headers
-
proxy
-
connectionTimeout
private int connectionTimeout -
requestTimeout
-
followRedirects
private boolean followRedirects -
cookieManagement
private boolean cookieManagement -
useSystemProperties
private boolean useSystemProperties -
defaultResponseEncoding
-
clientBuilder
-
requestCompressionOn
private boolean requestCompressionOn -
verifySsl
private boolean verifySsl -
keystore
-
keystorePassword
-
cookieSpec
-
metrics
-
sslContext
-
ciphers
-
protocols
-
interceptor
-
defaultBaseUrl
-
cache
-
version
-
retry
-
-
Constructor Details
-
Config
public Config()
-
-
Method Details
-
setDefaults
private void setDefaults() -
httpClient
Set the HttpClient implementation to use for every synchronous request- Parameters:
httpClient
- Custom httpClient implementation- Returns:
- this config object
-
httpClient
Provide a builder for a client- Parameters:
httpClient
- Custom httpClient implementation- Returns:
- this config object
-
executor
Sets a custom executor for requests- Parameters:
executor
- – the Executor- Returns:
- this config builder Implementation Note: The default executor uses a thread pool, with a custom thread factory. If a security manager has been installed, the thread factory creates threads that run with an access control context that has no permissions.
-
proxy
Set a proxy- Parameters:
value
- Proxy settings object.- Returns:
- this config object
-
proxy
Set a proxy- Parameters:
host
- the hostname of the proxy server.port
- the port of the proxy server- Returns:
- this config object
-
proxy
Set an authenticated proxy- Parameters:
host
- the hostname of the proxy server.port
- the port of the proxy serverusername
- username for authenticated proxypassword
- password for authenticated proxy- Returns:
- this config object
-
setObjectMapper
Set the ObjectMapper implementation to use for Response to Object binding- Parameters:
om
- Custom implementation of ObjectMapper interface- Returns:
- this config object
-
sslContext
Set a custom SSLContext.- Parameters:
ssl
- the SSLContext to use for custom ssl context- Returns:
- this config object
- Throws:
UnirestConfigException
- if a keystore was already configured.
-
ciphers
Set a custom array of ciphers- Parameters:
values
- the array of ciphers- Returns:
- this config object
-
protocols
Set a custom array of protocols- Parameters:
values
- the array of protocols- Returns:
- this config object
-
verifySecurityConfig
-
clientCertificateStore
Set a custom keystore- Parameters:
store
- the keystore to use for a custom ssl contextpassword
- the password for the store- Returns:
- this config object
- Throws:
UnirestConfigException
- if a SSLContext was already configured.
-
clientCertificateStore
Set a custom keystore via a file path. Must be a valid PKCS12 file- Parameters:
fileLocation
- the path keystore to use for a custom ssl contextpassword
- the password for the store- Returns:
- this config object
- Throws:
UnirestConfigException
- if a SSLContext was already configured.
-
connectTimeout
Sets the connect timeout duration for this client.In the case where a new connection needs to be established, if the connection cannot be established within the given
duration
, thenHttpClient::send
throws anHttpConnectTimeoutException
, orHttpClient::sendAsync
completes exceptionally with anHttpConnectTimeoutException
. If a new connection does not need to be established, for example if a connection can be reused from a previous request, then this timeout duration has no effect.- Parameters:
inMillies
- the duration to allow the underlying connection to be established- Returns:
- this builder
-
requestTimeout
Sets a default timeout for all requests. If the response is not received within the specified timeout then anHttpTimeoutException
is thrown. completes exceptionally with anHttpTimeoutException
. The effect of not setting a timeout is the same as setting an infinite Duration, ie. block forever.- Parameters:
inMillies
- the timeout duration in millies- Returns:
- this builder
- Throws:
IllegalArgumentException
- if the duration is non-positive
-
clearDefaultHeaders
Clear default headers- Returns:
- this config object
-
setDefaultBasicAuth
Default basic auth credentials- Parameters:
username
- the usernamepassword
- the password- Returns:
- this config object
-
setDefaultHeader
Set default header to appear on all requests- Parameters:
name
- The name of the header.value
- The value of the header.- Returns:
- this config object
-
setDefaultHeader
Set default header to appear on all requests, value is through a Supplier This is useful for adding tracing elements to requests.- Parameters:
name
- The name of the header.value
- a supplier that will get called as part of the request.- Returns:
- this config object
-
addDefaultHeader
Add default header to appear on all requests- Parameters:
name
- The name of the header.value
- The value of the header.- Returns:
- this config object
-
addDefaultCookie
Adds a default cookie to be added to all requests with this config- Parameters:
name
- the name of the cookievalue
- the value of the cookie- Returns:
- this config object
-
addDefaultCookie
Adds a default cookie to be added to all requests with this config- Parameters:
cookie
- the cookie- Returns:
- this config object
-
instrumentWith
Add a metric object for instrumentation- Parameters:
metric
- a UniMetric object- Returns:
- this config object
-
interceptor
Add a Interceptor which will be called before and after the request;- Parameters:
value
- The Interceptor- Returns:
- this config object
-
followRedirects
Allow the client to follow redirects. Defaults to TRUE- Parameters:
enable
- The name of the header.- Returns:
- this config object
-
enableCookieManagement
Allow the client to manage cookies. Defaults to TRUE- Parameters:
enable
- The name of the header.- Returns:
- this config object
-
verifySsl
Toggle verifying SSL/TLS certificates. Defaults to TRUE- Parameters:
value
- a bool is its true or not.- Returns:
- this config object
-
useSystemProperties
Tell the HttpClients to use the system properties for things like proxies- Parameters:
value
- a bool is its true or not.- Returns:
- this config object
-
requestCompression
Turn on or off requesting all content as compressed. (GZIP encoded) Default is true- Parameters:
value
- a bool is its true or not.- Returns:
- this config object
-
cookieSpec
Sets a cookie policy Acceptable values: 'default' (same as Netscape), 'netscape', 'ignoreCookies', 'standard' (RFC 6265 interoprability profile) , 'standard-strict' (RFC 6265 strict profile)- Parameters:
policy
- : the policy for cookies to follow- Returns:
- this config object
-
cacheResponses
Enable Response Caching with default options- Parameters:
value
- enable or disable response caching- Returns:
- this config object
-
cacheResponses
Enable Response Caching with custom options- Parameters:
value
- enable or disable response caching- Returns:
- this config object
-
setDefaultResponseEncoding
Set the default encoding that will be used for serialization into Strings. The default-default is UTF-8- Parameters:
value
- a bool is its true or not.- Returns:
- this config object
-
connectionTTL
Sets the jdk.httpclient.keepalive.timeout setting https://docs.oracle.com/en/java/javase/20/docs/api/java.net.http/module-summary.html The number of seconds to keep idle HTTP connections alive in the keep alive cache. This property applies to both HTTP/1.1 and HTTP/2.- Parameters:
duration
- of ttl.unit
- the time unit of the ttl- Returns:
- this config object
-
connectionTTL
Sets the jdk.httpclient.keepalive.timeout setting https://docs.oracle.com/en/java/javase/20/docs/api/java.net.http/module-summary.html The number of seconds to keep idle HTTP connections alive in the keep alive cache. This property applies to both HTTP/1.1 and HTTP/2.- Parameters:
duration
- of ttl.- Returns:
- this config object
-
retryAfter
Automatically retry synchronous requests on 429/529 responses with the Retry-After response header Default is false- Parameters:
value
- a bool is its true or not.- Returns:
- this config object
-
retryAfter
Automatically retry synchronous requests on 429/529 responses with the Retry-After response header Default is false- Parameters:
value
- a bool is its true or not.maxRetryAttempts
- max retry attempts- Returns:
- this config object
-
retryAfter
Automatically retry synchronous requests on 429/529 responses with the Retry-After response header Default is false- Parameters:
strategy
- a RetryStrategy- Returns:
- this config object
-
version
Requests a specific HTTP protocol version where possible. This is a direct proxy setter for the Java Http-Client that powers unirest.If this method is not invoked prior to using, then newly built clients will prefer HTTP/2.
If set to HTTP/2, then each request will attempt to upgrade to HTTP/2. If the upgrade succeeds, then the response to this request will use HTTP/2 and all subsequent requests and responses to the same origin server will use HTTP/2. If the upgrade fails, then the response will be handled using HTTP/1.1 Constraints may also affect the selection of protocol version. For example, if HTTP/2 is requested through a proxy, and if the implementation does not support this mode, then HTTP/1.1 may be used
- Parameters:
value
- the requested HTTP protocol version- Returns:
- this config
-
defaultBaseUrl
set a default base url for all routes. this is overridden if the url contains a valid base already the url may contain path params for example. Setting a default path of 'http://somwhere' and then calling Unirest with Unirest.get('/place') will result in a path of 'https://somwehre/place'- Parameters:
value
- the base URL to use- Returns:
- this config object
-
getDefaultHeaders
Return default headers that are added to every request- Returns:
- Headers
-
isRunning
public boolean isRunning()Does the config have currently running clients? Find out here.- Returns:
- boolean
-
reset
Shutdown the current config and re-init.- Returns:
- this config
-
reset
public void reset(boolean clearOptions) Shut down the configuration and its clients. The config can be re-initialized with its settings- Parameters:
clearOptions
- should the current non-client settings be retained.
-
getClient
Return the current Client. One will be build if it does not yet exist.- Returns:
- A synchronous Client
-
getFinalClient
-
buildClient
private void buildClient() -
getEnabledCookieManagement
public boolean getEnabledCookieManagement()- Returns:
- if cookie management should be enabled. default: true
-
getFollowRedirects
public boolean getFollowRedirects()- Returns:
- if the clients should follow redirects default: true
-
getConnectionTimeout
public int getConnectionTimeout()- Returns:
- the connection timeout in milliseconds default: 10000
-
getRequestTimeout
- Returns:
- the connection timeout in milliseconds default: null (infinite)
-
getKeystore
- Returns:
- a security keystore if one has been provided
-
getKeyStorePassword
- Returns:
- The password for the keystore if provided
-
getObjectMapper
- Returns:
- a configured object mapper
- Throws:
UnirestException
- if none has been configured.
-
validateClientsNotRunning
private void validateClientsNotRunning() -
getProxy
- Returns:
- the configured proxy configuration
-
useSystemProperties
public boolean useSystemProperties()- Returns:
- if the system will pick up system properties (default is false)
-
getDefaultResponseEncoding
- Returns:
- the default encoding (UTF-8 is the default default)
-
isRequestCompressionOn
public boolean isRequestCompressionOn()- Returns:
- if request compression is on (default is true)
-
isVerifySsl
public boolean isVerifySsl()Will unirest verify the SSL? You should only do this in non-prod environments. Default is true- Returns:
- if unirest will verify the SSL
-
getCookieSpec
- Returns:
- the configured Cookie Spec
-
getMetric
- Returns:
- the currently configured UniMetric object
-
getUniInterceptor
- Returns:
- the currently configured Interceptor
-
getSslContext
- Returns:
- the SSL connection configuration
-
getCiphers
- Returns:
- the ciphers for the SSL connection configuration
-
getProtocols
- Returns:
- the protocols for the SSL connection configuration
-
getDefaultBaseUrl
- Returns:
- the default base URL
-
getCustomExecutor
-
getVersion
- Returns:
- the preferred http version
-
isAutomaticRetryAfter
public boolean isAutomaticRetryAfter()- Returns:
- if unirest will retry requests on 429/529
-
maxRetries
public int maxRetries()- Returns:
- the max number of times to attempt to do a 429/529 retry-after
-
getTTL
public long getTTL()- Returns:
- the maximum life span of persistent connections regardless of their expiration setting.
-
getRetryStrategy
-