Search in sources :

Example 56 with ArtifactMetadata

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);
    }
}
Also used : ManagedRepository(org.apache.archiva.repository.ManagedRepository) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) ArrayList(java.util.ArrayList) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) RepositoryException(org.apache.archiva.repository.RepositoryException) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata) ArtifactBuilder(org.apache.archiva.rest.services.utils.ArtifactBuilder) Artifact(org.apache.archiva.maven2.model.Artifact)

Example 57 with ArtifactMetadata

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);
}
Also used : ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata)

Example 58 with ArtifactMetadata

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;
}
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 59 with ArtifactMetadata

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;
}
Also used : ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) ArrayList(java.util.ArrayList) MetadataResolver(org.apache.archiva.metadata.repository.MetadataResolver) RepositorySession(org.apache.archiva.metadata.repository.RepositorySession) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata) Artifact(org.apache.archiva.maven2.model.Artifact) ArchivaArtifact(org.apache.archiva.model.ArchivaArtifact) MetadataResolutionException(org.apache.archiva.metadata.repository.MetadataResolutionException)

Example 60 with ArtifactMetadata

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();
}
Also used : SyndFeed(com.sun.syndication.feed.synd.SyndFeed) HashMap(java.util.HashMap) SyndEntry(com.sun.syndication.feed.synd.SyndEntry) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata) Date(java.util.Date) Test(org.junit.Test)

Aggregations

ArtifactMetadata (org.apache.archiva.metadata.model.ArtifactMetadata)96 Test (org.junit.Test)53 ArrayList (java.util.ArrayList)23 Path (java.nio.file.Path)20 MetadataRepositoryException (org.apache.archiva.metadata.repository.MetadataRepositoryException)17 Date (java.util.Date)15 HashSet (java.util.HashSet)11 MetadataFacet (org.apache.archiva.metadata.model.MetadataFacet)10 MavenArtifactFacet (org.apache.archiva.metadata.model.maven2.MavenArtifactFacet)9 HashMap (java.util.HashMap)8 MetadataRepository (org.apache.archiva.metadata.repository.MetadataRepository)8 MetadataResolutionException (org.apache.archiva.metadata.repository.MetadataResolutionException)7 RepositorySession (org.apache.archiva.metadata.repository.RepositorySession)7 ReadMetadataRequest (org.apache.archiva.metadata.repository.storage.ReadMetadataRequest)7 ArtifactCleanupFeature (org.apache.archiva.repository.features.ArtifactCleanupFeature)7 ArchivaRestServiceException (org.apache.archiva.rest.api.services.ArchivaRestServiceException)7 Node (javax.jcr.Node)6 ProjectVersionMetadata (org.apache.archiva.metadata.model.ProjectVersionMetadata)6 IOException (java.io.IOException)5 RepositoryException (javax.jcr.RepositoryException)5