Class HttpUtil

java.lang.Object
io.strimzi.kafka.oauth.common.HttpUtil

public class HttpUtil extends Object
A helper class that performs all network calls using java.net.HttpURLConnection.

If application uses many concurrent threads initiating many Kafka sessions in parallel, consider setting 'http.maxConnections' system property to value closer to the number of parallel sessions.

This value controls the size of internal connection pool per destination in JDK's java.net.HttpURLConnection implementation.

See: https://docs.oracle.com/javase/8/docs/technotes/guides/net/http-keepalive.html

By default the connect timeout and read timeout are set to 60 seconds. Use system properties oauth.connect.timeout.seconds and oauth.read.timeout.seconds, or corresponding env variables to set custom timeouts in seconds.

  • Constructor Details

    • HttpUtil

      public HttpUtil()
  • Method Details

    • doWithRetries

      public static <T> T doWithRetries(int retries, long retryPauseMillis, MetricsHandler metricsHandler, HttpTask<T> task) throws ExecutionException
      A helper method that implements logic for retrying unsuccessful HTTP requests
      Type Parameters:
      T - Generic type of the result type of the HttpTask
      Parameters:
      retries - a maximum number of retries to attempt
      retryPauseMillis - a pause between two consecutive retries in millis
      metricsHandler - a metrics handler to track request times and failures
      task - the task to run with retries
      Returns:
      The result of the successfully completed task
      Throws:
      ExecutionException - The exception thrown if the last retry still failed, wrapping the cause exception
    • get

      public static <T> T get(URI uri, String authorization, Class<T> responseType) throws IOException
      Perform HTTP GET request and return the response in the specified type.
      Type Parameters:
      T - Generic type of the responseType
      Parameters:
      uri - The target url
      authorization - The Authorization header value
      responseType - The type to which to convert the response (String or one of the Jackson Mapper types)
      Returns:
      The response as specified by the responseType.
      Throws:
      IOException - A connection, timeout, or network exception that occurs while performing the request
      HttpException - A runtime exception when an HTTP response status signals a failed request
    • get

      public static <T> T get(URI uri, SSLSocketFactory socketFactory, String authorization, Class<T> responseType) throws IOException
      Perform HTTP GET request and return the response in the specified type.
      Type Parameters:
      T - Generic type of the responseType
      Parameters:
      uri - The target url
      socketFactory - Socket factory to use with https:// url
      authorization - The Authorization header value
      responseType - The type to which to convert the response (String or one of the Jackson Mapper types)
      Returns:
      The response as specified by the responseType.
      Throws:
      IOException - A connection, timeout, or network exception that occurs while performing the request
      HttpException - A runtime exception when an HTTP response status signals a failed request
    • get

      public static <T> T get(URI uri, SSLSocketFactory socketFactory, HostnameVerifier hostnameVerifier, String authorization, Class<T> responseType) throws IOException
      Perform HTTP GET request and return the response in the specified type.
      Type Parameters:
      T - Generic type of the responseType
      Parameters:
      uri - The target url
      socketFactory - Socket factory to use with https:// url
      hostnameVerifier - HostnameVerifier to use with https:// url
      authorization - The Authorization header value
      responseType - The type to which to convert the response (String or one of the Jackson Mapper types)
      Returns:
      The response as specified by the responseType.
      Throws:
      IOException - A connection, timeout, or network exception that occurs while performing the request
      HttpException - A runtime exception when an HTTP response status signals a failed request
    • get

      public static <T> T get(URI uri, SSLSocketFactory socketFactory, HostnameVerifier hostnameVerifier, String authorization, Class<T> responseType, int connectTimeout, int readTimeout) throws IOException
      Perform HTTP GET request and return the response in the specified type.
      Type Parameters:
      T - Generic type of the responseType
      Parameters:
      uri - The target url
      socketFactory - Socket factory to use with https:// url
      hostnameVerifier - HostnameVerifier to use with https:// url
      authorization - The Authorization header value
      responseType - The type to which to convert the response (String or one of the Jackson Mapper types)
      connectTimeout - Connect timeout in seconds
      readTimeout - Read timeout in seconds
      Returns:
      The response as specified by the responseType.
      Throws:
      IOException - A connection, timeout, or network exception that occurs while performing the request
      HttpException - A runtime exception when an HTTP response status signals a failed request
    • get

      public static <T> T get(URI uri, SSLSocketFactory socketFactory, HostnameVerifier hostnameVerifier, String authorization, Class<T> responseType, int connectTimeout, int readTimeout, boolean includeAcceptHeader) throws IOException
      Perform HTTP GET request and return the response in the specified type.
      Type Parameters:
      T - Generic type of the responseType
      Parameters:
      uri - The target url
      socketFactory - Socket factory to use with https:// url
      hostnameVerifier - HostnameVerifier to use with https:// url
      authorization - The Authorization header value
      responseType - The type to which to convert the response (String or one of the Jackson Mapper types)
      connectTimeout - Connect timeout in seconds
      readTimeout - Read timeout in seconds
      includeAcceptHeader - Determines if Accept application/json is sent to the remote server.
      Returns:
      The response as specified by the responseType.
      Throws:
      IOException - A connection, timeout, or network exception that occurs while performing the request
      HttpException - A runtime exception when an HTTP response status signals a failed request
    • post

      public static <T> T post(URI uri, String authorization, String contentType, String body, Class<T> responseType) throws IOException
      Perform HTTP POST request and return the response in the specified type.
      Type Parameters:
      T - Generic type of the responseType
      Parameters:
      uri - The target url
      authorization - The Authorization header value
      contentType - MIME type of the request body
      body - The request body
      responseType - The type to which to convert the response (String or one of the Jackson Mapper types)
      Returns:
      The response as specified by the responseType.
      Throws:
      IOException - A connection, timeout, or network exception that occurs while performing the request
      HttpException - A runtime exception when an HTTP response status signals a failed request
    • post

      public static <T> T post(URI uri, SSLSocketFactory socketFactory, String authorization, String contentType, String body, Class<T> responseType) throws IOException
      Perform HTTP POST request and return the response in the specified type.
      Type Parameters:
      T - Generic type of the responseType
      Parameters:
      uri - The target url
      socketFactory - Socket factory to use with https:// url
      authorization - The Authorization header value
      contentType - MIME type of the request body
      body - The request body
      responseType - The type to which to convert the response (String or one of the Jackson Mapper types)
      Returns:
      The response as specified by the responseType.
      Throws:
      IOException - A connection, timeout, or network exception that occurs while performing the request
      HttpException - A runtime exception when an HTTP response status signals a failed request
    • post

      public static <T> T post(URI uri, SSLSocketFactory socketFactory, HostnameVerifier verifier, String authorization, String contentType, String body, Class<T> responseType) throws IOException
      Perform HTTP POST request and return the response in the specified type.
      Type Parameters:
      T - Generic type of the responseType
      Parameters:
      uri - The target url
      socketFactory - Socket factory to use with https:// url
      verifier - HostnameVerifier to use with https:// url
      authorization - The Authorization header value
      contentType - MIME type of the request body
      body - The request body
      responseType - The type to which to convert the response (String or one of the Jackson Mapper types)
      Returns:
      The response as specified by the responseType.
      Throws:
      IOException - A connection, timeout, or network exception that occurs while performing the request
      HttpException - A runtime exception when an HTTP response status signals a failed request
    • post

      public static <T> T post(URI uri, SSLSocketFactory socketFactory, HostnameVerifier verifier, String authorization, String contentType, String body, Class<T> responseType, int connectTimeout, int readTimeout, boolean includeAcceptHeader) throws IOException
      Perform HTTP POST request and return the response in the specified type.
      Type Parameters:
      T - Generic type of the responseType
      Parameters:
      uri - The target url
      socketFactory - Socket factory to use with https:// url
      verifier - HostnameVerifier to use with https:// url
      authorization - The Authorization header value
      contentType - MIME type of the request body
      body - The request body
      responseType - The type to which to convert the response (String or one of the Jackson Mapper types)
      connectTimeout - Connect timeout in seconds
      readTimeout - Read timeout in seconds
      includeAcceptHeader - Determines if Accept: application/json is sent to the remote server.
      Returns:
      The response as specified by the responseType.
      Throws:
      IOException - A connection, timeout, or network exception that occurs while performing the request
      HttpException - A runtime exception when an HTTP response status signals a failed request
    • put

      public static void put(URI uri, String authorization, String contentType, String body) throws IOException
      Perform HTTP PUT request
      Parameters:
      uri - The target url
      authorization - The Authorization header value
      contentType - MIME type of the request body
      body - The request body
      Throws:
      IOException - A connection, timeout, or network exception that occurs while performing the request
      HttpException - A runtime exception when an HTTP response status signals a failed request
    • put

      public static void put(URI uri, SSLSocketFactory socketFactory, String authorization, String contentType, String body) throws IOException
      Perform HTTP PUT request
      Parameters:
      uri - The target url
      socketFactory - Socket factory to use with https:// url
      authorization - The Authorization header value
      contentType - MIME type of the request body
      body - The request body
      Throws:
      IOException - A connection, timeout, or network exception that occurs while performing the request
      HttpException - A runtime exception when an HTTP response status signals a failed request
    • put

      public static void put(URI uri, SSLSocketFactory socketFactory, HostnameVerifier verifier, String authorization, String contentType, String body) throws IOException
      Perform HTTP PUT request
      Parameters:
      uri - The target url
      socketFactory - Socket factory to use with https:// url
      verifier - HostnameVerifier to use with https:// url
      authorization - The Authorization header value
      contentType - MIME type of the request body
      body - The request body
      Throws:
      IOException - A connection, timeout, or network exception that occurs while performing the request
      HttpException - A runtime exception when an HTTP response status signals a failed request
    • put

      public static void put(URI uri, SSLSocketFactory socketFactory, HostnameVerifier verifier, String authorization, String contentType, String body, int connectTimeout, int readTimeout) throws IOException
      Perform HTTP PUT request
      Parameters:
      uri - The target url
      socketFactory - Socket factory to use with https:// url
      verifier - HostnameVerifier to use with https:// url
      authorization - The Authorization header value
      contentType - MIME type of the request body
      body - The request body
      connectTimeout - Connect timeout in seconds
      readTimeout - Read timeout in seconds
      Throws:
      IOException - A connection, timeout, or network exception that occurs while performing the request
      HttpException - A runtime exception when an HTTP response status signals a failed request
    • delete

      public static void delete(URI uri, String authorization) throws IOException
      Perform HTTP DELETE request using the default connect and read timeouts (60 seconds).
      Parameters:
      uri - The target url
      authorization - The Authorization header value
      Throws:
      IOException - A connection, timeout, or network exception that occurs while performing the request
      HttpException - A runtime exception when an HTTP response status signals a failed request
    • delete

      public static void delete(URI uri, SSLSocketFactory socketFactory, HostnameVerifier verifier, String authorization) throws IOException
      Perform HTTP DELETE request using the default connect and read timeouts (60 seconds).
      Parameters:
      uri - The target url
      socketFactory - Socket factory to use with https:// url
      verifier - HostnameVerifier to use with https:// url
      authorization - The Authorization header value
      Throws:
      IOException - A connection, timeout, or network exception that occurs while performing the request
      HttpException - A runtime exception when an HTTP response status signals a failed request
    • delete

      public static void delete(URI uri, SSLSocketFactory socketFactory, HostnameVerifier verifier, String authorization, int connectTimeout, int readTimeout) throws IOException
      Perform HTTP DELETE request
      Parameters:
      uri - The target url
      socketFactory - Socket factory to use with https:// url
      verifier - HostnameVerifier to use with https:// url
      authorization - The Authorization header value
      connectTimeout - Connect timeout in seconds
      readTimeout - Read timeout in seconds
      Throws:
      IOException - A connection, timeout, or network exception that occurs while performing the request
      HttpException - A runtime exception when an HTTP response status signals a failed request
    • request

      public static <T> T request(URI uri, SSLSocketFactory socketFactory, HostnameVerifier hostnameVerifier, String authorization, String contentType, String body, Class<T> responseType) throws IOException
      Perform an HTTP request, auto-detecting a method, and using the default connect and read timeouts (60 seconds).. If body is null the HTTP method GET is used. If responseType is null the HTTP method PUT is used. Otherwise, the HTTP method POST is used.
      Type Parameters:
      T - Generic type of the responseType
      Parameters:
      uri - The target url
      socketFactory - Socket factory to use with https:// url
      hostnameVerifier - HostnameVerifier to use with https:// url
      authorization - The Authorization header value
      contentType - MIME type of the request body
      body - The request body
      responseType - The type to which to convert the response (String or one of the Jackson Mapper types)
      Returns:
      The response as specified by the responseType.
      Throws:
      IOException - A connection, timeout, or network exception that occurs while performing the request
      HttpException - A runtime exception when an HTTP response status signals a failed request
    • request

      public static <T> T request(URI uri, String method, SSLSocketFactory socketFactory, HostnameVerifier hostnameVerifier, String authorization, String contentType, String body, Class<T> responseType) throws IOException
      Perform an HTTP request using the default connect and read timeouts (60 seconds).
      Type Parameters:
      T - Generic type of the responseType
      Parameters:
      uri - The target url
      method - The HTTP request method
      socketFactory - Socket factory to use with https:// url
      hostnameVerifier - HostnameVerifier to use with https:// url
      authorization - The Authorization header value
      contentType - MIME type of the request body
      body - The request body
      responseType - The type to which to convert the response (String or one of the Jackson Mapper types)
      Returns:
      The response as specified by the responseType.
      Throws:
      IOException - A connection, timeout, or network exception that occurs while performing the request
      HttpException - A runtime exception when an HTTP response status signals a failed request
    • request

      public static <T> T request(URI uri, String method, SSLSocketFactory socketFactory, HostnameVerifier hostnameVerifier, String authorization, String contentType, String body, Class<T> responseType, int connectTimeout, int readTimeout, boolean includeAcceptHeader) throws IOException
      Perform an HTTP request using the default connect and read timeouts (60 seconds).
      Type Parameters:
      T - Generic type of the responseType
      Parameters:
      uri - The target url
      method - The HTTP request method
      socketFactory - Socket factory to use with https:// url
      hostnameVerifier - HostnameVerifier to use with https:// url
      authorization - The Authorization header value
      contentType - MIME type of the request body
      body - The request body
      responseType - The type to which to convert the response (String or one of the Jackson Mapper types)
      connectTimeout - Connect timeout in seconds
      readTimeout - Read timeout in seconds
      includeAcceptHeader - Determines if Accept: application/json is sent to the remote server.
      Returns:
      The response as specified by the responseType.
      Throws:
      IOException - A connection, timeout, or network exception that occurs while performing the request
      HttpException - A runtime exception when an HTTP response status signals a failed request