PackageOperationDownload implements PackageOperation { @Override public void execute(PackageOperationContext context, ProgressReceiver progressReceiver) throws IOException { progressReceiver.progress("Downloading package '" + pkg.getName() + "' " + FileUtils.byteCountToDisplaySize(pkg.getSize())); URL sourceUrl = pkg.getSource().getUrl(); if (!sourceUrl.toExternalForm().endsWith("/")) sourceUrl = new URL(sourceUrl.toExternalForm() + "/"); final URL packageURL = new URL(sourceUrl, pkg.getFilename()); LOG.info("Downloading package {}", packageURL); context.getLocalPackageRepository().addPackage(pkg, targetPath -> { downloadManager.download(packageURL, in -> { try ( final OutputStream out = Files.newOutputStream(targetPath); final DigestOutputStream digestOut = new DigestOutputStream(out, getMD5Digest())) { ByteStreams.copy(in, digestOut); digestOut.flush(); final MessageDigest digest = digestOut.getMessageDigest(); final String md5sum = byteArrayToHexString(digest.digest()); LOG.info("Download of {} complete. Computed MD5 {}", packageURL, md5sum); if (!Strings.isNullOrEmpty(pkg.getMD5sum()) && !pkg.getMD5sum().equalsIgnoreCase(md5sum)) { LOG.error("Checksum validation failed. Exected {}, actual {}", pkg.getMD5sum().toLowerCase(), md5sum.toLowerCase()); throw new PackageChecksumVerificationFailedException("checksum validation failed", pkg, md5sum); } } return null; }, progressReceiver); }); } PackageOperationDownload(Package pkg, DownloadManager downloadManager); static String byteArrayToHexString(byte[] bytes); @Override Package getPackage(); @Override void execute(PackageOperationContext context, ProgressReceiver progressReceiver);  }
@Test public void testDownloadFile() throws Exception { final Package pkg = createPackage("foo", "2.0-1", "./foo_2.0-1_i386.deb", "f20b39b2e66616a6a3aaf0156305501c"); final Path repoTarget = createTestDirectory("testDownloadFile"); final PackageOperationDownload dl = new PackageOperationDownload(pkg, createDownloadManager()); dl.execute(new DefaultPackageOperationContext(new DefaultLocalPackageRepository(repoTarget), null, null, null, pkg), new NoopProgressReceiver()); assertTrue(Files.exists(repoTarget.resolve("736").resolve("foo_2.0-1_i386.deb"))); }
@Test(expected = NotFoundException.class) public void testNonExistingPackage() throws Exception { final Package pkg = createPackage("fooXX", "2.0-1", "./fooXX_2.0-1_i386.deb", "f20b39b2e66616a6a3aaf0156305501c"); final Path repoTarget = createTestDirectory("testNonExistingPackage"); final PackageOperationDownload dl = new PackageOperationDownload(pkg, createDownloadManager()); dl.execute(new DefaultPackageOperationContext(new DefaultLocalPackageRepository(repoTarget), null, null, null, pkg), new NoopProgressReceiver()); }
@Test(expected = PackageChecksumVerificationFailedException.class) public void testMismatchingChecksum() throws Exception { final Package pkg = createPackage("foo", "2.0-1", "./foo_2.0-1_i386.deb", "ababababababababababababababababab"); final Path repoTarget = createTestDirectory("testMismatchingChecksum"); final PackageOperationDownload dl = new PackageOperationDownload(pkg, createDownloadManager()); dl.execute(new DefaultPackageOperationContext(new DefaultLocalPackageRepository(repoTarget), null, null, null, pkg), new NoopProgressReceiver()); }