Package org.kissweb

Class RestClient

java.lang.Object
org.kissweb.RestClient

public class RestClient extends Object
Provides the ability to act as a client to an external REST server. Supports text, XML, and JSON communications.

Author: Blake McBride
Date: 2/21/21
  • Constructor Details

    • RestClient

      public RestClient()
  • Method Details

    • setProxy

      public RestClient setProxy(String proxyServerURL, int proxyServerPort)
    • setTLSKey

      public RestClient setTLSKey(String clientKeystoreType, String clientKeystorePath, String clientKeystorePassword) throws KeyStoreException, IOException, CertificateException, NoSuchAlgorithmException, UnrecoverableKeyException, KeyManagementException
      Set a TLS key.
      Parameters:
      clientKeystoreType -
      clientKeystorePath -
      clientKeystorePassword -
      Returns:
      Throws:
      KeyStoreException
      IOException
      CertificateException
      NoSuchAlgorithmException
      UnrecoverableKeyException
      KeyManagementException
    • jsonCall

      public org.json.JSONObject jsonCall(String method, String urlStr, org.json.JSONObject out, org.json.JSONObject headers) throws IOException
      Call a REST service sending a JSON object and returning a JSON object.
      Parameters:
      method - POST / GET / etc.
      urlStr - URL endpoint
      out - what is sent to the service
      headers - representing all the headers
      Returns:
      what is returned from the service
      Throws:
      IOException - if error
    • jsonCall

      public org.json.JSONObject jsonCall(String method, String urlStr, org.json.JSONArray out, org.json.JSONObject headers) throws IOException
      Call a REST service sending a JSON array and returning a JSON object.
      Parameters:
      method - POST / GET / etc.
      urlStr - URL endpoint
      out - what is sent to the service
      headers - representing all the headers
      Returns:
      what is returned from the service
      Throws:
      IOException - if error
    • jsonCall

      public org.json.JSONObject jsonCall(String method, String urlStr) throws IOException
      Call a REST service sending nothing and returning a JSON object.
      Parameters:
      method -
      urlStr -
      Returns:
      Throws:
      IOException
    • xmlCall

      public Document xmlCall(String method, String urlStr, String outStr, org.json.JSONObject headers) throws IOException, ParserConfigurationException, SAXException
      Call a REST service sending a String but returning an XML document.
      Parameters:
      method - POST / GET / etc.
      urlStr - URL endpoint
      outStr - what is sent to the service
      headers - representing all of the headers
      Returns:
      what is returned from the service
      Throws:
      IOException - if error
      ParserConfigurationException - if error
      SAXException - if error
    • xmlCall

      public Document xmlCall(String method, String urlStr) throws IOException, ParserConfigurationException, SAXException
      Call a REST service sending nothing and returning an XML document.
      Parameters:
      method -
      urlStr -
      Returns:
      Throws:
      IOException
      ParserConfigurationException
      SAXException
    • jsonCall

      public org.json.JSONObject jsonCall(String method, String urlStr, String outStr, org.json.JSONObject headers) throws IOException
      Call a REST service sending a string and returning a JSON object.

      'headers' is a JSON object. Each element is a header / value combination. These are sent as the header of the REST call.
      Parameters:
      method - POST / GET / etc.
      urlStr - URL endpoint
      outStr - what is sent to the service
      headers - representing all of the headers
      Returns:
      the JSON return from the call
      Throws:
      IOException - if the communication fail, an exception is thrown
    • performService

      public int performService(String method, String urlStr, String outStr, org.json.JSONObject headers) throws IOException
      Performs the web service call. Sends and returns raw strings.

      'headers' is a JSON object. Each element is a header / value combination. These are sent as the header of the REST call.
      Parameters:
      method - POST / GET / etc.
      urlStr - the URL endpoint
      outStr - what is sent to the service
      headers - representing all of the headers
      Returns:
      the HTTP return code
      Throws:
      IOException - if the communication fail, an exception is thrown
    • basicAuthenticationHeader

      public static org.json.JSONObject basicAuthenticationHeader(String user, String pw)
      The following builds a JSON header that implements HTTP basic authentication. Additional items may be added to the returned header object.
      Parameters:
      user -
      pw -
      Returns:
      a new JSON header object containing the basic authentication
    • getResponseCode

      public int getResponseCode()
      The HTTP response code
      Returns:
    • getResponseString

      public String getResponseString()
      Returns the response string.
      Returns:
    • setDebugFileName

      public String setDebugFileName(String fname)
      Set a file for debug output.
      Parameters:
      fname -
      Returns: