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;
}
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());
}
}
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());
}
}
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);
}
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);
}
Aggregations