Class ManUrlExt


  • public class ManUrlExt
    extends Object
    Adds extension methods to URL for handling JSON, plain text, and binary content.
    • Constructor Detail

      • ManUrlExt

        public ManUrlExt()
    • Method Detail

      • makeUrl

        public static URL makeUrl​(String url,
                                  Object jsonValue)
        Make a JSON-compatible URL with the arguments from the jsonValue. Encodes the arguments in UTF-8 and appends them to the list using standard URL query delimiters.

        If an individual argument is a Bindings or a List, it is transformed to JSON. Otherwise, the argument is coerced to a String and URL encoded.

      • encode

        public static String encode​(String urlPart)
        Convenience method to encode a URL string and not have to handle the UnsupportedEncodingException.
      • sendJsonRequest

        public static Object sendJsonRequest​(URL url,
                                             String httpMethod,
                                             Object jsonValue)
        Use HTTP GET, POST, PUT, or PATCH to send JSON bindings to a URL with a JSON response.
        Parameters:
        httpMethod - The HTTP method to use: "GET", "POST", "PUT", or "PATCH"
        jsonValue - A JSON value to send (primitive/boxed type, String, List of JSON values, or Bindings of String/JSON value)
        Returns:
        A JSON value parsed from the JSON response.
        See Also:
        sendRequest(URL, String, Object, Map, int)
      • sendYamlRequest

        public static Object sendYamlRequest​(URL url,
                                             String httpMethod,
                                             Object jsonValue)
        Use HTTP GET, POST, PUT, or PATCH to send JSON bindings to a URL with a YAML response.
        Parameters:
        httpMethod - The HTTP method to use: "GET", "POST", "PUT", or "PATCH"
        jsonValue - A JSON value to send (primitive/boxed type, String, List of JSON values, or Bindings of String/JSON value)
        Returns:
        A JSON value parsed from the YAML response.
        See Also:
        sendRequest(URL, String, Object, Map, int)
      • sendXmlRequest

        public static Object sendXmlRequest​(URL url,
                                            String httpMethod,
                                            Object jsonValue,
                                            Map<String,​String> headers,
                                            int timeout)
        Use HTTP GET, POST, PUT, or PATCH to send JSON bindings to a URL with a XML response.
        Parameters:
        httpMethod - The HTTP method to use: "GET", "POST", "PUT", or "PATCH"
        jsonValue - A JSON value to send (primitive/boxed type, String, List of JSON values, or Bindings of String/JSON value)
        Returns:
        A JSON bindings value parsed from the XML response.
        See Also:
        sendRequest(URL, String, Object, Map, int)
      • sendCsvRequest

        public static Object sendCsvRequest​(URL url,
                                            String httpMethod,
                                            Object jsonValue,
                                            Map<String,​String> headers,
                                            int timeout)
        Use HTTP GET, POST, PUT, or PATCH to send JSON bindings to a URL with a CSV response.
        Parameters:
        httpMethod - The HTTP method to use: "GET", "POST", "PUT", or "PATCH"
        jsonValue - A JSON value to send (primitive/boxed type, String, List of JSON values, or Bindings of String/JSON value)
        Returns:
        A JSON bindings value parsed from the CSV response.
        See Also:
        sendRequest(URL, String, Object, Map, int)
      • sendPlainTextRequest

        public static String sendPlainTextRequest​(URL url,
                                                  String httpMethod,
                                                  Object jsonValue)
        Use HTTP GET, POST, PUT, or PATCH to send JSON bindings to a URL with a plain text response.
        Parameters:
        httpMethod - The HTTP method to use: "GET", "POST", "PUT", or "PATCH"
        jsonValue - A JSON value to send (primitive/boxed type, String, List of JSON values, or Bindings of String/JSON value)
        Returns:
        The raw response body as plain text
        See Also:
        sendRequest(URL, String, Object, Map, int)
      • getTextContent

        public static String getTextContent​(URL thiz)
        Returns:
        The full text content of this URL's stream.
      • getBinaryContent

        public static byte[] getBinaryContent​(URL thiz)
        Returns:
        The full binary content of this URL's stream.
      • getJsonContent

        public static Object getJsonContent​(URL thiz)
        Returns:
        A JSON object reflecting the contents of this URL, otherwise a RuntimeException results if the content is not a JSON document.
      • getYamlContent

        public static Object getYamlContent​(URL thiz)
        Returns:
        A YAML object reflecting the contents of this URL, otherwise a RuntimeException results if the content is not a YAML document.
      • getXmlContent

        public static Object getXmlContent​(URL thiz)
        Returns:
        An XML object reflecting the contents of this URL, otherwise a RuntimeException results if the content is not a XML document.
      • getCsvContent

        public static Object getCsvContent​(URL thiz)
        Returns:
        An CSV object reflecting the contents of this URL, otherwise a RuntimeException results if the content is not a CSV document.