use of org.apache.archiva.metadata.model.ArtifactMetadata in project archiva by apache.
the class AbstractRestService method buildArtifacts.
protected List<Artifact> buildArtifacts(Collection<ArtifactMetadata> artifactMetadatas, String repositoryId) throws ArchivaRestServiceException {
try {
if (artifactMetadatas != null && !artifactMetadatas.isEmpty()) {
List<Artifact> artifacts = new ArrayList<>(artifactMetadatas.size());
for (ArtifactMetadata artifact : artifactMetadatas) {
String repoId = repositoryId != null ? repositoryId : artifact.getRepositoryId();
if (repoId == null) {
throw new IllegalStateException("Repository Id is null");
}
ManagedRepository repo = repositoryRegistry.getManagedRepository(repoId);
if (repo == null) {
throw new RepositoryException("Repository not found " + repoId);
}
ManagedRepositoryContent content = repo.getContent();
ArtifactBuilder builder = new ArtifactBuilder().forArtifactMetadata(artifact).withManagedRepositoryContent(content);
Artifact art = builder.build();
art.setUrl(getArtifactUrl(art, repositoryId));
artifacts.add(art);
}
return artifacts;
}
return Collections.emptyList();
} catch (RepositoryException e) {
log.error(e.getMessage(), e);
throw new ArchivaRestServiceException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
}
}
use of org.apache.archiva.metadata.model.ArtifactMetadata in project archiva by apache.
the class TestMetadataRepository method addArtifact.
private void addArtifact(String projectId, String projectVersion, Date whenGathered) {
ArtifactMetadata artifact = new ArtifactMetadata();
artifact.setFileLastModified(System.currentTimeMillis());
artifact.setNamespace(TEST_NAMESPACE);
artifact.setProjectVersion(projectVersion);
artifact.setVersion(projectVersion);
artifact.setId(projectId + "-" + projectVersion + ".jar");
artifact.setProject(projectId);
artifact.setRepositoryId(TEST_REPO);
artifact.setWhenGathered(whenGathered);
artifacts.add(artifact);
versions.add(projectVersion);
}
use of org.apache.archiva.metadata.model.ArtifactMetadata 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.metadata.model.ArtifactMetadata in project archiva by apache.
the class DefaultBrowseService method getArtifactDownloadInfos.
@Override
public List<Artifact> getArtifactDownloadInfos(String groupId, String artifactId, String version, String repositoryId) throws ArchivaRestServiceException {
List<String> selectedRepos = getSelectedRepos(repositoryId);
List<Artifact> artifactDownloadInfos = new ArrayList<>();
try (RepositorySession session = repositorySessionFactory.createSession()) {
MetadataResolver metadataResolver = session.getResolver();
for (String repoId : selectedRepos) {
List<ArtifactMetadata> artifacts = new ArrayList<>(metadataResolver.resolveArtifacts(session, repoId, groupId, artifactId, version));
Collections.sort(artifacts, ArtifactMetadataVersionComparator.INSTANCE);
if (artifacts != null && !artifacts.isEmpty()) {
return buildArtifacts(artifacts, repoId);
}
}
} catch (MetadataResolutionException e) {
log.error(e.getMessage(), e);
throw new ArchivaRestServiceException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
}
return artifactDownloadInfos;
}
use of org.apache.archiva.metadata.model.ArtifactMetadata in project archiva by apache.
the class NewVersionsOfArtifactRssFeedProcessorTest method testProcess.
@SuppressWarnings("unchecked")
@Test
public void testProcess() throws Exception {
Date whenGathered = new Date(123456789);
ArtifactMetadata artifact1 = createArtifact(whenGathered, "1.0.1");
ArtifactMetadata artifact2 = createArtifact(whenGathered, "1.0.2");
Date whenGatheredNext = new Date(345678912);
ArtifactMetadata artifact3 = createArtifact(whenGatheredNext, "1.0.3-SNAPSHOT");
Map<String, String> reqParams = new HashMap<>();
reqParams.put(RssFeedProcessor.KEY_GROUP_ID, GROUP_ID);
reqParams.put(RssFeedProcessor.KEY_ARTIFACT_ID, ARTIFACT_ID);
expect(metadataRepository.getRepositories()).andReturn(Collections.singletonList(TEST_REPO));
expect(metadataRepository.getProjectVersions(TEST_REPO, GROUP_ID, ARTIFACT_ID)).andReturn(Arrays.asList("1.0.1", "1.0.2", "1.0.3-SNAPSHOT"));
expect(metadataRepository.getArtifacts(TEST_REPO, GROUP_ID, ARTIFACT_ID, "1.0.1")).andReturn(Collections.singletonList(artifact1));
expect(metadataRepository.getArtifacts(TEST_REPO, GROUP_ID, ARTIFACT_ID, "1.0.2")).andReturn(Collections.singletonList(artifact2));
expect(metadataRepository.getArtifacts(TEST_REPO, GROUP_ID, ARTIFACT_ID, "1.0.3-SNAPSHOT")).andReturn(Collections.singletonList(artifact3));
metadataRepositoryControl.replay();
SyndFeed feed = newVersionsProcessor.process(reqParams, metadataRepository);
assertEquals("New Versions of Artifact 'org.apache.archiva:artifact-two'", feed.getTitle());
assertEquals("New versions of artifact 'org.apache.archiva:artifact-two' found during repository scan.", feed.getDescription());
assertEquals("en-us", feed.getLanguage());
assertEquals(whenGatheredNext, feed.getPublishedDate());
List<SyndEntry> entries = feed.getEntries();
assertEquals(2, entries.size());
assertEquals("New Versions of Artifact 'org.apache.archiva:artifact-two' as of " + whenGathered, entries.get(0).getTitle());
assertEquals(whenGathered, entries.get(0).getPublishedDate());
assertEquals("New Versions of Artifact 'org.apache.archiva:artifact-two' as of " + whenGatheredNext, entries.get(1).getTitle());
assertEquals(whenGatheredNext, entries.get(1).getPublishedDate());
metadataRepositoryControl.verify();
}
Aggregations