Package ch.autumo.commons.utils.web
Class RESTUtils
java.lang.Object
ch.autumo.commons.utils.web.RESTUtils
REST Utils.
-
Field Summary
FieldsModifier 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 StringJSON entity assign char.static final StringJSON HubSpot field value.static final StringJSON parameter assign char.static final StringJSON param-value delimiter.static StringJSON record begin char.static StringJSON record begin char.static final StringJSON value delimiter.static final StringChar set. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddUseOnceHeader(String[] useOnceHeader) Add a header that is used only once when creating the next PUT/POST or GET,static com.fasterxml.jackson.databind.JsonNodecallJSONGet(String url, String param, String value) Call JSON get.static voidcheckHttpResponse(org.apache.http.client.methods.CloseableHttpResponse resp) Check closeable HTTP response with HttpResponse.static voidcheckResponse(int code, String message) Check HTTP response with status code.static StringcreateJSONBody(Map<String, String> data) Create a JSON POST according to given map.static org.apache.http.client.methods.HttpGetcreateJSONGet(String request) Create JSON Get.static org.apache.http.client.methods.HttpGetcreateJSONGet(String request, String headerName, String headerValue) Create JSON get with a header.static org.apache.http.client.methods.HttpPostcreateJSONPost(String request) Create JSON post.static org.apache.http.client.methods.HttpPostcreateJSONPost(String request, String headerName, String headerValue) Create JSON post.static org.apache.http.client.methods.HttpPutcreateJSONPut(String request) Create JSON put.static org.apache.http.entity.StringEntitycreateJSONStringEntity(String jsonBody) Create UTF-8 string entity for JSON body.static StringcreatePrintableUrl(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 StringenrichQuery(String url, String parameters) Enrich URL with already packed parameters.static StringenrichQuery(String url, String name, Object value) Enrich URL with parameters.static StringenrichQuery(String url, Map<String, String> params) Enrich URL with parameters.static org.json.JSONArrayextractJsonArray(org.json.JSONObject json, String rootPath) Extract JSON array from path.static ObjectextractJsonObject(org.json.JSONObject json, String path) Extracts JSON object value from path.static ObjectextractJsonObjectOld(org.json.JSONObject json, String path) Deprecated.static booleangetBooleanValue(org.json.JSONObject object, String key) Get boolean value from JSON object.static StringgetHubSpotValue(org.json.JSONObject props, String key) Get value from HubSpot JSON object.static org.json.JSONObjectgetJsonObjectFromResponse(org.apache.http.client.methods.CloseableHttpResponse response) Get JSON object from traditional HTTP response.static StringgetJsonString(org.json.JSONObject json) String string of JSON object.static StringgetJsonStringFromResponse(com.fasterxml.jackson.databind.JsonNode response) Get JSON string from JSON node response (typed HttpReponse).static StringgetJsonStringFromResponse(org.apache.http.client.methods.CloseableHttpResponse response) Get JSON string from traditional HTTP response.static longgetNumberValue(org.json.JSONObject object, String key) Get number value from a JSON object.static StringGet a simple string response.static StringgetTextValue(org.json.JSONObject object, String key) Get text value from a JSON object.static HttpURLConnectionprepareConnection(String url, byte[] content) Prepare connection.static HttpURLConnectionprepareConnection(String url, byte[] content, int connTimeout, int readTimeout) Prepare connection.static voiduseApplicationHeader(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.
-
-
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
Extracts JSON object value from path. The path elements are separated by '.'. You can use a direct array index access, e.g. 'pathpart1.pathpart2[3].value'. You can also specify an entire array access within a path, but then all other array references must refer to the same dimension. In this case, an array is returned and the array should be flattened for meaningful data. For array access use 'pathpart1.pathpart2[*].value'. The array access '[*]' must be the penultimate part of the path. After that, only a value access partial path can be specified last. Though, it isn't used for further extraction when an array is returned; the caller must extract the last path part!- Parameters:
json- JSON objectpath- the SON path.- Returns:
- extracted object, can be a String, an Integer or even a JSONArray!
- Throws:
UtilsException- if an invalid access occurs.
-
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)