Search in sources :

Example 16 with VersionedReference

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

the class DefaultRepositoriesService method removeProjectVersion.

@Override
public Boolean removeProjectVersion(String repositoryId, String namespace, String projectId, String version) throws ArchivaRestServiceException {
    // if not a generic we can use the standard way to delete artifact
    if (!VersionUtil.isGenericSnapshot(version)) {
        Artifact artifact = new Artifact(namespace, projectId, version);
        artifact.setRepositoryId(repositoryId);
        artifact.setContext(repositoryId);
        return deleteArtifact(artifact);
    }
    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 (StringUtils.isEmpty(namespace)) {
        throw new ArchivaRestServiceException("groupId cannot be null", 400, null);
    }
    if (StringUtils.isEmpty(projectId)) {
        throw new ArchivaRestServiceException("artifactId cannot be null", 400, null);
    }
    if (StringUtils.isEmpty(version)) {
        throw new ArchivaRestServiceException("version cannot be null", 400, null);
    }
    RepositorySession repositorySession = repositorySessionFactory.createSession();
    try {
        ManagedRepositoryContent repository = getManagedRepositoryContent(repositoryId);
        VersionedReference ref = new VersionedReference();
        ref.setArtifactId(projectId);
        ref.setGroupId(namespace);
        ref.setVersion(version);
        repository.deleteVersion(ref);
        /*
            ProjectReference projectReference = new ProjectReference();
            projectReference.setGroupId( namespace );
            projectReference.setArtifactId( projectId );

            repository.getVersions(  )
            */
        ArtifactReference artifactReference = new ArtifactReference();
        artifactReference.setGroupId(namespace);
        artifactReference.setArtifactId(projectId);
        artifactReference.setVersion(version);
        MetadataRepository metadataRepository = repositorySession.getRepository();
        Set<ArtifactReference> related = repository.getRelatedArtifacts(artifactReference);
        log.debug("related: {}", related);
        for (ArtifactReference artifactRef : related) {
            repository.deleteArtifact(artifactRef);
        }
        Collection<ArtifactMetadata> artifacts = metadataRepository.getArtifacts(repositoryId, namespace, projectId, version);
        for (ArtifactMetadata artifactMetadata : artifacts) {
            metadataRepository.removeArtifact(artifactMetadata, version);
        }
        metadataRepository.removeProjectVersion(repositoryId, namespace, projectId, version);
    } catch (MetadataRepositoryException e) {
        throw new ArchivaRestServiceException("Repository exception: " + e.getMessage(), 500, e);
    } catch (MetadataResolutionException e) {
        throw new ArchivaRestServiceException("Repository exception: " + e.getMessage(), 500, e);
    } catch (RepositoryException e) {
        throw new ArchivaRestServiceException("Repository exception: " + e.getMessage(), 500, e);
    } finally {
        repositorySession.save();
        repositorySession.close();
    }
    return Boolean.TRUE;
}
Also used : MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) RepositoryException(org.apache.archiva.repository.RepositoryException) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) RepositorySession(org.apache.archiva.metadata.repository.RepositorySession) Artifact(org.apache.archiva.maven2.model.Artifact) MetadataResolutionException(org.apache.archiva.metadata.repository.MetadataResolutionException) MetadataRepository(org.apache.archiva.metadata.repository.MetadataRepository) VersionedReference(org.apache.archiva.model.VersionedReference) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) ArtifactReference(org.apache.archiva.model.ArtifactReference) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata)

Example 17 with VersionedReference

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

the class MetadataUpdaterConsumer method updateVersionMetadata.

private void updateVersionMetadata(ArtifactReference artifact, String path) {
    VersionedReference versionRef = new VersionedReference();
    versionRef.setGroupId(artifact.getGroupId());
    versionRef.setArtifactId(artifact.getArtifactId());
    versionRef.setVersion(artifact.getVersion());
    try {
        String metadataPath = this.metadataTools.toPath(versionRef);
        Path projectMetadata = this.repositoryDir.resolve(metadataPath);
        if (Files.exists(projectMetadata) && (Files.getLastModifiedTime(projectMetadata).toMillis() >= this.scanStartTimestamp)) {
            // This metadata is up to date. skip it.
            log.debug("Skipping uptodate metadata: {}", this.metadataTools.toPath(versionRef));
            return;
        }
        metadataTools.updateMetadata(this.repository, metadataPath);
        log.debug("Updated metadata: {}", this.metadataTools.toPath(versionRef));
    } catch (RepositoryMetadataException e) {
        log.error("Unable to write version metadata for artifact [{}]: ", path, e);
        triggerConsumerError(TYPE_METADATA_WRITE_FAILURE, "Unable to write version metadata for artifact [" + path + "]: " + e.getMessage());
    } catch (IOException e) {
        log.warn("Version metadata not written due to IO warning: ", e);
        triggerConsumerWarning(TYPE_METADATA_IO, "Version metadata not written due to IO warning: " + e.getMessage());
    }
}
Also used : Path(java.nio.file.Path) VersionedReference(org.apache.archiva.model.VersionedReference) RepositoryMetadataException(org.apache.archiva.repository.metadata.RepositoryMetadataException) IOException(java.io.IOException)

Example 18 with VersionedReference

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

the class DaysOldRepositoryPurge 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);
        Calendar olderThanThisDate = Calendar.getInstance(DateUtils.UTC_TIME_ZONE);
        olderThanThisDate.add(Calendar.DATE, -retentionPeriod);
        // respect retention count
        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()) {
            // 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;
            }
            ArtifactReference newArtifactReference = repository.toArtifactReference(artifactFile.toAbsolutePath().toString());
            newArtifactReference.setVersion(version);
            Path newArtifactFile = repository.toFile(newArtifactReference);
            // Is this a generic snapshot "1.0-SNAPSHOT" ?
            if (VersionUtil.isGenericSnapshot(newArtifactReference.getVersion())) {
                if (Files.getLastModifiedTime(newArtifactFile).toMillis() < olderThanThisDate.getTimeInMillis()) {
                    artifactsToDelete.addAll(repository.getRelatedArtifacts(newArtifactReference));
                }
            } else // Is this a timestamp snapshot "1.0-20070822.123456-42" ?
            if (VersionUtil.isUniqueSnapshot(newArtifactReference.getVersion())) {
                Calendar timestampCal = uniqueSnapshotToCalendar(newArtifactReference.getVersion());
                if (timestampCal.getTimeInMillis() < olderThanThisDate.getTimeInMillis()) {
                    artifactsToDelete.addAll(repository.getRelatedArtifacts(newArtifactReference));
                }
            }
        }
        purge(artifactsToDelete);
    } catch (ContentNotFoundException | IOException e) {
        throw new RepositoryPurgeException(e.getMessage(), e);
    } catch (LayoutException e) {
        log.debug("Not processing file that is not an artifact: {}", e.getMessage());
    }
}
Also used : Path(java.nio.file.Path) ContentNotFoundException(org.apache.archiva.repository.ContentNotFoundException) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) IOException(java.io.IOException) VersionedReference(org.apache.archiva.model.VersionedReference) LayoutException(org.apache.archiva.repository.LayoutException) ArtifactReference(org.apache.archiva.model.ArtifactReference) HashSet(java.util.HashSet)

Example 19 with VersionedReference

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

the class MetadataTransferTest method assertRepoReleaseMetadataContents.

/**
 * Ensures that the repository specific maven metadata file exists, and contains the appropriate
 * list of expected versions within.
 *
 * @param proxiedRepoId
 * @param requestedResource
 */
private void assertRepoReleaseMetadataContents(String proxiedRepoId, String requestedResource) throws Exception {
    String proxiedFile = metadataTools.getRepositorySpecificName(proxiedRepoId, requestedResource);
    Path actualFile = managedDefaultDir.resolve(proxiedFile);
    assertTrue("Release metadata for repo should exist: " + actualFile, Files.exists(actualFile));
    VersionedReference metadata = createVersionedReference(requestedResource);
    // Build expected metadata XML
    StringWriter expectedMetadataXml = new StringWriter();
    ArchivaRepositoryMetadata m = new ArchivaRepositoryMetadata();
    m.setGroupId(metadata.getGroupId());
    m.setArtifactId(metadata.getArtifactId());
    m.setVersion(metadata.getVersion());
    RepositoryMetadataWriter.write(m, expectedMetadataXml);
    // Compare the file to the actual contents.
    assertMetadataEquals(expectedMetadataXml.toString(), actualFile);
}
Also used : Path(java.nio.file.Path) VersionedReference(org.apache.archiva.model.VersionedReference) StringWriter(java.io.StringWriter) ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata)

Example 20 with VersionedReference

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

the class MetadataToolsTest method assertUpdatedSnapshotVersionMetadata.

private void assertUpdatedSnapshotVersionMetadata(String artifactId, String version, String expectedDate, String expectedTime, String expectedBuildNumber) throws Exception {
    ManagedRepositoryContent testRepo = createTestRepoContent();
    VersionedReference reference = new VersionedReference();
    reference.setGroupId("org.apache.archiva.metadata.tests");
    reference.setArtifactId(artifactId);
    reference.setVersion(version);
    prepTestRepo(testRepo, reference);
    tools.updateMetadata(testRepo, reference);
    StringBuilder buf = new StringBuilder();
    buf.append("<metadata>\n");
    buf.append("  <groupId>").append(reference.getGroupId()).append("</groupId>\n");
    buf.append("  <artifactId>").append(reference.getArtifactId()).append("</artifactId>\n");
    buf.append("  <version>").append(reference.getVersion()).append("</version>\n");
    buf.append("  <versioning>\n");
    buf.append("    <snapshot>\n");
    buf.append("      <buildNumber>").append(expectedBuildNumber).append("</buildNumber>\n");
    buf.append("      <timestamp>");
    buf.append(expectedDate).append(".").append(expectedTime);
    buf.append("</timestamp>\n");
    buf.append("    </snapshot>\n");
    buf.append("    <lastUpdated>").append(expectedDate).append(expectedTime).append("</lastUpdated>\n");
    buf.append("  </versioning>\n");
    buf.append("</metadata>");
    assertMetadata(buf.toString(), testRepo, reference);
}
Also used : VersionedReference(org.apache.archiva.model.VersionedReference) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent)

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