use of org.apache.archiva.checksum.ChecksummedFile in project archiva by apache.
the class Maven2RepositoryStorage method populateArtifactMetadataFromFile.
private static void populateArtifactMetadataFromFile(ArtifactMetadata metadata, Path file) throws IOException {
metadata.setWhenGathered(new Date());
metadata.setFileLastModified(Files.getLastModifiedTime(file).toMillis());
ChecksummedFile checksummedFile = new ChecksummedFile(file);
try {
metadata.setMd5(checksummedFile.calculateChecksum(ChecksumAlgorithm.MD5));
} catch (IOException e) {
LOGGER.error("Unable to checksum file {}: {},MD5", file, e.getMessage());
}
try {
metadata.setSha1(checksummedFile.calculateChecksum(ChecksumAlgorithm.SHA1));
} catch (IOException e) {
LOGGER.error("Unable to checksum file {}: {},SHA1", file, e.getMessage());
}
metadata.setSize(Files.size(file));
}
use of org.apache.archiva.checksum.ChecksummedFile in project archiva by apache.
the class ArtifactMissingChecksumsConsumerTest method testExistingIncorrectChecksums.
@Test
public void testExistingIncorrectChecksums() throws Exception {
Path newLocation = Paths.get("target/test-repo");
org.apache.archiva.common.utils.FileUtils.deleteDirectory(newLocation);
FileUtils.copyDirectory(Paths.get(repoConfig.getLocation()).toFile(), newLocation.toFile());
repoConfig.setLocation(newLocation.toAbsolutePath().toUri());
Path basePath = PathUtil.getPathFromUri(repoConfig.getLocation());
String path = "incorrect-checksums/1.0/incorrect-checksums-1.0.jar";
Path sha1Path = basePath.resolve(path + ".sha1");
Path md5Path = basePath.resolve(path + ".md5");
ChecksummedFile checksum = new ChecksummedFile(basePath.resolve(path));
Assertions.assertThat(sha1Path.toFile()).exists();
Assertions.assertThat(md5Path.toFile()).exists();
Assertions.assertThat(//
checksum.isValidChecksums(Arrays.asList(ChecksumAlgorithm.MD5, ChecksumAlgorithm.SHA1))).isFalse();
consumer.beginScan(repoConfig, Calendar.getInstance().getTime());
consumer.processFile(path);
Assertions.assertThat(sha1Path.toFile()).exists();
Assertions.assertThat(md5Path.toFile()).exists();
Assertions.assertThat(//
checksum.isValidChecksums(Arrays.asList(ChecksumAlgorithm.MD5, ChecksumAlgorithm.SHA1))).isTrue();
}
use of org.apache.archiva.checksum.ChecksummedFile in project archiva by apache.
the class LegacyToDefaultConverter method verifyChecksum.
private boolean verifyChecksum(Path file, String fileName, ChecksumAlgorithm digester, Artifact artifact, String key) throws IOException {
boolean result;
Path checksumFile = file.resolveSibling(fileName);
// We ignore the check, if the checksum file does not exist
if (!Files.exists(checksumFile)) {
return true;
}
ChecksummedFile csFile = new ChecksummedFile(file);
try {
result = csFile.isValidChecksum(digester, true);
} catch (ChecksumValidationException e) {
addWarning(artifact, Messages.getString(key));
result = false;
}
return result;
}
use of org.apache.archiva.checksum.ChecksummedFile in project archiva by apache.
the class DefaultRepositoriesService method updateMetadata.
/**
* Update artifact level metadata. Creates one if metadata does not exist after artifact deletion.
*
* @param metadata
*/
private void updateMetadata(ArchivaRepositoryMetadata metadata, Path metadataFile, Date lastUpdatedTimestamp, Artifact artifact) throws RepositoryMetadataException {
List<String> availableVersions = new ArrayList<>();
String latestVersion = "";
if (Files.exists(metadataFile)) {
if (metadata.getAvailableVersions() != null) {
availableVersions = metadata.getAvailableVersions();
if (availableVersions.size() > 0) {
Collections.sort(availableVersions, VersionComparator.getInstance());
if (availableVersions.contains(artifact.getVersion())) {
availableVersions.remove(availableVersions.indexOf(artifact.getVersion()));
}
if (availableVersions.size() > 0) {
latestVersion = availableVersions.get(availableVersions.size() - 1);
}
}
}
}
if (metadata.getGroupId() == null) {
metadata.setGroupId(artifact.getGroupId());
}
if (metadata.getArtifactId() == null) {
metadata.setArtifactId(artifact.getArtifactId());
}
if (!VersionUtil.isSnapshot(artifact.getVersion())) {
if (metadata.getReleasedVersion() != null && metadata.getReleasedVersion().equals(artifact.getVersion())) {
metadata.setReleasedVersion(latestVersion);
}
}
metadata.setLatestVersion(latestVersion);
metadata.setLastUpdatedTimestamp(lastUpdatedTimestamp);
metadata.setAvailableVersions(availableVersions);
RepositoryMetadataWriter.write(metadata, metadataFile);
ChecksummedFile checksum = new ChecksummedFile(metadataFile);
checksum.fixChecksums(algorithms);
}
use of org.apache.archiva.checksum.ChecksummedFile in project archiva by apache.
the class DefaultRepositoriesService method fixChecksums.
private void fixChecksums(Path file) {
ChecksummedFile checksum = new ChecksummedFile(file);
checksum.fixChecksums(algorithms);
}
Aggregations