Package com.rapid7.appspider
Class ContentHelper
- java.lang.Object
-
- com.rapid7.appspider.ContentHelper
-
public class ContentHelper extends Object
parsing and serializing helper methods for handling JSONObject manipulation
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Optional<org.json.JSONObject>asJson(org.apache.http.HttpEntity entity)converts the provided HttpEntity to a JSONObjectOptional<Map<String,String>>asMapOfStringToString(String key, String value, Optional<org.json.JSONObject> optionalJsonObject)extracts the key/value pairs from JSONObject and returns them as a Map{String, String}static ContentHelpercreateInstanceOrThrow(LoggerFacade logger)org.apache.http.entity.StringEntityentityFrom(org.apache.http.NameValuePair... pairs)simple wrapper around entityFrom(JSONObject) that uses jsonFrom to build the JSONObjectorg.apache.http.entity.StringEntityentityFrom(org.json.JSONObject jsonObject)builds a StringEntity using the string provided by jsonObject.toString()Optional<org.json.JSONArray>getArrayFrom(org.json.JSONObject jsonObject, String key)returns JSONArray from jsonObject matching keyOptional<InputStream>getInputStream(org.apache.http.HttpEntity entity)returns InputStream for the content of entityOptional<String>getTextHtmlOrXmlContent(org.apache.http.HttpEntity entity)returns text/html or text/html content from enttity if found; otherwise Optional.empty()org.json.JSONObjectjsonFrom(org.apache.http.NameValuePair... pairs)builds a simple JSON object from name value pairsorg.apache.http.NameValuePairpairFrom(String key, String value)simple wrapper around BasicNameValuePair constructor provided to shorten syntaxOptional<org.json.JSONObject>responseToJSONObject(org.apache.http.HttpResponse response, String path)extracts the response content into a JSONObject
-
-
-
Method Detail
-
createInstanceOrThrow
public static ContentHelper createInstanceOrThrow(LoggerFacade logger)
-
getArrayFrom
public Optional<org.json.JSONArray> getArrayFrom(org.json.JSONObject jsonObject, String key)
returns JSONArray from jsonObject matching key- Parameters:
jsonObject- JSONObject containing JSONArray using keykey- key of the JSONArray within jsonObject- Returns:
- Optional containing JSONArray if present; otherwise Optional.empty()
-
jsonFrom
public org.json.JSONObject jsonFrom(org.apache.http.NameValuePair... pairs)
builds a simple JSON object from name value pairs- Parameters:
pairs- name value pairs to add to the new JSON Object- Returns:
- JSONObject constructed from pairs
-
entityFrom
public org.apache.http.entity.StringEntity entityFrom(org.json.JSONObject jsonObject)
builds a StringEntity using the string provided by jsonObject.toString()- Parameters:
jsonObject- jsonObject which provides String via toString- Returns:
- StringEntity containing the string value of jsonObject
-
entityFrom
public org.apache.http.entity.StringEntity entityFrom(org.apache.http.NameValuePair... pairs) throws UnsupportedEncodingExceptionsimple wrapper around entityFrom(JSONObject) that uses jsonFrom to build the JSONObject- Parameters:
pairs- passed to jsonFrom- Returns:
- entity returned from entityFrom(JSONObject)
- Throws:
UnsupportedEncodingException- thrown from entityFrom(JSONObject)
-
pairFrom
public org.apache.http.NameValuePair pairFrom(String key, String value)
simple wrapper around BasicNameValuePair constructor provided to shorten syntax- Parameters:
key- key of the new NameValuePairvalue- value of the new NameValuePair- Returns:
- NameValuePair of key and value
-
responseToJSONObject
public Optional<org.json.JSONObject> responseToJSONObject(org.apache.http.HttpResponse response, String path)
extracts the response content into a JSONObject- Parameters:
response- response to extract JSONObject frompath- the path used for diagnostic purposes, this is the path to the request which can be logged in the event of failure- Returns:
- on success an Optional containing a JSONObject; otherwise, Optional.empty()
-
asJson
public Optional<org.json.JSONObject> asJson(org.apache.http.HttpEntity entity)
converts the provided HttpEntity to a JSONObject- Parameters:
entity- entity to convert- Returns:
- on success an Optional containing a JSONObject; otherwise, Optional.empty()
-
asMapOfStringToString
public Optional<Map<String,String>> asMapOfStringToString(String key, String value, Optional<org.json.JSONObject> optionalJsonObject)
extracts the key/value pairs from JSONObject and returns them as a Map{String, String}- Parameters:
key- key in the json object to serve as key in the mapvalue- value in the json object to serve as value in the mapoptionalJsonObject- Optional{JSONObject} to extract key/value pairs from if present- Returns:
- on successes a Map{String, String} of key/value pairs from JSONObject; otherwise Optional.empty()
-
getTextHtmlOrXmlContent
public Optional<String> getTextHtmlOrXmlContent(org.apache.http.HttpEntity entity)
returns text/html or text/html content from enttity if found; otherwise Optional.empty()- Parameters:
entity- entity containing the String content to return- Returns:
- Optional containing the String content of entity on success; otherwise, Optional.empty()
-
getInputStream
public Optional<InputStream> getInputStream(org.apache.http.HttpEntity entity)
returns InputStream for the content of entity- Parameters:
entity- entity to return InputStream of content for- Returns:
- Optional containing InputStream on success; otherwise, Optional.empty()
-
-