Class GrouperHttpClient

java.lang.Object
edu.internet2.middleware.grouper.util.GrouperHttpClient

public class GrouperHttpClient extends Object

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 Details

    • GrouperHttpClient

      public GrouperHttpClient()
  • Method Details

    • main

      public static void main(String[] args)
    • shutdown

      public static void shutdown()
    • assignDoNotLogResponseBody

      public GrouperHttpClient assignDoNotLogResponseBody(boolean theDoNotLogResponseBody)
      if response body contains sensitive info and shouldnt be logged
      Parameters:
      theDoNotLogResponseBody -
      Returns:
      this for chaining
    • assignDoNotLogRequestBody

      public GrouperHttpClient assignDoNotLogRequestBody(boolean theDoNotLogRequestBody)
      if request body contains sensitive info and shouldnt be logged
      Parameters:
      theDoNotLogRequestBody -
      Returns:
      this for chaining
    • assignDoNotLogParameters

      public GrouperHttpClient assignDoNotLogParameters(String paramsCommaSeparated)
    • assignDoNotLogHeaders

      public GrouperHttpClient assignDoNotLogHeaders(String headersCommaSeparated)
    • assignDoNotLogParameters

      public GrouperHttpClient assignDoNotLogParameters(Set<String> params)
    • assignDoNotLogHeaders

      public GrouperHttpClient assignDoNotLogHeaders(Set<String> headers)
    • getDoNotLogParameters

      public Set<String> getDoNotLogParameters()
    • getDoNotLogHeaders

      public Set<String> getDoNotLogHeaders()
    • isDoNotLogResponseBody

      public boolean isDoNotLogResponseBody()
    • assignGrouperHttpMethod

      public GrouperHttpClient assignGrouperHttpMethod(GrouperHttpMethod grouperHttpMethod)
    • assignGrouperHttpMethod

      public GrouperHttpClient assignGrouperHttpMethod(String grouperHttpMethodType)
    • assignUrl

      public GrouperHttpClient assignUrl(String _url)
      Sets the url being called.
      Parameters:
      _url - the new url being called
    • assignProxyUrl

      public GrouperHttpClient assignProxyUrl(String proxyUrl1)
      proxy url to proxy to (override other configuration)
      Parameters:
      proxyUrl1 -
      Returns:
      this for chaining
    • assignProxyType

      public GrouperHttpClient assignProxyType(GrouperProxyType grouperProxyType1)
      proxy type to override other configuration
      Parameters:
      grouperProxyType1 -
      Returns:
      this for chaining
    • assignProxyType

      public GrouperHttpClient assignProxyType(String grouperProxyTypeString)
      proxy type to override other configuration
      Parameters:
      grouperProxyType1 -
      Returns:
      this for chaining
    • assignUser

      public GrouperHttpClient assignUser(String _user)
      Sets the user for basic auth.
      Parameters:
      _user - the new user for basic auth
    • assignPassword

      public GrouperHttpClient assignPassword(String _password)
      Sets the password for basic auth.
      Parameters:
      _password - the new password for basic auth
    • addBodyParameter

      public GrouperHttpClient addBodyParameter(String key, String value)
      Add a parameter to the BODY for a POST body form.
      Parameters:
      key - is the name.
      value - is the value.
    • addUrlParameter

      public GrouperHttpClient addUrlParameter(String key, String value)
      Add a parameter to the URL.
      Parameters:
      key - is the name.
      value - is the value.
    • addHeader

      public GrouperHttpClient addHeader(String key, String value)
      Adds a parameter to the header.
      Parameters:
      key - is the name.
      value - is the value.
    • assignBodyBytes

      public GrouperHttpClient assignBodyBytes(byte[] _body)
      Sets the body to send.
      Parameters:
      _body - the body to send
    • assignBody

      public GrouperHttpClient assignBody(String _body)
      Sets the body to send.
      Parameters:
      _body - the body to send
    • getResponseBody

      public String getResponseBody()
      Get the response body of the call.
      Returns:
      the responseBodyHolder
    • getResponseCode

      public int getResponseCode()
      Get the response code.
      Returns:
      the responseCode
    • assignTrustStore

      public GrouperHttpClient assignTrustStore(File _trustStore)
      Truststore (.jks) to add dynamically to list of truststores.
      Parameters:
      _trustStore - the trustStore to set.
    • assignTrustStorePassword

      public GrouperHttpClient assignTrustStorePassword(String _trustStoreKey)
      Password for truststore.
      Parameters:
      _trustStoreKey - the trustStoreKey to set.
    • assignHttpResponseBodyCallback

      public GrouperHttpClient assignHttpResponseBodyCallback(GrouperHttpResponseBodyCallback httpResponseBodyCallback)
    • addFileToSend

      public GrouperHttpClient addFileToSend(String filename, File file)
      Parameters:
      filename - The name of the file to send.
      file - The file to send.
    • getResponseHeaders

      public Map<String,String> getResponseHeaders()
      Any headers sent back in the resopnse.
      Returns:
      the responseHeaders
    • getResponseHeadersLower

      public Map<String,String> getResponseHeadersLower()
      Any headers (lower case) sent back in the resopnse.
      Returns:
      the responseHeadersLower
    • assignResponseHeaders

      public GrouperHttpClient assignResponseHeaders(Map<String,String> _responseHeaders)
      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

      public GrouperHttpClient assignResponseAsFile(boolean _responseAsFile)
      Whether you want the response as a file as opposed to a string in memory.
      Parameters:
      _responseAsFile - the responseAsFile to set,
    • getResponseFile

      public File getResponseFile()
      If getting the reponse body as a file, this is the file.
      Returns:
      the responseFile.
    • assignResponseFile

      public GrouperHttpClient assignResponseFile(File _responseFile)
      If getting the reponse body as a file, this is the file.
      Parameters:
      _responseFile - the responseFile to set.
    • getResponseFileName

      public String getResponseFileName()
      The filename to use for the response, if the response is retrieved as a file.
      Returns:
      the responseFileName
    • assignResponseFileName

      public GrouperHttpClient assignResponseFileName(String _responseFileName)
      The filename to use for the response, if the response is retrieved as a file.
      Parameters:
      _responseFileName - the responseFileName to set
    • assignTrust

      public GrouperHttpClient assignTrust(boolean _trust)
      Trust regardless of cert; ONLY use when you KNOW the endpoint.
      Parameters:
      _trust - the trust to set
    • responseBodyAsString

      public static String responseBodyAsString(org.apache.commons.httpclient.HttpMethod httpMethod)
      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

      public static String basicAuthenticationString(String login, String password)
      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

      public GrouperHttpClient assignRetries(int theRetries)
      how many times to retry for a non fatal error on idempotent requests
      Parameters:
      theRetries -
      Returns:
      retries
    • assignTimeoutMillies

      public GrouperHttpClient assignTimeoutMillies(int theTimeoutMillies)
    • 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

      public void setThrottlingCallback(GrouperHttpThrottlingCallback grouperHttpThrottlingCallback)
      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

      public GrouperHttpClient executeRequest()
      Execute a post with the given parameters, set teh code and the response into the call.
    • logCurrent

      public static GrouperHttpClientLog logCurrent()
      get the current log log start
    • logEnd

      public static String logEnd()
      stop a debug log in a finally block
      Returns:
      the log message
    • logStart

      public static boolean logStart(GrouperHttpClientLog grouperHttpCallLog)
      start a static debug log log start
    • assignDebugMap

      public GrouperHttpClient assignDebugMap(Map<String,Object> debugMap)
      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

      public GrouperHttpClient assignAssertResponseCode(int expectedCode)
      if the response code is not this, then exception and log response
      Parameters:
      expectedCode -
      Returns:
      this for chaining
    • assignAssertJsonPointer

      public GrouperHttpClient assignAssertJsonPointer(String assertJsonPointer1)
      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