in taverna-download-impl/src/main/java/org/apache/taverna/download/impl/DownloadManagerImpl.java [76:126]
public void download(URI source, Path destination, String digestAlgorithm, URI digestSource)
throws DownloadException {
MessageDigest md = null;
if (digestAlgorithm != null) {
try {
md = MessageDigest.getInstance(digestAlgorithm);
} catch (NoSuchAlgorithmException e) {
throw new IllegalArgumentException("Unsupported digestAlgorithm: " + digestAlgorithm, e);
}
}
// download the file
Path tempFile;
try {
tempFile = Files.createTempFile(destination.getParent(), "." + destination.getFileName(), ".tmp");
} catch (IOException e1) {
// perhaps a permission problem?
throw new DownloadException("Can't create temporary file in folder " + destination.getParent(), e1);
}
logger.info(String.format("Downloading %1$s to %2$s", source, tempFile));
downloadToFile(source, tempFile);
if (digestSource != null) {
// download the digest file
String expectedDigest;
expectedDigest = downloadHash(digestSource).trim().toLowerCase(Locale.ROOT);
// check if the digest matches
try {
try (InputStream s = Files.newInputStream(tempFile)) {
DigestUtils.updateDigest(md, s);
String actualDigest = Hex.encodeHexString(md.digest());
if (!actualDigest.equals(expectedDigest)) {
throw new DownloadException(
String.format("Error downloading file: checksum mismatch (%1$s != %2$s)",
actualDigest, expectedDigest));
}
}
} catch (IOException e) {
throw new DownloadException(String.format("Error checking digest for %1$s", destination), e);
}
}
// All fine, move to destination
try {
logger.info(String.format("Copying %1$s to %2$s", tempFile, destination));
Files.move(tempFile, destination, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new DownloadException(String.format("Error downloading %1$s to %2$s.", source, destination), e);
}
}