ContentProvider provider = cpMgr.getIsolatedContentProvider(contentSource.getId());
if (!(provider instanceof DistributionSource)) {
return;
}
DistributionSource distSource = (DistributionSource) provider;
//
// Following same sort of workaround done in ContentProviderManager for synchronizeContentProvider
// Assume this will need to be updated when we place syncing in repo layer
//
final RepoCriteria reposForContentSource = new RepoCriteria();
reposForContentSource.addFilterContentSourceIds(contentSourceId);
reposForContentSource.addFilterCandidate(false); // Don't sync distributions for candidates
final Subject overlord = LookupUtil.getSubjectManager().getOverlord();
//Use CriteriaQuery to automatically chunk/page through criteria query results
CriteriaQueryExecutor<Repo, RepoCriteria> queryExecutor = new CriteriaQueryExecutor<Repo, RepoCriteria>() {
@Override
public PageList<Repo> execute(RepoCriteria criteria) {
return repoManager.findReposByCriteria(overlord, reposForContentSource);
}
};
CriteriaQuery<Repo, RepoCriteria> repos = new CriteriaQuery<Repo, RepoCriteria>(reposForContentSource,
queryExecutor);
int repoCount = 0;
for (Repo repo : repos) {
repoCount++;
log.debug("downloadDistributionBits operating on repo: " + repo.getName() + " id = " + repo.getId());
// Look up Distributions associated with this ContentSource.
PageControl pageControl = PageControl.getUnlimitedInstance();
log.debug("Looking up existing distributions for repoId: " + repo.getId());
List<Distribution> dists = repoManager.findAssociatedDistributions(overlord, repo.getId(), pageControl);
log.debug("Found " + dists.size() + " Distributions for repoId " + repo.getId());
for (Distribution dist : dists) {
log.debug("Looking up DistributionFiles for dist: " + dist);
List<DistributionFile> distFiles = distManager.getDistributionFilesByDistId(dist.getId());
log.debug("Found " + distFiles.size() + " DistributionFiles");
for (DistributionFile dFile : distFiles) {
String relPath = dist.getBasePath() + "/" + dFile.getRelativeFilename();
File outputFile = getDistLocalFileAndCreateParentDir(dist.getLabel(), relPath);
log.debug("Checking if file exists at: " + outputFile.getAbsolutePath());
if (outputFile.exists()) {
log.debug("File " + outputFile.getAbsolutePath() + " exists, checking md5sum");
String expectedMD5 = (dFile.getMd5sum() != null) ? dFile.getMd5sum() : "<unspecified MD5>";
String actualMD5 = MessageDigestGenerator.getDigestString(outputFile);
if (!expectedMD5.trim().toLowerCase().equals(actualMD5.toLowerCase())) {
log.error("Expected [" + expectedMD5 + "] versus actual [" + actualMD5
+ "] md5sums for file " + outputFile.getAbsolutePath() + " do not match.");
log.error("Need to re-fetch file. Will download from DistributionSource"
+ " and overwrite local file.");
} else {
log.info(outputFile + " exists and md5sum matches [" + actualMD5
+ "] no need to re-download");
continue; // skip the download from bitsStream
}
}
log.debug("Attempting download of " + dFile.getRelativeFilename() + " from contentSourceId "
+ contentSourceId);
String remoteFetchLoc = distSource.getDistFileRemoteLocation(repo.getName(), dist.getLabel(),
dFile.getRelativeFilename());
InputStream bitsStream = pc.getAdapterManager().loadDistributionFileBits(contentSourceId,
remoteFetchLoc);
StreamUtil.copy(bitsStream, new FileOutputStream(outputFile), true);
bitsStream = null;