Package org.opennms.core.web
Class HttpClientWrapper
- java.lang.Object
-
- org.opennms.core.web.HttpClientWrapper
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
public class HttpClientWrapper extends java.lang.Object implements java.io.Closeable
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
HttpClientWrapper()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description HttpClientWrapper
addBasicCredentials(java.lang.String username, java.lang.String password)
Add basic auth credentials to requests created by the HttpClientWrapper.HttpClientWrapper
addRequestInterceptor(org.apache.http.HttpRequestInterceptor interceptor)
Add anHttpRequestInterceptor
for all requests.HttpClientWrapper
addResponseInterceptor(org.apache.http.HttpResponseInterceptor interceptor)
Add anHttpResponseInterceptor
for all responses.protected void
assertNotInitialized()
void
close()
Safely clean up the HttpClient.void
close(org.apache.http.client.methods.CloseableHttpResponse response)
Safely clean up after a response.protected void
configureSSLContext(org.apache.http.impl.client.HttpClientBuilder builder)
static HttpClientWrapper
create()
Create a new HTTP client wrapper.HttpClientWrapper
dontReuseConnections()
Configure HttpClient to not reuse connections for multiple requests.HttpClientWrapper
duplicate()
Create a duplicate HttpClientWrapper from this wrapper.protected void
enablePreemptiveAuth(org.apache.http.impl.client.HttpClientBuilder builder)
org.apache.http.client.methods.CloseableHttpResponse
execute(org.apache.http.client.methods.HttpUriRequest method)
Execute the given HTTP method, returning an HTTP response.org.apache.http.impl.client.CloseableHttpClient
getClient()
HttpClientWrapper
setConnectionTimeout(java.lang.Integer connectionTimeout)
Set the connection timeout on connections.protected void
setCredentials(org.apache.http.impl.client.HttpClientBuilder httpClientBuilder, java.lang.String username, java.lang.String password)
HttpClientWrapper
setRetries(java.lang.Integer retries)
Set the number of retries when making requests.HttpClientWrapper
setSocketTimeout(java.lang.Integer socketTimeout)
Set the socket timeout on connections.HttpClientWrapper
setUserAgent(java.lang.String userAgent)
Set the User-Agent header used when making requests.HttpClientWrapper
setVersion(org.apache.http.HttpVersion httpVersion)
Set the HTTP version used when making requests.HttpClientWrapper
setVirtualHost(java.lang.String host)
Set the Host header used when making requests.java.lang.String
toString()
HttpClientWrapper
trustSelfSigned(java.lang.String scheme)
Trust self-signed certificates.HttpClientWrapper
useBrowserCompatibleCookies()
Use browser-compatible cookies rather than the default.HttpClientWrapper
useLaxRedirect()
Use LAX redirect strategy.HttpClientWrapper
usePreemptiveAuth()
Preemptively pass basic authentication headers, rather than waiting for the server to response asking for it.HttpClientWrapper
useRelaxedSSL(java.lang.String scheme)
Use relaxed SSL connection handling (EmptyKeyRelaxedTrustSSLContext.ALGORITHM, allows any certificate)HttpClientWrapper
useSystemProxySettings()
Configure HttpClient to honor the system java proxy settings (-Dhttp.proxyHost= -Dhttp.proxyPort=)
-
-
-
Method Detail
-
create
public static HttpClientWrapper create()
Create a new HTTP client wrapper.
-
addBasicCredentials
public HttpClientWrapper addBasicCredentials(java.lang.String username, java.lang.String password)
Add basic auth credentials to requests created by the HttpClientWrapper.- Parameters:
username
- The username to connect aspassword
- The password to use
-
addRequestInterceptor
public HttpClientWrapper addRequestInterceptor(org.apache.http.HttpRequestInterceptor interceptor)
Add anHttpRequestInterceptor
for all requests.
-
addResponseInterceptor
public HttpClientWrapper addResponseInterceptor(org.apache.http.HttpResponseInterceptor interceptor)
Add anHttpResponseInterceptor
for all responses.- Returns:
-
dontReuseConnections
public HttpClientWrapper dontReuseConnections()
Configure HttpClient to not reuse connections for multiple requests.
-
useSystemProxySettings
public HttpClientWrapper useSystemProxySettings()
Configure HttpClient to honor the system java proxy settings (-Dhttp.proxyHost= -Dhttp.proxyPort=)
-
useBrowserCompatibleCookies
public HttpClientWrapper useBrowserCompatibleCookies()
Use browser-compatible cookies rather than the default.
-
useRelaxedSSL
public HttpClientWrapper useRelaxedSSL(java.lang.String scheme) throws java.security.GeneralSecurityException
Use relaxed SSL connection handling (EmptyKeyRelaxedTrustSSLContext.ALGORITHM, allows any certificate)- Throws:
java.security.NoSuchAlgorithmException
java.security.GeneralSecurityException
-
trustSelfSigned
public HttpClientWrapper trustSelfSigned(java.lang.String scheme) throws java.security.GeneralSecurityException
Trust self-signed certificates.- Throws:
java.security.GeneralSecurityException
-
usePreemptiveAuth
public HttpClientWrapper usePreemptiveAuth()
Preemptively pass basic authentication headers, rather than waiting for the server to response asking for it.
-
useLaxRedirect
public HttpClientWrapper useLaxRedirect()
Use LAX redirect strategy. Automatically redirects all HEAD, GET and POST requests. This strategy relaxes restrictions on automatic redirection of POST methods imposed by the HTTP specification.
-
setSocketTimeout
public HttpClientWrapper setSocketTimeout(java.lang.Integer socketTimeout)
Set the socket timeout on connections.
-
setConnectionTimeout
public HttpClientWrapper setConnectionTimeout(java.lang.Integer connectionTimeout)
Set the connection timeout on connections.
-
setRetries
public HttpClientWrapper setRetries(java.lang.Integer retries)
Set the number of retries when making requests.
-
setUserAgent
public HttpClientWrapper setUserAgent(java.lang.String userAgent)
Set the User-Agent header used when making requests.
-
setVirtualHost
public HttpClientWrapper setVirtualHost(java.lang.String host)
Set the Host header used when making requests.
-
setVersion
public HttpClientWrapper setVersion(org.apache.http.HttpVersion httpVersion)
Set the HTTP version used when making requests.
-
close
public void close(org.apache.http.client.methods.CloseableHttpResponse response)
Safely clean up after a response.
-
close
public void close() throws java.io.IOException
Safely clean up the HttpClient.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Throws:
java.io.IOException
-
execute
public org.apache.http.client.methods.CloseableHttpResponse execute(org.apache.http.client.methods.HttpUriRequest method) throws org.apache.http.client.ClientProtocolException, java.io.IOException
Execute the given HTTP method, returning an HTTP response. Note that when you are done with the response, you must call#closeResponse()
so that it gets cleaned up properly.- Throws:
org.apache.http.client.ClientProtocolException
java.io.IOException
-
duplicate
public HttpClientWrapper duplicate()
Create a duplicate HttpClientWrapper from this wrapper. All settings are preserved, and the session/cookie store is shared between duplicate wrappers and their parent.
-
getClient
public org.apache.http.impl.client.CloseableHttpClient getClient()
-
assertNotInitialized
protected void assertNotInitialized()
-
setCredentials
protected void setCredentials(org.apache.http.impl.client.HttpClientBuilder httpClientBuilder, java.lang.String username, java.lang.String password)
-
enablePreemptiveAuth
protected void enablePreemptiveAuth(org.apache.http.impl.client.HttpClientBuilder builder)
-
configureSSLContext
protected void configureSSLContext(org.apache.http.impl.client.HttpClientBuilder builder)
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-