Class RESTUtils

java.lang.Object
ch.autumo.commons.utils.web.RESTUtils

public class RESTUtils extends Object
REST Utils.
  • Field Details

    • JSON_RECORD_BEGIN_CHARACTER

      public static String JSON_RECORD_BEGIN_CHARACTER
      JSON record begin char.
    • JSON_RECORD_END_CHARACTER

      public static String JSON_RECORD_END_CHARACTER
      JSON record begin char.
    • JSON_PARAM_VALUE_DELIMITER

      public static final String JSON_PARAM_VALUE_DELIMITER
      JSON param-value delimiter.
      See Also:
    • JSON_VALUE_DELIMITER

      public static final String JSON_VALUE_DELIMITER
      JSON value delimiter.
      See Also:
    • JSON_PARAM_ASSIGN_CHAR

      public static final String JSON_PARAM_ASSIGN_CHAR
      JSON parameter assign char.
      See Also:
    • JSON_ENTITY_ASSIGN_CHAR

      public static final String JSON_ENTITY_ASSIGN_CHAR
      JSON entity assign char.
      See Also:
    • JSON_HUBSPOT_FIELD_VALUE

      public static final String JSON_HUBSPOT_FIELD_VALUE
      JSON HubSpot field value.
      See Also:
    • HTTP_HEADER_ACCEPT_JSON

      public static final String[] HTTP_HEADER_ACCEPT_JSON
      HTTP Header accept with 'application/json'.
    • HTTP_HEADER_ACCEPT_HAL_JSON

      public static final String[] HTTP_HEADER_ACCEPT_HAL_JSON
      HTTP Header accept with 'application/hal+json'.
    • HTTP_HEADER_CONTENTTYPE_JSON_UTF8

      public static final String[] HTTP_HEADER_CONTENTTYPE_JSON_UTF8
      HTTP Header accept with 'application/json; charset=utf-8'.
    • UTF_8

      public static final String UTF_8
      Char set.
  • Constructor Details

    • RESTUtils

      public RESTUtils()
  • Method Details

    • useApplicationHeader

      public static void useApplicationHeader(String[] appHeader)
      Set standard application header.
      Parameters:
      appHeader - app header
    • addUseOnceHeader

      public static void addUseOnceHeader(String[] useOnceHeader)
      Add a header that is used only once when creating the next PUT/POST or GET,
      Parameters:
      useOnceHeader - header that should be used only once
    • createJSONBody

      public static String createJSONBody(Map<String,String> data)
      Create a JSON POST according to given map. E.g. '{"param1":"value1","param2":"value2","param3":"value3"}'.
      Parameters:
      data - keys and values
      Returns:
      JSON body
    • createUrlByteBody

      public static byte[] createUrlByteBody(Map<String,String> data) throws IOException
      Create a Url byte body according to given map, UTF-8 encoded.
      Parameters:
      data - keys and values
      Returns:
      byte body
      Throws:
      IOException - if URL encoding fails
    • prepareConnection

      public static HttpURLConnection prepareConnection(String url, byte[] content, int connTimeout, int readTimeout) throws IOException
      Prepare connection.
      Parameters:
      url - URL
      content - byte content
      connTimeout - connection timeout in milliseconds
      readTimeout - connection timeout in milliseconds
      Returns:
      prepared connection
      Throws:
      IOException - if URL connection cannot be opened
    • prepareConnection

      public static HttpURLConnection prepareConnection(String url, byte[] content) throws IOException
      Prepare connection.
      Parameters:
      url - URL
      content - byte content
      Returns:
      prepared connection
      Throws:
      IOException - if URL commection cannot be opened
    • getResponseAndClose

      public static String getResponseAndClose(HttpURLConnection con) throws IOException
      Get a simple string response.
      Parameters:
      con - connection
      Returns:
      response
      Throws:
      IOException - if response cannot be read
    • extractJsonArray

      public static org.json.JSONArray extractJsonArray(org.json.JSONObject json, String rootPath) throws UtilsException
      Extract JSON array from path. The path elements are separated by '.'; If we reach an array before the path, this one is returned. because we cannot guess indexes! That would be a bad API!
      Parameters:
      json - json object
      rootPath - the ifaceX-internally JSON path.
      Returns:
      extracted JSON array
      Throws:
      UtilsException - when another JSON class than JSONObject or JSONArray is received before reaching the last path element
    • extractJsonObject

      public static Object extractJsonObject(org.json.JSONObject json, String path) throws UtilsException
      Extract JSON object or value from path. The path elements are separated by '.';
      Parameters:
      json - json object
      path - the ifaceX-internally JSON path.
      Returns:
      extracted object, can be a String, an Integer or a JSONArray
      Throws:
      UtilsException - when another JSON class than JSONObject is received before reaching the last path element
    • extractJsonObjectOld

      @Deprecated public static Object extractJsonObjectOld(org.json.JSONObject json, String path) throws UtilsException
      OLD: Extract JSON object or value from path. The path elements are separated by '.';
      Parameters:
      json - json object
      path - the ifaceX-internally JSON path.
      Returns:
      extracted object, can be a String, an Integer or a JSONArray
      Throws:
      UtilsException - when another JSON class than JSONObject is received before reaching the last path element
    • getJsonString

      public static String getJsonString(org.json.JSONObject json)
      String string of JSON object.
      Parameters:
      json - json object
      Returns:
      json string
    • getJsonStringFromResponse

      public static String getJsonStringFromResponse(com.fasterxml.jackson.databind.JsonNode response) throws IOException
      Get JSON string from JSON node response (typed HttpReponse).
      Parameters:
      response - JSON node response
      Returns:
      JSON string
      Throws:
      IOException - if JSON cannot be extracted from response
    • getJsonStringFromResponse

      public static String getJsonStringFromResponse(org.apache.http.client.methods.CloseableHttpResponse response) throws IOException
      Get JSON string from traditional HTTP response.
      Parameters:
      response - HTTP response
      Returns:
      JSON string
      Throws:
      IOException - if JSON cannot be extracted from response
    • getJsonObjectFromResponse

      public static org.json.JSONObject getJsonObjectFromResponse(org.apache.http.client.methods.CloseableHttpResponse response) throws IOException
      Get JSON object from traditional HTTP response.
      Parameters:
      response - HTTP response
      Returns:
      JSON object
      Throws:
      IOException - if JSON cannot be extracted from response
    • createJSONStringEntity

      public static org.apache.http.entity.StringEntity createJSONStringEntity(String jsonBody)
      Create UTF-8 string entity for JSON body.
      Parameters:
      jsonBody - JSON body
      Returns:
      string entity
    • createJSONPut

      public static org.apache.http.client.methods.HttpPut createJSONPut(String request)
      Create JSON put.
      Parameters:
      request - request
      Returns:
      PUT
    • createJSONGet

      public static org.apache.http.client.methods.HttpGet createJSONGet(String request)
      Create JSON Get.
      Parameters:
      request - request
      Returns:
      GET
    • createJSONGet

      public static org.apache.http.client.methods.HttpGet createJSONGet(String request, String headerName, String headerValue)
      Create JSON get with a header.
      Parameters:
      request - request
      headerName - header name
      headerValue - header value
      Returns:
      GET
    • createJSONPost

      public static org.apache.http.client.methods.HttpPost createJSONPost(String request)
      Create JSON post.
      Parameters:
      request - request
      Returns:
      POST
    • createJSONPost

      public static org.apache.http.client.methods.HttpPost createJSONPost(String request, String headerName, String headerValue)
      Create JSON post.
      Parameters:
      request - request
      headerName - header name
      headerValue - header value
      Returns:
      POST
    • enrichQuery

      public static String enrichQuery(String url, String parameters)
      Enrich URL with already packed parameters.
      Parameters:
      url - url
      parameters - parameters
      Returns:
      new url
    • enrichQuery

      public static String enrichQuery(String url, String name, Object value)
      Enrich URL with parameters.
      Parameters:
      url - url
      name - parameter name
      value - parameter value
      Returns:
      new url
    • enrichQuery

      public static String enrichQuery(String url, Map<String,String> params)
      Enrich URL with parameters.
      Parameters:
      url - url
      params - parameter map
      Returns:
      new url
    • callJSONGet

      public static com.fasterxml.jackson.databind.JsonNode callJSONGet(String url, String param, String value) throws UtilsException
      Call JSON get.
      Parameters:
      url - url
      param - parameter key
      value - value
      Returns:
      JSON result
      Throws:
      UtilsException - if response is not OK
    • checkResponse

      public static void checkResponse(int code, String message) throws UtilsException
      Check HTTP response with status code.
      Parameters:
      code - HTTP status code
      message - HTTP message
      Throws:
      UtilsException - if response is not OK
    • checkHttpResponse

      public static void checkHttpResponse(org.apache.http.client.methods.CloseableHttpResponse resp) throws UtilsException
      Check closeable HTTP response with HttpResponse.
      Parameters:
      resp - HTTP response
      Throws:
      UtilsException - if response is not OK
    • getNumberValue

      public static long getNumberValue(org.json.JSONObject object, String key)
      Get number value from a JSON object.
      Parameters:
      object - JSON object
      key - key for value
      Returns:
      number value
    • getTextValue

      public static String getTextValue(org.json.JSONObject object, String key)
      Get text value from a JSON object.
      Parameters:
      object - JSON object
      key - key for value
      Returns:
      text value
    • getBooleanValue

      public static boolean getBooleanValue(org.json.JSONObject object, String key)
      Get boolean value from JSON object.
      Parameters:
      object - JSON object
      key - boolean value
      Returns:
      value
    • getHubSpotValue

      public static String getHubSpotValue(org.json.JSONObject props, String key)
      Get value from HubSpot JSON object. NOTE: This is HubSpot-specific.
      Parameters:
      props - JSON object
      key - key for value
      Returns:
      value
    • createPrintableUrl

      public static String createPrintableUrl(String apiKeyName, String url)
      Create an URL with hidden API key.
      Parameters:
      apiKeyName - API key name
      url - the url where the API keys needs to hide
      Returns:
      URL with hidden API key