Class HttpConnectionUtils

java.lang.Object
org.omegat.util.HttpConnectionUtils

public final class HttpConnectionUtils extends Object
Utility collection for http connections.
  • Field Details

    • URL_PATTERN

      public static final Pattern 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

      public static final Pattern FILE_URL_PATTERN
      Regular Expression for file URL validation.
  • Method Details

    • getURL

      public static String getURL(URL url) throws IOException
      Get resource from URL with default timeout.
      Parameters:
      url - resource URL.
      Returns:
      string returned from server.
      Throws:
      IOException - raises when connection is failed.
    • getURL

      public static String getURL(URL url, int timeout) throws IOException
      Download a file to memory.
      Parameters:
      url - resource URL to download
      timeout - 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 extract
      expectedFiles - 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.FlakyDownloadException
      Downloads a binary file from a URL.
      Parameters:
      fileURL - HTTP URL of the file to be downloaded
      headers - Additional HTTP headers
      expectedMime - 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

      public static byte[] getURLasByteArray(String target) throws IOException
      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

      public static String post(String address, Map<String,String> params) throws IOException
      Method call without additional headers for possible calls from plugins.
      Parameters:
      address - URL to post
      params - 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,String> additionalHeaders) throws IOException
      Get data from the remote URL.
      Parameters:
      address - address to post
      params - parameters
      additionalHeaders - 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,String> additionalHeaders, String defaultOutputCharset) throws IOException
      Get data from the remote URL.
      Parameters:
      address - address to post
      params - parameters
      additionalHeaders - additional headers for request, can be null
      defaultOutputCharset - 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,String> additionalHeaders) throws IOException
      Post data to the remote URL.
      Parameters:
      address - address to post
      params - parameters
      additionalHeaders - 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 IOException
      Post JSON data to the remote URL.
      Parameters:
      address - address to post
      json - JSON-encoded data
      additionalHeaders - additional headers for request, can be null
      Returns:
      Server output
      Throws:
      IOException
    • encodeHttpURLs

      public static String encodeHttpURLs(String text)
    • encodeURIComponent

      public static String encodeURIComponent(String component) throws org.apache.commons.codec.EncoderException
      Throws:
      org.apache.commons.codec.EncoderException
    • encodePath

      public static String encodePath(String path) throws org.apache.commons.codec.EncoderException
      Throws:
      org.apache.commons.codec.EncoderException
    • encodeQuery

      public static String encodeQuery(String query) throws org.apache.commons.codec.EncoderException
      Throws:
      org.apache.commons.codec.EncoderException
    • decodeHttpURLs

      public static String decodeHttpURLs(String text)
    • checkUrl

      public static boolean checkUrl(String remoteUrl)
      Validate URL string.
      Parameters:
      remoteUrl - URL candidate string.
      Returns:
      true when valid, otherwise false.