Search in sources :

Example 1 with ChecksummedFile

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));
}
Also used : IOException(java.io.IOException) ChecksummedFile(org.apache.archiva.checksum.ChecksummedFile) Date(java.util.Date)

Example 2 with ChecksummedFile

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();
}
Also used : Path(java.nio.file.Path) ChecksummedFile(org.apache.archiva.checksum.ChecksummedFile) Test(org.junit.Test)

Example 3 with ChecksummedFile

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;
}
Also used : Path(java.nio.file.Path) ChecksummedFile(org.apache.archiva.checksum.ChecksummedFile) ChecksumValidationException(org.apache.archiva.checksum.ChecksumValidationException)

Example 4 with ChecksummedFile

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);
}
Also used : ArrayList(java.util.ArrayList) ChecksummedFile(org.apache.archiva.checksum.ChecksummedFile)

Example 5 with ChecksummedFile

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);
}
Also used : ChecksummedFile(org.apache.archiva.checksum.ChecksummedFile)

Aggregations

ChecksummedFile (org.apache.archiva.checksum.ChecksummedFile)12 Path (java.nio.file.Path)8 IOException (java.io.IOException)3 ArchivaRepositoryMetadata (org.apache.archiva.model.ArchivaRepositoryMetadata)3 ArrayList (java.util.ArrayList)2 Date (java.util.Date)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Matcher (java.util.regex.Matcher)1 ChecksumAlgorithm (org.apache.archiva.checksum.ChecksumAlgorithm)1 ChecksumValidationException (org.apache.archiva.checksum.ChecksumValidationException)1 VersionComparator (org.apache.archiva.common.utils.VersionComparator)1 ConsumerException (org.apache.archiva.consumers.ConsumerException)1 ArtifactMetadata (org.apache.archiva.metadata.model.ArtifactMetadata)1 RepositoryProblemFacet (org.apache.archiva.metadata.model.facets.RepositoryProblemFacet)1 MetadataRepository (org.apache.archiva.metadata.repository.MetadataRepository)1 MetadataRepositoryException (org.apache.archiva.metadata.repository.MetadataRepositoryException)1 Plugin (org.apache.archiva.model.Plugin)1 SnapshotVersion (org.apache.archiva.model.SnapshotVersion)1 ContentNotFoundException (org.apache.archiva.repository.ContentNotFoundException)1