public class HttpUtil
extends java.lang.Object
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 and Description |
|---|
HttpUtil() |
| Modifier and Type | Method and Description |
|---|---|
static void |
delete(java.net.URI uri,
javax.net.ssl.SSLSocketFactory socketFactory,
javax.net.ssl.HostnameVerifier verifier,
java.lang.String authorization)
Perform HTTP DELETE request using the default connect and read timeouts (60 seconds).
|
static void |
delete(java.net.URI uri,
javax.net.ssl.SSLSocketFactory socketFactory,
javax.net.ssl.HostnameVerifier verifier,
java.lang.String authorization,
int connectTimeout,
int readTimeout)
Perform HTTP DELETE request
|
static void |
delete(java.net.URI uri,
java.lang.String authorization)
Perform HTTP DELETE request using the default connect and read timeouts (60 seconds).
|
static <T> T |
doWithRetries(int retries,
long retryPauseMillis,
MetricsHandler metricsHandler,
HttpTask<T> task)
A helper method that implements logic for retrying unsuccessful HTTP requests
|
static <T> T |
get(java.net.URI uri,
javax.net.ssl.SSLSocketFactory socketFactory,
javax.net.ssl.HostnameVerifier hostnameVerifier,
java.lang.String authorization,
java.lang.Class<T> responseType)
Perform HTTP GET request and return the response in the specified type.
|
static <T> T |
get(java.net.URI uri,
javax.net.ssl.SSLSocketFactory socketFactory,
javax.net.ssl.HostnameVerifier hostnameVerifier,
java.lang.String authorization,
java.lang.Class<T> responseType,
int connectTimeout,
int readTimeout)
Perform HTTP GET request and return the response in the specified type.
|
static <T> T |
get(java.net.URI uri,
javax.net.ssl.SSLSocketFactory socketFactory,
javax.net.ssl.HostnameVerifier hostnameVerifier,
java.lang.String authorization,
java.lang.Class<T> responseType,
int connectTimeout,
int readTimeout,
boolean includeAcceptHeader)
Perform HTTP GET request and return the response in the specified type.
|
static <T> T |
get(java.net.URI uri,
javax.net.ssl.SSLSocketFactory socketFactory,
java.lang.String authorization,
java.lang.Class<T> responseType)
Perform HTTP GET request and return the response in the specified type.
|
static <T> T |
get(java.net.URI uri,
java.lang.String authorization,
java.lang.Class<T> responseType)
Perform HTTP GET request and return the response in the specified type.
|
static <T> T |
post(java.net.URI uri,
javax.net.ssl.SSLSocketFactory socketFactory,
javax.net.ssl.HostnameVerifier verifier,
java.lang.String authorization,
java.lang.String contentType,
java.lang.String body,
java.lang.Class<T> responseType)
Perform HTTP POST request and return the response in the specified type.
|
static <T> T |
post(java.net.URI uri,
javax.net.ssl.SSLSocketFactory socketFactory,
javax.net.ssl.HostnameVerifier verifier,
java.lang.String authorization,
java.lang.String contentType,
java.lang.String body,
java.lang.Class<T> responseType,
int connectTimeout,
int readTimeout,
boolean includeAcceptHeader)
Perform HTTP POST request and return the response in the specified type.
|
static <T> T |
post(java.net.URI uri,
javax.net.ssl.SSLSocketFactory socketFactory,
java.lang.String authorization,
java.lang.String contentType,
java.lang.String body,
java.lang.Class<T> responseType)
Perform HTTP POST request and return the response in the specified type.
|
static <T> T |
post(java.net.URI uri,
java.lang.String authorization,
java.lang.String contentType,
java.lang.String body,
java.lang.Class<T> responseType)
Perform HTTP POST request and return the response in the specified type.
|
static void |
put(java.net.URI uri,
javax.net.ssl.SSLSocketFactory socketFactory,
javax.net.ssl.HostnameVerifier verifier,
java.lang.String authorization,
java.lang.String contentType,
java.lang.String body)
Perform HTTP PUT request
|
static void |
put(java.net.URI uri,
javax.net.ssl.SSLSocketFactory socketFactory,
javax.net.ssl.HostnameVerifier verifier,
java.lang.String authorization,
java.lang.String contentType,
java.lang.String body,
int connectTimeout,
int readTimeout)
Perform HTTP PUT request
|
static void |
put(java.net.URI uri,
javax.net.ssl.SSLSocketFactory socketFactory,
java.lang.String authorization,
java.lang.String contentType,
java.lang.String body)
Perform HTTP PUT request
|
static void |
put(java.net.URI uri,
java.lang.String authorization,
java.lang.String contentType,
java.lang.String body)
Perform HTTP PUT request
|
static <T> T |
request(java.net.URI uri,
javax.net.ssl.SSLSocketFactory socketFactory,
javax.net.ssl.HostnameVerifier hostnameVerifier,
java.lang.String authorization,
java.lang.String contentType,
java.lang.String body,
java.lang.Class<T> responseType)
Perform an HTTP request, auto-detecting a method, and using the default connect and read timeouts (60 seconds)..
|
static <T> T |
request(java.net.URI uri,
java.lang.String method,
javax.net.ssl.SSLSocketFactory socketFactory,
javax.net.ssl.HostnameVerifier hostnameVerifier,
java.lang.String authorization,
java.lang.String contentType,
java.lang.String body,
java.lang.Class<T> responseType)
Perform an HTTP request using the default connect and read timeouts (60 seconds).
|
static <T> T |
request(java.net.URI uri,
java.lang.String method,
javax.net.ssl.SSLSocketFactory socketFactory,
javax.net.ssl.HostnameVerifier hostnameVerifier,
java.lang.String authorization,
java.lang.String contentType,
java.lang.String body,
java.lang.Class<T> responseType,
int connectTimeout,
int readTimeout,
boolean includeAcceptHeader)
Perform an HTTP request using the default connect and read timeouts (60 seconds).
|
public static <T> T doWithRetries(int retries,
long retryPauseMillis,
MetricsHandler metricsHandler,
HttpTask<T> task)
throws java.util.concurrent.ExecutionException
T - Generic type of the result type of the HttpTaskretries - 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 retriesjava.util.concurrent.ExecutionException - The exception thrown if the last retry still failed, wrapping the cause exceptionpublic static <T> T get(java.net.URI uri,
java.lang.String authorization,
java.lang.Class<T> responseType)
throws java.io.IOException
T - Generic type of the responseTypeuri - The target urlauthorization - The Authorization header valueresponseType - The type to which to convert the response (String or one of the Jackson Mapper types)responseType.java.io.IOException - A connection, timeout, or network exception that occurs while performing the requestHttpException - A runtime exception when an HTTP response status signals a failed requestpublic static <T> T get(java.net.URI uri,
javax.net.ssl.SSLSocketFactory socketFactory,
java.lang.String authorization,
java.lang.Class<T> responseType)
throws java.io.IOException
T - Generic type of the responseTypeuri - 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)responseType.java.io.IOException - A connection, timeout, or network exception that occurs while performing the requestHttpException - A runtime exception when an HTTP response status signals a failed requestpublic static <T> T get(java.net.URI uri,
javax.net.ssl.SSLSocketFactory socketFactory,
javax.net.ssl.HostnameVerifier hostnameVerifier,
java.lang.String authorization,
java.lang.Class<T> responseType)
throws java.io.IOException
T - Generic type of the responseTypeuri - 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)responseType.java.io.IOException - A connection, timeout, or network exception that occurs while performing the requestHttpException - A runtime exception when an HTTP response status signals a failed requestpublic static <T> T get(java.net.URI uri,
javax.net.ssl.SSLSocketFactory socketFactory,
javax.net.ssl.HostnameVerifier hostnameVerifier,
java.lang.String authorization,
java.lang.Class<T> responseType,
int connectTimeout,
int readTimeout)
throws java.io.IOException
T - Generic type of the responseTypeuri - 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 secondsresponseType.java.io.IOException - A connection, timeout, or network exception that occurs while performing the requestHttpException - A runtime exception when an HTTP response status signals a failed requestpublic static <T> T get(java.net.URI uri,
javax.net.ssl.SSLSocketFactory socketFactory,
javax.net.ssl.HostnameVerifier hostnameVerifier,
java.lang.String authorization,
java.lang.Class<T> responseType,
int connectTimeout,
int readTimeout,
boolean includeAcceptHeader)
throws java.io.IOException
T - Generic type of the responseTypeuri - 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 if Accept application/json is sent to the remote server.responseType.java.io.IOException - A connection, timeout, or network exception that occurs while performing the requestHttpException - A runtime exception when an HTTP response status signals a failed requestpublic static <T> T post(java.net.URI uri,
java.lang.String authorization,
java.lang.String contentType,
java.lang.String body,
java.lang.Class<T> responseType)
throws java.io.IOException
T - Generic type of the responseTypeuri - 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)responseType.java.io.IOException - A connection, timeout, or network exception that occurs while performing the requestHttpException - A runtime exception when an HTTP response status signals a failed requestpublic static <T> T post(java.net.URI uri,
javax.net.ssl.SSLSocketFactory socketFactory,
java.lang.String authorization,
java.lang.String contentType,
java.lang.String body,
java.lang.Class<T> responseType)
throws java.io.IOException
T - Generic type of the responseTypeuri - 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)responseType.java.io.IOException - A connection, timeout, or network exception that occurs while performing the requestHttpException - A runtime exception when an HTTP response status signals a failed requestpublic static <T> T post(java.net.URI uri,
javax.net.ssl.SSLSocketFactory socketFactory,
javax.net.ssl.HostnameVerifier verifier,
java.lang.String authorization,
java.lang.String contentType,
java.lang.String body,
java.lang.Class<T> responseType)
throws java.io.IOException
T - Generic type of the responseTypeuri - 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)responseType.java.io.IOException - A connection, timeout, or network exception that occurs while performing the requestHttpException - A runtime exception when an HTTP response status signals a failed requestpublic static <T> T post(java.net.URI uri,
javax.net.ssl.SSLSocketFactory socketFactory,
javax.net.ssl.HostnameVerifier verifier,
java.lang.String authorization,
java.lang.String contentType,
java.lang.String body,
java.lang.Class<T> responseType,
int connectTimeout,
int readTimeout,
boolean includeAcceptHeader)
throws java.io.IOException
T - Generic type of the responseTypeuri - 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 if Accept: application/json is sent to the remote server.responseType.java.io.IOException - A connection, timeout, or network exception that occurs while performing the requestHttpException - A runtime exception when an HTTP response status signals a failed requestpublic static void put(java.net.URI uri,
java.lang.String authorization,
java.lang.String contentType,
java.lang.String body)
throws java.io.IOException
uri - The target urlauthorization - The Authorization header valuecontentType - MIME type of the request bodybody - The request bodyjava.io.IOException - A connection, timeout, or network exception that occurs while performing the requestHttpException - A runtime exception when an HTTP response status signals a failed requestpublic static void put(java.net.URI uri,
javax.net.ssl.SSLSocketFactory socketFactory,
java.lang.String authorization,
java.lang.String contentType,
java.lang.String body)
throws java.io.IOException
uri - The target urlsocketFactory - Socket factory to use with https:// urlauthorization - The Authorization header valuecontentType - MIME type of the request bodybody - The request bodyjava.io.IOException - A connection, timeout, or network exception that occurs while performing the requestHttpException - A runtime exception when an HTTP response status signals a failed requestpublic static void put(java.net.URI uri,
javax.net.ssl.SSLSocketFactory socketFactory,
javax.net.ssl.HostnameVerifier verifier,
java.lang.String authorization,
java.lang.String contentType,
java.lang.String body)
throws java.io.IOException
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 bodyjava.io.IOException - A connection, timeout, or network exception that occurs while performing the requestHttpException - A runtime exception when an HTTP response status signals a failed requestpublic static void put(java.net.URI uri,
javax.net.ssl.SSLSocketFactory socketFactory,
javax.net.ssl.HostnameVerifier verifier,
java.lang.String authorization,
java.lang.String contentType,
java.lang.String body,
int connectTimeout,
int readTimeout)
throws java.io.IOException
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 secondsjava.io.IOException - A connection, timeout, or network exception that occurs while performing the requestHttpException - A runtime exception when an HTTP response status signals a failed requestpublic static void delete(java.net.URI uri,
java.lang.String authorization)
throws java.io.IOException
uri - The target urlauthorization - The Authorization header valuejava.io.IOException - A connection, timeout, or network exception that occurs while performing the requestHttpException - A runtime exception when an HTTP response status signals a failed requestpublic static void delete(java.net.URI uri,
javax.net.ssl.SSLSocketFactory socketFactory,
javax.net.ssl.HostnameVerifier verifier,
java.lang.String authorization)
throws java.io.IOException
uri - The target urlsocketFactory - Socket factory to use with https:// urlverifier - HostnameVerifier to use with https:// urlauthorization - The Authorization header valuejava.io.IOException - A connection, timeout, or network exception that occurs while performing the requestHttpException - A runtime exception when an HTTP response status signals a failed requestpublic static void delete(java.net.URI uri,
javax.net.ssl.SSLSocketFactory socketFactory,
javax.net.ssl.HostnameVerifier verifier,
java.lang.String authorization,
int connectTimeout,
int readTimeout)
throws java.io.IOException
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 secondsjava.io.IOException - A connection, timeout, or network exception that occurs while performing the requestHttpException - A runtime exception when an HTTP response status signals a failed requestpublic static <T> T request(java.net.URI uri,
javax.net.ssl.SSLSocketFactory socketFactory,
javax.net.ssl.HostnameVerifier hostnameVerifier,
java.lang.String authorization,
java.lang.String contentType,
java.lang.String body,
java.lang.Class<T> responseType)
throws java.io.IOException
T - Generic type of the responseTypeuri - 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)responseType.java.io.IOException - A connection, timeout, or network exception that occurs while performing the requestHttpException - A runtime exception when an HTTP response status signals a failed requestpublic static <T> T request(java.net.URI uri,
java.lang.String method,
javax.net.ssl.SSLSocketFactory socketFactory,
javax.net.ssl.HostnameVerifier hostnameVerifier,
java.lang.String authorization,
java.lang.String contentType,
java.lang.String body,
java.lang.Class<T> responseType)
throws java.io.IOException
T - Generic type of the responseTypeuri - 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)responseType.java.io.IOException - A connection, timeout, or network exception that occurs while performing the requestHttpException - A runtime exception when an HTTP response status signals a failed requestpublic static <T> T request(java.net.URI uri,
java.lang.String method,
javax.net.ssl.SSLSocketFactory socketFactory,
javax.net.ssl.HostnameVerifier hostnameVerifier,
java.lang.String authorization,
java.lang.String contentType,
java.lang.String body,
java.lang.Class<T> responseType,
int connectTimeout,
int readTimeout,
boolean includeAcceptHeader)
throws java.io.IOException
T - Generic type of the responseTypeuri - 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 if Accept: application/json is sent to the remote server.responseType.java.io.IOException - A connection, timeout, or network exception that occurs while performing the requestHttpException - A runtime exception when an HTTP response status signals a failed requestCopyright © 2024. All rights reserved.