Package ch.autumo.commons.utils.web
Class RESTUtils
java.lang.Object
ch.autumo.commons.utils.web.RESTUtils
REST Utils.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String[]
HTTP Header accept with 'application/hal+json'.static final String[]
HTTP Header accept with 'application/json'.static final String[]
HTTP Header accept with 'application/json; charset=utf-8'.static final String
JSON entity assign char.static final String
JSON HubSpot field value.static final String
JSON parameter assign char.static final String
JSON param-value delimiter.static String
JSON record begin char.static String
JSON record begin char.static final String
JSON value delimiter.static final String
Char set. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
addUseOnceHeader
(String[] useOnceHeader) Add a header that is used only once when creating the next PUT/POST or GET,static com.fasterxml.jackson.databind.JsonNode
callJSONGet
(String url, String param, String value) Call JSON get.static void
checkHttpResponse
(org.apache.http.client.methods.CloseableHttpResponse resp) Check closeable HTTP response with HttpResponse.static void
checkResponse
(int code, String message) Check HTTP response with status code.static String
createJSONBody
(Map<String, String> data) Create a JSON POST according to given map.static org.apache.http.client.methods.HttpGet
createJSONGet
(String request) Create JSON Get.static org.apache.http.client.methods.HttpGet
createJSONGet
(String request, String headerName, String headerValue) Create JSON get with a header.static org.apache.http.client.methods.HttpPost
createJSONPost
(String request) Create JSON post.static org.apache.http.client.methods.HttpPost
createJSONPost
(String request, String headerName, String headerValue) Create JSON post.static org.apache.http.client.methods.HttpPut
createJSONPut
(String request) Create JSON put.static org.apache.http.entity.StringEntity
createJSONStringEntity
(String jsonBody) Create UTF-8 string entity for JSON body.static String
createPrintableUrl
(String apiKeyName, String url) Create an URL with hidden API key.static byte[]
createUrlByteBody
(Map<String, String> data) Create a Url byte body according to given map, UTF-8 encoded.static String
enrichQuery
(String url, String parameters) Enrich URL with already packed parameters.static String
enrichQuery
(String url, String name, Object value) Enrich URL with parameters.static String
enrichQuery
(String url, Map<String, String> params) Enrich URL with parameters.static org.json.JSONArray
extractJsonArray
(org.json.JSONObject json, String rootPath) Extract JSON array from path.static Object
extractJsonObject
(org.json.JSONObject json, String path) Extract JSON object or value from path.static Object
extractJsonObjectOld
(org.json.JSONObject json, String path) Deprecated.static boolean
getBooleanValue
(org.json.JSONObject object, String key) Get boolean value from JSON object.static String
getHubSpotValue
(org.json.JSONObject props, String key) Get value from HubSpot JSON object.static org.json.JSONObject
getJsonObjectFromResponse
(org.apache.http.client.methods.CloseableHttpResponse response) Get JSON object from traditional HTTP response.static String
getJsonString
(org.json.JSONObject json) String string of JSON object.static String
getJsonStringFromResponse
(com.fasterxml.jackson.databind.JsonNode response) Get JSON string from JSON node response (typed HttpReponse).static String
getJsonStringFromResponse
(org.apache.http.client.methods.CloseableHttpResponse response) Get JSON string from traditional HTTP response.static long
getNumberValue
(org.json.JSONObject object, String key) Get number value from a JSON object.static String
Get a simple string response.static String
getTextValue
(org.json.JSONObject object, String key) Get text value from a JSON object.static HttpURLConnection
prepareConnection
(String url, byte[] content) Prepare connection.static HttpURLConnection
prepareConnection
(String url, byte[] content, int connTimeout, int readTimeout) Prepare connection.static void
useApplicationHeader
(String[] appHeader) Set standard application header.
-
Field Details
-
JSON_RECORD_BEGIN_CHARACTER
JSON record begin char. -
JSON_RECORD_END_CHARACTER
JSON record begin char. -
JSON_PARAM_VALUE_DELIMITER
JSON param-value delimiter.- See Also:
-
JSON_VALUE_DELIMITER
JSON value delimiter.- See Also:
-
JSON_PARAM_ASSIGN_CHAR
JSON parameter assign char.- See Also:
-
JSON_ENTITY_ASSIGN_CHAR
JSON entity assign char.- See Also:
-
JSON_HUBSPOT_FIELD_VALUE
JSON HubSpot field value.- See Also:
-
HTTP_HEADER_ACCEPT_JSON
HTTP Header accept with 'application/json'. -
HTTP_HEADER_ACCEPT_HAL_JSON
HTTP Header accept with 'application/hal+json'. -
HTTP_HEADER_CONTENTTYPE_JSON_UTF8
HTTP Header accept with 'application/json; charset=utf-8'. -
UTF_8
Char set.
-
-
Constructor Details
-
RESTUtils
public RESTUtils()
-
-
Method Details
-
useApplicationHeader
Set standard application header.- Parameters:
appHeader
- app header
-
addUseOnceHeader
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
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
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
- URLcontent
- byte contentconnTimeout
- connection timeout in millisecondsreadTimeout
- connection timeout in milliseconds- Returns:
- prepared connection
- Throws:
IOException
- if URL connection cannot be opened
-
prepareConnection
Prepare connection.- Parameters:
url
- URLcontent
- byte content- Returns:
- prepared connection
- Throws:
IOException
- if URL commection cannot be opened
-
getResponseAndClose
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 objectrootPath
- 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
Extract JSON object or value from path. The path elements are separated by '.';- Parameters:
json
- json objectpath
- 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 Deprecated.OLD: Extract JSON object or value from path. The path elements are separated by '.';- Parameters:
json
- json objectpath
- 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
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
Create UTF-8 string entity for JSON body.- Parameters:
jsonBody
- JSON body- Returns:
- string entity
-
createJSONPut
Create JSON put.- Parameters:
request
- request- Returns:
- PUT
-
createJSONGet
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
- requestheaderName
- header nameheaderValue
- header value- Returns:
- GET
-
createJSONPost
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
- requestheaderName
- header nameheaderValue
- header value- Returns:
- POST
-
enrichQuery
Enrich URL with already packed parameters.- Parameters:
url
- urlparameters
- parameters- Returns:
- new url
-
enrichQuery
Enrich URL with parameters.- Parameters:
url
- urlname
- parameter namevalue
- parameter value- Returns:
- new url
-
enrichQuery
Enrich URL with parameters.- Parameters:
url
- urlparams
- 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
- urlparam
- parameter keyvalue
- value- Returns:
- JSON result
- Throws:
UtilsException
- if response is not OK
-
checkResponse
Check HTTP response with status code.- Parameters:
code
- HTTP status codemessage
- 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
Get number value from a JSON object.- Parameters:
object
- JSON objectkey
- key for value- Returns:
- number value
-
getTextValue
Get text value from a JSON object.- Parameters:
object
- JSON objectkey
- key for value- Returns:
- text value
-
getBooleanValue
Get boolean value from JSON object.- Parameters:
object
- JSON objectkey
- boolean value- Returns:
- value
-
getHubSpotValue
Get value from HubSpot JSON object. NOTE: This is HubSpot-specific.- Parameters:
props
- JSON objectkey
- key for value- Returns:
- value
-
createPrintableUrl
Create an URL with hidden API key.- Parameters:
apiKeyName
- API key nameurl
- the url where the API keys needs to hide- Returns:
- URL with hidden API key
-
extractJsonObject(JSONObject, String)