Class Config


  • public class Config
    extends Object
    • Constructor Detail

      • Config

        public Config()
    • Method Detail

      • httpClient

        public Config httpClient​(Client httpClient)
        Set the HttpClient implementation to use for every synchronous request
        Parameters:
        httpClient - Custom httpClient implementation
        Returns:
        this config object
      • httpClient

        public Config httpClient​(Function<Config,​Client> httpClient)
        Provide a builder for a client
        Parameters:
        httpClient - Custom httpClient implementation
        Returns:
        this config object
      • executor

        public Config executor​(Executor 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

        public Config proxy​(Proxy value)
        Set a proxy
        Parameters:
        value - Proxy settings object.
        Returns:
        this config object
      • proxy

        public Config proxy​(String host,
                            int port)
        Set a proxy
        Parameters:
        host - the hostname of the proxy server.
        port - the port of the proxy server
        Returns:
        this config object
      • proxy

        public Config proxy​(String host,
                            int port,
                            String username,
                            String password)
        Set an authenticated proxy
        Parameters:
        host - the hostname of the proxy server.
        port - the port of the proxy server
        username - username for authenticated proxy
        password - password for authenticated proxy
        Returns:
        this config object
      • setObjectMapper

        public Config setObjectMapper​(ObjectMapper om)
        Set the ObjectMapper implementation to use for Response to Object binding
        Parameters:
        om - Custom implementation of ObjectMapper interface
        Returns:
        this config object
      • sslContext

        public Config sslContext​(SSLContext ssl)
        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

        public Config ciphers​(String... values)
        Set a custom array of ciphers
        Parameters:
        values - the array of ciphers
        Returns:
        this config object
      • protocols

        public Config protocols​(String... values)
        Set a custom array of protocols
        Parameters:
        values - the array of protocols
        Returns:
        this config object
      • clientCertificateStore

        public Config clientCertificateStore​(KeyStore store,
                                             String password)
        Set a custom keystore
        Parameters:
        store - the keystore to use for a custom ssl context
        password - the password for the store
        Returns:
        this config object
        Throws:
        UnirestConfigException - if a SSLContext was already configured.
      • clientCertificateStore

        public Config clientCertificateStore​(String fileLocation,
                                             String password)
        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 context
        password - the password for the store
        Returns:
        this config object
        Throws:
        UnirestConfigException - if a SSLContext was already configured.
      • connectTimeout

        public Config connectTimeout​(int inMillies)
        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, then HttpClient::send throws an HttpConnectTimeoutException, or HttpClient::sendAsync completes exceptionally with an HttpConnectTimeoutException. 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
      • clearDefaultHeaders

        public Config clearDefaultHeaders()
        Clear default headers
        Returns:
        this config object
      • setDefaultBasicAuth

        public Config setDefaultBasicAuth​(String username,
                                          String password)
        Default basic auth credentials
        Parameters:
        username - the username
        password - the password
        Returns:
        this config object
      • setDefaultHeader

        public Config setDefaultHeader​(String name,
                                       String value)
        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

        public Config setDefaultHeader​(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.
        Parameters:
        name - The name of the header.
        value - a supplier that will get called as part of the request.
        Returns:
        this config object
      • addDefaultHeader

        public Config addDefaultHeader​(String name,
                                       String value)
        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

        public Config addDefaultCookie​(String name,
                                       String value)
        Adds a default cookie to be added to all requests with this config
        Parameters:
        name - the name of the cookie
        value - the value of the cookie
        Returns:
        this config object
      • addDefaultCookie

        public Config addDefaultCookie​(Cookie cookie)
        Adds a default cookie to be added to all requests with this config
        Parameters:
        cookie - the cookie
        Returns:
        this config object
      • instrumentWith

        public Config instrumentWith​(UniMetric metric)
        Add a metric object for instrumentation
        Parameters:
        metric - a UniMetric object
        Returns:
        this config object
      • interceptor

        public Config interceptor​(Interceptor value)
        Add a Interceptor which will be called before and after the request;
        Parameters:
        value - The Interceptor
        Returns:
        this config object
      • followRedirects

        public Config followRedirects​(boolean enable)
        Allow the client to follow redirects. Defaults to TRUE
        Parameters:
        enable - The name of the header.
        Returns:
        this config object
      • enableCookieManagement

        public Config enableCookieManagement​(boolean enable)
        Allow the client to manage cookies. Defaults to TRUE
        Parameters:
        enable - The name of the header.
        Returns:
        this config object
      • verifySsl

        public Config verifySsl​(boolean value)
        Toggle verifying SSL/TLS certificates. Defaults to TRUE
        Parameters:
        value - a bool is its true or not.
        Returns:
        this config object
      • useSystemProperties

        public Config useSystemProperties​(boolean value)
        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

        public Config requestCompression​(boolean value)
        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

        public Config 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)
        Parameters:
        policy - : the policy for cookies to follow
        Returns:
        this config object
      • cacheResponses

        public Config cacheResponses​(boolean value)
        Enable Response Caching with default options
        Parameters:
        value - enable or disable response caching
        Returns:
        this config object
      • cacheResponses

        public Config cacheResponses​(Cache.Builder value)
        Enable Response Caching with custom options
        Parameters:
        value - enable or disable response caching
        Returns:
        this config object
      • setDefaultResponseEncoding

        public Config setDefaultResponseEncoding​(String value)
        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

        public Config connectionTTL​(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. 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

        public Config 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. This property applies to both HTTP/1.1 and HTTP/2.
        Parameters:
        duration - of ttl.
        Returns:
        this config object
      • retryAfter

        public Config retryAfter​(boolean value)
        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

        public Config retryAfter​(boolean value,
                                 int maxRetryAttempts)
        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

        public Config retryAfter​(RetryStrategy strategy)
        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

        public Config version​(HttpClient.Version value)
        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

        public Config defaultBaseUrl​(String value)
        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

        public Headers 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

        public Config 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

        public Client getClient()
        Return the current Client. One will be build if it does not yet exist.
        Returns:
        A synchronous Client
      • 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
      • getKeystore

        public KeyStore getKeystore()
        Returns:
        a security keystore if one has been provided
      • getKeyStorePassword

        public String getKeyStorePassword()
        Returns:
        The password for the keystore if provided
      • getObjectMapper

        public ObjectMapper getObjectMapper()
        Returns:
        a configured object mapper
        Throws:
        UnirestException - if none has been configured.
      • getProxy

        public Proxy getProxy()
        Returns:
        the configured proxy configuration
      • useSystemProperties

        public boolean useSystemProperties()
        Returns:
        if the system will pick up system properties (default is false)
      • getDefaultResponseEncoding

        public String 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

        public String getCookieSpec()
        Returns:
        the configured Cookie Spec
      • getMetric

        public UniMetric getMetric()
        Returns:
        the currently configured UniMetric object
      • getUniInterceptor

        public Interceptor getUniInterceptor()
        Returns:
        the currently configured Interceptor
      • getSslContext

        public SSLContext getSslContext()
        Returns:
        the SSL connection configuration
      • getCiphers

        public String[] getCiphers()
        Returns:
        the ciphers for the SSL connection configuration
      • getProtocols

        public String[] getProtocols()
        Returns:
        the protocols for the SSL connection configuration
      • getDefaultBaseUrl

        public String getDefaultBaseUrl()
        Returns:
        the default base URL
      • getCustomExecutor

        public Executor getCustomExecutor()
      • 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.