Class DefaultHttpClient

java.lang.Object
io.micronaut.http.client.netty.DefaultHttpClient
All Implemented Interfaces:
LifeCycle<HttpClient>, HttpClient, ProxyHttpClient, RawHttpClient, SseClient, StreamingHttpClient, WebSocketClient, Closeable, AutoCloseable

Default implementation of the HttpClient interface based on Netty.
Since:
1.0
Author:
Graeme Rocher
  • Field Details

    • mediaTypeCodecRegistry

      protected MediaTypeCodecRegistry mediaTypeCodecRegistry
    • byteBufferFactory

      protected final ByteBufferFactory<io.netty.buffer.ByteBufAllocator,io.netty.buffer.ByteBuf> byteBufferFactory
  • Constructor Details

    • DefaultHttpClient

      @Deprecated public DefaultHttpClient(@Nullable LoadBalancer loadBalancer, @NonNull HttpClientConfiguration configuration, @Nullable String contextPath, @Nullable ThreadFactory threadFactory, ClientSslBuilder nettyClientSslBuilder, @NonNull MediaTypeCodecRegistry codecRegistry, @NonNull MessageBodyHandlerRegistry handlerRegistry, @Nullable AnnotationMetadataResolver annotationMetadataResolver, ConversionService conversionService, HttpClientFilter... filters)
      Deprecated.
      Please go through the builder() instead. If you need access to properties that are not public in the builder, make them public in core and document their usage.
      Construct a client for the given arguments.
      Parameters:
      loadBalancer - The LoadBalancer to use for selecting servers
      configuration - The HttpClientConfiguration object
      contextPath - The base URI to prepend to request uris
      threadFactory - The thread factory to use for client threads
      nettyClientSslBuilder - The SSL builder
      codecRegistry - The MediaTypeCodecRegistry to use for encoding and decoding objects
      handlerRegistry - The handler registry for encoding and decoding
      annotationMetadataResolver - The annotation metadata resolver
      conversionService - The conversion service
      filters - The filters to use
    • DefaultHttpClient

      @Deprecated public DefaultHttpClient(@Nullable LoadBalancer loadBalancer, @Nullable HttpVersionSelection explicitHttpVersion, @NonNull HttpClientConfiguration configuration, @Nullable String contextPath, @NonNull HttpClientFilterResolver<ClientFilterResolutionContext> filterResolver, @NonNull List<HttpFilterResolver.FilterEntry> clientFilterEntries, @Nullable ThreadFactory threadFactory, @NonNull ClientSslBuilder nettyClientSslBuilder, @NonNull MediaTypeCodecRegistry codecRegistry, @NonNull MessageBodyHandlerRegistry handlerRegistry, @NonNull WebSocketBeanRegistry webSocketBeanRegistry, @NonNull RequestBinderRegistry requestBinderRegistry, @Nullable io.netty.channel.EventLoopGroup eventLoopGroup, @NonNull io.netty.channel.ChannelFactory<? extends io.netty.channel.socket.SocketChannel> socketChannelFactory, @NonNull io.netty.channel.ChannelFactory<? extends io.netty.channel.socket.DatagramChannel> udpChannelFactory, NettyClientCustomizer clientCustomizer, @Nullable String informationalServiceId, ConversionService conversionService, @Nullable io.netty.resolver.AddressResolverGroup<?> resolverGroup)
      Deprecated.
      Please go through the builder() instead. If you need access to properties that are not public in the builder, make them public in core and document their usage.
      Construct a client for the given arguments.
      Parameters:
      loadBalancer - The LoadBalancer to use for selecting servers
      explicitHttpVersion - The HTTP version to use. Can be null and defaults to HttpVersion.HTTP_1_1
      configuration - The HttpClientConfiguration object
      contextPath - The base URI to prepend to request uris
      filterResolver - The http client filter resolver
      clientFilterEntries - The client filter entries
      threadFactory - The thread factory to use for client threads
      nettyClientSslBuilder - The SSL builder
      codecRegistry - The MediaTypeCodecRegistry to use for encoding and decoding objects
      handlerRegistry - The handler registry for encoding and decoding
      webSocketBeanRegistry - The websocket bean registry
      requestBinderRegistry - The request binder registry
      eventLoopGroup - The event loop group to use
      socketChannelFactory - The socket channel factory
      udpChannelFactory - The UDP channel factory
      clientCustomizer - The pipeline customizer
      informationalServiceId - Optional service ID that will be passed to exceptions created by this client
      conversionService - The conversion service
      resolverGroup - Optional predefined resolver group
    • DefaultHttpClient

      @Deprecated public DefaultHttpClient(@Nullable URI uri)
      Deprecated.
      Please go through the builder() instead.
      Parameters:
      uri - The URL
    • DefaultHttpClient

      @Deprecated public DefaultHttpClient()
      Deprecated.
      Please go through the builder() instead.
    • DefaultHttpClient

      @Deprecated public DefaultHttpClient(@Nullable URI uri, @NonNull HttpClientConfiguration configuration)
      Deprecated.
      Please go through the builder() instead.
      Parameters:
      uri - The URI
      configuration - The HttpClientConfiguration object
    • DefaultHttpClient

      @Deprecated public DefaultHttpClient(@Nullable URI uri, @NonNull HttpClientConfiguration configuration, @NonNull ClientSslBuilder clientSslBuilder)
      Deprecated.
      Please go through the builder() instead.
      Constructor used by micronaut-oracle-cloud.
      Parameters:
      uri - The URI
      configuration - The HttpClientConfiguration object
      clientSslBuilder - The SSL builder
    • DefaultHttpClient

      @Deprecated public DefaultHttpClient(@Nullable LoadBalancer loadBalancer, HttpClientConfiguration configuration)
      Deprecated.
      Please go through the builder() instead. If you need access to properties that are not public in the builder, make them public in core and document their usage.
      Parameters:
      loadBalancer - The LoadBalancer to use for selecting servers
      configuration - The HttpClientConfiguration object
  • Method Details

    • builder

      public static @NonNull DefaultHttpClientBuilder builder()
      Create a new builder for a DefaultHttpClient.
      Returns:
      The builder
      Since:
      4.7.0
    • getConfiguration

      public HttpClientConfiguration getConfiguration()
      Returns:
      The configuration used by this client
    • getLog

      public org.slf4j.Logger getLog()
      Returns:
      The client-specific logger name
    • connectionManager

      public ConnectionManager connectionManager()
      Access to the connection manager, for micronaut-oracle-cloud.
      Returns:
      The connection manager of this client
    • start

      public HttpClient start()
      Description copied from interface: LifeCycle
      Starts the lifecyle component.
      Specified by:
      start in interface LifeCycle<HttpClient>
      Returns:
      This lifecycle component
    • isRunning

      public boolean isRunning()
      Specified by:
      isRunning in interface LifeCycle<HttpClient>
      Returns:
      Whether the component is running
    • stop

      public HttpClient stop()
      Description copied from interface: LifeCycle
      Stops the life cycle component.
      Specified by:
      stop in interface LifeCycle<HttpClient>
      Returns:
      This lifecycle component
    • getMediaTypeCodecRegistry

      @Deprecated public MediaTypeCodecRegistry getMediaTypeCodecRegistry()
      Deprecated.
      Use body handlers instead
      Returns:
      The MediaTypeCodecRegistry used by this client
    • setMediaTypeCodecRegistry

      @Deprecated(forRemoval=true) public void setMediaTypeCodecRegistry(MediaTypeCodecRegistry mediaTypeCodecRegistry)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Use builder instead
      Sets the MediaTypeCodecRegistry used by this client.
      Parameters:
      mediaTypeCodecRegistry - The registry to use. Should not be null
    • getHandlerRegistry

      public final @NonNull MessageBodyHandlerRegistry getHandlerRegistry()
      Get the handler registry for this client.
      Returns:
      The handler registry
    • setHandlerRegistry

      @Deprecated(forRemoval=true) public final void setHandlerRegistry(@NonNull MessageBodyHandlerRegistry handlerRegistry)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Use builder instead
      Set the handler registry for this client.
      Parameters:
      handlerRegistry - The handler registry
    • toBlocking

      public BlockingHttpClient toBlocking()
      Specified by:
      toBlocking in interface HttpClient
      Returns:
      A blocking HTTP client suitable for testing and non-production scenarios.
    • eventStream

      public <I> Publisher<Event<ByteBuffer<?>>> eventStream(@NonNull HttpRequest<I> request)
      Description copied from interface: 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

      Specified by:
      eventStream in interface SseClient
      Type Parameters:
      I - The request body type
      Parameters:
      request - The HttpRequest to execute
      Returns:
      A Publisher that emits an Event with the data represented as a ByteBuffer
    • eventStream

      public <I, B> Publisher<Event<B>> eventStream(@NonNull HttpRequest<I> request, @NonNull Argument<B> eventType)
      Description copied from interface: 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

      Specified by:
      eventStream in interface SseClient
      Type Parameters:
      I - The request body type
      B - The event body type
      Parameters:
      request - The HttpRequest to execute
      eventType - The event data type
      Returns:
      A Publisher that emits an Event with the data represented by the eventType argument
    • eventStream

      public <I, B> Publisher<Event<B>> eventStream(@NonNull HttpRequest<I> request, @NonNull Argument<B> eventType, @NonNull Argument<?> errorType)
      Description copied from interface: 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

      Specified by:
      eventStream in interface SseClient
      Type Parameters:
      I - The request body type
      B - The event body type
      Parameters:
      request - The HttpRequest to execute
      eventType - The event data type
      errorType - The type that the response body should be coerced into if the server responds with an error
      Returns:
      A Publisher that emits an Event with the data represented by the eventType argument
    • dataStream

      public <I> Publisher<ByteBuffer<?>> dataStream(@NonNull HttpRequest<I> request)
      Description copied from interface: StreamingHttpClient
      Request a stream of data where each emitted item is a ByteBuffer instance.
      Specified by:
      dataStream in interface StreamingHttpClient
      Type Parameters:
      I - The request body type
      Parameters:
      request - The request
      Returns:
      A Publisher that emits a stream of ByteBuffer instances
    • dataStream

      public <I> Publisher<ByteBuffer<?>> dataStream(@NonNull HttpRequest<I> request, @NonNull Argument<?> errorType)
      Description copied from interface: StreamingHttpClient
      Request a stream of data where each emitted item is a ByteBuffer instance.
      Specified by:
      dataStream in interface StreamingHttpClient
      Type Parameters:
      I - The request body type
      Parameters:
      request - The request
      errorType - The type that the response body should be coerced into if the server responds with an error
      Returns:
      A Publisher that emits a stream of ByteBuffer instances
    • exchangeStream

      public <I> Publisher<HttpResponse<ByteBuffer<?>>> exchangeStream(@NonNull HttpRequest<I> request)
      Description copied from interface: StreamingHttpClient
      Requests a stream data where each emitted item is a ByteBuffer wrapped in the HttpResponse object (which remains the same for each emitted item).
      Specified by:
      exchangeStream in interface StreamingHttpClient
      Type Parameters:
      I - The request body type
      Parameters:
      request - The HttpRequest
      Returns:
      A Publisher that emits a stream of ByteBuffer instances wrapped by a HttpResponse
    • exchangeStream

      public <I> Publisher<HttpResponse<ByteBuffer<?>>> exchangeStream(@NonNull HttpRequest<I> request, @NonNull Argument<?> errorType)
      Description copied from interface: StreamingHttpClient
      Requests a stream data where each emitted item is a ByteBuffer wrapped in the HttpResponse object (which remains the same for each emitted item).
      Specified by:
      exchangeStream in interface StreamingHttpClient
      Type Parameters:
      I - The request body type
      Parameters:
      request - The HttpRequest
      errorType - The type that the response body should be coerced into if the server responds with an error
      Returns:
      A Publisher that emits a stream of ByteBuffer instances wrapped by a HttpResponse
    • jsonStream

      public <I, O> Publisher<O> jsonStream(@NonNull HttpRequest<I> request, @NonNull Argument<O> type)
      Description copied from interface: 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.

      Specified by:
      jsonStream in interface StreamingHttpClient
      Type Parameters:
      I - The request body type
      O - The response type
      Parameters:
      request - The HttpRequest to execute
      type - The type of object to convert the JSON into
      Returns:
      A Publisher that emits the full HttpResponse object
    • jsonStream

      public <I, O> Publisher<O> jsonStream(@NonNull HttpRequest<I> request, @NonNull Argument<O> type, @NonNull Argument<?> errorType)
      Description copied from interface: 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.

      Specified by:
      jsonStream in interface StreamingHttpClient
      Type Parameters:
      I - The request body type
      O - The response type
      Parameters:
      request - The HttpRequest to execute
      type - The type of object to convert the JSON into
      errorType - The type that the response body should be coerced into if the server responds with an error
      Returns:
      A Publisher that emits the full HttpResponse object
    • jsonStream

      public <I> Publisher<Map<String,Object>> jsonStream(@NonNull HttpRequest<I> request)
      Description copied from interface: 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

      Specified by:
      jsonStream in interface StreamingHttpClient
      Type Parameters:
      I - The request body type
      Parameters:
      request - The HttpRequest to execute
      Returns:
      A Publisher that emits the full HttpResponse object
    • jsonStream

      public <I, O> Publisher<O> jsonStream(@NonNull HttpRequest<I> request, @NonNull Class<O> type)
      Description copied from interface: 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

      Specified by:
      jsonStream in interface StreamingHttpClient
      Type Parameters:
      I - The request body type
      O - The response type
      Parameters:
      request - The HttpRequest to execute
      type - The type of object to convert the JSON into
      Returns:
      A Publisher that emits the full HttpResponse object
    • exchange

      public <I, O, E> Publisher<HttpResponse<O>> exchange(@NonNull HttpRequest<I> request, @NonNull Argument<O> bodyType, @NonNull Argument<E> errorType)
      Description copied from interface: 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

      Specified by:
      exchange in interface HttpClient
      Type Parameters:
      I - The request body type
      O - The response body type
      E - The error type
      Parameters:
      request - The HttpRequest to execute
      bodyType - The body type
      errorType - The error type
      Returns:
      A Publisher that emits the full HttpResponse object
    • retrieve

      public <I, O, E> Publisher<O> retrieve(HttpRequest<I> request, Argument<O> bodyType, Argument<E> errorType)
      Description copied from interface: 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.
      Specified by:
      retrieve in interface HttpClient
      Type Parameters:
      I - The request body type
      O - The response body type
      E - The error type
      Parameters:
      request - The HttpRequest to execute
      bodyType - The body type
      errorType - The error type
      Returns:
      A Publisher that emits a result of the given type
    • connect

      public <T extends AutoCloseable> Publisher<T> connect(Class<T> clientEndpointType, MutableHttpRequest<?> request)
      Description copied from interface: WebSocketClient
      Connect the given client endpoint type to the URI over WebSocket.
      Specified by:
      connect in interface WebSocketClient
      Type Parameters:
      T - The generic type
      Parameters:
      clientEndpointType - The endpoint type. Should be a class annotated with ClientWebSocket
      request - The original request to establish the connection
      Returns:
      A Publisher that emits the ClientWebSocket instance
    • connect

      public <T extends AutoCloseable> Publisher<T> connect(Class<T> clientEndpointType, Map<String,Object> parameters)
      Description copied from interface: WebSocketClient
      Connect the given client endpoint type. Unlike WebSocketClient.connect(Class, URI) this method will the value declared within the ClientWebSocket as the URI and expand the URI with the given parameters.
      Specified by:
      connect in interface WebSocketClient
      Type Parameters:
      T - The generic type
      Parameters:
      clientEndpointType - The endpoint type. Should be a class annotated with ClientWebSocket
      parameters - The URI parameters for the endpoint
      Returns:
      A Publisher that emits the ClientWebSocket instance
    • close

      public void close()
      Description copied from interface: LifeCycle
      Delegates to LifeCycle.stop().
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in interface LifeCycle<HttpClient>
      Specified by:
      close in interface WebSocketClient
    • proxy

      public Publisher<MutableHttpResponse<?>> proxy(@NonNull HttpRequest<?> request)
      Description copied from interface: ProxyHttpClient
      Proxy the given request and emit the response. This method expects the full absolute URL to be included in the request. If a relative URL is specified then the method will try to resolve the URI for the current server otherwise an exception will be thrown.
      Specified by:
      proxy in interface ProxyHttpClient
      Parameters:
      request - The request
      Returns:
      A publisher that emits the response.
    • proxy

      public Publisher<MutableHttpResponse<?>> proxy(@NonNull HttpRequest<?> request, @NonNull ProxyRequestOptions options)
      Description copied from interface: ProxyHttpClient
      Proxy the given request and emit the response. This method expects the full absolute URL to be included in the request. If a relative URL is specified then the method will try to resolve the URI for the current server otherwise an exception will be thrown.
      Specified by:
      proxy in interface ProxyHttpClient
      Parameters:
      request - The request
      options - Further options for the proxy request
      Returns:
      A publisher that emits the response.
    • resolveRequestURI

      protected <I> ExecutionFlow<URI> resolveRequestURI(HttpRequest<I> request)
      Type Parameters:
      I - The input type
      Parameters:
      request - The request
      Returns:
      A Publisher with the resolved URI
    • resolveRequestURI

      protected <I> ExecutionFlow<URI> resolveRequestURI(HttpRequest<I> request, boolean includeContextPath)
      Type Parameters:
      I - The input type
      Parameters:
      request - The request
      includeContextPath - Whether to prepend the client context path
      Returns:
      A Publisher with the resolved URI
    • resolveRedirectURI

      protected <I> ExecutionFlow<URI> resolveRedirectURI(HttpRequest<?> parentRequest, HttpRequest<I> request)
      Type Parameters:
      I - The input type
      Parameters:
      parentRequest - The parent request
      request - The redirect location request
      Returns:
      A Publisher with the resolved URI
    • getLoadBalancerDiscriminator

      protected Object getLoadBalancerDiscriminator()
      Returns:
      The discriminator to use when selecting a server for the purposes of load balancing (defaults to null)
    • exchange

      public Publisher<? extends HttpResponse<?>> exchange(HttpRequest<?> request, @Nullable CloseableByteBody requestBody, @Nullable Thread blockedThread)
      Description copied from interface: RawHttpClient
      Send a raw request.
      Specified by:
      exchange in interface RawHttpClient
      Parameters:
      request - The request metadata (method, URI, headers). The body of this object is ignored
      requestBody - The request body bytes. null is equivalent to an empty body. The ownership of the body immediately transfers to the client, i.e. the client will always call CloseableByteBody.close() on the body even if there is an error before the request is sent.
      blockedThread - The thread that is blocked waiting for this request. This is used for deadlock detection. Optional parameter.
      Returns:
      A mono that will contain the response to this request. This response will usually be a ByteBodyHttpResponse, unless a filter replaced it.