Package io.strimzi.kafka.oauth.common
Class HttpUtil
java.lang.Object
io.strimzi.kafka.oauth.common.HttpUtil
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidPerform HTTP DELETE request using the default connect and read timeouts (60 seconds).static voiddelete(URI uri, SSLSocketFactory socketFactory, HostnameVerifier verifier, String authorization) Perform HTTP DELETE request using the default connect and read timeouts (60 seconds).static voiddelete(URI uri, SSLSocketFactory socketFactory, HostnameVerifier verifier, String authorization, int connectTimeout, int readTimeout) Perform HTTP DELETE requeststatic <T> TdoWithRetries(int retries, long retryPauseMillis, MetricsHandler metricsHandler, HttpTask<T> task) A helper method that implements logic for retrying unsuccessful HTTP requestsstatic <T> TPerform HTTP GET request and return the response in the specified type.static <T> Tget(URI uri, SSLSocketFactory socketFactory, String authorization, Class<T> responseType) Perform HTTP GET request and return the response in the specified type.static <T> Tget(URI uri, SSLSocketFactory socketFactory, HostnameVerifier hostnameVerifier, String authorization, Class<T> responseType) Perform HTTP GET request and return the response in the specified type.static <T> Tget(URI uri, SSLSocketFactory socketFactory, HostnameVerifier hostnameVerifier, String authorization, Class<T> responseType, int connectTimeout, int readTimeout) Perform HTTP GET request and return the response in the specified type.static <T> Tget(URI uri, SSLSocketFactory socketFactory, HostnameVerifier hostnameVerifier, String authorization, Class<T> responseType, int connectTimeout, int readTimeout, boolean includeAcceptHeader) Perform HTTP GET request and return the response in the specified type.static <T> TPerform HTTP POST request and return the response in the specified type.static <T> Tpost(URI uri, SSLSocketFactory socketFactory, String authorization, String contentType, String body, Class<T> responseType) Perform HTTP POST request and return the response in the specified type.static <T> Tpost(URI uri, SSLSocketFactory socketFactory, HostnameVerifier verifier, String authorization, String contentType, String body, Class<T> responseType) Perform HTTP POST request and return the response in the specified type.static <T> Tpost(URI uri, SSLSocketFactory socketFactory, HostnameVerifier verifier, String authorization, String contentType, String body, Class<T> responseType, int connectTimeout, int readTimeout, boolean includeAcceptHeader) Perform HTTP POST request and return the response in the specified type.static voidPerform HTTP PUT requeststatic voidPerform HTTP PUT requeststatic voidput(URI uri, SSLSocketFactory socketFactory, HostnameVerifier verifier, String authorization, String contentType, String body) Perform HTTP PUT requeststatic voidput(URI uri, SSLSocketFactory socketFactory, HostnameVerifier verifier, String authorization, String contentType, String body, int connectTimeout, int readTimeout) Perform HTTP PUT requeststatic <T> Trequest(URI uri, String method, SSLSocketFactory socketFactory, HostnameVerifier hostnameVerifier, String authorization, String contentType, String body, Class<T> responseType) Perform an HTTP request using the default connect and read timeouts (60 seconds).static <T> Trequest(URI uri, String method, SSLSocketFactory socketFactory, HostnameVerifier hostnameVerifier, String authorization, String contentType, String body, Class<T> responseType, int connectTimeout, int readTimeout, boolean includeAcceptHeader) Perform an HTTP request using the default connect and read timeouts (60 seconds).static <T> Trequest(URI uri, SSLSocketFactory socketFactory, HostnameVerifier hostnameVerifier, String authorization, String contentType, String body, Class<T> responseType) Perform an HTTP request, auto-detecting a method, and using the default connect and read timeouts (60 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 attemptretryPauseMillis- a pause between two consecutive retries in millismetricsHandler- a metrics handler to track request times and failurestask- 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
Perform HTTP GET request and return the response in the specified type.- Type Parameters:
T- Generic type of theresponseType- Parameters:
uri- The target urlauthorization- The Authorization header valueresponseType- 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 requestHttpException- 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 theresponseType- Parameters:
uri- The target urlsocketFactory- Socket factory to use with https:// urlauthorization- The Authorization header valueresponseType- 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 requestHttpException- 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 theresponseType- Parameters:
uri- The target urlsocketFactory- Socket factory to use with https:// urlhostnameVerifier- HostnameVerifier to use with https:// urlauthorization- The Authorization header valueresponseType- 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 requestHttpException- 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 theresponseType- Parameters:
uri- The target urlsocketFactory- Socket factory to use with https:// urlhostnameVerifier- HostnameVerifier to use with https:// urlauthorization- The Authorization header valueresponseType- The type to which to convert the response (String or one of the Jackson Mapper types)connectTimeout- Connect timeout in secondsreadTimeout- 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 requestHttpException- 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 theresponseType- Parameters:
uri- The target urlsocketFactory- Socket factory to use with https:// urlhostnameVerifier- HostnameVerifier to use with https:// urlauthorization- The Authorization header valueresponseType- The type to which to convert the response (String or one of the Jackson Mapper types)connectTimeout- Connect timeout in secondsreadTimeout- Read timeout in secondsincludeAcceptHeader- Determines ifAccept application/jsonis 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 requestHttpException- 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 theresponseType- Parameters:
uri- The target urlauthorization- The Authorization header valuecontentType- MIME type of the request bodybody- The request bodyresponseType- 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 requestHttpException- 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 theresponseType- Parameters:
uri- The target urlsocketFactory- Socket factory to use with https:// urlauthorization- The Authorization header valuecontentType- MIME type of the request bodybody- The request bodyresponseType- 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 requestHttpException- 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 theresponseType- Parameters:
uri- The target urlsocketFactory- Socket factory to use with https:// urlverifier- HostnameVerifier to use with https:// urlauthorization- The Authorization header valuecontentType- MIME type of the request bodybody- The request bodyresponseType- 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 requestHttpException- 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 theresponseType- Parameters:
uri- The target urlsocketFactory- Socket factory to use with https:// urlverifier- HostnameVerifier to use with https:// urlauthorization- The Authorization header valuecontentType- MIME type of the request bodybody- The request bodyresponseType- The type to which to convert the response (String or one of the Jackson Mapper types)connectTimeout- Connect timeout in secondsreadTimeout- Read timeout in secondsincludeAcceptHeader- Determines ifAccept: application/jsonis 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 requestHttpException- 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 urlauthorization- The Authorization header valuecontentType- MIME type of the request bodybody- The request body- Throws:
IOException- A connection, timeout, or network exception that occurs while performing the requestHttpException- 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 urlsocketFactory- Socket factory to use with https:// urlauthorization- The Authorization header valuecontentType- MIME type of the request bodybody- The request body- Throws:
IOException- A connection, timeout, or network exception that occurs while performing the requestHttpException- 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 urlsocketFactory- Socket factory to use with https:// urlverifier- HostnameVerifier to use with https:// urlauthorization- The Authorization header valuecontentType- MIME type of the request bodybody- The request body- Throws:
IOException- A connection, timeout, or network exception that occurs while performing the requestHttpException- 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 urlsocketFactory- Socket factory to use with https:// urlverifier- HostnameVerifier to use with https:// urlauthorization- The Authorization header valuecontentType- MIME type of the request bodybody- The request bodyconnectTimeout- Connect timeout in secondsreadTimeout- Read timeout in seconds- Throws:
IOException- A connection, timeout, or network exception that occurs while performing the requestHttpException- A runtime exception when an HTTP response status signals a failed request
-
delete
Perform HTTP DELETE request using the default connect and read timeouts (60 seconds).- Parameters:
uri- The target urlauthorization- The Authorization header value- Throws:
IOException- A connection, timeout, or network exception that occurs while performing the requestHttpException- 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 urlsocketFactory- Socket factory to use with https:// urlverifier- HostnameVerifier to use with https:// urlauthorization- The Authorization header value- Throws:
IOException- A connection, timeout, or network exception that occurs while performing the requestHttpException- 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 urlsocketFactory- Socket factory to use with https:// urlverifier- HostnameVerifier to use with https:// urlauthorization- The Authorization header valueconnectTimeout- Connect timeout in secondsreadTimeout- Read timeout in seconds- Throws:
IOException- A connection, timeout, or network exception that occurs while performing the requestHttpException- 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 theresponseType- Parameters:
uri- The target urlsocketFactory- Socket factory to use with https:// urlhostnameVerifier- HostnameVerifier to use with https:// urlauthorization- The Authorization header valuecontentType- MIME type of the request bodybody- The request bodyresponseType- 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 requestHttpException- 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 theresponseType- Parameters:
uri- The target urlmethod- The HTTP request methodsocketFactory- Socket factory to use with https:// urlhostnameVerifier- HostnameVerifier to use with https:// urlauthorization- The Authorization header valuecontentType- MIME type of the request bodybody- The request bodyresponseType- 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 requestHttpException- 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 theresponseType- Parameters:
uri- The target urlmethod- The HTTP request methodsocketFactory- Socket factory to use with https:// urlhostnameVerifier- HostnameVerifier to use with https:// urlauthorization- The Authorization header valuecontentType- MIME type of the request bodybody- The request bodyresponseType- The type to which to convert the response (String or one of the Jackson Mapper types)connectTimeout- Connect timeout in secondsreadTimeout- Read timeout in secondsincludeAcceptHeader- Determines ifAccept: application/jsonis 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 requestHttpException- A runtime exception when an HTTP response status signals a failed request
-