Package kong.unirest

Class Config


  • public class Config
    extends Object
    • Field Detail

      • DEFAULT_CONNECT_TIMEOUT

        public static final int DEFAULT_CONNECT_TIMEOUT
        See Also:
        Constant Field Values
    • 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)
        Set the connection timeout
        Parameters:
        inMillies - The timeout until a connection with the server is established (in milliseconds). Default is 10000. Set to zero to disable the timeout.
        Returns:
        this config object
      • 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
      • automaticRetries

        public Config automaticRetries​(boolean value)
        Automaticly retry certain recoverable errors like socket timeouts. Up to 4 times Note that currently this only works on synchronous calls. 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)
        Total time to live (TTL) defines maximum life span of persistent connections regardless of their expiration setting. No persistent connection will be re-used past its TTL value.
        Parameters:
        duration - of ttl.
        unit - the time unit of the ttl
        Returns:
        this config object
      • connectionTTL

        public Config connectionTTL​(Duration duration)
        Sugar! Total time to live (TTL) defines maximum life span of persistent connections regardless of their expiration setting. No persistent connection will be re-used past its TTL value.
        Parameters:
        duration - of ttl.
        Returns:
        this config object
      • 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)
      • isAutomaticRetries

        public boolean isAutomaticRetries()
        Returns:
        if automatic retries are on (default is false)
      • 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
      • getTTL

        public long getTTL()
        Returns:
        the maximum life span of persistent connections regardless of their expiration setting.
      • 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()