Search in sources :

Example 6 with ChecksummedFile

use of org.apache.archiva.checksum.ChecksummedFile in project archiva by apache.

the class MetadataTools method updateMetadata.

/**
 * Update the metadata to represent the all versions/plugins of
 * the provided groupId:artifactId project or group reference,
 * based off of information present in the repository,
 * the maven-metadata.xml files, and the proxy/repository specific
 * metadata file contents.
 * <p>
 * We must treat this as a group or a project metadata file as there is no way to know in advance
 *
 * @param managedRepository the managed repository where the metadata is kept.
 * @param reference         the reference to update.
 * @throws LayoutException
 * @throws RepositoryMetadataException
 * @throws IOException
 * @throws ContentNotFoundException
 * @deprecated
 */
public void updateMetadata(ManagedRepositoryContent managedRepository, ProjectReference reference) throws LayoutException, RepositoryMetadataException, IOException, ContentNotFoundException {
    Path metadataFile = Paths.get(managedRepository.getRepoRoot(), toPath(reference));
    long lastUpdated = getExistingLastUpdated(metadataFile);
    ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
    metadata.setGroupId(reference.getGroupId());
    metadata.setArtifactId(reference.getArtifactId());
    // Gather up all versions found in the managed repository.
    Set<String> allVersions = managedRepository.getVersions(reference);
    // Gather up all plugins found in the managed repository.
    // TODO: do we know this information instead?
    // Set<Plugin> allPlugins = managedRepository.getPlugins( reference );
    Set<Plugin> allPlugins;
    if (Files.exists(metadataFile)) {
        try {
            allPlugins = new LinkedHashSet<Plugin>(MavenMetadataReader.read(metadataFile).getPlugins());
        } catch (XMLException e) {
            throw new RepositoryMetadataException(e.getMessage(), e);
        }
    } else {
        allPlugins = new LinkedHashSet<Plugin>();
    }
    // Does this repository have a set of remote proxied repositories?
    Set<String> proxiedRepoIds = this.proxies.get(managedRepository.getId());
    if (CollectionUtils.isNotEmpty(proxiedRepoIds)) {
        // Add in the proxied repo version ids too.
        Iterator<String> it = proxiedRepoIds.iterator();
        while (it.hasNext()) {
            String proxyId = it.next();
            ArchivaRepositoryMetadata proxyMetadata = readProxyMetadata(managedRepository, reference, proxyId);
            if (proxyMetadata != null) {
                allVersions.addAll(proxyMetadata.getAvailableVersions());
                allPlugins.addAll(proxyMetadata.getPlugins());
                long proxyLastUpdated = getLastUpdated(proxyMetadata);
                lastUpdated = Math.max(lastUpdated, proxyLastUpdated);
            }
        }
    }
    if (!allVersions.isEmpty()) {
        updateMetadataVersions(allVersions, metadata);
    } else {
        // Add the plugins to the metadata model.
        metadata.setPlugins(new ArrayList<>(allPlugins));
        // artifact ID was actually the last part of the group
        metadata.setGroupId(metadata.getGroupId() + "." + metadata.getArtifactId());
        metadata.setArtifactId(null);
    }
    if (lastUpdated > 0) {
        metadata.setLastUpdatedTimestamp(toLastUpdatedDate(lastUpdated));
    }
    // Save the metadata model to disk.
    RepositoryMetadataWriter.write(metadata, metadataFile);
    ChecksummedFile checksum = new ChecksummedFile(metadataFile);
    checksum.fixChecksums(algorithms);
}
Also used : Path(java.nio.file.Path) ChecksummedFile(org.apache.archiva.checksum.ChecksummedFile) XMLException(org.apache.archiva.xml.XMLException) ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata) Plugin(org.apache.archiva.model.Plugin)

Example 7 with ChecksummedFile

use of org.apache.archiva.checksum.ChecksummedFile in project archiva by apache.

the class MetadataTools method updateMetadata.

public void updateMetadata(ManagedRepositoryContent managedRepository, String logicalResource) throws RepositoryMetadataException {
    final Path metadataFile = Paths.get(managedRepository.getRepoRoot(), logicalResource);
    ArchivaRepositoryMetadata metadata = null;
    // Gather and merge all metadata available
    List<ArchivaRepositoryMetadata> metadatas = getMetadatasForManagedRepository(managedRepository, logicalResource);
    for (ArchivaRepositoryMetadata proxiedMetadata : metadatas) {
        if (metadata == null) {
            metadata = proxiedMetadata;
            continue;
        }
        metadata = RepositoryMetadataMerge.merge(metadata, proxiedMetadata);
    }
    if (metadata == null) {
        log.debug("No metadata to update for {}", logicalResource);
        return;
    }
    Set<String> availableVersions = new HashSet<String>();
    List<String> metadataAvailableVersions = metadata.getAvailableVersions();
    if (metadataAvailableVersions != null) {
        availableVersions.addAll(metadataAvailableVersions);
    }
    availableVersions = findPossibleVersions(availableVersions, metadataFile.getParent());
    if (availableVersions.size() > 0) {
        updateMetadataVersions(availableVersions, metadata);
    }
    RepositoryMetadataWriter.write(metadata, metadataFile);
    ChecksummedFile checksum = new ChecksummedFile(metadataFile);
    checksum.fixChecksums(algorithms);
}
Also used : Path(java.nio.file.Path) ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata) ChecksummedFile(org.apache.archiva.checksum.ChecksummedFile) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 8 with ChecksummedFile

use of org.apache.archiva.checksum.ChecksummedFile in project archiva by apache.

the class ArtifactMissingChecksumsConsumer method createFixChecksum.

private void createFixChecksum(String path, ChecksumAlgorithm checksumAlgorithm) {
    Path artifactFile = repositoryDir.resolve(path);
    Path checksumFile = repositoryDir.resolve(path + "." + checksumAlgorithm.getExt().get(0));
    if (Files.exists(checksumFile)) {
        checksum = new ChecksummedFile(artifactFile);
        if (!checksum.isValidChecksum(checksumAlgorithm)) {
            checksum.fixChecksum(checksumAlgorithm);
            log.info("Fixed checksum file {}", checksumFile.toAbsolutePath());
            triggerConsumerInfo("Fixed checksum file " + checksumFile.toAbsolutePath());
        }
    } else if (!Files.exists(checksumFile)) {
        checksum = new ChecksummedFile(artifactFile);
        try {
            checksum.createChecksum(checksumAlgorithm);
            log.info("Created missing checksum file {}", checksumFile.toAbsolutePath());
            triggerConsumerInfo("Created missing checksum file " + checksumFile.toAbsolutePath());
        } catch (IOException e) {
            log.error("Cannot create checksum for file {} :", checksumFile, e);
            triggerConsumerError(TYPE_CHECKSUM_CANNOT_CREATE, "Cannot create checksum for file " + checksumFile + ": " + e.getMessage());
        }
    } else {
        log.warn("Checksum file {} is not a file. ", checksumFile.toAbsolutePath());
        triggerConsumerWarning(TYPE_CHECKSUM_NOT_FILE, "Checksum file " + checksumFile.toAbsolutePath() + " is not a file.");
    }
}
Also used : Path(java.nio.file.Path) IOException(java.io.IOException) ChecksummedFile(org.apache.archiva.checksum.ChecksummedFile)

Example 9 with ChecksummedFile

use of org.apache.archiva.checksum.ChecksummedFile in project archiva by apache.

the class DefaultFileUploadService method fixChecksums.

private void fixChecksums(Path file) {
    ChecksummedFile checksum = new ChecksummedFile(file);
    checksum.fixChecksums(algorithms);
}
Also used : ChecksummedFile(org.apache.archiva.checksum.ChecksummedFile)

Example 10 with ChecksummedFile

use of org.apache.archiva.checksum.ChecksummedFile in project archiva by apache.

the class MetadataTools method updateMetadata.

/**
 * Update the metadata based on the following rules.
 * <p>
 * 1) If this is a SNAPSHOT reference, then utilize the proxy/repository specific
 * metadata files to represent the current / latest SNAPSHOT available.
 * 2) If this is a RELEASE reference, and the metadata file does not exist, then
 * create the metadata file with contents required of the VersionedReference
 *
 * @param managedRepository the managed repository where the metadata is kept.
 * @param reference         the versioned reference to update
 * @throws LayoutException
 * @throws RepositoryMetadataException
 * @throws IOException
 * @throws ContentNotFoundException
 * @deprecated
 */
public void updateMetadata(ManagedRepositoryContent managedRepository, VersionedReference reference) throws LayoutException, RepositoryMetadataException, IOException, ContentNotFoundException {
    Path metadataFile = Paths.get(managedRepository.getRepoRoot(), toPath(reference));
    long lastUpdated = getExistingLastUpdated(metadataFile);
    ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
    metadata.setGroupId(reference.getGroupId());
    metadata.setArtifactId(reference.getArtifactId());
    if (VersionUtil.isSnapshot(reference.getVersion())) {
        // Do SNAPSHOT handling.
        metadata.setVersion(VersionUtil.getBaseVersion(reference.getVersion()));
        // Gather up all of the versions found in the reference dir, and any
        // proxied maven-metadata.xml files.
        Set<String> snapshotVersions = gatherSnapshotVersions(managedRepository, reference);
        if (snapshotVersions.isEmpty()) {
            throw new ContentNotFoundException("No snapshot versions found on reference [" + VersionedReference.toKey(reference) + "].");
        }
        // sort the list to determine to aide in determining the Latest version.
        List<String> sortedVersions = new ArrayList<>();
        sortedVersions.addAll(snapshotVersions);
        Collections.sort(sortedVersions, new VersionComparator());
        String latestVersion = sortedVersions.get(sortedVersions.size() - 1);
        if (VersionUtil.isUniqueSnapshot(latestVersion)) {
            // The latestVersion will contain the full version string "1.0-alpha-5-20070821.213044-8"
            // This needs to be broken down into ${base}-${timestamp}-${build_number}
            Matcher m = VersionUtil.UNIQUE_SNAPSHOT_PATTERN.matcher(latestVersion);
            if (m.matches()) {
                metadata.setSnapshotVersion(new SnapshotVersion());
                int buildNumber = NumberUtils.toInt(m.group(3), -1);
                metadata.getSnapshotVersion().setBuildNumber(buildNumber);
                Matcher mtimestamp = VersionUtil.TIMESTAMP_PATTERN.matcher(m.group(2));
                if (mtimestamp.matches()) {
                    String tsDate = mtimestamp.group(1);
                    String tsTime = mtimestamp.group(2);
                    long snapshotLastUpdated = toLastUpdatedLong(tsDate + tsTime);
                    lastUpdated = Math.max(lastUpdated, snapshotLastUpdated);
                    metadata.getSnapshotVersion().setTimestamp(m.group(2));
                }
            }
        } else if (VersionUtil.isGenericSnapshot(latestVersion)) {
            // The latestVersion ends with the generic version string.
            // Example: 1.0-alpha-5-SNAPSHOT
            metadata.setSnapshotVersion(new SnapshotVersion());
        /* Disabled due to decision in [MRM-535].
                 * Do not set metadata.lastUpdated to file.lastModified.
                 * 
                 * Should this be the last updated timestamp of the file, or in the case of an 
                 * archive, the most recent timestamp in the archive?
                 * 
                ArtifactReference artifact = getFirstArtifact( managedRepository, reference );

                if ( artifact == null )
                {
                    throw new IOException( "Not snapshot artifact found to reference in " + reference );
                }

                File artifactFile = managedRepository.toFile( artifact );

                if ( artifactFile.exists() )
                {
                    Date lastModified = new Date( artifactFile.lastModified() );
                    metadata.setLastUpdatedTimestamp( lastModified );
                }
                */
        } else {
            throw new RepositoryMetadataException("Unable to process snapshot version <" + latestVersion + "> reference <" + reference + ">");
        }
    } else {
        // Do RELEASE handling.
        metadata.setVersion(reference.getVersion());
    }
    // Set last updated
    if (lastUpdated > 0) {
        metadata.setLastUpdatedTimestamp(toLastUpdatedDate(lastUpdated));
    }
    // Save the metadata model to disk.
    RepositoryMetadataWriter.write(metadata, metadataFile);
    ChecksummedFile checksum = new ChecksummedFile(metadataFile);
    checksum.fixChecksums(algorithms);
}
Also used : Path(java.nio.file.Path) ContentNotFoundException(org.apache.archiva.repository.ContentNotFoundException) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) ChecksummedFile(org.apache.archiva.checksum.ChecksummedFile) SnapshotVersion(org.apache.archiva.model.SnapshotVersion) VersionComparator(org.apache.archiva.common.utils.VersionComparator) ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata)

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