Class HttpConnectStrategy


  • public class HttpConnectStrategy
    extends ConnectStrategy
    Allows configuration of HTTP request behavior for EventSource.

    EventSource uses this as its default implementation of how to connect to a stream. The underlying implementation is OkHttp.

    If you do not need to specify any options other than the stream URI, then you do not need to reference this class directly; you can just call one of the Builder(URI).

    To configure additional options, obtain an HttpConnectStrategy instance by calling ConnectStrategy.http(URI), and then call any of the methods of this class to specify your options. The class is immutable, so each of these methods returns a new modified instance, and an EventSource created with this configuration will not be affected by any subsequent changes you make.

    Once you have configured all desired options, pass the object to the Builder(ConnectStrategy) constructor:

    
       EventSource es = new EventSource.Builder(
         ConnectStrategy.http()
           .addHeader("Authorization", "xyz")
           .connectTimeout(3, TimeUnit.SECONDS)
         )
         .build();
     
    Since:
    4.0.0
    • Method Detail

      • createClient

        public com.launchdarkly.eventsource.HttpConnectStrategy.Client createClient​(LDLogger logger)
        Description copied from class: ConnectStrategy
        Creates a client instance.

        This is called once when an EventSource is created. The EventSource retains the returned Client and uses it to perform all subsequent connection attempts.

        Specified by:
        createClient in class ConnectStrategy
        Parameters:
        logger - the logger belonging to EventSource
        Returns:
        a ConnectStrategy.Client instance
      • clientBuilderActions

        public HttpConnectStrategy clientBuilderActions​(HttpConnectStrategy.ClientConfigurer configurer)
        Specifies any type of configuration actions you want to perform on the OkHttpClient builder.

        HttpConnectStrategy.ClientConfigurer is an interface with a single method, so you can use a lambda instead. EventSource will call this method when it creates an HTTP client. If you call this method multiple times, or use it in combination with other client configuration methods, the actions are performed in the same order as the calls.

        
           EventSource es = new EventSource.Builder(
             ConnectStrategy.http().clientBuilderActions(clientBuilder -> {
               clientBuilder.sslSocketFactory(mySocketFactory, myTrustManager);
             });
         
        Parameters:
        configurer - a ClientConfigurer (or lambda) that will act on the HTTP client builder
        Returns:
        a new HttpConnectStrategy instance with this property modified
      • header

        public HttpConnectStrategy header​(java.lang.String name,
                                          java.lang.String value)
        Sets a custom header to be included in each request.

        Any existing headers with the same name are overwritten.

        Parameters:
        name - the header name
        value - the header value
        Returns:
        a new HttpConnectStrategy instance with this property modified
      • headers

        public HttpConnectStrategy headers​(okhttp3.Headers headers)
        Sets custom headers to be included in each request.

        Any existing headers with the same name are overwritten.

        Parameters:
        headers - headers to be sent with the HTTP request
        Returns:
        a new HttpConnectStrategy instance with this property modified
      • httpClient

        public HttpConnectStrategy httpClient​(okhttp3.OkHttpClient httpClient)
        Set a custom HTTP client that will be used to make the EventSource connection.

        If you use this method, all other methods that correspond to client configuration properties (timeouts, proxy, etc.) will be ignored, and this client instance will be used exactly as is. Closing the EventSource will not cause this client to be closed; you are responsible for its lifecycle.

        Parameters:
        httpClient - the HTTP client, or null to allow EventSource to create a client
        Returns:
        a new HttpConnectStrategy instance with this property modified
      • methodAndBody

        public HttpConnectStrategy methodAndBody​(java.lang.String method,
                                                 okhttp3.RequestBody body)
        Specifies the request method and body to be used for HTTP requests. The default is to use the GET method with no request body. A non-null body is only valid for some request methods, such as POST.
        Parameters:
        method - the HTTP method
        body - the request body or null
        Returns:
        a new HttpConnectStrategy instance with these properties modified
      • proxy

        public HttpConnectStrategy proxy​(java.net.Proxy proxy)
        Specifies a web proxy to be used for all HTTP connections.
        Parameters:
        proxy - the proxy
        Returns:
        a new HttpConnectStrategy instance with this property modified
      • proxy

        public HttpConnectStrategy proxy​(java.lang.String proxyHost,
                                         int proxyPort)
        Specifies a web proxy to be used for all HTTP connections.
        Parameters:
        proxyHost - the proxy hostname
        proxyPort - the proxy port
        Returns:
        a new HttpConnectStrategy instance with this property modified
      • proxyAuthenticator

        public HttpConnectStrategy proxyAuthenticator​(okhttp3.Authenticator proxyAuthenticator)
        Specifies a web proxy authenticator to be used for all HTTP connections.
        Parameters:
        proxyAuthenticator - the proxy authenticator
        Returns:
        a new HttpConnectStrategy instance with this property modified
      • readTimeout

        public HttpConnectStrategy readTimeout​(long readTimeout,
                                               java.util.concurrent.TimeUnit timeUnit)
        Sets the read timeout. If a read timeout happens, the EventSource will restart the connection.
        Parameters:
        readTimeout - the read timeout, in whatever time unit is specified by timeUnit
        timeUnit - the time unit, or TimeUnit.MILLISECONDS if null
        Returns:
        a new HttpConnectStrategy instance with this property modified
        See Also:
        DEFAULT_READ_TIMEOUT_MILLIS, connectTimeout(long, TimeUnit), writeTimeout(long, TimeUnit)
      • requestTransformer

        public HttpConnectStrategy requestTransformer​(HttpConnectStrategy.RequestTransformer requestTransformer)
        Specifies an object that will be used to customize outgoing requests.

        Use this if you need to set request properties other than the ones that are already supported by HttpConnectStrategy methods, or if you need to determine the request properties dynamically rather than setting them to fixed values initially.

        
           EventSource es = new EventSource.Builder(
             ConnectStrategy.http().requestTransformer(request -> {
               return request.newBuilder().tag("hello").build();
             }).build();
         

        If you call this method multiple times, the transformations are applied in the same order as the calls.

        Parameters:
        requestTransformer - the transformer object
        Returns:
        a new HttpConnectStrategy instance with this property modified
      • uri

        public HttpConnectStrategy uri​(java.net.URI uri)
        Specifies a different stream URI.
        Parameters:
        uri - the stream URI
        Returns:
        a new HttpConnectStrategy instance with this property modified
      • writeTimeout

        public HttpConnectStrategy writeTimeout​(long writeTimeout,
                                                java.util.concurrent.TimeUnit timeUnit)
        Sets the write timeout. If a write timeout happens, the EventSource will restart the connection.
        Parameters:
        writeTimeout - the write timeout, in whatever time unit is specified by timeUnit
        timeUnit - the time unit, or TimeUnit.MILLISECONDS if null
        Returns:
        a new HttpConnectStrategy instance with this property modified
        See Also:
        DEFAULT_READ_TIMEOUT_MILLIS, connectTimeout(long, TimeUnit), readTimeout(long, TimeUnit)