use of org.apache.archiva.repository.content.base.ArchivaItemSelector in project archiva by apache.
the class DefaultRepositoriesService method removeProjectVersion.
@Override
public ActionStatus 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 (!getPermissionStatus(repositoryId).isAuthorizedToDeleteArtifacts()) {
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 = null;
try {
repositorySession = repositorySessionFactory.createSession();
} catch (MetadataRepositoryException e) {
e.printStackTrace();
}
try {
ManagedRepositoryContent repository = getManagedRepositoryContent(repositoryId);
BaseRepositoryContentLayout layout = repository.getLayout(BaseRepositoryContentLayout.class);
ArchivaItemSelector selector = ArchivaItemSelector.builder().withNamespace(namespace).withProjectId(projectId).withVersion(version).build();
Version versionItem = layout.getVersion(selector);
if (versionItem != null && versionItem.exists()) {
repository.deleteItem(versionItem);
}
MetadataRepository metadataRepository = repositorySession.getRepository();
Collection<ArtifactMetadata> artifacts = metadataRepository.getArtifacts(repositorySession, repositoryId, namespace, projectId, version);
for (ArtifactMetadata artifactMetadata : artifacts) {
metadataRepository.removeTimestampedArtifact(repositorySession, artifactMetadata, version);
}
metadataRepository.removeProjectVersion(repositorySession, repositoryId, namespace, projectId, version);
} catch (MetadataRepositoryException | MetadataResolutionException | RepositoryException | ItemNotFoundException | LayoutException e) {
throw new ArchivaRestServiceException("Repository exception: " + e.getMessage(), 500, e);
} finally {
try {
repositorySession.save();
} catch (MetadataSessionException e) {
log.error("Session save failed {}", e.getMessage());
}
repositorySession.close();
}
return ActionStatus.SUCCESS;
}
use of org.apache.archiva.repository.content.base.ArchivaItemSelector in project archiva by apache.
the class DaysOldRepositoryPurge method process.
@Override
public void process(String path) throws RepositoryPurgeException {
try {
ContentItem item = repository.toItem(path);
Artifact artifactItem = repository.getLayout(BaseRepositoryContentLayout.class).adaptItem(Artifact.class, item);
if (!artifactItem.exists()) {
return;
}
// ArtifactReference artifact = repository.toArtifactReference( path );
Calendar olderThanThisDate = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
olderThanThisDate.add(Calendar.DATE, -retentionPeriod);
ArchivaItemSelector selector = ArchivaItemSelector.builder().withNamespace(artifactItem.getVersion().getProject().getNamespace().getId()).withProjectId(artifactItem.getVersion().getProject().getId()).withVersion(artifactItem.getVersion().getId()).withClassifier("*").includeRelatedArtifacts().build();
List<String> artifactVersions;
try (Stream<? extends Artifact> stream = repository.getLayout(BaseRepositoryContentLayout.class).newArtifactStream(selector)) {
artifactVersions = stream.map(a -> a.getArtifactVersion()).filter(StringUtils::isNotEmpty).distinct().collect(Collectors.toList());
}
Collections.sort(artifactVersions, VersionComparator.getInstance());
if (retentionCount > artifactVersions.size()) {
// Done. nothing to do here. skip it.
return;
}
int countToPurge = artifactVersions.size() - retentionCount;
ArchivaItemSelector.Builder artifactSelectorBuilder = ArchivaItemSelector.builder().withNamespace(artifactItem.getVersion().getProject().getNamespace().getId()).withProjectId(artifactItem.getVersion().getProject().getId()).withVersion(artifactItem.getVersion().getId()).withArtifactId(artifactItem.getId()).withClassifier("*").includeRelatedArtifacts();
Set<Artifact> artifactsToDelete = new HashSet<>();
for (String version : artifactVersions) {
if (countToPurge-- <= 0) {
break;
}
ArchivaItemSelector artifactSelector = artifactSelectorBuilder.withArtifactVersion(version).build();
try {
// Is this a generic snapshot "1.0-SNAPSHOT" ?
if (VersionUtil.isGenericSnapshot(version)) {
List<? extends Artifact> artifactList = repository.getLayout(BaseRepositoryContentLayout.class).getArtifacts(artifactSelector);
if (artifactList.size() > 0 && artifactList.get(0).getAsset().getModificationTime().toEpochMilli() < olderThanThisDate.getTimeInMillis()) {
artifactsToDelete.addAll(artifactList);
}
} else // Is this a timestamp snapshot "1.0-20070822.123456-42" ?
if (VersionUtil.isUniqueSnapshot(version)) {
Calendar timestampCal = uniqueSnapshotToCalendar(version);
if (timestampCal.getTimeInMillis() < olderThanThisDate.getTimeInMillis()) {
artifactsToDelete.addAll(repository.getLayout(BaseRepositoryContentLayout.class).getArtifacts(artifactSelector));
}
}
} catch (IllegalArgumentException e) {
log.error("Bad selector for artifact: {}", e.getMessage(), e);
// continue
}
}
purge(artifactsToDelete);
} catch (LayoutException e) {
log.debug("Not processing file that is not an artifact: {}", e.getMessage());
} catch (ContentAccessException e) {
e.printStackTrace();
}
}
use of org.apache.archiva.repository.content.base.ArchivaItemSelector in project archiva by apache.
the class MetadataTools method gatherSnapshotVersions.
/**
* Gather the set of snapshot versions found in a particular versioned reference.
*
* @return the Set of snapshot artifact versions found.
* @throws LayoutException
* @throws ContentNotFoundException
*/
public Set<String> gatherSnapshotVersions(ManagedRepositoryContent managedRepository, ItemSelector reference) throws LayoutException, IOException, ContentNotFoundException {
Set<String> foundVersions = null;
try {
ArchivaItemSelector selector = ArchivaItemSelector.builder().withNamespace(reference.getNamespace()).withProjectId(reference.getArtifactId()).withArtifactId(reference.getArtifactId()).withVersion(reference.getVersion()).build();
try (Stream<? extends Artifact> stream = managedRepository.getLayout(BaseRepositoryContentLayout.class).newArtifactStream(selector)) {
foundVersions = stream.map(a -> a.getArtifactVersion()).filter(StringUtils::isNotEmpty).collect(Collectors.toSet());
}
} catch (ContentAccessException e) {
log.error("Error while accessing content {}", e.getMessage());
throw new IOException("Could not access repository content: " + e.getMessage());
}
// Next gather up the referenced 'latest' versions found in any proxied repositories
// maven-metadata-${proxyId}.xml files that may be present.
// Does this repository have a set of remote proxied repositories?
Set<String> proxiedRepoIds = this.proxies.get(managedRepository.getId());
if (CollectionUtils.isNotEmpty(proxiedRepoIds)) {
String baseVersion = VersionUtil.getBaseVersion(reference.getVersion());
baseVersion = baseVersion.substring(0, baseVersion.indexOf(VersionUtil.SNAPSHOT) - 1);
// 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) {
// There is no proxy metadata, skip it.
continue;
}
// Is there some snapshot info?
SnapshotVersion snapshot = proxyMetadata.getSnapshotVersion();
if (snapshot != null) {
String timestamp = snapshot.getTimestamp();
int buildNumber = snapshot.getBuildNumber();
// Only interested in the timestamp + buildnumber.
if (StringUtils.isNotBlank(timestamp) && (buildNumber > 0)) {
foundVersions.add(baseVersion + "-" + timestamp + "-" + buildNumber);
}
}
}
}
return foundVersions;
}
use of org.apache.archiva.repository.content.base.ArchivaItemSelector in project archiva by apache.
the class ManagedDefaultRepositoryContentTest method deleteItemNotFound.
@Test
public void deleteItemNotFound() throws IOException, URISyntaxException, ItemNotFoundException {
ManagedRepository repo = createManagedRepoWithContent("delete-repository");
ManagedRepositoryContent myRepoContent = repo.getContent();
Path repoRoot = repo.getRoot().getFilePath();
ArchivaItemSelector selector = ArchivaItemSelector.builder().withNamespace("org.apache.test2").build();
ContentItem item = myRepoContent.getItem(selector);
assertTrue(item instanceof Namespace);
try {
myRepoContent.deleteItem(item);
assertTrue("ItemNotFoundException expected for non existing namespace", false);
} catch (ItemNotFoundException e) {
}
selector = ArchivaItemSelector.builder().withNamespace("org.apache.test").withProjectId("samplejar2").build();
item = myRepoContent.getItem(selector);
assertTrue(item instanceof Project);
try {
myRepoContent.deleteItem(item);
assertTrue("ItemNotFoundException expected for non existing project", false);
} catch (ItemNotFoundException e) {
}
selector = ArchivaItemSelector.builder().withNamespace("org.apache.test").withProjectId("samplejar").withVersion("1.1").build();
item = myRepoContent.getItem(selector);
assertTrue(item instanceof Version);
try {
myRepoContent.deleteItem(item);
assertTrue("ItemNotFoundException expected for non existing version", false);
} catch (ItemNotFoundException e) {
}
selector = ArchivaItemSelector.builder().withNamespace("org.apache.test").withProjectId("samplejar").withVersion("1.0").withArtifactId("samplejar").withArtifactVersion("1.0").withExtension("jax").build();
item = myRepoContent.getItem(selector);
assertTrue(item instanceof Artifact);
try {
myRepoContent.deleteItem(item);
assertTrue("ItemNotFoundException expected for non existing artifact", false);
} catch (ItemNotFoundException e) {
}
}
use of org.apache.archiva.repository.content.base.ArchivaItemSelector in project archiva by apache.
the class ManagedDefaultRepositoryContentTest method getNonExistingMetadataItem.
@Test
public void getNonExistingMetadataItem() {
// org/apache/maven/some-ejb/1.0
ArchivaItemSelector versionSelector = ArchivaItemSelector.builder().withNamespace("javax.sql").withProjectId("jdbc").withVersion("2.0").build();
Version version = repoContent.getVersion(versionSelector);
DataItem metaData = repoContent.getMetadataItem(version);
assertFalse(metaData.exists());
assertEquals("/javax/sql/jdbc/2.0/maven-metadata.xml", metaData.getAsset().getPath());
}
Aggregations