Search in sources :

Example 6 with VersionedReference

use of org.apache.archiva.model.VersionedReference in project archiva by apache.

the class CleanupReleasedSnapshotsRepositoryPurge method process.

@Override
public void process(String path) throws RepositoryPurgeException {
    try {
        Path artifactFile = Paths.get(repository.getRepoRoot(), path);
        if (!Files.exists(artifactFile)) {
            // Nothing to do here, file doesn't exist, skip it.
            return;
        }
        ArtifactReference artifactRef = repository.toArtifactReference(path);
        if (!VersionUtil.isSnapshot(artifactRef.getVersion())) {
            // Nothing to do here, not a snapshot, skip it.
            return;
        }
        ProjectReference reference = new ProjectReference();
        reference.setGroupId(artifactRef.getGroupId());
        reference.setArtifactId(artifactRef.getArtifactId());
        // Gether the released versions
        List<String> releasedVersions = new ArrayList<>();
        Collection<org.apache.archiva.repository.ManagedRepository> repos = repositoryRegistry.getManagedRepositories();
        for (org.apache.archiva.repository.ManagedRepository repo : repos) {
            if (repo.getActiveReleaseSchemes().contains(ReleaseScheme.RELEASE)) {
                try {
                    ManagedRepositoryContent repoContent = repo.getContent();
                    for (String version : repoContent.getVersions(reference)) {
                        if (!VersionUtil.isSnapshot(version)) {
                            releasedVersions.add(version);
                        }
                    }
                } catch (RepositoryException e) {
                // swallow
                }
            }
        }
        Collections.sort(releasedVersions, VersionComparator.getInstance());
        // Now clean out any version that is earlier than the highest released version.
        boolean needsMetadataUpdate = false;
        VersionedReference versionRef = new VersionedReference();
        versionRef.setGroupId(artifactRef.getGroupId());
        versionRef.setArtifactId(artifactRef.getArtifactId());
        MetadataRepository metadataRepository = repositorySession.getRepository();
        if (releasedVersions.contains(VersionUtil.getReleaseVersion(artifactRef.getVersion()))) {
            versionRef.setVersion(artifactRef.getVersion());
            repository.deleteVersion(versionRef);
            for (RepositoryListener listener : listeners) {
                listener.deleteArtifact(metadataRepository, repository.getId(), artifactRef.getGroupId(), artifactRef.getArtifactId(), artifactRef.getVersion(), artifactFile.getFileName().toString());
            }
            metadataRepository.removeProjectVersion(repository.getId(), artifactRef.getGroupId(), artifactRef.getArtifactId(), artifactRef.getVersion());
            needsMetadataUpdate = true;
        }
        if (needsMetadataUpdate) {
            updateMetadata(artifactRef);
        }
    } catch (LayoutException e) {
        log.debug("Not processing file that is not an artifact: {}", e.getMessage());
    } catch (ContentNotFoundException e) {
        throw new RepositoryPurgeException(e.getMessage(), e);
    } catch (MetadataRepositoryException e) {
        log.error("Could not remove metadata during cleanup of released snapshots of {}", path, e);
    }
}
Also used : Path(java.nio.file.Path) RepositoryListener(org.apache.archiva.repository.events.RepositoryListener) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) ProjectReference(org.apache.archiva.model.ProjectReference) ContentNotFoundException(org.apache.archiva.repository.ContentNotFoundException) ArrayList(java.util.ArrayList) RepositoryException(org.apache.archiva.repository.RepositoryException) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) MetadataRepository(org.apache.archiva.metadata.repository.MetadataRepository) VersionedReference(org.apache.archiva.model.VersionedReference) LayoutException(org.apache.archiva.repository.LayoutException) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) ArtifactReference(org.apache.archiva.model.ArtifactReference)

Example 7 with VersionedReference

use of org.apache.archiva.model.VersionedReference in project archiva by apache.

the class RetentionCountRepositoryPurge method process.

@Override
public void process(String path) throws RepositoryPurgeException {
    try {
        Path artifactFile = Paths.get(repository.getRepoRoot(), path);
        if (!Files.exists(artifactFile)) {
            return;
        }
        ArtifactReference artifact = repository.toArtifactReference(path);
        if (VersionUtil.isSnapshot(artifact.getVersion())) {
            VersionedReference reference = new VersionedReference();
            reference.setGroupId(artifact.getGroupId());
            reference.setArtifactId(artifact.getArtifactId());
            reference.setVersion(artifact.getVersion());
            List<String> versions = new ArrayList<>(repository.getVersions(reference));
            Collections.sort(versions, VersionComparator.getInstance());
            if (retentionCount > versions.size()) {
                log.trace("No deletion, because retention count is higher than actual number of artifacts.");
                // Done. nothing to do here. skip it.
                return;
            }
            int countToPurge = versions.size() - retentionCount;
            Set<ArtifactReference> artifactsToDelete = new HashSet<>();
            for (String version : versions) {
                if (countToPurge-- <= 0) {
                    break;
                }
                artifactsToDelete.addAll(repository.getRelatedArtifacts(getNewArtifactReference(artifact, version)));
            }
            purge(artifactsToDelete);
        }
    } catch (LayoutException le) {
        throw new RepositoryPurgeException(le.getMessage(), le);
    } catch (ContentNotFoundException e) {
        log.error("Repostory artifact not found {}", path);
    }
}
Also used : Path(java.nio.file.Path) VersionedReference(org.apache.archiva.model.VersionedReference) LayoutException(org.apache.archiva.repository.LayoutException) ContentNotFoundException(org.apache.archiva.repository.ContentNotFoundException) ArrayList(java.util.ArrayList) ArtifactReference(org.apache.archiva.model.ArtifactReference) HashSet(java.util.HashSet)

Example 8 with VersionedReference

use of org.apache.archiva.model.VersionedReference in project archiva by apache.

the class MetadataTransferTest method assertFetchVersioned.

/**
 * Transfer the metadata file.
 *
 * @param requestedResource the requested resource
 * @throws Exception
 */
private void assertFetchVersioned(String requestedResource) throws Exception {
    Path expectedFile = managedDefaultDir.resolve(requestedResource);
    VersionedReference metadata = createVersionedReference(requestedResource);
    Path downloadedFile = proxyHandler.fetchMetadataFromProxies(managedDefaultRepository, managedDefaultRepository.toMetadataPath(metadata)).getFile();
    assertNotNull("Should have downloaded a file.", downloadedFile);
    assertNoTempFiles(expectedFile);
}
Also used : Path(java.nio.file.Path) VersionedReference(org.apache.archiva.model.VersionedReference)

Example 9 with VersionedReference

use of org.apache.archiva.model.VersionedReference in project archiva by apache.

the class MetadataTransferTest method assertRepoSnapshotMetadataContents.

/**
 * Test for the existance of the proxied repository specific snapshot metadata in the default managed
 * repository, and if it exists, does it contain the expected release maven-metadata.xml contents?
 *
 * @param proxiedRepoId       the repository id of the proxied repository.
 * @param requestedResource   the requested resource
 * @param expectedDate        the date in "yyyyMMdd" format
 * @param expectedTime        the time in "hhmmss" format
 * @param expectedBuildnumber the build number
 * @throws Exception
 */
private void assertRepoSnapshotMetadataContents(String proxiedRepoId, String requestedResource, String expectedDate, String expectedTime, int expectedBuildnumber) throws Exception {
    String proxiedFile = metadataTools.getRepositorySpecificName(proxiedRepoId, requestedResource);
    Path actualFile = managedDefaultDir.resolve(proxiedFile);
    assertTrue("Repo Specific Snapshot Metadata should exist: " + requestedResource, Files.exists(actualFile));
    VersionedReference actualMetadata = createVersionedReference(requestedResource);
    assertSnapshotMetadata(actualFile, actualMetadata, expectedDate, expectedTime, expectedBuildnumber);
}
Also used : Path(java.nio.file.Path) VersionedReference(org.apache.archiva.model.VersionedReference)

Example 10 with VersionedReference

use of org.apache.archiva.model.VersionedReference in project archiva by apache.

the class DefaultRepositoriesService method deleteArtifact.

@Override
public Boolean deleteArtifact(Artifact artifact) throws ArchivaRestServiceException {
    String repositoryId = artifact.getContext();
    // so try both!!
    if (StringUtils.isEmpty(repositoryId)) {
        repositoryId = artifact.getRepositoryId();
    }
    if (StringUtils.isEmpty(repositoryId)) {
        throw new ArchivaRestServiceException("repositoryId cannot be null", 400, null);
    }
    if (!isAuthorizedToDeleteArtifacts(repositoryId)) {
        throw new ArchivaRestServiceException("not authorized to delete artifacts", 403, null);
    }
    if (artifact == null) {
        throw new ArchivaRestServiceException("artifact cannot be null", 400, null);
    }
    if (StringUtils.isEmpty(artifact.getGroupId())) {
        throw new ArchivaRestServiceException("artifact.groupId cannot be null", 400, null);
    }
    if (StringUtils.isEmpty(artifact.getArtifactId())) {
        throw new ArchivaRestServiceException("artifact.artifactId cannot be null", 400, null);
    }
    // TODO more control on artifact fields
    boolean snapshotVersion = VersionUtil.isSnapshot(artifact.getVersion()) | VersionUtil.isGenericSnapshot(artifact.getVersion());
    RepositorySession repositorySession = repositorySessionFactory.createSession();
    try {
        Date lastUpdatedTimestamp = Calendar.getInstance().getTime();
        TimeZone timezone = TimeZone.getTimeZone("UTC");
        DateFormat fmt = new SimpleDateFormat("yyyyMMdd.HHmmss");
        fmt.setTimeZone(timezone);
        ManagedRepository repoConfig = managedRepositoryAdmin.getManagedRepository(repositoryId);
        VersionedReference ref = new VersionedReference();
        ref.setArtifactId(artifact.getArtifactId());
        ref.setGroupId(artifact.getGroupId());
        ref.setVersion(artifact.getVersion());
        ManagedRepositoryContent repository = getManagedRepositoryContent(repositoryId);
        ArtifactReference artifactReference = new ArtifactReference();
        artifactReference.setArtifactId(artifact.getArtifactId());
        artifactReference.setGroupId(artifact.getGroupId());
        artifactReference.setVersion(artifact.getVersion());
        artifactReference.setClassifier(artifact.getClassifier());
        artifactReference.setType(artifact.getPackaging());
        MetadataRepository metadataRepository = repositorySession.getRepository();
        String path = repository.toMetadataPath(ref);
        if (StringUtils.isNotBlank(artifact.getClassifier())) {
            if (StringUtils.isBlank(artifact.getPackaging())) {
                throw new ArchivaRestServiceException("You must configure a type/packaging when using classifier", 400, null);
            }
            repository.deleteArtifact(artifactReference);
        } else {
            int index = path.lastIndexOf('/');
            path = path.substring(0, index);
            Path targetPath = Paths.get(repoConfig.getLocation(), path);
            if (!Files.exists(targetPath)) {
                // throw new ContentNotFoundException(
                // artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion() );
                log.warn("targetPath {} not found skip file deletion", targetPath);
            }
            // delete from file system
            if (!snapshotVersion) {
                repository.deleteVersion(ref);
            } else {
                Set<ArtifactReference> related = repository.getRelatedArtifacts(artifactReference);
                log.debug("related: {}", related);
                for (ArtifactReference artifactRef : related) {
                    repository.deleteArtifact(artifactRef);
                }
            }
            Path metadataFile = getMetadata(targetPath.toAbsolutePath().toString());
            ArchivaRepositoryMetadata metadata = getMetadata(metadataFile);
            updateMetadata(metadata, metadataFile, lastUpdatedTimestamp, artifact);
        }
        Collection<ArtifactMetadata> artifacts = Collections.emptyList();
        if (snapshotVersion) {
            String baseVersion = VersionUtil.getBaseVersion(artifact.getVersion());
            artifacts = metadataRepository.getArtifacts(repositoryId, artifact.getGroupId(), artifact.getArtifactId(), baseVersion);
        } else {
            artifacts = metadataRepository.getArtifacts(repositoryId, artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());
        }
        log.debug("artifacts: {}", artifacts);
        if (artifacts.isEmpty()) {
            if (!snapshotVersion) {
                // verify metata repository doesn't contains anymore the version
                Collection<String> projectVersions = metadataRepository.getProjectVersions(repositoryId, artifact.getGroupId(), artifact.getArtifactId());
                if (projectVersions.contains(artifact.getVersion())) {
                    log.warn("artifact not found when deleted but version still here ! so force cleanup");
                    metadataRepository.removeProjectVersion(repositoryId, artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());
                }
            }
        }
        for (ArtifactMetadata artifactMetadata : artifacts) {
            // TODO: mismatch between artifact (snapshot) version and project (base) version here
            if (artifactMetadata.getVersion().equals(artifact.getVersion())) {
                if (StringUtils.isNotBlank(artifact.getClassifier())) {
                    if (StringUtils.isBlank(artifact.getPackaging())) {
                        throw new ArchivaRestServiceException("You must configure a type/packaging when using classifier", 400, null);
                    }
                    // cleanup facet which contains classifier information
                    MavenArtifactFacet mavenArtifactFacet = (MavenArtifactFacet) artifactMetadata.getFacet(MavenArtifactFacet.FACET_ID);
                    if (StringUtils.equals(artifact.getClassifier(), mavenArtifactFacet.getClassifier())) {
                        artifactMetadata.removeFacet(MavenArtifactFacet.FACET_ID);
                        String groupId = artifact.getGroupId(), artifactId = artifact.getArtifactId(), version = artifact.getVersion();
                        MavenArtifactFacet mavenArtifactFacetToCompare = new MavenArtifactFacet();
                        mavenArtifactFacetToCompare.setClassifier(artifact.getClassifier());
                        metadataRepository.removeArtifact(repositoryId, groupId, artifactId, version, mavenArtifactFacetToCompare);
                        metadataRepository.save();
                    }
                } else {
                    if (snapshotVersion) {
                        metadataRepository.removeArtifact(artifactMetadata, VersionUtil.getBaseVersion(artifact.getVersion()));
                    } else {
                        metadataRepository.removeArtifact(artifactMetadata.getRepositoryId(), artifactMetadata.getNamespace(), artifactMetadata.getProject(), artifact.getVersion(), artifactMetadata.getId());
                    }
                }
                // repository metadata to an artifact
                for (RepositoryListener listener : listeners) {
                    listener.deleteArtifact(metadataRepository, repository.getId(), artifactMetadata.getNamespace(), artifactMetadata.getProject(), artifactMetadata.getVersion(), artifactMetadata.getId());
                }
                triggerAuditEvent(repositoryId, path, AuditEvent.REMOVE_FILE);
            }
        }
    } catch (ContentNotFoundException e) {
        throw new ArchivaRestServiceException("Artifact does not exist: " + e.getMessage(), 400, e);
    } catch (RepositoryNotFoundException e) {
        throw new ArchivaRestServiceException("Target repository cannot be found: " + e.getMessage(), 400, e);
    } catch (RepositoryException e) {
        throw new ArchivaRestServiceException("Repository exception: " + e.getMessage(), 500, e);
    } catch (MetadataResolutionException e) {
        throw new ArchivaRestServiceException("Repository exception: " + e.getMessage(), 500, e);
    } catch (MetadataRepositoryException e) {
        throw new ArchivaRestServiceException("Repository exception: " + e.getMessage(), 500, e);
    } catch (RepositoryAdminException e) {
        throw new ArchivaRestServiceException("RepositoryAdmin exception: " + e.getMessage(), 500, e);
    } finally {
        repositorySession.save();
        repositorySession.close();
    }
    return Boolean.TRUE;
}
Also used : ManagedRepository(org.apache.archiva.admin.model.beans.ManagedRepository) MavenArtifactFacet(org.apache.archiva.metadata.model.maven2.MavenArtifactFacet) RepositorySession(org.apache.archiva.metadata.repository.RepositorySession) MetadataRepository(org.apache.archiva.metadata.repository.MetadataRepository) VersionedReference(org.apache.archiva.model.VersionedReference) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata) Path(java.nio.file.Path) RepositoryListener(org.apache.archiva.repository.events.RepositoryListener) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) ContentNotFoundException(org.apache.archiva.repository.ContentNotFoundException) RepositoryException(org.apache.archiva.repository.RepositoryException) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) RepositoryNotFoundException(org.apache.archiva.repository.RepositoryNotFoundException) RepositoryAdminException(org.apache.archiva.admin.model.RepositoryAdminException) Date(java.util.Date) MetadataResolutionException(org.apache.archiva.metadata.repository.MetadataResolutionException) TimeZone(java.util.TimeZone) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata) SimpleDateFormat(java.text.SimpleDateFormat) ArtifactReference(org.apache.archiva.model.ArtifactReference)

Aggregations

VersionedReference (org.apache.archiva.model.VersionedReference)24 Path (java.nio.file.Path)15 ManagedRepositoryContent (org.apache.archiva.repository.ManagedRepositoryContent)8 ArtifactReference (org.apache.archiva.model.ArtifactReference)7 ContentNotFoundException (org.apache.archiva.repository.ContentNotFoundException)7 ArrayList (java.util.ArrayList)6 LayoutException (org.apache.archiva.repository.LayoutException)6 IOException (java.io.IOException)5 HashSet (java.util.HashSet)4 ArchivaRepositoryMetadata (org.apache.archiva.model.ArchivaRepositoryMetadata)4 ProjectReference (org.apache.archiva.model.ProjectReference)4 RepositoryException (org.apache.archiva.repository.RepositoryException)4 VersionComparator (org.apache.archiva.common.utils.VersionComparator)3 MetadataRepository (org.apache.archiva.metadata.repository.MetadataRepository)3 MetadataRepositoryException (org.apache.archiva.metadata.repository.MetadataRepositoryException)3 StringWriter (java.io.StringWriter)2 Files (java.nio.file.Files)2 Paths (java.nio.file.Paths)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Calendar (java.util.Calendar)2