Package io.jans.configapi.core.service
Class ConfigHttpService
- java.lang.Object
-
- io.jans.configapi.core.service.ConfigHttpService
-
- All Implemented Interfaces:
Serializable
@ApplicationScoped public class ConfigHttpService extends Object implements Serializable
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected org.apache.logging.log4j.Loggerlog
-
Constructor Summary
Constructors Constructor Description ConfigHttpService()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description org.apache.http.conn.routing.HttpRoutePlannerbuildDefaultRoutePlanner(String proxy)org.apache.http.conn.routing.HttpRoutePlannerbuildDefaultRoutePlanner(String hostname, int port, String scheme)StringconstructServerUrl(jakarta.servlet.http.HttpServletRequest request)voidconsume(org.apache.http.HttpResponse httpResponse)StringconvertEntityToString(byte[] responseBytes)StringconvertEntityToString(byte[] responseBytes, String charsetName)StringconvertEntityToString(byte[] responseBytes, Charset charset)org.apache.http.impl.client.CloseableHttpClientcreateHttpsClientWithTlsPolicy(String[] enabledProtocols, String[] allowedCipherSuites)Create a CloseableHttpClient configured with an optional TLS protocol and cipher-suite policy.StringencodeBase64(String value)StringencodeUrl(String value)URL-encodes the given string using UTF-8 encoding.io.jans.model.net.HttpServiceResponseexecuteDelete(String requestUri, Map<String,String> headers, Map<String,String> data)Execute an HTTP DELETE request to the given URI using the service's default HTTPS client.io.jans.model.net.HttpServiceResponseexecuteDelete(org.apache.http.client.HttpClient httpClient, String requestUri, Map<String,String> headers, Map<String,String> parameters)Execute an HTTP DELETE to the specified URI, applying provided headers and URL-encoded query parameters.io.jans.model.net.HttpServiceResponseexecuteGet(String requestUri, Map<String,String> headers)io.jans.model.net.HttpServiceResponseexecuteGet(String requestUri, Map<String,String> headers, Map<String,String> data)io.jans.model.net.HttpServiceResponseexecuteGet(org.apache.http.client.HttpClient httpClient, String requestUri)io.jans.model.net.HttpServiceResponseexecuteGet(org.apache.http.client.HttpClient httpClient, String requestUri, Map<String,String> headers, Map<String,String> parameters)Executes an HTTP GET against the given URI with optional headers and query parameters and returns the request/response pair.io.jans.model.net.HttpServiceResponseexecutePost(String uri, String authCode, String postData, org.apache.http.entity.ContentType contentType, String authType)Sends an HTTP POST to the given URI using the service's default HTTPS client.io.jans.model.net.HttpServiceResponseexecutePost(org.apache.http.client.HttpClient httpClient, String uri, String authCode, String postData, org.apache.http.entity.ContentType contentType)Execute an HTTP POST request using the provided HTTP client.io.jans.model.net.HttpServiceResponseexecutePost(org.apache.http.client.HttpClient httpClient, String uri, String authCode, Map<String,String> headers, String postData)Execute an HTTP POST to the specified URI using the provided HttpClient and payload.io.jans.model.net.HttpServiceResponseexecutePost(org.apache.http.client.HttpClient httpClient, String uri, String authCode, Map<String,String> headers, String postData, org.apache.http.entity.ContentType contentType, String authType)Execute an HTTP POST to the given URI and return the request/response pair.StringgetContent(org.apache.http.HttpEntity httpEntity)Read and return the UTF-8 string content of the provided HttpEntity.org.apache.http.impl.client.CloseableHttpClientgetHttpsClient()org.apache.http.impl.client.CloseableHttpClientgetHttpsClient(String trustStorePath, String trustStorePassword)Create an HTTP client configured to use the specified trust store for TLS trust material.org.apache.http.impl.client.CloseableHttpClientgetHttpsClient(String trustStorePath, String trustStorePassword, String keyStorePath, String keyStorePassword)Builds an HTTPS CloseableHttpClient configured to use the provided trust store and key store.org.apache.http.impl.client.CloseableHttpClientgetHttpsClient(org.apache.http.client.config.RequestConfig requestConfig)org.apache.http.impl.client.CloseableHttpClientgetHttpsClient(org.apache.http.client.config.RequestConfig requestConfig, org.apache.http.conn.routing.HttpRoutePlanner routerPlanner)org.apache.http.impl.client.CloseableHttpClientgetHttpsClient(org.apache.http.conn.routing.HttpRoutePlanner routerPlanner)org.apache.http.impl.client.CloseableHttpClientgetHttpsClientTrustAll()static StringgetOpenidConfigurationUrl()byte[]getResponseContent(org.apache.http.HttpResponse httpResponse)StringgetResponseEntityString(io.jans.model.net.HttpServiceResponse serviceResponse)Extracts the HTTP response body as UTF-8 text and enforces an OK (200) response status.com.fasterxml.jackson.databind.JsonNodegetResponseJsonNode(io.jans.model.net.HttpServiceResponse serviceResponse)Parse the JSON body of an HttpServiceResponse and return its "response" node.com.fasterxml.jackson.databind.JsonNodegetResponseJsonNode(String jsonSring, String nodeName)jakarta.ws.rs.core.Response.StatusgetResponseStatus(io.jans.model.net.HttpServiceResponse serviceResponse)Map an HttpServiceResponse's HTTP status code to a JAX-RS Status.voidinit()booleanisContentTypeXml(org.apache.http.HttpResponse httpResponse)booleanisResponseStastusCodeOk(org.apache.http.HttpResponse httpResponse)booleanisResponseStatusCodeOk(org.apache.http.HttpResponse httpResponse)StringBuilderreadEntity(org.apache.http.HttpEntity httpEntity)Read and return the UTF-8 text content of the given HttpEntity as a StringBuilder.
-
-
-
Method Detail
-
init
@PostConstruct public void init()
-
getOpenidConfigurationUrl
public static String getOpenidConfigurationUrl()
-
getHttpsClientTrustAll
public org.apache.http.impl.client.CloseableHttpClient getHttpsClientTrustAll() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException
-
getHttpsClient
public org.apache.http.impl.client.CloseableHttpClient getHttpsClient()
-
getHttpsClient
public org.apache.http.impl.client.CloseableHttpClient getHttpsClient(org.apache.http.client.config.RequestConfig requestConfig)
-
getHttpsClient
public org.apache.http.impl.client.CloseableHttpClient getHttpsClient(org.apache.http.conn.routing.HttpRoutePlanner routerPlanner)
-
getHttpsClient
public org.apache.http.impl.client.CloseableHttpClient getHttpsClient(org.apache.http.client.config.RequestConfig requestConfig, org.apache.http.conn.routing.HttpRoutePlanner routerPlanner)
-
getHttpsClient
public org.apache.http.impl.client.CloseableHttpClient getHttpsClient(String trustStorePath, String trustStorePassword) throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException
Create an HTTP client configured to use the specified trust store for TLS trust material.- Parameters:
trustStorePath- filesystem path to the trust store file (e.g., JKS or PKCS12)trustStorePassword- password for the trust store- Returns:
- a CloseableHttpClient that uses the provided trust store for SSL/TLS connections
- Throws:
KeyManagementException- if the SSL context cannot be initializedNoSuchAlgorithmException- if a required cryptographic algorithm is not availableKeyStoreException- if the trust store cannot be loaded or is invalidCertificateException- if any certificates in the trust store cannot be parsedIOException- if an I/O error occurs while reading the trust store file
-
createHttpsClientWithTlsPolicy
public org.apache.http.impl.client.CloseableHttpClient createHttpsClientWithTlsPolicy(String[] enabledProtocols, String[] allowedCipherSuites) throws NoSuchAlgorithmException, KeyManagementException
Create a CloseableHttpClient configured with an optional TLS protocol and cipher-suite policy. If `enabledProtocols` is null or empty, the returned client uses the JVM/default TLS configuration.- Parameters:
enabledProtocols- the TLS protocols to enable (e.g., {"TLSv1.2","TLSv1.3"}); when null or empty, default protocols are usedallowedCipherSuites- the cipher suites to enable, or null to use the default cipher suite list- Returns:
- a CloseableHttpClient configured with the specified TLS protocols and cipher suites (or a default TLS configuration when `enabledProtocols` is null/empty)
- Throws:
NoSuchAlgorithmException- if the requested SSL algorithm is not available when building the SSLContextKeyManagementException- if there is an issue initializing the SSLContext's key management
-
getHttpsClient
public org.apache.http.impl.client.CloseableHttpClient getHttpsClient(String trustStorePath, String trustStorePassword, String keyStorePath, String keyStorePassword) throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException, UnrecoverableKeyException
Builds an HTTPS CloseableHttpClient configured to use the provided trust store and key store. The returned client uses an SSLContext initialized from the specified trust store and key store, and is configured with the service's connection manager and a standard cookie specification.- Parameters:
trustStorePath- filesystem path to the trust store file (e.g., JKS/PKCS12)trustStorePassword- password for the trust storekeyStorePath- filesystem path to the key store containing client certificate/private keykeyStorePassword- password for the key store (and key material)- Returns:
- a CloseableHttpClient that uses the loaded SSL context
- Throws:
KeyManagementException- if the SSLContext cannot be initializedNoSuchAlgorithmException- if a required cryptographic algorithm is not availableKeyStoreException- if the key store cannot be loaded or accessedCertificateException- if any certificate cannot be parsed or validatedIOException- if an I/O error occurs reading the store filesUnrecoverableKeyException- if a key in the key store cannot be recovered (e.g., wrong password)
-
executePost
public io.jans.model.net.HttpServiceResponse executePost(org.apache.http.client.HttpClient httpClient, String uri, String authCode, Map<String,String> headers, String postData, org.apache.http.entity.ContentType contentType, String authType)Execute an HTTP POST to the given URI and return the request/response pair.- Parameters:
httpClient- the HttpClient used to execute the requesturi- the target URI for the POST requestauthCode- an authorization token to include; if blank, no Authorization header is addedheaders- additional headers to include in the request; may be nullpostData- the request body as a string; may be nullcontentType- the content type for the request body; if null, defaults to application/jsonauthType- the authentication scheme prefix (for example "Bearer ") to prepend toauthCode- Returns:
- an HttpServiceResponse containing the executed HttpPost and HttpResponse, or
nullif execution failed
-
executePost
public io.jans.model.net.HttpServiceResponse executePost(org.apache.http.client.HttpClient httpClient, String uri, String authCode, Map<String,String> headers, String postData)Execute an HTTP POST to the specified URI using the provided HttpClient and payload.- Parameters:
httpClient- the HttpClient used to execute the requesturi- the target request URIauthCode- an optional authorization value; if non-null an Authorization header is setheaders- additional request headers to include (may be null)postData- the request body to send (may be null)- Returns:
- an HttpServiceResponse wrapping the executed HttpPost and HttpResponse, or `null` if execution failed
-
executePost
public io.jans.model.net.HttpServiceResponse executePost(org.apache.http.client.HttpClient httpClient, String uri, String authCode, String postData, org.apache.http.entity.ContentType contentType)Execute an HTTP POST request using the provided HTTP client.- Parameters:
httpClient- the HttpClient to execute the requesturi- the target URI for the POST requestauthCode- optional authorization token value; when provided it will be set on the `Authorization` headerpostData- the request body to sendcontentType- the content type of the request body; if `null`, `application/json` is used- Returns:
- an HttpServiceResponse containing the executed HttpPost and the HttpResponse, or `null` if the request failed
-
executePost
public io.jans.model.net.HttpServiceResponse executePost(String uri, String authCode, String postData, org.apache.http.entity.ContentType contentType, String authType)
Sends an HTTP POST to the given URI using the service's default HTTPS client.- Parameters:
uri- the target request URIauthCode- optional authorization credentials (token or credential string) to include in the Authorization headerpostData- the request body to sendcontentType- the content type of the request bodyauthType- the scheme or prefix to use for the Authorization header (for example "Bearer ")- Returns:
- an HttpServiceResponse containing the executed request and response, or
nullif the request failed
-
encodeUrl
public String encodeUrl(String value)
URL-encodes the given string using UTF-8 encoding.- Parameters:
value- the string to URL-encode- Returns:
- the URL-encoded form of the input string
-
executeGet
public io.jans.model.net.HttpServiceResponse executeGet(org.apache.http.client.HttpClient httpClient, String requestUri, Map<String,String> headers, Map<String,String> parameters)Executes an HTTP GET against the given URI with optional headers and query parameters and returns the request/response pair. The method appends URL-encoded query parameters to the requestUri when provided, applies any supplied headers, and executes the request using the given HttpClient.- Parameters:
requestUri- the target URI (may be appended with URL-encoded query parameters)headers- optional HTTP headers to include on the request (may be null)parameters- optional query parameters to append to the URI; entries with blank values are ignored (may be null)- Returns:
- a HttpServiceResponse containing the executed HttpGet and the HttpResponse, or `null` if the request failed due to an I/O error
-
executeGet
public io.jans.model.net.HttpServiceResponse executeGet(String requestUri, Map<String,String> headers, Map<String,String> data)
-
executeGet
public io.jans.model.net.HttpServiceResponse executeGet(String requestUri, Map<String,String> headers)
-
executeGet
public io.jans.model.net.HttpServiceResponse executeGet(org.apache.http.client.HttpClient httpClient, String requestUri)
-
executeDelete
public io.jans.model.net.HttpServiceResponse executeDelete(String requestUri, Map<String,String> headers, Map<String,String> data)
Execute an HTTP DELETE request to the given URI using the service's default HTTPS client.- Parameters:
requestUri- the target URI for the DELETE requestheaders- optional HTTP headers to includedata- optional query parameters to append to the URI- Returns:
- the HttpServiceResponse containing the executed request and response, or
nullif the request failed
-
executeDelete
public io.jans.model.net.HttpServiceResponse executeDelete(org.apache.http.client.HttpClient httpClient, String requestUri, Map<String,String> headers, Map<String,String> parameters)Execute an HTTP DELETE to the specified URI, applying provided headers and URL-encoded query parameters. Query parameters with blank values are skipped; keys and values are URL-encoded using UTF-8 before being appended to the URI.- Parameters:
requestUri- the target URI (query string will be appended if parameters are provided)headers- map of header names to values to set on the request; may be nullparameters- map of query parameter names to values to append to the URI; may be null- Returns:
- an HttpServiceResponse containing the executed HttpDelete and the HttpResponse, or `null` if execution failed
-
getResponseContent
public byte[] getResponseContent(org.apache.http.HttpResponse httpResponse) throws IOException- Throws:
IOException
-
consume
public void consume(org.apache.http.HttpResponse httpResponse) throws IOException- Throws:
IOException
-
convertEntityToString
public String convertEntityToString(byte[] responseBytes)
-
convertEntityToString
public String convertEntityToString(byte[] responseBytes, String charsetName) throws UnsupportedEncodingException
- Throws:
UnsupportedEncodingException
-
isResponseStastusCodeOk
public boolean isResponseStastusCodeOk(org.apache.http.HttpResponse httpResponse)
-
isResponseStatusCodeOk
public boolean isResponseStatusCodeOk(org.apache.http.HttpResponse httpResponse)
-
isContentTypeXml
public boolean isContentTypeXml(org.apache.http.HttpResponse httpResponse)
-
constructServerUrl
public String constructServerUrl(jakarta.servlet.http.HttpServletRequest request)
-
buildDefaultRoutePlanner
public org.apache.http.conn.routing.HttpRoutePlanner buildDefaultRoutePlanner(String hostname, int port, String scheme)
-
buildDefaultRoutePlanner
public org.apache.http.conn.routing.HttpRoutePlanner buildDefaultRoutePlanner(String proxy)
-
getResponseJsonNode
public com.fasterxml.jackson.databind.JsonNode getResponseJsonNode(io.jans.model.net.HttpServiceResponse serviceResponse) throws com.fasterxml.jackson.core.JsonProcessingExceptionParse the JSON body of an HttpServiceResponse and return its "response" node. If the provided serviceResponse is null or does not contain a parsable JSON body, this method returns null.- Parameters:
serviceResponse- the HttpServiceResponse whose entity will be parsed- Returns:
- the JsonNode corresponding to the top-level "response" field, or null if unavailable
- Throws:
com.fasterxml.jackson.core.JsonProcessingException- if the response body cannot be parsed as JSON
-
getResponseEntityString
public String getResponseEntityString(io.jans.model.net.HttpServiceResponse serviceResponse)
Extracts the HTTP response body as UTF-8 text and enforces an OK (200) response status.- Parameters:
serviceResponse- the HttpServiceResponse wrapper containing the HttpResponse to read; may be null- Returns:
- the response body decoded as UTF-8, or
nullifserviceResponse, the contained HttpResponse, or its entity is null - Throws:
jakarta.ws.rs.WebApplicationException- if the HttpResponse status code is not 200; the exception message is formatted as ":"
-
getResponseJsonNode
public com.fasterxml.jackson.databind.JsonNode getResponseJsonNode(String jsonSring, String nodeName) throws com.fasterxml.jackson.core.JsonProcessingException
- Throws:
com.fasterxml.jackson.core.JsonProcessingException
-
getResponseStatus
public jakarta.ws.rs.core.Response.Status getResponseStatus(io.jans.model.net.HttpServiceResponse serviceResponse)
Map an HttpServiceResponse's HTTP status code to a JAX-RS Status.- Parameters:
serviceResponse- the HttpServiceResponse whose HTTP response status will be mapped; may be null- Returns:
- the `Status` corresponding to the response's HTTP status code, or `Status.INTERNAL_SERVER_ERROR` if the response is missing or the code cannot be mapped
-
readEntity
public StringBuilder readEntity(org.apache.http.HttpEntity httpEntity) throws IOException
Read and return the UTF-8 text content of the given HttpEntity as a StringBuilder.- Parameters:
httpEntity- the HTTP entity whose content will be read; may be null- Returns:
- a StringBuilder containing the entity content (empty if the entity is null or has no content)
- Throws:
IOException- if an I/O error occurs while reading the entity content
-
getContent
public String getContent(org.apache.http.HttpEntity httpEntity)
Read and return the UTF-8 string content of the provided HttpEntity.- Parameters:
httpEntity- the HttpEntity to read; if null, the method returns null- Returns:
- the entity content decoded as a UTF-8 string, or null if
httpEntityis null - Throws:
jakarta.ws.rs.WebApplicationException- if an I/O error occurs while reading the entity
-
-