Class ContentHelper


  • public class ContentHelper
    extends Object
    parsing and serializing helper methods for handling JSONObject manipulation
    • Method Detail

      • 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 key
        key - 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 UnsupportedEncodingException
        simple 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 NameValuePair
        value - 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 from
        path - 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 map
        value - value in the json object to serve as value in the map
        optionalJsonObject - 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()