Package com.networknt.http.client
Class HttpClientRequest
- java.lang.Object
-
- com.networknt.http.client.HttpClientRequest
-
public class HttpClientRequest extends Object
-
-
Constructor Summary
Constructors Constructor Description HttpClientRequest()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddAuthToken(HttpRequest.Builder builder, String token)Add Authorization Code grant token the caller app gets from OAuth2 server.ResultaddCcToken(HttpRequest.Builder builder)Add Client Credentials token cached in the client for standalone applicationvoidaddCorrelationId(HttpRequest.Builder builder, String correlationId)voidaddRequestHeader(HttpRequest.Builder builder, String headerName, String headerValue)voidaddRequestHeaders(HttpRequest.Builder builder, Map<String,String> headers)voidaddTraceabilityId(HttpRequest.Builder builder, String traceabilityId)protected HttpClientbuildHttpClient(ClientConfig clientConfig, boolean isHttps)static SSLContextcreateSSLContext()default method for creating ssl context.static SSLContextcreateSSLContext(String trustedNamesGroupKey)create ssl context using specified trustedName configprotected HttpRequest.BodyPublishergetBodyPublisher(Optional<?> body)HttpRequest.BuilderinitBuilder(String url, HttpMethod method)HttpRequest.BuilderinitBuilder(String url, HttpMethod method, Optional<?> body)HttpRequest.BuilderinitBuilder(URI uri, HttpMethod method)HttpRequest.BuilderinitBuilder(URI uri, HttpMethod method, Optional<?> body)ResultpopulateHeader(HttpRequest.Builder builder, String authToken)Support API to API calls with scope token.HttpResponse<?>send(HttpRequest.Builder builder, HttpResponse.BodyHandler<?> handler)CompletableFuture<? extends HttpResponse<?>>sendAsync(HttpRequest.Builder builder, HttpResponse.BodyHandler<?> handler)voidsetAuthenticator(Authenticator authenticator)Sets an authenticator to use for HTTP authentication.voidsetExecutorService(ExecutorService executorService)Sets the executor to be used for asynchronous and dependent tasks.voidsetProxy(String hostname, int port)Selects the proxy server to use, if any, when connecting to the network resource referenced by a URL
-
-
-
Field Detail
-
TLS
public static final String TLS
- See Also:
- Constant Field Values
-
-
Method Detail
-
setProxy
public void setProxy(String hostname, int port)
Selects the proxy server to use, if any, when connecting to the network resource referenced by a URL- Parameters:
hostname- Http call proxy host name.port- Http call proxy host port number.
-
setExecutorService
public void setExecutorService(ExecutorService executorService)
Sets the executor to be used for asynchronous and dependent tasks.- Parameters:
executorService- the Executor
-
setAuthenticator
public void setAuthenticator(Authenticator authenticator)
Sets an authenticator to use for HTTP authentication.- Parameters:
authenticator- Http call proxy host name.
-
buildHttpClient
protected HttpClient buildHttpClient(ClientConfig clientConfig, boolean isHttps)
-
send
public HttpResponse<?> send(HttpRequest.Builder builder, HttpResponse.BodyHandler<?> handler) throws InterruptedException, IOException
- Throws:
InterruptedExceptionIOException
-
sendAsync
public CompletableFuture<? extends HttpResponse<?>> sendAsync(HttpRequest.Builder builder, HttpResponse.BodyHandler<?> handler) throws InterruptedException, IOException
- Throws:
InterruptedExceptionIOException
-
initBuilder
public HttpRequest.Builder initBuilder(String url, HttpMethod method) throws Exception
- Throws:
Exception
-
initBuilder
public HttpRequest.Builder initBuilder(String url, HttpMethod method, Optional<?> body) throws Exception
- Throws:
Exception
-
initBuilder
public HttpRequest.Builder initBuilder(URI uri, HttpMethod method)
-
initBuilder
public HttpRequest.Builder initBuilder(URI uri, HttpMethod method, Optional<?> body)
-
addAuthToken
public void addAuthToken(HttpRequest.Builder builder, String token)
Add Authorization Code grant token the caller app gets from OAuth2 server. This is the method called from client like web server- Parameters:
builder- the http request buildertoken- the bearer token
-
addRequestHeader
public void addRequestHeader(HttpRequest.Builder builder, String headerName, String headerValue)
-
addRequestHeaders
public void addRequestHeaders(HttpRequest.Builder builder, Map<String,String> headers)
-
addTraceabilityId
public void addTraceabilityId(HttpRequest.Builder builder, String traceabilityId)
-
addCorrelationId
public void addCorrelationId(HttpRequest.Builder builder, String correlationId)
-
addCcToken
public Result addCcToken(HttpRequest.Builder builder)
Add Client Credentials token cached in the client for standalone application- Parameters:
builder- the http request builder- Returns:
- Result when fail to get jwt, it will return a Status.
-
populateHeader
public Result populateHeader(HttpRequest.Builder builder, String authToken)
Support API to API calls with scope token. The token is the original token from consumer and the client credentials token of caller API is added from cache. This method is used in API to API call- Parameters:
builder- the http request builderauthToken- the authorization token- Returns:
- Result when fail to get jwt, it will return a Status.
-
getBodyPublisher
protected HttpRequest.BodyPublisher getBodyPublisher(Optional<?> body)
-
createSSLContext
public static SSLContext createSSLContext() throws IOException
default method for creating ssl context. trustedNames config is not used.- Returns:
- SSLContext
- Throws:
IOException- IOException
-
createSSLContext
public static SSLContext createSSLContext(String trustedNamesGroupKey) throws IOException
create ssl context using specified trustedName config- Parameters:
trustedNamesGroupKey- - the trustedName config to be used- Returns:
- SSLContext
- Throws:
IOException- IOException
-
-