Class HttpClientBuilder

java.lang.Object
com.mastfrog.netty.http.client.HttpClientBuilder

public final class HttpClientBuilder extends Object
Builds an HTTP client.
Author:
Tim Boudreau
  • Constructor Details

    • HttpClientBuilder

      public HttpClientBuilder()
  • Method Details

    • withWebsocketSupport

      public HttpClientBuilder withWebsocketSupport()
    • setObjectMapper

      public HttpClientBuilder setObjectMapper(com.fasterxml.jackson.databind.ObjectMapper mapper)
      Set the ObjectMapper used to read and write JSON.
      Parameters:
      mapper - The mapper
      Returns:
      this
    • addMarshaller

      public <T> HttpClientBuilder addMarshaller(Class<T> forType, Marshaller<T,io.netty.buffer.ByteBuf> marshaller)
      Set the marshaller used to convert inbound and outbound message bodies to / from objects. If you wanted to, say, read a response that is CSV as some kind of object, you would write a marshaller that can read and write that to a ByteBuf and add it here.
      Parameters:
      marshaller - The marshaller
      Returns:
      this
    • setSslContext

      public HttpClientBuilder setSslContext(io.netty.handler.ssl.SslContext ctx)
      Set the SSL context to use when accessing HTTPS addresses. In particular, use this if you want to abort untrusted connections.
      Parameters:
      ctx - The context
      Returns:
      this
    • setTimeout

      public HttpClientBuilder setTimeout(Duration timeout)
      Set the timeout for requests. Note that this timeout is independent of the timeout that can be set individually on requests, but whichever timeout is shorter will take precedence. The default is no timeout.
      Parameters:
      timeout - The timeout, or null for no timeout (the default)
      Returns:
      This
    • followRedirects

      public HttpClientBuilder followRedirects()
      HTTP requests will transparently load a redirects. Note that this means that handlers for events such as Connected may be called more than once - once for each request. Following redirects is the default behavior.
      Returns:
    • send100Continue

      public HttpClientBuilder send100Continue()
      Http requests will where appropriate set the Expect: 100-CONTINUE header
      Returns:
      this
    • dontSend100Continue

      public HttpClientBuilder dontSend100Continue()
      Turn off the default behavior of setting the Expect: 100-CONTINUE header when
      Returns:
    • dontFollowRedirects

      public HttpClientBuilder dontFollowRedirects()
      Turn off following of redirects
      Returns:
      this
    • threadCount

      public HttpClientBuilder threadCount(int count)
      The number of worker threads for processing requests and responses. Netty is asynchronous, so you do not need as many threads as you will have simultaneous requests; the default is 4. Best to see if you have problems, and increase this value only if it makes a measurable improvement in throughput.
      Parameters:
      count - The number of threads
      Returns:
      this
    • maxChunkSize

      public HttpClientBuilder maxChunkSize(int bytes)
      The maximum size of a chunk in bytes. The default is 64K.
      Parameters:
      bytes - A number of bytes
      Returns:
      this
    • maxInitialLineLength

      public HttpClientBuilder maxInitialLineLength(int max)
      Set the maximum length of the HTTP initial line, e.g. HTTP/1.1 GET /path/to/something. Unless you will be sending extremely long URLs, the default of 2048 should be plenty.
      Parameters:
      max -
      Returns:
      this
    • maxHeadersSize

      public HttpClientBuilder maxHeadersSize(int max)
      Set the maximum size of headers in bytes
      Returns:
      this
    • useCompression

      public HttpClientBuilder useCompression()
      Turn on HTTP gzip or deflate compression
      Returns:
      this
    • noCompression

      public HttpClientBuilder noCompression()
      Turn off HTTP gzip or deflate compression
      Returns:
      this
    • resolveAllHostsToLocalhost

      public HttpClientBuilder resolveAllHostsToLocalhost()
      For test environments using multiple host names - attaches a DNS resolver that will resolve all host name addresses to InetAddress.getLocalHost().
      Returns:
      this
    • resolver

      public HttpClientBuilder resolver(io.netty.resolver.AddressResolverGroup<? extends SocketAddress> resolver)
      Set the DNS resolver to use, bypassing the default one.the passed resolver will be used to resolve all host names by the resulting HttpClient.
      Parameters:
      resolver - The addresss setEventLoopGroup resolver
      Returns:
      this
    • resolver

      public <T extends SocketAddress> HttpClientBuilder resolver(io.netty.resolver.AddressResolver<T> resolver)
      Set the DNS resolver to use, bypassing the default one.the passed resolver will be used to resolve all host names by the resulting HttpClient.
      Type Parameters:
      T - The type of address the resolver resolves
      Parameters:
      resolver - The addresss setEventLoopGroup resolver
      Returns:
      this
    • setMaxRedirects

      public HttpClientBuilder setMaxRedirects(int maxRedirects)
      Set the maximum number of redirects this client can encounter before it considers itself to be in a redirect loop and cancels the request, sending a cancelled event.
      Parameters:
      maxRedirects - The maximum number of redirects
      Returns:
      this
    • setEventLoopGroup

      public HttpClientBuilder setEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup group)
      Set the thread pool used to perform network I/O.
      Parameters:
      group - The thread pool to use
      Returns:
      this
    • build

      public HttpClient build()
      Build an HTTP client
      Returns:
      an http client
    • setUserAgent

      public HttpClientBuilder setUserAgent(String userAgent)
      Set the user agent
      Parameters:
      userAgent -
      Returns:
      this
    • addRequestInterceptor

      public HttpClientBuilder addRequestInterceptor(RequestInterceptor interceptor)
      Add an interceptor which should get a chance to process every request before it is invoked; useful for things that sign requests and such.
      Parameters:
      interceptor - An interceptor
      Returns:
      this
    • setChannelOption

      public <T> HttpClientBuilder setChannelOption(io.netty.channel.ChannelOption<T> option, T value)
      Set a low-level setting for the Netty pipeline. See the Netty documentation for what these are.
      Type Parameters:
      T - The type
      Parameters:
      option - The applyOption
      value - The value type
      Returns:
      this
    • setCookieStore

      public HttpClientBuilder setCookieStore(CookieStore store)
      Set a cookie store which will be used for all HTTP requests on the resulting HttpClient (unless overriddeen in RequestBuilder).
      Parameters:
      store - A cookie store
      Returns:
      this