Package jetbrains.buildServer.util
Class HTTPRequestBuilder
- java.lang.Object
-
- jetbrains.buildServer.util.HTTPRequestBuilder
-
- Direct Known Subclasses:
AgentHttpRequestBuilder
public class HTTPRequestBuilder extends Object
A helper class providing easy-to-use builder and support for https- Author:
- Andrey Titov on 06.09.16
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
HTTPRequestBuilder.ApacheClient43RequestHandler
static class
HTTPRequestBuilder.DelegatingRequestHandler
static interface
HTTPRequestBuilder.ExceptionConsumer
static class
HTTPRequestBuilder.LegacyApacheCommonsRequestHandler
class
HTTPRequestBuilder.Request
static interface
HTTPRequestBuilder.RequestHandler
Component for processingHTTPRequestBuilder.Request
.static interface
HTTPRequestBuilder.Response
Representation of http(s) response.static interface
HTTPRequestBuilder.ResponseConsumer
-
Constructor Summary
Constructors Constructor Description HTTPRequestBuilder(String url)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description HTTPRequestBuilder
addParameters(Pair<String,String>... pairs)
Add parameters in a request.HTTPRequestBuilder
addParameters(List<Pair<String,String>> pairs)
Add parameters in a request.HTTPRequestBuilder
allowNonSecureConnection(boolean state)
Set strategy for host https verification.HTTPRequestBuilder
apply(Consumer<HTTPRequestBuilder> consumer)
Applies modifications to request inside provided consumerHTTPRequestBuilder.Request
build()
withExecutorService(java.util.concurrent.ExecutorService)
will be ignored.AsyncRequest
buildAsync()
Provides a way of asynchronously handling a request.HTTPRequestBuilder
onErrorResponse(BiConsumer<Integer,String> consumer)
Set consumer for http(s) errors (not status code 200).HTTPRequestBuilder
onErrorResponse(HTTPRequestBuilder.ResponseConsumer dataConsumer)
Set consumer for http(s) errors (not 200 status code).HTTPRequestBuilder
onException(Consumer<Exception> exceptionConsumer)
Set consumer for occurred exceptions while call http(s) methods.HTTPRequestBuilder
onException(HTTPRequestBuilder.ExceptionConsumer exceptionConsumer)
Set consumer for occurred exceptions while call http(s) methods.HTTPRequestBuilder
onSuccess(HTTPRequestBuilder.ResponseConsumer dataConsumer)
Set consumer for success (status code 200) call http(s) methods.static HTTPRequestBuilder
request(String url)
HTTPRequestBuilder
withAuthenticateHeader(SimpleCredentials authenticateHeader)
Set credentials for an authentication header.HTTPRequestBuilder
withConnectionTimeoutMs(int connectionTimeoutMs)
Sets the timeout for establishing tcp connection with the server.HTTPRequestBuilder
withCookie(org.apache.commons.httpclient.Cookie cookie)
Add cookie in a request.HTTPRequestBuilder
withCookies(org.apache.commons.httpclient.Cookie... cookies)
Add cookies in a request.HTTPRequestBuilder
withCredentials(SimpleCredentials credentials)
Set credentials for a target host.HTTPRequestBuilder
withCustomRedirectStrategy(CustomRedirectStrategy redirectStrategy)
Set custom redirect strategy.HTTPRequestBuilder
withData(byte[] bytes)
Set payload bytes entity for POST calls.HTTPRequestBuilder
withData(List<Pair<String,String>> pairs)
Add request parameters.HTTPRequestBuilder
withData(Map<String,String> params)
Add request parameters.HTTPRequestBuilder
withData(EntityProducer entityProducer)
HTTPRequestBuilder
withDomainCheck(boolean state)
HTTPRequestBuilder
withEncodingInterceptor(boolean encodingInterceptor)
Settrue
if you want to use encoding interceptor for request and response.HTTPRequestBuilder
withExecutorService(ExecutorService executorService)
Set the executor service that will be used.HTTPRequestBuilder
withHeader(String name, String value)
Add header in a request.HTTPRequestBuilder
withHeader(Map<String,String> headers)
Add headers in a request.HTTPRequestBuilder
withMaxConnections(int maxConnections)
Set the maximum number of connections allowedHTTPRequestBuilder
withMethod(String method)
Method type for call.HTTPRequestBuilder
withMethod(HttpMethod method)
Method type for call.HTTPRequestBuilder
withPostStringEntity(String data, String mimeType, Charset charset)
Set payload string entity for POST calls.HTTPRequestBuilder
withPreemptiveAuthentication(boolean state)
Settrue
if you want to use preemptive authentication strategy.HTTPRequestBuilder
withProxyCredentials(SimpleCredentials proxyCredentials)
Add proxy credentials in a request.HTTPRequestBuilder
withProxyHost(URI proxyUri)
Add proxy host in a request.HTTPRequestBuilder
withRedirectStrategy(RedirectStrategy redirectStrategyType)
Set redirect strategy.HTTPRequestBuilder
withRetryCount(int retryCount)
Set how many times to retry;0
means no retriesHTTPRequestBuilder
withTimeout(int timeoutMS)
Set the timeout for the requestHTTPRequestBuilder
withTrustStore(KeyStore trustStore)
Set custom trust store with ssl certificates.HTTPRequestBuilder
withUserAgent(String userAgent)
Set User-Agent header.
-
-
-
Constructor Detail
-
HTTPRequestBuilder
public HTTPRequestBuilder(@NotNull String url) throws URISyntaxException
- Throws:
URISyntaxException
-
-
Method Detail
-
onErrorResponse
public HTTPRequestBuilder onErrorResponse(@NotNull BiConsumer<Integer,String> consumer)
Set consumer for http(s) errors (not status code 200). Specified consumer will be call with status code and content string. Only used withbuildAsync()
-
onErrorResponse
public HTTPRequestBuilder onErrorResponse(@NotNull HTTPRequestBuilder.ResponseConsumer dataConsumer)
Set consumer for http(s) errors (not 200 status code). Specified consumer will be call withHTTPRequestBuilder.Response
object. Only used withbuildAsync()
-
onSuccess
public HTTPRequestBuilder onSuccess(@NotNull HTTPRequestBuilder.ResponseConsumer dataConsumer)
Set consumer for success (status code 200) call http(s) methods. Specified consumer will be call withHTTPRequestBuilder.Response
object. Only used withbuildAsync()
-
onException
public HTTPRequestBuilder onException(@NotNull Consumer<Exception> exceptionConsumer)
Set consumer for occurred exceptions while call http(s) methods. Specified consumer will be call with exception object. Only used withbuildAsync()
-
onException
public HTTPRequestBuilder onException(@NotNull HTTPRequestBuilder.ExceptionConsumer exceptionConsumer)
Set consumer for occurred exceptions while call http(s) methods. Specified consumer will be call with exception object. Only used withbuildAsync()
-
allowNonSecureConnection
public HTTPRequestBuilder allowNonSecureConnection(boolean state)
Set strategy for host https verification. If setfalse
then STRICT mode will be used and you have to call over https scheme.By default used only strict connections.
-
withDomainCheck
public HTTPRequestBuilder withDomainCheck(boolean state)
-
withPreemptiveAuthentication
public HTTPRequestBuilder withPreemptiveAuthentication(boolean state)
Settrue
if you want to use preemptive authentication strategy.
-
withEncodingInterceptor
public HTTPRequestBuilder withEncodingInterceptor(boolean encodingInterceptor)
Settrue
if you want to use encoding interceptor for request and response.
-
withRetryCount
public HTTPRequestBuilder withRetryCount(int retryCount)
Set how many times to retry;0
means no retries
-
addParameters
public HTTPRequestBuilder addParameters(@NotNull Pair<String,String>... pairs)
Add parameters in a request.
-
addParameters
public HTTPRequestBuilder addParameters(@NotNull List<Pair<String,String>> pairs)
Add parameters in a request.
-
withHeader
public HTTPRequestBuilder withHeader(@NotNull String name, @NotNull String value)
Add header in a request.
-
withHeader
public HTTPRequestBuilder withHeader(@Nullable Map<String,String> headers)
Add headers in a request.
-
withCookie
public HTTPRequestBuilder withCookie(@NotNull org.apache.commons.httpclient.Cookie cookie)
Add cookie in a request.
-
withCookies
public HTTPRequestBuilder withCookies(@Nullable org.apache.commons.httpclient.Cookie... cookies)
Add cookies in a request.
-
withProxyHost
public HTTPRequestBuilder withProxyHost(@NotNull URI proxyUri)
Add proxy host in a request.
-
withProxyCredentials
public HTTPRequestBuilder withProxyCredentials(@NotNull SimpleCredentials proxyCredentials)
Add proxy credentials in a request.
-
withPostStringEntity
public HTTPRequestBuilder withPostStringEntity(@Nullable String data, @Nullable String mimeType, @Nullable Charset charset)
Set payload string entity for POST calls.
-
withData
public HTTPRequestBuilder withData(@Nullable byte[] bytes)
Set payload bytes entity for POST calls.
-
withData
public HTTPRequestBuilder withData(@NotNull EntityProducer entityProducer)
-
withData
public HTTPRequestBuilder withData(@NotNull List<Pair<String,String>> pairs)
Add request parameters.
-
withData
public HTTPRequestBuilder withData(@NotNull Map<String,String> params)
Add request parameters.
-
withMethod
public HTTPRequestBuilder withMethod(@NotNull String method)
Method type for call. OnlyPOST
andGET
is allowed.
-
withMethod
public HTTPRequestBuilder withMethod(@NotNull HttpMethod method)
Method type for call.- See Also:
HttpMethod
-
withCredentials
public HTTPRequestBuilder withCredentials(@Nullable SimpleCredentials credentials)
Set credentials for a target host.
-
withAuthenticateHeader
public HTTPRequestBuilder withAuthenticateHeader(@Nullable SimpleCredentials authenticateHeader)
Set credentials for an authentication header.
-
withTrustStore
public HTTPRequestBuilder withTrustStore(@Nullable KeyStore trustStore)
Set custom trust store with ssl certificates.
-
withRedirectStrategy
public HTTPRequestBuilder withRedirectStrategy(@Nullable RedirectStrategy redirectStrategyType)
Set redirect strategy.RedirectStrategy.DEFAULT
will be used by default.- See Also:
RedirectStrategy
-
withCustomRedirectStrategy
public HTTPRequestBuilder withCustomRedirectStrategy(@NotNull CustomRedirectStrategy redirectStrategy)
Set custom redirect strategy.- Since:
- 2024.12
-
withUserAgent
public HTTPRequestBuilder withUserAgent(@Nullable String userAgent)
Set User-Agent header.By default
value will be used."TeamCity Server " + ServerVersionHolder.getVersion().getDisplayVersion()
-
apply
public HTTPRequestBuilder apply(@NotNull Consumer<HTTPRequestBuilder> consumer)
Applies modifications to request inside provided consumer- Parameters:
consumer
- consumer that modifies the request
-
withTimeout
public HTTPRequestBuilder withTimeout(int timeoutMS)
Set the timeout for the request- Parameters:
timeoutMS
- the timeout in milliseconds
-
withConnectionTimeoutMs
public HTTPRequestBuilder withConnectionTimeoutMs(int connectionTimeoutMs)
Sets the timeout for establishing tcp connection with the server. If connection timeout is not specified, then the timeout set via withTimeoutMs will be used.- Parameters:
connectTimeoutMS
- timeout in milliseconds- Since:
- 2024.12
-
withMaxConnections
public HTTPRequestBuilder withMaxConnections(int maxConnections)
Set the maximum number of connections allowed- Parameters:
maxConnections
- - maximum number of connections
-
withExecutorService
public HTTPRequestBuilder withExecutorService(@NotNull ExecutorService executorService)
Set the executor service that will be used.Only used with
buildAsync()
- Parameters:
executorService
- = the executor service to use
-
build
public HTTPRequestBuilder.Request build()
withExecutorService(java.util.concurrent.ExecutorService)
will be ignored.- Returns:
- request that will handle responses using callbacks
-
buildAsync
public AsyncRequest buildAsync()
Provides a way of asynchronously handling a request. Clients are responsible for callingAutoCloseable.close()
Callbacks methods such as
onException(java.util.function.Consumer<java.lang.Exception>)
,onErrorResponse(java.util.function.BiConsumer<java.lang.Integer, java.lang.String>)
, andonSuccess(jetbrains.buildServer.util.HTTPRequestBuilder.ResponseConsumer)
- Returns:
- request that will return the response as a
CompletableFuture
-
request
public static HTTPRequestBuilder request(@NotNull String url) throws URISyntaxException
- Throws:
URISyntaxException
-
-