Package com.jk.core.http
Class JKHttpUtil
java.lang.Object
com.jk.core.http.JKHttpUtil
This class provides utility methods to deal with URLs and file downloads from
them.
- Version:
- 1.0
- Author:
- Dr. Jalal H. Kiswani
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected static HttpURLConnection
This method tried to connect to the specified URL.static void
downloadFile
(String fileUrl, String localFile) This method downloads a file from the provided URL and saves it locally.static File
downloadFileToTemp
(String fileUrl, String ext) This method downloads a file from the provided URL to a temporary file in the default temporary-file directory.static String
getUrlContents
(String urlString) This method gets the URL contents as string.static InputStream
getUrlInputStream
(String urlString) This method gets the specified URL input streamstatic String
getValueFromUrl
(String url, String preText, int length) This method gets the specified value contents from the specified URL.static void
This main method ping a deployer URL as a test.static void
This method tries to ping the specified URL, with the specified number of retries in case it failed the first time.static <T> T
readUrlAsObject
(String url, Class<T> type) This method reads the URL as object for easier accessibility.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.
-
Constructor Details
-
JKHttpUtil
public JKHttpUtil()
-
-
Method Details
-
downloadFile
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
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
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
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
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
This method gets the specified URL input stream- Parameters:
urlString
- Specified the URL.- Returns:
- the URL input stream.
-
readUrlAsObject
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
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
This main method ping a deployer URL as a test.- Parameters:
args
- the command line arguments
-