Package org.omegat.util
Class HttpConnectionUtils
java.lang.Object
org.omegat.util.HttpConnectionUtils
Utility collection for http connections.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classDownloaded file error.static classHTTP response error storage. -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanValidate URL string.static StringdecodeHttpURLs(String text) static booleandownloadBinaryFile(URL fileURL, Map<String, String> headers, Set<String> expectedMime, File saveFilePath) Downloads a binary file from a URL.static voiddownloadZipFileAndExtract(URL url, File dir, List<String> expectedFiles) Download Zip file from remote site and extract it to specified directory.static StringencodeHttpURLs(String text) static StringencodePath(String path) static StringencodeQuery(String query) static StringencodeURIComponent(String component) static StringGet data from the remote URL.static Stringget(String address, Map<String, String> params, Map<String, String> additionalHeaders, String defaultOutputCharset) Get data from the remote URL.static StringGet resource from URL with default timeout.static StringDownload a file to memory.static byte[]getURLasByteArray(String target) Obtain byte array context from remote URL.static StringMethod call without additional headers for possible calls from plugins.static StringPost data to the remote URL.static StringPost JSON data to the remote URL.
-
Field Details
-
URL_PATTERN
Regular Expression for https and ftp URL validation.You are recommended to use commons-validator instead of hand-crafted here. We leave it as is for keeping compatibility.
-
FILE_URL_PATTERN
Regular Expression for file URL validation.
-
-
Method Details
-
getURL
Get resource from URL with default timeout.- Parameters:
url- resource URL.- Returns:
- string returned from server.
- Throws:
IOException- raises when connection is failed.
-
getURL
Download a file to memory.- Parameters:
url- resource URL to downloadtimeout- timeout to connect and read.- Returns:
- returned string
- Throws:
IOException- when connection and read method error.
-
downloadZipFileAndExtract
public static void downloadZipFileAndExtract(URL url, File dir, List<String> expectedFiles) throws IOException Download Zip file from remote site and extract it to specified directory.- Parameters:
url- URL of zip file resource to download.dir- target directory to extractexpectedFiles- filter extract file names- Throws:
IOException- raises when extraction is failed, maybe flaky download happened.
-
downloadBinaryFile
public static boolean downloadBinaryFile(URL fileURL, Map<String, String> headers, Set<String> expectedMime, File saveFilePath) throws IOException, HttpConnectionUtils.FlakyDownloadExceptionDownloads a binary file from a URL.- Parameters:
fileURL- HTTP URL of the file to be downloadedheaders- Additional HTTP headersexpectedMime- Mime type expected and check against such as ["application/octet-stream", "application/jar-archive"]. If getting type is differed, return false.saveFilePath- path of the file- Returns:
- true when succeeded, otherwise false.
- Throws:
IOException- raise when connection and file write failed.HttpConnectionUtils.FlakyDownloadException- raise when downloaded file length differs from expected content length.
-
getURLasByteArray
Obtain byte array context from remote URL.- Parameters:
target- String representation of well-formed URL.- Returns:
- byte array or null if status is not 200 OK
- Throws:
IOException- raise when connection failed.
-
post
Method call without additional headers for possible calls from plugins.- Parameters:
address- URL to postparams- post parameters in Map- Returns:
- result string returned from server.
- Throws:
IOException- raises when connection failed.
-
get
public static String get(String address, Map<String, String> params, Map<String, throws IOExceptionString> additionalHeaders) Get data from the remote URL.- Parameters:
address- address to postparams- parametersadditionalHeaders- additional headers for request, can be null- Returns:
- server output
- Throws:
IOException
-
get
public static String get(String address, Map<String, String> params, Map<String, throws IOExceptionString> additionalHeaders, String defaultOutputCharset) Get data from the remote URL.- Parameters:
address- address to postparams- parametersadditionalHeaders- additional headers for request, can be nulldefaultOutputCharset- default charset used to interpret the response- Returns:
- server output
- Throws:
IOException
-
post
public static String post(String address, Map<String, String> params, Map<String, throws IOExceptionString> additionalHeaders) Post data to the remote URL.- Parameters:
address- address to postparams- parametersadditionalHeaders- additional headers for request, can be null- Returns:
- Server output
- Throws:
IOException
-
postJSON
public static String postJSON(String address, String json, Map<String, String> additionalHeaders) throws IOExceptionPost JSON data to the remote URL.- Parameters:
address- address to postjson- JSON-encoded dataadditionalHeaders- additional headers for request, can be null- Returns:
- Server output
- Throws:
IOException
-
encodeHttpURLs
-
encodeURIComponent
public static String encodeURIComponent(String component) throws org.apache.commons.codec.EncoderException - Throws:
org.apache.commons.codec.EncoderException
-
encodePath
- Throws:
org.apache.commons.codec.EncoderException
-
encodeQuery
- Throws:
org.apache.commons.codec.EncoderException
-
decodeHttpURLs
-
checkUrl
Validate URL string.- Parameters:
remoteUrl- URL candidate string.- Returns:
- true when valid, otherwise false.
-