@Prototype @Primary @Internal public class DefaultHttpClient extends Object implements RxWebSocketClient, RxHttpClient, RxStreamingHttpClient, RxSseClient, Closeable, AutoCloseable
HttpClient
interface based on Netty.Modifier and Type | Class and Description |
---|---|
protected class |
DefaultHttpClient.HttpClientInitializer
Initializes the HTTP client channel.
|
protected class |
DefaultHttpClient.NettyRequestWriter
A Netty request writer.
|
Modifier and Type | Field and Description |
---|---|
protected io.netty.bootstrap.Bootstrap |
bootstrap |
protected ByteBufferFactory<io.netty.buffer.ByteBufAllocator,io.netty.buffer.ByteBuf> |
byteBufferFactory |
protected io.netty.channel.EventLoopGroup |
group |
protected static String |
HANDLER_AGGREGATOR |
protected static String |
HANDLER_CHUNK |
protected static String |
HANDLER_DECODER |
protected static String |
HANDLER_STREAM |
protected MediaTypeCodecRegistry |
mediaTypeCodecRegistry |
DEFAULT_ERROR_TYPE
Modifier and Type | Method and Description |
---|---|
protected <I,O> Publisher<HttpResponse<O>> |
applyFilterToResponsePublisher(HttpRequest<?> parentRequest,
HttpRequest<I> request,
URI requestURI,
AtomicReference<HttpRequest> requestWrapper,
Publisher<HttpResponse<O>> responsePublisher) |
protected <I> io.reactivex.functions.Function<URI,io.reactivex.Flowable<ByteBuffer<?>>> |
buildDataStreamPublisher(HttpRequest<I> request) |
protected <I,O,E> io.reactivex.functions.Function<URI,Publisher<? extends HttpResponse<O>>> |
buildExchangePublisher(HttpRequest<?> parentRequest,
HttpRequest<I> request,
Argument<O> bodyType,
Argument<E> errorType) |
protected <I> io.reactivex.functions.Function<URI,io.reactivex.Flowable<HttpResponse<ByteBuffer<?>>>> |
buildExchangeStreamPublisher(HttpRequest<I> request) |
protected <I,O> io.reactivex.functions.Function<URI,io.reactivex.Flowable<O>> |
buildJsonStreamPublisher(HttpRequest<?> parentRequest,
HttpRequest<I> request,
Argument<O> type) |
protected DefaultHttpClient.NettyRequestWriter |
buildNettyRequest(MutableHttpRequest request,
URI requestURI,
MediaType requestContentType,
boolean permitsBody) |
protected io.netty.handler.ssl.SslContext |
buildSslContext(URI uriObject)
Builds an
SslContext for the given URI if necessary. |
protected <I> io.reactivex.Flowable<HttpResponse<Object>> |
buildStreamExchange(HttpRequest<?> parentRequest,
HttpRequest<I> request,
URI requestURI) |
void |
close()
Delegates to
LifeCycle.stop() . |
protected void |
closeChannelAsync(io.netty.channel.Channel channel) |
protected void |
configure(BeanContext beanContext)
Configure this client for the active bean context.
|
protected void |
configureProxy(io.netty.channel.ChannelPipeline pipeline,
Proxy.Type proxyType,
SocketAddress proxyAddress)
Configures the HTTP proxy for the pipeline.
|
protected void |
configureProxy(io.netty.channel.ChannelPipeline pipeline,
Proxy proxy)
Configures the HTTP proxy for the pipeline.
|
<T extends AutoCloseable> |
connect(Class<T> clientEndpointType,
Map<String,Object> parameters)
Connect the given client endpoint type.
|
<T extends AutoCloseable> |
connect(Class<T> clientEndpointType,
MutableHttpRequest<?> request)
Connect the given client endpoint type to the URI over WebSocket.
|
protected io.netty.channel.nio.NioEventLoopGroup |
createEventLoopGroup(HttpClientConfiguration configuration,
ThreadFactory threadFactory)
Creates the
NioEventLoopGroup for this client. |
<I> io.reactivex.Flowable<ByteBuffer<?>> |
dataStream(HttpRequest<I> request)
Request a stream of data where each emitted item is a
ByteBuffer instance. |
protected io.netty.channel.ChannelFuture |
doConnect(io.netty.bootstrap.Bootstrap bootstrap,
String host,
int port)
Creates an initial connection with the given bootstrap and remote host.
|
protected io.netty.channel.ChannelFuture |
doConnect(HttpRequest<?> request,
String host,
int port,
io.netty.handler.ssl.SslContext sslCtx,
boolean isStream)
Creates an initial connection to the given remote host.
|
protected io.netty.channel.ChannelFuture |
doConnect(HttpRequest<?> request,
URI uri,
io.netty.handler.ssl.SslContext sslCtx,
boolean isStream)
Creates an initial connection to the given remote host.
|
<I> io.reactivex.Flowable<Event<ByteBuffer<?>>> |
eventStream(HttpRequest<I> request)
Perform an HTTP request and receive data as a stream of SSE
Event objects as they become available without blocking. |
<I,B> io.reactivex.Flowable<Event<B>> |
eventStream(HttpRequest<I> request,
Argument<B> eventType)
Perform an HTTP request and receive data as a stream of SSE
Event objects as they become available without blocking. |
<I,O,E> io.reactivex.Flowable<HttpResponse<O>> |
exchange(HttpRequest<I> request,
Argument<O> bodyType,
Argument<E> errorType)
Perform an HTTP request for the given request object emitting the full HTTP response from returned
Publisher and converting the response body to the specified type. |
<I> io.reactivex.Flowable<HttpResponse<ByteBuffer<?>>> |
exchangeStream(HttpRequest<I> request)
Requests a stream data where each emitted item is a
ByteBuffer wrapped in the HttpResponse object
(which remains the same for each emitted item). |
HttpClientConfiguration |
getConfiguration() |
protected Object |
getLoadBalancerDiscriminator() |
org.slf4j.Logger |
getLog() |
MediaTypeCodecRegistry |
getMediaTypeCodecRegistry() |
boolean |
isRunning() |
<I> io.reactivex.Flowable<Map<String,Object>> |
jsonStream(HttpRequest<I> request)
Perform an HTTP request and receive data as a stream of JSON objects as they become available without blocking.
|
<I,O> io.reactivex.Flowable<O> |
jsonStream(HttpRequest<I> request,
Argument<O> type)
Perform an HTTP request and receive data as a stream of JSON objects as they become available without blocking.
|
<I,O> io.reactivex.Flowable<O> |
jsonStream(HttpRequest<I> request,
Class<O> type)
Perform an HTTP request and receive data as a stream of JSON objects as they become available without blocking.
|
protected List<HttpClientFilter> |
resolveFilters(HttpRequest<?> parentRequest,
HttpRequest<?> request,
URI requestURI)
Resolve the filters for the request path.
|
protected <I> Publisher<URI> |
resolveRequestURI(HttpRequest<I> request) |
protected URI |
resolveRequestURI(URI requestURI) |
void |
setClientIdentifiers(Set<String> clientIdentifiers)
Sets the client identifiers that this client applies to.
|
void |
setClientIdentifiers(String... clientIdentifiers) |
void |
setMediaTypeCodecRegistry(MediaTypeCodecRegistry mediaTypeCodecRegistry)
Sets the
MediaTypeCodecRegistry used by this client. |
HttpClient |
start()
Starts the lifecyle component.
|
HttpClient |
stop()
Stops the life cycle component.
|
BlockingHttpClient |
toBlocking() |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
connect, connect
create
exchange, exchange, exchange, exchange, exchange, retrieve, retrieve, retrieve, retrieve, retrieve
refresh
eventStream, eventStream, eventStream
protected static final String HANDLER_AGGREGATOR
protected static final String HANDLER_CHUNK
protected static final String HANDLER_STREAM
protected static final String HANDLER_DECODER
protected final io.netty.bootstrap.Bootstrap bootstrap
protected io.netty.channel.EventLoopGroup group
protected MediaTypeCodecRegistry mediaTypeCodecRegistry
protected ByteBufferFactory<io.netty.buffer.ByteBufAllocator,io.netty.buffer.ByteBuf> byteBufferFactory
public DefaultHttpClient(@Parameter LoadBalancer loadBalancer, @Parameter HttpClientConfiguration configuration, @Parameter @Nullable String contextPath, @Named(value="netty") @Nullable ThreadFactory threadFactory, NettyClientSslBuilder nettyClientSslBuilder, MediaTypeCodecRegistry codecRegistry, @Nullable AnnotationMetadataResolver annotationMetadataResolver, HttpClientFilter... filters)
loadBalancer
- The LoadBalancer
to use for selecting serversconfiguration
- The HttpClientConfiguration
objectcontextPath
- The base URI to prepend to request uristhreadFactory
- The thread factory to use for client threadsnettyClientSslBuilder
- The SSL buildercodecRegistry
- The MediaTypeCodecRegistry
to use for encoding and decoding objectsannotationMetadataResolver
- The annotation metadata resolverfilters
- The filters to use@Inject public DefaultHttpClient(@Parameter LoadBalancer loadBalancer, @Parameter HttpClientConfiguration configuration, @Parameter @Nullable String contextPath, @Named(value="netty") @Nullable ThreadFactory threadFactory, NettyClientSslBuilder nettyClientSslBuilder, MediaTypeCodecRegistry codecRegistry, @Nullable AnnotationMetadataResolver annotationMetadataResolver, List<HttpClientFilter> filters)
loadBalancer
- The LoadBalancer
to use for selecting serversconfiguration
- The HttpClientConfiguration
objectcontextPath
- The base URI to prepend to request uristhreadFactory
- The thread factory to use for client threadsnettyClientSslBuilder
- The SSL buildercodecRegistry
- The MediaTypeCodecRegistry
to use for encoding and decoding objectsannotationMetadataResolver
- The annotation metadata resolverfilters
- The filters to usepublic DefaultHttpClient(URL url, HttpClientConfiguration configuration, NettyClientSslBuilder nettyClientSslBuilder, MediaTypeCodecRegistry codecRegistry, HttpClientFilter... filters)
url
- The URLconfiguration
- The HttpClientConfiguration
objectnettyClientSslBuilder
- The SSL buildercodecRegistry
- The MediaTypeCodecRegistry
to use for encoding and decoding objectsfilters
- The filters to usepublic DefaultHttpClient(LoadBalancer loadBalancer)
loadBalancer
- The LoadBalancer
to use for selecting serverspublic DefaultHttpClient(@Parameter URL url)
url
- The URLpublic DefaultHttpClient(URL url, HttpClientConfiguration configuration)
url
- The URLconfiguration
- The HttpClientConfiguration
objectpublic DefaultHttpClient(URL url, HttpClientConfiguration configuration, String contextPath)
url
- The URLconfiguration
- The HttpClientConfiguration
objectcontextPath
- The base URI to prepend to request urispublic DefaultHttpClient(LoadBalancer loadBalancer, HttpClientConfiguration configuration)
loadBalancer
- The LoadBalancer
to use for selecting serversconfiguration
- The HttpClientConfiguration
objectpublic DefaultHttpClient(LoadBalancer loadBalancer, HttpClientConfiguration configuration, String contextPath)
loadBalancer
- The LoadBalancer
to use for selecting serversconfiguration
- The HttpClientConfiguration
objectcontextPath
- The base URI to prepend to request urispublic HttpClientConfiguration getConfiguration()
public org.slf4j.Logger getLog()
public HttpClient start()
LifeCycle
start
in interface LifeCycle<HttpClient>
public boolean isRunning()
isRunning
in interface LifeCycle<HttpClient>
@PreDestroy public HttpClient stop()
LifeCycle
stop
in interface LifeCycle<HttpClient>
public void setClientIdentifiers(Set<String> clientIdentifiers)
HttpClientFilter
.
The client identifiers are equivalents to the value of Client.id()
clientIdentifiers
- The client identifierspublic void setClientIdentifiers(String... clientIdentifiers)
clientIdentifiers
- The client identifierssetClientIdentifiers(Set)
public MediaTypeCodecRegistry getMediaTypeCodecRegistry()
MediaTypeCodecRegistry
used by this clientpublic void setMediaTypeCodecRegistry(MediaTypeCodecRegistry mediaTypeCodecRegistry)
MediaTypeCodecRegistry
used by this client.mediaTypeCodecRegistry
- The registry to use. Should not be nullpublic BlockingHttpClient toBlocking()
toBlocking
in interface HttpClient
public <I> io.reactivex.Flowable<Event<ByteBuffer<?>>> eventStream(HttpRequest<I> request)
SseClient
Perform an HTTP request and receive data as a stream of SSE Event
objects as they become available without blocking.
The downstream Subscriber
can regulate demand via the subscription
eventStream
in interface RxSseClient
eventStream
in interface SseClient
I
- The request body typerequest
- The HttpRequest
to executePublisher
that emits an Event
with the data represented as a ByteBuffer
public <I,B> io.reactivex.Flowable<Event<B>> eventStream(HttpRequest<I> request, Argument<B> eventType)
SseClient
Perform an HTTP request and receive data as a stream of SSE Event
objects as they become available without blocking.
The downstream Subscriber
can regulate demand via the subscription
eventStream
in interface RxSseClient
eventStream
in interface SseClient
I
- The request body typeB
- The event body typerequest
- The HttpRequest
to executeeventType
- The event data typePublisher
that emits an Event
with the data represented by the eventType argumentpublic <I> io.reactivex.Flowable<ByteBuffer<?>> dataStream(HttpRequest<I> request)
StreamingHttpClient
ByteBuffer
instance.dataStream
in interface RxStreamingHttpClient
dataStream
in interface StreamingHttpClient
I
- The request body typerequest
- The requestPublisher
that emits a stream of ByteBuffer
instancespublic <I> io.reactivex.Flowable<HttpResponse<ByteBuffer<?>>> exchangeStream(HttpRequest<I> request)
StreamingHttpClient
ByteBuffer
wrapped in the HttpResponse
object
(which remains the same for each emitted item).exchangeStream
in interface RxStreamingHttpClient
exchangeStream
in interface StreamingHttpClient
I
- The request body typerequest
- The HttpRequest
Publisher
that emits a stream of ByteBuffer
instances wrapped by a HttpResponse
public <I,O> io.reactivex.Flowable<O> jsonStream(HttpRequest<I> request, Argument<O> type)
StreamingHttpClient
Perform an HTTP request and receive data as a stream of JSON objects as they become available without blocking.
The downstream Subscriber
can regulate demand via the subscription. Incoming data
is buffered.
jsonStream
in interface RxStreamingHttpClient
jsonStream
in interface StreamingHttpClient
I
- The request body typeO
- The response typerequest
- The HttpRequest
to executetype
- The type of object to convert the JSON intoPublisher
that emits the full HttpResponse
objectpublic <I> io.reactivex.Flowable<Map<String,Object>> jsonStream(HttpRequest<I> request)
StreamingHttpClient
Perform an HTTP request and receive data as a stream of JSON objects as they become available without blocking.
The downstream Subscriber
can regulate demand via the subscription
jsonStream
in interface RxStreamingHttpClient
jsonStream
in interface StreamingHttpClient
I
- The request body typerequest
- The HttpRequest
to executePublisher
that emits the full HttpResponse
objectpublic <I,O> io.reactivex.Flowable<O> jsonStream(HttpRequest<I> request, Class<O> type)
StreamingHttpClient
Perform an HTTP request and receive data as a stream of JSON objects as they become available without blocking.
The downstream Subscriber
can regulate demand via the subscription
jsonStream
in interface RxStreamingHttpClient
jsonStream
in interface StreamingHttpClient
I
- The request body typeO
- The response typerequest
- The HttpRequest
to executetype
- The type of object to convert the JSON intoPublisher
that emits the full HttpResponse
objectpublic <I,O,E> io.reactivex.Flowable<HttpResponse<O>> exchange(HttpRequest<I> request, Argument<O> bodyType, Argument<E> errorType)
HttpClient
Perform an HTTP request for the given request object emitting the full HTTP response from returned
Publisher
and converting the response body to the specified type.
This method will send a Content-Length
header and except a content length header the response and is
designed for simple non-streaming exchanges of data
By default the exchange Content-Type
is application/json, unless otherwise specified in the passed
HttpRequest
exchange
in interface HttpClient
exchange
in interface RxHttpClient
I
- The request body typeO
- The response body typeE
- The error typerequest
- The HttpRequest
to executebodyType
- The body typeerrorType
- The error typePublisher
that emits the full HttpResponse
objectpublic <T extends AutoCloseable> io.reactivex.Flowable<T> connect(Class<T> clientEndpointType, MutableHttpRequest<?> request)
WebSocketClient
connect
in interface RxWebSocketClient
connect
in interface WebSocketClient
T
- The generic typeclientEndpointType
- The endpoint type. Should be a class annotated with ClientWebSocket
request
- The original request to establish the connectionPublisher
that emits the ClientWebSocket
instancepublic <T extends AutoCloseable> io.reactivex.Flowable<T> connect(Class<T> clientEndpointType, Map<String,Object> parameters)
RxWebSocketClient
WebSocketClient.connect(Class, URI)
this method will the value declared within the ClientWebSocket
as the URI
and expand the URI with the given parameters.connect
in interface RxWebSocketClient
connect
in interface WebSocketClient
T
- The generic typeclientEndpointType
- The endpoint type. Should be a class annotated with ClientWebSocket
parameters
- The URI parameters for the endpointPublisher
that emits the ClientWebSocket
instancepublic void close()
LifeCycle
LifeCycle.stop()
.close
in interface LifeCycle<HttpClient>
close
in interface WebSocketClient
close
in interface Closeable
close
in interface AutoCloseable
@Inject protected void configure(BeanContext beanContext)
beanContext
- The bean contextprotected <I> io.reactivex.functions.Function<URI,io.reactivex.Flowable<HttpResponse<ByteBuffer<?>>>> buildExchangeStreamPublisher(HttpRequest<I> request)
I
- The input typerequest
- The requestFunction
protected <I,O> io.reactivex.functions.Function<URI,io.reactivex.Flowable<O>> buildJsonStreamPublisher(HttpRequest<?> parentRequest, HttpRequest<I> request, Argument<O> type)
I
- The input typeO
- The output typeparentRequest
- The parent requestrequest
- The requesttype
- The typeFunction
protected <I> io.reactivex.functions.Function<URI,io.reactivex.Flowable<ByteBuffer<?>>> buildDataStreamPublisher(HttpRequest<I> request)
I
- The input typerequest
- The requestFunction
protected <I> io.reactivex.Flowable<HttpResponse<Object>> buildStreamExchange(HttpRequest<?> parentRequest, HttpRequest<I> request, URI requestURI)
I
- The input typeparentRequest
- The parent requestrequest
- The requestrequestURI
- The request URIFlowable
protected <I,O,E> io.reactivex.functions.Function<URI,Publisher<? extends HttpResponse<O>>> buildExchangePublisher(HttpRequest<?> parentRequest, HttpRequest<I> request, Argument<O> bodyType, Argument<E> errorType)
I
- The input typeO
- The output typeE
- The error typeparentRequest
- The parent requestrequest
- The requestbodyType
- The body typeerrorType
- The error typeFunction
protected void closeChannelAsync(io.netty.channel.Channel channel)
channel
- The channel to close asynchronouslyprotected <I> Publisher<URI> resolveRequestURI(HttpRequest<I> request)
I
- The input typerequest
- The requestPublisher
with the resolved URIprotected URI resolveRequestURI(URI requestURI)
requestURI
- The request URIprotected Object getLoadBalancerDiscriminator()
protected io.netty.channel.ChannelFuture doConnect(HttpRequest<?> request, URI uri, @Nullable io.netty.handler.ssl.SslContext sslCtx, boolean isStream)
request
- The requesturi
- The URI to connect tosslCtx
- The SslContext instanceisStream
- Is the connection a stream connectionprotected io.netty.channel.ChannelFuture doConnect(HttpRequest<?> request, String host, int port, @Nullable io.netty.handler.ssl.SslContext sslCtx, boolean isStream)
request
- The requesthost
- The hostport
- The portsslCtx
- The SslContext instanceisStream
- Is the connection a stream connectionprotected io.netty.channel.nio.NioEventLoopGroup createEventLoopGroup(HttpClientConfiguration configuration, ThreadFactory threadFactory)
NioEventLoopGroup
for this client.configuration
- The configurationthreadFactory
- The thread factoryprotected io.netty.channel.ChannelFuture doConnect(io.netty.bootstrap.Bootstrap bootstrap, String host, int port)
bootstrap
- The bootstrap instancehost
- The hostport
- The portprotected io.netty.handler.ssl.SslContext buildSslContext(URI uriObject)
SslContext
for the given URI if necessary.uriObject
- The URISslContext
instanceprotected List<HttpClientFilter> resolveFilters(@Nullable HttpRequest<?> parentRequest, HttpRequest<?> request, URI requestURI)
parentRequest
- The parent requestrequest
- The pathrequestURI
- The URI of the requestprotected void configureProxy(io.netty.channel.ChannelPipeline pipeline, Proxy proxy)
pipeline
- The pipelineproxy
- The proxyprotected void configureProxy(io.netty.channel.ChannelPipeline pipeline, Proxy.Type proxyType, SocketAddress proxyAddress)
pipeline
- The pipelineproxyType
- The proxy typeproxyAddress
- The proxy addressprotected <I,O> Publisher<HttpResponse<O>> applyFilterToResponsePublisher(HttpRequest<?> parentRequest, HttpRequest<I> request, URI requestURI, AtomicReference<HttpRequest> requestWrapper, Publisher<HttpResponse<O>> responsePublisher)
I
- The input typeO
- The output typeparentRequest
- The parent requestrequest
- The requestrequestURI
- The URI of the requestrequestWrapper
- The request wrapperresponsePublisher
- The response publisherPublisher
for the responseprotected DefaultHttpClient.NettyRequestWriter buildNettyRequest(MutableHttpRequest request, URI requestURI, MediaType requestContentType, boolean permitsBody) throws io.netty.handler.codec.http.multipart.HttpPostRequestEncoder.ErrorDataEncoderException
request
- The requestrequestURI
- The URI of the requestrequestContentType
- The request content typepermitsBody
- Whether permits bodyDefaultHttpClient.NettyRequestWriter
HttpPostRequestEncoder.ErrorDataEncoderException
- if there is an encoder exception