Module methanol

Class Methanol


  • public class Methanol
    extends java.net.http.HttpClient
    An HttpClient with interceptors, request decoration, HTTP caching and reactive extensions.

    In addition to implementing the HttpClient API, this class allows to:

    • Specify a base URI.
    • Specify a default request timeout.
    • Specify a read timeout.
    • Add a set of default HTTP headers for inclusion in requests if absent.
    • Add an HTTP caching layer.
    • Transparent response decompression.
    • Intercept requests and responses going through this client.
    • Specify an AdapterCodec to automatically convert to/from request/response bodies.
    • Get Publisher<HttpResponse<T>> for asynchronous requests.

    A Methanol client relies on a standard HttpClient instance for sending requests, referred to as its backend. You can obtain builders for Methanol using either newBuilder() or newBuilder(HttpClient). The latter takes a prebuilt backend, while the former allows configuring a backend to be newly created each time Methanol.BaseBuilder.build() is invoked. Note that HttpCaches are not usable with a prebuilt backend.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Methanol.BaseBuilder<B extends Methanol.BaseBuilder<B>>
      A base Methanol builder allowing to set the non-standard properties.
      static class  Methanol.Builder
      A builder of Methanol instances.
      static interface  Methanol.Interceptor
      An object that intercepts requests before being sent and responses before being returned.
      static class  Methanol.WithClientBuilder
      A builder for Methanol instances with a pre-specified backend HttpClient.
      • Nested classes/interfaces inherited from class java.net.http.HttpClient

        java.net.http.HttpClient.Redirect, java.net.http.HttpClient.Version
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      java.util.Optional<AdapterCodec> adapterCodec()  
      java.util.Optional<java.net.Authenticator> authenticator()  
      boolean autoAcceptEncoding()
      Returns this client's auto Accept-Encoding setting.
      java.util.List<Methanol.Interceptor> backendInterceptors()
      Returns an immutable list of this client's backend interceptors.
      java.util.Optional<java.net.URI> baseUri()
      Returns this client's base URI.
      java.util.Optional<HttpCache> cache()
      Returns this client's cache.
      java.util.List<HttpCache> caches()  
      java.util.Optional<java.time.Duration> connectTimeout()  
      java.util.Optional<java.net.CookieHandler> cookieHandler()  
      static Methanol create()
      Creates a default Methanol instance.
      java.net.http.HttpHeaders defaultHeaders()
      Returns this client's default headers.
      <T> java.util.concurrent.Flow.Publisher<java.net.http.HttpResponse<T>> exchange​(java.net.http.HttpRequest request, java.net.http.HttpResponse.BodyHandler<T> bodyHandler)
      Returns a Publisher for the HttpResponse<T> resulting from asynchronously sending the given request.
      <T> java.util.concurrent.Flow.Publisher<java.net.http.HttpResponse<T>> exchange​(java.net.http.HttpRequest request, java.net.http.HttpResponse.BodyHandler<T> bodyHandler, java.util.function.Function<java.net.http.HttpRequest,​@Nullable java.net.http.HttpResponse.BodyHandler<T>> pushPromiseMapper)
      Returns a Publisher for the sequence of HttpResponse<T> resulting from asynchronously sending the given request along with accepting incoming push promises using the given Function.
      java.util.Optional<java.util.concurrent.Executor> executor()  
      java.net.http.HttpClient.Redirect followRedirects()  
      java.util.Optional<java.time.Duration> headersTimeout()
      Returns the headers timeout.
      java.util.List<Methanol.Interceptor> interceptors()
      Returns an immutable list of this client's interceptors.
      static Methanol.Builder newBuilder()
      Returns a new Methanol.Builder.
      static Methanol.WithClientBuilder newBuilder​(java.net.http.HttpClient backend)
      Returns a new Methanol.WithClientBuilder with a prebuilt backend.
      java.net.http.WebSocket.Builder newWebSocketBuilder()  
      java.util.List<Methanol.Interceptor> postDecorationInterceptors()
      Deprecated.
      java.util.Optional<java.net.ProxySelector> proxy()  
      java.util.Optional<java.time.Duration> readTimeout()
      Returns the read timeout used for every request.
      java.util.Optional<java.time.Duration> requestTimeout()
      Returns the default request timeout used when no timeout is explicitly set on an HttpRequest.
      <T> java.net.http.HttpResponse<T> send​(java.net.http.HttpRequest request, TypeRef<T> typeRef)
      Sends the given request and converts the response body into an object of the given type.
      <T> java.net.http.HttpResponse<T> send​(java.net.http.HttpRequest request, java.lang.Class<T> type)
      Sends the given request and converts the response body into an object of the given type.
      <T> java.net.http.HttpResponse<T> send​(java.net.http.HttpRequest request, java.net.http.HttpResponse.BodyHandler<T> bodyHandler)  
      <T> java.util.concurrent.CompletableFuture<java.net.http.HttpResponse<T>> sendAsync​(java.net.http.HttpRequest request, TypeRef<T> typeRef)
      Asynchronously sends the given request and converts the response body into an object of the given type.
      <T> java.util.concurrent.CompletableFuture<java.net.http.HttpResponse<T>> sendAsync​(java.net.http.HttpRequest request, java.lang.Class<T> type)
      Asynchronously sends the given request and converts the response body into an object of the given type.
      <T> java.util.concurrent.CompletableFuture<java.net.http.HttpResponse<T>> sendAsync​(java.net.http.HttpRequest request, java.net.http.HttpResponse.BodyHandler<T> bodyHandler)  
      <T> java.util.concurrent.CompletableFuture<java.net.http.HttpResponse<T>> sendAsync​(java.net.http.HttpRequest request, java.net.http.HttpResponse.BodyHandler<T> bodyHandler, @Nullable java.net.http.HttpResponse.PushPromiseHandler<T> pushPromiseHandler)  
      javax.net.ssl.SSLContext sslContext()  
      javax.net.ssl.SSLParameters sslParameters()  
      java.net.http.HttpClient underlyingClient()
      Returns the underlying HttpClient used for sending requests.
      java.util.Optional<java.lang.String> userAgent()
      Returns this client's User-Agent.
      java.net.http.HttpClient.Version version()  
      • Methods inherited from class java.net.http.HttpClient

        newHttpClient
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • exchange

        public <T> java.util.concurrent.Flow.Publisher<java.net.http.HttpResponse<T>> exchange​(java.net.http.HttpRequest request,
                                                                                               java.net.http.HttpResponse.BodyHandler<T> bodyHandler)
        Returns a Publisher for the HttpResponse<T> resulting from asynchronously sending the given request.
      • exchange

        public <T> java.util.concurrent.Flow.Publisher<java.net.http.HttpResponse<T>> exchange​(java.net.http.HttpRequest request,
                                                                                               java.net.http.HttpResponse.BodyHandler<T> bodyHandler,
                                                                                               java.util.function.Function<java.net.http.HttpRequest,​@Nullable java.net.http.HttpResponse.BodyHandler<T>> pushPromiseMapper)
        Returns a Publisher for the sequence of HttpResponse<T> resulting from asynchronously sending the given request along with accepting incoming push promises using the given Function. The function accepts an incoming push promise by returning a non-null BodyHandler<T> for handling the pushed response body. If a null handler is returned, the push promise will be rejected.

        Note that the published sequence has no specific order, and hence the main response is not guaranteed to be the first and may appear anywhere in the sequence.

      • underlyingClient

        public java.net.http.HttpClient underlyingClient()
        Returns the underlying HttpClient used for sending requests.
      • userAgent

        public java.util.Optional<java.lang.String> userAgent()
        Returns this client's User-Agent.
      • baseUri

        public java.util.Optional<java.net.URI> baseUri()
        Returns this client's base URI.
      • requestTimeout

        public java.util.Optional<java.time.Duration> requestTimeout()
        Returns the default request timeout used when no timeout is explicitly set on an HttpRequest.
      • headersTimeout

        public java.util.Optional<java.time.Duration> headersTimeout()
        Returns the headers timeout.
      • readTimeout

        public java.util.Optional<java.time.Duration> readTimeout()
        Returns the read timeout used for every request.
      • postDecorationInterceptors

        @Deprecated(since="1.5.0")
        public java.util.List<Methanol.Interceptor> postDecorationInterceptors()
        Deprecated.
        Returns the list of interceptors invoked after request decoration.
      • defaultHeaders

        public java.net.http.HttpHeaders defaultHeaders()
        Returns this client's default headers.
      • autoAcceptEncoding

        public boolean autoAcceptEncoding()
        Returns this client's auto Accept-Encoding setting.
      • cache

        public java.util.Optional<HttpCache> cache()
        Returns this client's cache.
      • caches

        public java.util.List<HttpCache> caches()
      • adapterCodec

        public java.util.Optional<AdapterCodec> adapterCodec()
      • cookieHandler

        public java.util.Optional<java.net.CookieHandler> cookieHandler()
        Specified by:
        cookieHandler in class java.net.http.HttpClient
      • connectTimeout

        public java.util.Optional<java.time.Duration> connectTimeout()
        Specified by:
        connectTimeout in class java.net.http.HttpClient
      • followRedirects

        public java.net.http.HttpClient.Redirect followRedirects()
        Specified by:
        followRedirects in class java.net.http.HttpClient
      • proxy

        public java.util.Optional<java.net.ProxySelector> proxy()
        Specified by:
        proxy in class java.net.http.HttpClient
      • sslContext

        public javax.net.ssl.SSLContext sslContext()
        Specified by:
        sslContext in class java.net.http.HttpClient
      • sslParameters

        public javax.net.ssl.SSLParameters sslParameters()
        Specified by:
        sslParameters in class java.net.http.HttpClient
      • authenticator

        public java.util.Optional<java.net.Authenticator> authenticator()
        Specified by:
        authenticator in class java.net.http.HttpClient
      • version

        public java.net.http.HttpClient.Version version()
        Specified by:
        version in class java.net.http.HttpClient
      • executor

        public java.util.Optional<java.util.concurrent.Executor> executor()
        Specified by:
        executor in class java.net.http.HttpClient
      • newWebSocketBuilder

        public java.net.http.WebSocket.Builder newWebSocketBuilder()
        Overrides:
        newWebSocketBuilder in class java.net.http.HttpClient
      • send

        public <T> java.net.http.HttpResponse<T> send​(java.net.http.HttpRequest request,
                                                      java.net.http.HttpResponse.BodyHandler<T> bodyHandler)
                                               throws java.io.IOException,
                                                      java.lang.InterruptedException
        Specified by:
        send in class java.net.http.HttpClient
        Throws:
        java.io.IOException
        java.lang.InterruptedException
      • sendAsync

        public <T> java.util.concurrent.CompletableFuture<java.net.http.HttpResponse<T>> sendAsync​(java.net.http.HttpRequest request,
                                                                                                   java.net.http.HttpResponse.BodyHandler<T> bodyHandler)
        Specified by:
        sendAsync in class java.net.http.HttpClient
      • sendAsync

        public <T> java.util.concurrent.CompletableFuture<java.net.http.HttpResponse<T>> sendAsync​(java.net.http.HttpRequest request,
                                                                                                   java.net.http.HttpResponse.BodyHandler<T> bodyHandler,
                                                                                                   @Nullable java.net.http.HttpResponse.PushPromiseHandler<T> pushPromiseHandler)
        Specified by:
        sendAsync in class java.net.http.HttpClient
      • send

        public <T> java.net.http.HttpResponse<T> send​(java.net.http.HttpRequest request,
                                                      java.lang.Class<T> type)
                                               throws java.io.IOException,
                                                      java.lang.InterruptedException
        Sends the given request and converts the response body into an object of the given type.
        Throws:
        java.io.IOException
        java.lang.InterruptedException
      • sendAsync

        public <T> java.util.concurrent.CompletableFuture<java.net.http.HttpResponse<T>> sendAsync​(java.net.http.HttpRequest request,
                                                                                                   java.lang.Class<T> type)
        Asynchronously sends the given request and converts the response body into an object of the given type.
      • send

        public <T> java.net.http.HttpResponse<T> send​(java.net.http.HttpRequest request,
                                                      TypeRef<T> typeRef)
                                               throws java.io.IOException,
                                                      java.lang.InterruptedException
        Sends the given request and converts the response body into an object of the given type.
        Throws:
        java.io.IOException
        java.lang.InterruptedException
      • sendAsync

        public <T> java.util.concurrent.CompletableFuture<java.net.http.HttpResponse<T>> sendAsync​(java.net.http.HttpRequest request,
                                                                                                   TypeRef<T> typeRef)
        Asynchronously sends the given request and converts the response body into an object of the given type.
      • create

        public static Methanol create()
        Creates a default Methanol instance.