Class GrouperHttpClient
java.lang.Object
edu.internet2.middleware.grouper.util.GrouperHttpClient
HTTP call. Use this for all HTTP calls as a client
import edu.internet2.middleware.grouper.util.*; GrouperHttpClient grouperHttpCall = new GrouperHttpClient(); grouperHttpCall.assignUrl(url); grouperHttpCall.assignGrouperHttpMethod("POST"); grouperHttpCall.addHeader("Content-Type", "application/json"); grouperHttpCall.addHeader("Authorization", "Bearer " + bearerToken); grouperHttpCall.assignBody(body); grouperHttpCall.executeRequest(); if (grouperHttpCall.getResponseCode() != 200) { throw new RuntimeException("Error connecting to '" + url + "': " + grouperHttpCall.getResponseCode()); } String json = grouperHttpCall.getResponseBody();
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionaddBodyParameter
(String key, String value) Add a parameter to the BODY for a POST body form.addFileToSend
(String filename, File file) Adds a parameter to the header.addUrlParameter
(String key, String value) Add a parameter to the URL.assignAssertJsonPointer
(String assertJsonPointer1) check a json pointer for a value to see if request is success.assignAssertJsonPointerExpectedValueString
(String assertJsonPointerExpectedValueString1) if the response code is not this, then exception and log responseassignAssertResponseCode
(int expectedCode) if the response code is not this, then exception and log responseassignBody
(String _body) Sets the body to send.assignBodyBytes
(byte[] _body) Sets the body to send.assignDebugMap
(Map<String, Object> debugMap) debug map for timing and result codeassignDoNotLogHeaders
(String headersCommaSeparated) assignDoNotLogHeaders
(Set<String> headers) assignDoNotLogParameters
(String paramsCommaSeparated) assignDoNotLogParameters
(Set<String> params) assignDoNotLogRequestBody
(boolean theDoNotLogRequestBody) if request body contains sensitive info and shouldnt be loggedassignDoNotLogResponseBody
(boolean theDoNotLogResponseBody) if response body contains sensitive info and shouldnt be loggedassignGrouperHttpMethod
(GrouperHttpMethod grouperHttpMethod) assignGrouperHttpMethod
(String grouperHttpMethodType) assignHttpMultipartMode
(org.apache.http.entity.mime.HttpMultipartMode _httpMultipartMode) Use to override HttpMultipartMode.BROWSER_COMPATIBLE which is the default.assignHttpResponseBodyCallback
(GrouperHttpResponseBodyCallback httpResponseBodyCallback) assignPassword
(String _password) Sets the password for basic auth.assignProxyType
(GrouperProxyType grouperProxyType1) proxy type to override other configurationassignProxyType
(String grouperProxyTypeString) proxy type to override other configurationassignProxyUrl
(String proxyUrl1) proxy url to proxy to (override other configuration)assignRequestBodyCharset
(Charset theRequestBodyCharset) e.g.assignRequestBodyCharType
(org.apache.http.entity.ContentType theRequestBodyCharType) look in StringEntity for usageassignResponseAsFile
(boolean _responseAsFile) Whether you want the response as a file as opposed to a string in memory.assignResponseFile
(File _responseFile) If getting the reponse body as a file, this is the file.assignResponseFileName
(String _responseFileName) The filename to use for the response, if the response is retrieved as a file.assignResponseHeaders
(Map<String, String> _responseHeaders) Any headers sent back in the resopnse.assignRetries
(int theRetries) how many times to retry for a non fatal error on idempotent requestsassignTimeoutMillies
(int theTimeoutMillies) assignTrust
(boolean _trust) Trust regardless of cert; ONLY use when you KNOW the endpoint.assignTrustStore
(File _trustStore) Truststore (.jks) to add dynamically to list of truststores.assignTrustStorePassword
(String _trustStoreKey) Password for truststore.Sets the url being called.assignUser
(String _user) Sets the user for basic auth.static String
basicAuthenticationString
(String login, String password) Create a basic authentication string.Execute a post with the given parameters, set teh code and the response into the call.Get the response body of the call.int
Get the response code.If getting the reponse body as a file, this is the file.The filename to use for the response, if the response is retrieved as a file.Any headers sent back in the resopnse.Any headers (lower case) sent back in the resopnse.int
if there's a 429 or connection timed out then count how many times we retried for loggingboolean
static GrouperHttpClientLog
get the current log log startstatic String
logEnd()
stop a debug log in a finally blockstatic boolean
logStart
(GrouperHttpClientLog grouperHttpCallLog) start a static debug log log startstatic void
org.apache.http.entity.StringEntity
newStringEntity
(String theBody) static String
responseBodyAsString
(org.apache.commons.httpclient.HttpMethod httpMethod) Get the body of the response as a string.static String
responseBodyAsString
(org.apache.http.client.methods.CloseableHttpResponse closeableHttpResponse) Get the body of the response as a string.com.fasterxml.jackson.databind.JsonNode
get the json node of the response (generate if not there already)void
setGrouperHttpClientSetupAuthorization
(GrouperHttpClientSetupAuthorization grouperHttpClientSetupAuthorization) implement this interface to have the grouper http client have the callback to set up the new authorization if there's a retry or delayvoid
setRetryForThrottlingOrNetworkIssues
(int retryForThrottlingOrNetworkIssues) if there's a 429 or a connection timed out exception then delay for sometime and retry these many times.void
setRetryForThrottlingOrNetworkIssuesBackOffMillis
(int retryForThrottlingOrNetworkIssuesBackOffMillis) if there's a 429 or connection timed out exception then after each retry, add to the sleep these many millisvoid
setRetryForThrottlingOrNetworkIssuesSleepMillis
(long retryForThrottlingOrNetworkIssuesSleepMillis) if there's a 429 or a connection timed out exception then retry and sleep for these many millisvoid
setThrottlingCallback
(GrouperHttpThrottlingCallback grouperHttpThrottlingCallback) implement this interface to customize to set custom condition on which you want to retrystatic void
shutdown()
-
Constructor Details
-
GrouperHttpClient
public GrouperHttpClient()
-
-
Method Details
-
main
-
shutdown
public static void shutdown() -
assignDoNotLogResponseBody
if response body contains sensitive info and shouldnt be logged- Parameters:
theDoNotLogResponseBody
-- Returns:
- this for chaining
-
assignDoNotLogRequestBody
if request body contains sensitive info and shouldnt be logged- Parameters:
theDoNotLogRequestBody
-- Returns:
- this for chaining
-
assignDoNotLogParameters
-
assignDoNotLogHeaders
-
assignDoNotLogParameters
-
assignDoNotLogHeaders
-
getDoNotLogParameters
-
getDoNotLogHeaders
-
isDoNotLogResponseBody
public boolean isDoNotLogResponseBody() -
assignGrouperHttpMethod
-
assignGrouperHttpMethod
-
assignUrl
Sets the url being called.- Parameters:
_url
- the new url being called
-
assignProxyUrl
proxy url to proxy to (override other configuration)- Parameters:
proxyUrl1
-- Returns:
- this for chaining
-
assignProxyType
proxy type to override other configuration- Parameters:
grouperProxyType1
-- Returns:
- this for chaining
-
assignProxyType
proxy type to override other configuration- Parameters:
grouperProxyType1
-- Returns:
- this for chaining
-
assignUser
Sets the user for basic auth.- Parameters:
_user
- the new user for basic auth
-
assignPassword
Sets the password for basic auth.- Parameters:
_password
- the new password for basic auth
-
addBodyParameter
Add a parameter to the BODY for a POST body form.- Parameters:
key
- is the name.value
- is the value.
-
addUrlParameter
Add a parameter to the URL.- Parameters:
key
- is the name.value
- is the value.
-
addHeader
Adds a parameter to the header.- Parameters:
key
- is the name.value
- is the value.
-
assignBodyBytes
Sets the body to send.- Parameters:
_body
- the body to send
-
assignBody
Sets the body to send.- Parameters:
_body
- the body to send
-
getResponseBody
Get the response body of the call.- Returns:
- the responseBodyHolder
-
getResponseCode
public int getResponseCode()Get the response code.- Returns:
- the responseCode
-
assignTrustStore
Truststore (.jks) to add dynamically to list of truststores.- Parameters:
_trustStore
- the trustStore to set.
-
assignTrustStorePassword
Password for truststore.- Parameters:
_trustStoreKey
- the trustStoreKey to set.
-
assignHttpResponseBodyCallback
public GrouperHttpClient assignHttpResponseBodyCallback(GrouperHttpResponseBodyCallback httpResponseBodyCallback) -
addFileToSend
- Parameters:
filename
- The name of the file to send.file
- The file to send.
-
getResponseHeaders
Any headers sent back in the resopnse.- Returns:
- the responseHeaders
-
getResponseHeadersLower
Any headers (lower case) sent back in the resopnse.- Returns:
- the responseHeadersLower
-
assignResponseHeaders
Any headers sent back in the resopnse.- Parameters:
_responseHeaders
- the responseHeaders to set
-
assignHttpMultipartMode
public GrouperHttpClient assignHttpMultipartMode(org.apache.http.entity.mime.HttpMultipartMode _httpMultipartMode) Use to override HttpMultipartMode.BROWSER_COMPATIBLE which is the default.- Parameters:
_httpMultipartMode
- the httpMultipartMode to set
-
assignResponseAsFile
Whether you want the response as a file as opposed to a string in memory.- Parameters:
_responseAsFile
- the responseAsFile to set,
-
getResponseFile
If getting the reponse body as a file, this is the file.- Returns:
- the responseFile.
-
assignResponseFile
If getting the reponse body as a file, this is the file.- Parameters:
_responseFile
- the responseFile to set.
-
getResponseFileName
The filename to use for the response, if the response is retrieved as a file.- Returns:
- the responseFileName
-
assignResponseFileName
The filename to use for the response, if the response is retrieved as a file.- Parameters:
_responseFileName
- the responseFileName to set
-
assignTrust
Trust regardless of cert; ONLY use when you KNOW the endpoint.- Parameters:
_trust
- the trust to set
-
responseBodyAsString
Get the body of the response as a string.- Parameters:
httpMethod
- is the method.- Returns:
- the string.
-
responseBodyAsString
public static String responseBodyAsString(org.apache.http.client.methods.CloseableHttpResponse closeableHttpResponse) Get the body of the response as a string.- Parameters:
closeableHttpResponse
- is the response.- Returns:
- the string.
-
basicAuthenticationString
Create a basic authentication string.- Parameters:
login
- is the login.password
- is the password.- Returns:
- "Basic login:password" where login:password is Base64 encoded.
-
assignRetries
how many times to retry for a non fatal error on idempotent requests- Parameters:
theRetries
-- Returns:
- retries
-
assignTimeoutMillies
-
setGrouperHttpClientSetupAuthorization
public void setGrouperHttpClientSetupAuthorization(GrouperHttpClientSetupAuthorization grouperHttpClientSetupAuthorization) implement this interface to have the grouper http client have the callback to set up the new authorization if there's a retry or delay -
setThrottlingCallback
implement this interface to customize to set custom condition on which you want to retry -
setRetryForThrottlingOrNetworkIssues
public void setRetryForThrottlingOrNetworkIssues(int retryForThrottlingOrNetworkIssues) if there's a 429 or a connection timed out exception then delay for sometime and retry these many times. -
setRetryForThrottlingOrNetworkIssuesSleepMillis
public void setRetryForThrottlingOrNetworkIssuesSleepMillis(long retryForThrottlingOrNetworkIssuesSleepMillis) if there's a 429 or a connection timed out exception then retry and sleep for these many millis -
setRetryForThrottlingOrNetworkIssuesBackOffMillis
public void setRetryForThrottlingOrNetworkIssuesBackOffMillis(int retryForThrottlingOrNetworkIssuesBackOffMillis) if there's a 429 or connection timed out exception then after each retry, add to the sleep these many millis -
getRetryForThrottlingTimesItWasRetried
public int getRetryForThrottlingTimesItWasRetried()if there's a 429 or connection timed out then count how many times we retried for logging -
executeRequest
Execute a post with the given parameters, set teh code and the response into the call.
-
assignRequestBodyCharType
public GrouperHttpClient assignRequestBodyCharType(org.apache.http.entity.ContentType theRequestBodyCharType) look in StringEntity for usage- Parameters:
theRequestBodyCharType
-- Returns:
- this for chaining
-
assignRequestBodyCharset
e.g. Consts.ISO_8859_1- Parameters:
theRequestBodyCharset
-- Returns:
- this for chaining
-
newStringEntity
public org.apache.http.entity.StringEntity newStringEntity(String theBody) throws UnsupportedEncodingException - Throws:
UnsupportedEncodingException
-
logCurrent
get the current log log start -
logEnd
stop a debug log in a finally block- Returns:
- the log message
-
logStart
start a static debug log log start -
assignDebugMap
debug map for timing and result code- Parameters:
debugMap
-- Returns:
- this for chaining
-
retrieveJsonNode
public com.fasterxml.jackson.databind.JsonNode retrieveJsonNode()get the json node of the response (generate if not there already)- Returns:
- the json node
-
assignAssertResponseCode
if the response code is not this, then exception and log response- Parameters:
expectedCode
-- Returns:
- this for chaining
-
assignAssertJsonPointer
check a json pointer for a value to see if request is success. note that the parsed JsonNode is available too. json pointer e.g. /a/b/c- Parameters:
assertJsonPointer1
-- Returns:
- this for chaining
-
assignAssertJsonPointerExpectedValueString
public GrouperHttpClient assignAssertJsonPointerExpectedValueString(String assertJsonPointerExpectedValueString1) if the response code is not this, then exception and log response- Parameters:
assertJsonPointerExpectedValueString1
-- Returns:
- this for chaining
-