Package com.mastfrog.netty.http.client
Class HttpClientBuilder
java.lang.Object
com.mastfrog.netty.http.client.HttpClientBuilder
Builds an HTTP client.
- Author:
- Tim Boudreau
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static final classEncapsulates a setting that can be set on the Netty Bootstrap; not really an API class, but exposed so that the HttpClient constructor can be invoked directly if someone wants to (using HttpClientBuilder is much easier). -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddMarshaller(Class<T> forType, Marshaller<T, io.netty.buffer.ByteBuf> marshaller) Set the marshaller used to convert inbound and outbound message bodies to / from objects.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.build()Build an HTTP clientTurn off following of redirectsTurn off the default behavior of setting the Expect: 100-CONTINUE header whenHTTP requests will transparently load a redirects.maxChunkSize(int bytes) The maximum size of a chunk in bytes.maxHeadersSize(int max) Set the maximum size of headers in bytesmaxInitialLineLength(int max) Set the maximum length of the HTTP initial line, e.g.Turn off HTTP gzip or deflate compressionFor test environments using multiple host names - attaches a DNS resolver that will resolve all host name addresses toInetAddress.getLocalHost().<T extends SocketAddress>
HttpClientBuilderresolver(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.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.Http requests will where appropriate set the Expect: 100-CONTINUE headersetChannelOption(io.netty.channel.ChannelOption<T> option, T value) Set a low-level setting for the Netty pipeline.setCookieStore(CookieStore store) Set a cookie store which will be used for all HTTP requests on the resulting HttpClient (unless overriddeen in RequestBuilder).setEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup group) Set the thread pool used to perform network I/O.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.setObjectMapper(com.fasterxml.jackson.databind.ObjectMapper mapper) Set the ObjectMapper used to read and write JSON.setSslContext(io.netty.handler.ssl.SslContext ctx) Set the SSL context to use when accessing HTTPS addresses.setTimeout(Duration timeout) Set the timeout for requests.setUserAgent(String userAgent) Set the user agentthreadCount(int count) The number of worker threads for processing requests and responses.Turn on HTTP gzip or deflate compression
-
Constructor Details
-
HttpClientBuilder
public HttpClientBuilder()
-
-
Method Details
-
withWebsocketSupport
-
setObjectMapper
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
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
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
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
Http requests will where appropriate set the Expect: 100-CONTINUE header- Returns:
- this
-
dontSend100Continue
Turn off the default behavior of setting the Expect: 100-CONTINUE header when- Returns:
-
dontFollowRedirects
Turn off following of redirects- Returns:
- this
-
threadCount
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
The maximum size of a chunk in bytes. The default is 64K.- Parameters:
bytes- A number of bytes- Returns:
- this
-
maxInitialLineLength
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
Set the maximum size of headers in bytes- Returns:
- this
-
useCompression
Turn on HTTP gzip or deflate compression- Returns:
- this
-
noCompression
Turn off HTTP gzip or deflate compression- Returns:
- this
-
resolveAllHostsToLocalhost
For test environments using multiple host names - attaches a DNS resolver that will resolve all host name addresses toInetAddress.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
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
Set the thread pool used to perform network I/O.- Parameters:
group- The thread pool to use- Returns:
- this
-
build
Build an HTTP client- Returns:
- an http client
-
setUserAgent
Set the user agent- Parameters:
userAgent-- Returns:
- this
-
addRequestInterceptor
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
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 applyOptionvalue- The value type- Returns:
- this
-
setCookieStore
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
-