Class DownloadUtil
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static PathchecksumPathFor(Path model) private static StringdownloadChecksumFile(String sha512, Path model) Retrieves the published".sha512"file.static <T extends BaseModel>
TdownloadModel(String language, ModelType modelType, Class<T> type) Triggers a download for the specifiedModelType.static <T extends BaseModel>
TdownloadModel(URL url, Class<T> type) Downloads a model from aURL.(package private) static booleanexistsModel(String language, ModelType modelType) Checks if a model of the specifiedmodelTypehas been downloaded already for a particularlanguage.Lists the models published in the model index.private static Path(package private) static StringindexFileName(String modelPath) Names the local copy of the model index after the last segment of the model path, for exampleud-models-1.3.index.htmlformodels/ud-models-1.3/.(package private) static StringparseChecksum(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.private static voidstoreChecksumFile(Path model, String checksumFileContent) Stores the published checksum file alongside the model.private static voidvalidateCachedModel(String sha512, Path cachedModel) Validates a model that is already present in the download home.private static voidvalidateModel(String sha512, Path downloadedModel) Validates a freshly downloaded model via the specifieddownloadedModel pathand stores the expected checksum next to it, so that subsequent loads of the cached file can be verified without contacting the CDN again.private static voidverifyChecksum(Path model, String expectedChecksum) Compares the SHA-512 hash of a model file with the expected one.
-
Field Details
-
logger
private static final org.slf4j.Logger logger -
BASE_URL
-
MODEL_URI_PATH
-
OPENNLP_DOWNLOAD_HOME
- See Also:
-
CHECKSUM_EXTENSION
- See Also:
-
INDEX_EXTENSION
- See Also:
-
TEMP_EXTENSION
- See Also:
-
availableModels
-
-
Constructor Details
-
DownloadUtil
public DownloadUtil()
-
-
Method Details
-
existsModel
Checks if a model of the specifiedmodelTypehas been downloaded already for a particularlanguage.- Parameters:
language- The ISO language code of the requested model.modelType- Thetypeof model.- Returns:
trueif a model exists locally,falseotherwise.- 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 specifiedModelType.- Type Parameters:
T- The generic type which is a subclass ofBaseModel.- Parameters:
language- The ISO language code of the requested model.modelType- Thetypeof 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
Downloads a model from aURL.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 ofBaseModel.- Parameters:
url- The model'sURL.type- The class of the resulting modelDownloadUtil.- 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
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
Validates a freshly downloaded model via the specifieddownloadedModel pathand 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 hashdownloadedModel- the model file to check- Throws:
IOException- thrown if the checksum could not be retrieved or computedIllegalArgumentException- thrown if the checksum file is blankIllegalStateException- thrown if the checksum did not match
-
validateCachedModel
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 hashcachedModel- the cached model file to check- Throws:
IOException- thrown if the checksum could not be retrieved or computedIllegalArgumentException- thrown if the checksum file is blankIllegalStateException- thrown if the checksum did not match
-
downloadChecksumFile
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
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 UnicodeWhite_Space, seeStringUtil.splitOnUnicodeWhitespace(CharSequence), so the result does not depend on the whitespace mode.- Parameters:
checksumFileContent- The file content.- Returns:
- The hash, or
nullif the content isnullor blank.
-
verifyChecksum
Compares the SHA-512 hash of a model file with the expected one.- Parameters:
model- The model file.expectedChecksum- The expected hash, ornullif 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
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
-
indexFileName
Names the local copy of the model index after the last segment of the model path, for exampleud-models-1.3.index.htmlformodels/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
-