Class DownloadUtil

java.lang.Object
opennlp.tools.util.DownloadUtil

public class DownloadUtil extends Object
This class facilitates the downloading of pretrained OpenNLP models.
  • Field Details

  • Constructor Details

    • DownloadUtil

      public DownloadUtil()
  • Method Details

    • existsModel

      static boolean existsModel(String language, ModelType modelType) throws IOException
      Checks if a model of the specified modelType has been downloaded already for a particular language.
      Parameters:
      language - The ISO language code of the requested model.
      modelType - The type of model.
      Returns:
      true if a model exists locally, false otherwise.
      Throws:
      IOException - Thrown if IO errors occurred.
      IllegalArgumentException - Thrown if the checksum file of the local model is blank.
      IllegalStateException - Thrown if the computed hash sum of the local model file does not match the expected one.
    • downloadModel

      public static <T extends BaseModel> T downloadModel(String language, ModelType modelType, Class<T> type) throws IOException
      Triggers a download for the specified ModelType.
      Type Parameters:
      T - The generic type which is a subclass of BaseModel.
      Parameters:
      language - The ISO language code of the requested model.
      modelType - The type of model.
      type - The class of the resulting model.
      Returns:
      A model instance of type DownloadUtil.
      Throws:
      IOException - Thrown if IO errors occurred or the model is invalid.
      IllegalArgumentException - Thrown if the checksum file of the model is blank.
      IllegalStateException - Thrown if the computed hash sum of the model file does not match the expected one.
    • downloadModel

      public static <T extends BaseModel> T downloadModel(URL url, Class<T> type) throws IOException
      Downloads a model from a URL.

      The model is saved to an .opennlp/ directory located in the user's home directory. This directory will be created if it does not already exist. If a model to be downloaded already exists in that directory, the model will not be re-downloaded, but it is verified against its SHA-512 checksum before it is loaded.

      Type Parameters:
      T - The generic type which is a subclass of BaseModel.
      Parameters:
      url - The model's URL.
      type - The class of the resulting model DownloadUtil.
      Returns:
      A model instance of type DownloadUtil.
      Throws:
      IOException - Thrown if the model cannot be downloaded.
      IllegalArgumentException - Thrown if the checksum file of the model is blank.
      IllegalStateException - Thrown if the computed hash sum of the model file does not match the expected one.
    • getAvailableModels

      public static Map<String,Map<ModelType,URL>> getAvailableModels()
      Lists the models published in the model index. A copy of the last index read is kept in the download directory and used when the index cannot be read. An empty result is not kept, so the next call reads the index again.
      Returns:
      The model URLs per language and ModelType. Empty if neither the index nor its local copy lists models.
    • validateModel

      private static void validateModel(String sha512, Path downloadedModel) throws IOException
      Validates a freshly downloaded model via the specified downloadedModel path and stores the expected checksum next to it, so that subsequent loads of the cached file can be verified without contacting the CDN again.
      Parameters:
      sha512 - the url to get the sha512 hash
      downloadedModel - the model file to check
      Throws:
      IOException - thrown if the checksum could not be retrieved or computed
      IllegalArgumentException - thrown if the checksum file is blank
      IllegalStateException - thrown if the checksum did not match
    • validateCachedModel

      private static void validateCachedModel(String sha512, Path cachedModel) throws IOException
      Validates a model that is already present in the download home.

      The expected checksum stored by a previous download is used, so no network access is required on this path. When no checksum was stored - the download home was populated by an OpenNLP version that predates this check - the published checksum is fetched once and then stored. If it cannot be retrieved, the model is loaded and a warning is logged, which preserves the behaviour of those earlier versions for offline environments.

      Parameters:
      sha512 - the url to get the sha512 hash
      cachedModel - the cached model file to check
      Throws:
      IOException - thrown if the checksum could not be retrieved or computed
      IllegalArgumentException - thrown if the checksum file is blank
      IllegalStateException - thrown if the checksum did not match
    • downloadChecksumFile

      private static String downloadChecksumFile(String sha512, Path model) throws IOException
      Retrieves the published ".sha512" file. Its content is returned unmodified so that the copy stored next to the model stays interchangeable with the published one.
      Throws:
      IOException
    • parseChecksum

      static String parseChecksum(String checksumFileContent)
      Extracts the hash from the content of a checksum file, which holds the hash followed by the name of the file it applies to. The hash is the first run of characters that are not Unicode White_Space, see StringUtil.splitOnUnicodeWhitespace(CharSequence), so the result does not depend on the whitespace mode.
      Parameters:
      checksumFileContent - The file content.
      Returns:
      The hash, or null if the content is null or blank.
    • verifyChecksum

      private static void verifyChecksum(Path model, String expectedChecksum) throws IOException
      Compares the SHA-512 hash of a model file with the expected one.
      Parameters:
      model - The model file.
      expectedChecksum - The expected hash, or null if the checksum file is blank.
      Throws:
      IllegalArgumentException - If the checksum file is blank.
      IOException - If the hash cannot be computed.
      IllegalStateException - If the hashes differ.
    • storeChecksumFile

      private static void storeChecksumFile(Path model, String checksumFileContent)
      Stores the published checksum file alongside the model. A failure to do so is not fatal: it only means the next load falls back to retrieving the published checksum again.
    • checksumPathFor

      private static Path checksumPathFor(Path model)
    • indexFileName

      static String indexFileName(String modelPath)
      Names the local copy of the model index after the last segment of the model path, for example ud-models-1.3.index.html for models/ud-models-1.3/. The base URL is not part of the name, so hosts that serve the same model path share the copy.
      Parameters:
      modelPath - The model path, with or without trailing slashes.
      Returns:
      The file name.
    • getDownloadHome

      private static Path getDownloadHome()