Class JKHttpUtil

java.lang.Object
com.jk.core.http.JKHttpUtil

public class JKHttpUtil extends Object
This class provides utility methods to deal with URLs and file downloads from them.
Version:
1.0
Author:
Dr. Jalal H. Kiswani
  • Constructor Details

    • JKHttpUtil

      public JKHttpUtil()
  • Method Details

    • downloadFile

      public static void downloadFile(String fileUrl, String localFile) throws IOException
      This method downloads a file from the provided URL and saves it locally.
      Parameters:
      fileUrl - Specifies the URL of the file to be downloaded.
      localFile - Specifies the local file path where the downloaded file will be saved.
      Throws:
      IOException - if an I/O error occurs during the download or saving process.
    • downloadFileToTemp

      public static File downloadFileToTemp(String fileUrl, String ext) throws IOException
      This method downloads a file from the provided URL to a temporary file in the default temporary-file directory.

      The file name will start with jk- followed by the provided suffix.

      Parameters:
      fileUrl - Specifies the URL of the file to be downloaded.
      ext - Specifies the temporary file name suffix.
      Returns:
      the temporary file.
      Throws:
      IOException - if an I/O error occurs during the download or saving process.
      See Also:
    • requestUrl

      public static String requestUrl(String url, String method, Properties header, String body)
      This method performs an HTTP request to the provided URL using the specified method, headers, and body.
      Parameters:
      url - Specifies the URL to which the HTTP request is made.
      method - Specifies the HTTP method (GET, POST, PUT, DELETE, etc.) to use for the request.
      header - Specifies the HTTP header to include in the request.
      body - Specifies the body content to send with the request.
      Returns:
      the response received from the URL after the HTTP request.
    • getUrlContents

      public static String getUrlContents(String urlString)
      This method gets the URL contents as string.
      Parameters:
      urlString - Specifies the URL whose contents need to be brought.
      Returns:
      a string representing the URL contents.
    • connect

      protected static HttpURLConnection connect(String urlString) throws MalformedURLException, IOException
      This method tried to connect to the specified URL.
      Parameters:
      urlString - Specifies the URL to connect to.
      Returns:
      the HttpURLConnection associated with the specified URL.
      Throws:
      MalformedURLException - indicates that a malformed URL has occurred. Either no legal protocol could be found in a specification string or the string could not be parsed.
      IOException - Signals that an I/O exception of some sort has occurred.
    • getValueFromUrl

      public static String getValueFromUrl(String url, String preText, int length)
      This method gets the specified value contents from the specified URL.
      Parameters:
      url - Specifies the URL to get contents from.
      preText - Specifies the value name in the URL to get contents from.
      length - Specifies the length of the value to return.
      Returns:
      the specified value contents from the specified URL.
    • getUrlInputStream

      public static InputStream getUrlInputStream(String urlString)
      This method gets the specified URL input stream
      Parameters:
      urlString - Specified the URL.
      Returns:
      the URL input stream.
    • readUrlAsObject

      public static <T> T readUrlAsObject(String url, Class<T> type)
      This method reads the URL as object for easier accessibility.
      Type Parameters:
      T - Specifies the URL returned type.
      Parameters:
      url - Specifies the URL to transform into object.
      type - Specifies the object type to be returned.
      Returns:
      an object representation of the specified URL.
    • pingUrl

      public static void pingUrl(String url, int retries)
      This method tries to ping the specified URL, with the specified number of retries in case it failed the first time.
      Parameters:
      url - Specifies the URL to be ping.
      retries - Specifies the number of retries in case it fails to ping the first time.
    • main

      public static void main(String[] args)
      This main method ping a deployer URL as a test.
      Parameters:
      args - the command line arguments