use of org.apache.archiva.metadata.model.ArtifactMetadata in project archiva by apache.
the class Maven2RepositoryMergerTest method testMergeWithOutConflictArtifacts.
@Test
public void testMergeWithOutConflictArtifacts() throws Exception {
String sourceRepoId = "source-repo";
ArtifactMetadata artifact1 = new ArtifactMetadata();
artifact1.setNamespace("org.testng");
artifact1.setProject("testng");
artifact1.setVersion("5.8");
artifact1.setProjectVersion("5.8");
artifact1.setId("testng-5.8-jdk15.jar");
artifact1.setRepositoryId(sourceRepoId);
List<ArtifactMetadata> sourceRepoArtifactsList = getArtifacts();
sourceRepoArtifactsList.add(artifact1);
List<ArtifactMetadata> targetRepoArtifactsList = getArtifacts();
Configuration c = new Configuration();
ManagedRepositoryConfiguration testRepo = new ManagedRepositoryConfiguration();
testRepo.setId(TEST_REPO_ID);
testRepo.setLocation("target/test-repository");
String sourceRepo = "src/test/resources/test-repository-with-conflict-artifacts";
ManagedRepositoryConfiguration testRepoWithConflicts = new ManagedRepositoryConfiguration();
testRepoWithConflicts.setId(sourceRepoId);
testRepoWithConflicts.setLocation(sourceRepo);
RepositoryScanningConfiguration repoScanConfig = new RepositoryScanningConfiguration();
List<String> knownContentConsumers = new ArrayList<>();
knownContentConsumers.add("metadata-updater");
repoScanConfig.setKnownContentConsumers(knownContentConsumers);
c.setRepositoryScanning(repoScanConfig);
c.addManagedRepository(testRepo);
c.addManagedRepository(testRepoWithConflicts);
configuration.save(c);
Path targetRepoFile = Paths.get("/target/test-repository/com/example/test/test-artifact/1.0-SNAPSHOT/test-artifact-1.0-20100308.230825-1.jar");
targetRepoFile.toFile().setReadOnly();
when(metadataRepository.getArtifacts(sourceRepoId)).thenReturn(sourceRepoArtifactsList);
when(metadataRepository.getArtifacts(TEST_REPO_ID)).thenReturn(targetRepoArtifactsList);
assertEquals(1, repositoryMerger.getConflictingArtifacts(metadataRepository, sourceRepoId, TEST_REPO_ID).size());
verify(metadataRepository).getArtifacts(TEST_REPO_ID);
}
use of org.apache.archiva.metadata.model.ArtifactMetadata in project archiva by apache.
the class Maven2RepositoryMergerTest method getArtifacts.
private List<ArtifactMetadata> getArtifacts() {
List<ArtifactMetadata> metadata = new ArrayList<>();
ArtifactMetadata artifact1 = new ArtifactMetadata();
artifact1.setNamespace("com.example.test");
artifact1.setProject("test-artifact");
artifact1.setVersion("1.0-SNAPSHOT");
artifact1.setProjectVersion("1.0-SNAPSHOT");
artifact1.setId("test-artifact-1.0-20100308.230825-1.jar");
metadata.add(artifact1);
return metadata;
}
use of org.apache.archiva.metadata.model.ArtifactMetadata 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;
}
use of org.apache.archiva.metadata.model.ArtifactMetadata in project archiva by apache.
the class DefaultSearchService method getArtifactByChecksum.
public List<Artifact> getArtifactByChecksum(ChecksumSearch checksumSearch) throws ArchivaRestServiceException {
// if no repos set we use ones available for the user
if (checksumSearch.getRepositories() == null || checksumSearch.getRepositories().isEmpty()) {
checksumSearch.setRepositories(getObservableRepos());
}
RepositorySession repositorySession = repositorySessionFactory.createSession();
MetadataRepository metadataRepository = repositorySession.getRepository();
Set<Artifact> artifactSet = new HashSet<>();
try {
for (String repoId : checksumSearch.getRepositories()) {
Collection<ArtifactMetadata> artifactMetadatas = metadataRepository.getArtifactsByChecksum(repoId, checksumSearch.getChecksum());
artifactSet.addAll(buildArtifacts(artifactMetadatas, repoId));
}
return new ArrayList<>(artifactSet);
} catch (MetadataRepositoryException e) {
log.error(e.getMessage(), e);
throw new ArchivaRestServiceException(e.getMessage(), e);
} finally {
repositorySession.closeQuietly();
}
}
use of org.apache.archiva.metadata.model.ArtifactMetadata in project archiva by apache.
the class NewArtifactsRssFeedProcessor method processNewArtifactsInRepo.
private SyndFeed processNewArtifactsInRepo(String repoId, MetadataRepository metadataRepository) throws FeedException {
Calendar greaterThanThisDate = Calendar.getInstance(GMT_TIME_ZONE);
greaterThanThisDate.add(Calendar.DATE, -(getNumberOfDaysBeforeNow()));
greaterThanThisDate.clear(Calendar.MILLISECOND);
List<ArtifactMetadata> artifacts;
try {
artifacts = metadataRepository.getArtifactsByDateRange(repoId, greaterThanThisDate.getTime(), null);
} catch (MetadataRepositoryException e) {
throw new FeedException("Unable to construct feed, metadata could not be retrieved: " + e.getMessage(), e);
}
long tmp = 0;
RssFeedEntry entry = null;
List<RssFeedEntry> entries = new ArrayList<>();
String description = "";
int idx = 0;
for (ArtifactMetadata artifact : artifacts) {
long whenGathered = artifact.getWhenGathered().getTime();
String id = artifact.getNamespace() + "/" + artifact.getProject() + "/" + artifact.getId();
if (tmp != whenGathered) {
if (entry != null) {
entry.setDescription(description);
entries.add(entry);
entry = null;
}
String repoId1 = artifact.getRepositoryId();
entry = new RssFeedEntry(this.getTitle() + "\'" + repoId1 + "\'" + " as of " + new Date(whenGathered));
entry.setPublishedDate(artifact.getWhenGathered());
description = this.getDescription() + "\'" + repoId1 + "\'" + ": \n" + id + " | ";
} else {
description = description + id + " | ";
}
if (idx == (artifacts.size() - 1)) {
entry.setDescription(description);
entries.add(entry);
}
tmp = whenGathered;
idx++;
}
return generator.generateFeed(getTitle() + "\'" + repoId + "\'", "New artifacts found in repository " + "\'" + repoId + "\'" + " during repository scan.", entries);
}
Aggregations