Search in sources :

Example 1 with MavenArtifactFacet

use of org.apache.archiva.metadata.model.maven2.MavenArtifactFacet in project archiva by apache.

the class Maven2RepositoryMetadataResolverTest method testGetArtifactMetadata.

@Test
public void testGetArtifactMetadata() throws Exception {
    Collection<ArtifactMetadata> springArtifacts = storage.readArtifactsMetadata(new ReadMetadataRequest(TEST_REPO_ID, "org.codehaus.plexus", "plexus-spring", "1.2", ALL));
    List<ArtifactMetadata> artifacts = new ArrayList<>(springArtifacts);
    Collections.sort(artifacts, new Comparator<ArtifactMetadata>() {

        @Override
        public int compare(ArtifactMetadata o1, ArtifactMetadata o2) {
            return o1.getId().compareTo(o2.getId());
        }
    });
    assertEquals(3, artifacts.size());
    ArtifactMetadata artifactMetadata = artifacts.get(0);
    assertEquals("plexus-spring-1.2-sources.jar", artifactMetadata.getId());
    MavenArtifactFacet facet = (MavenArtifactFacet) artifactMetadata.getFacet(MavenArtifactFacet.FACET_ID);
    assertEquals(0, facet.getBuildNumber());
    assertNull(facet.getTimestamp());
    assertEquals("sources", facet.getClassifier());
    assertEquals("java-source", facet.getType());
    artifactMetadata = artifacts.get(1);
    assertEquals("plexus-spring-1.2.jar", artifactMetadata.getId());
    facet = (MavenArtifactFacet) artifactMetadata.getFacet(MavenArtifactFacet.FACET_ID);
    assertEquals(0, facet.getBuildNumber());
    assertNull(facet.getTimestamp());
    assertNull(facet.getClassifier());
    assertEquals("jar", facet.getType());
    artifactMetadata = artifacts.get(2);
    assertEquals("plexus-spring-1.2.pom", artifactMetadata.getId());
    facet = (MavenArtifactFacet) artifactMetadata.getFacet(MavenArtifactFacet.FACET_ID);
    assertEquals(0, facet.getBuildNumber());
    assertNull(facet.getTimestamp());
    assertNull(facet.getClassifier());
    assertEquals("pom", facet.getType());
}
Also used : ReadMetadataRequest(org.apache.archiva.metadata.repository.storage.ReadMetadataRequest) ArrayList(java.util.ArrayList) MavenArtifactFacet(org.apache.archiva.metadata.model.maven2.MavenArtifactFacet) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata) Test(org.junit.Test)

Example 2 with MavenArtifactFacet

use of org.apache.archiva.metadata.model.maven2.MavenArtifactFacet in project archiva by apache.

the class Maven2RepositoryMetadataResolverTest method testGetArtifactMetadataSnapshotsMRM1859.

@Test
public void testGetArtifactMetadataSnapshotsMRM1859() throws Exception {
    Path repoDir = Paths.get("target/test-repository/com/example/test/test-artifact/1.0-SNAPSHOT");
    URL url = Thread.currentThread().getContextClassLoader().getResource("resolver-status.properties");
    Path resFile = Paths.get(url.toURI());
    Path destFile = repoDir.resolve(resFile.getFileName());
    Files.copy(resFile, destFile, StandardCopyOption.REPLACE_EXISTING);
    URL url2 = Thread.currentThread().getContextClassLoader().getResource("test01.properties");
    Path resFile2 = Paths.get(url2.toURI());
    Path destFile2 = repoDir.resolve(resFile2.getFileName());
    Files.copy(resFile2, destFile2, StandardCopyOption.REPLACE_EXISTING);
    try {
        Collection<ArtifactMetadata> testArtifacts = storage.readArtifactsMetadata(new ReadMetadataRequest(TEST_REPO_ID, "com.example.test", "test-artifact", "1.0-SNAPSHOT", ALL));
        List<ArtifactMetadata> artifacts = new ArrayList<>(testArtifacts);
        Collections.sort(artifacts, new Comparator<ArtifactMetadata>() {

            @Override
            public int compare(ArtifactMetadata o1, ArtifactMetadata o2) {
                return o1.getId().compareTo(o2.getId());
            }
        });
        assertEquals(6, artifacts.size());
        ArtifactMetadata artifactMetadata = artifacts.get(0);
        assertEquals("test-artifact-1.0-20100308.230825-1.jar", artifactMetadata.getId());
        MavenArtifactFacet facet = (MavenArtifactFacet) artifactMetadata.getFacet(MavenArtifactFacet.FACET_ID);
        assertEquals(1, facet.getBuildNumber());
        assertEquals("20100308.230825", facet.getTimestamp());
        assertNull(facet.getClassifier());
        assertEquals("jar", facet.getType());
        artifactMetadata = artifacts.get(1);
        assertEquals("test-artifact-1.0-20100308.230825-1.pom", artifactMetadata.getId());
        facet = (MavenArtifactFacet) artifactMetadata.getFacet(MavenArtifactFacet.FACET_ID);
        assertEquals(1, facet.getBuildNumber());
        assertEquals("20100308.230825", facet.getTimestamp());
        assertNull(facet.getClassifier());
        assertEquals("pom", facet.getType());
        artifactMetadata = artifacts.get(2);
        assertEquals("test-artifact-1.0-20100310.014828-2-javadoc.jar", artifactMetadata.getId());
        facet = (MavenArtifactFacet) artifactMetadata.getFacet(MavenArtifactFacet.FACET_ID);
        assertEquals(2, facet.getBuildNumber());
        assertEquals("20100310.014828", facet.getTimestamp());
        assertEquals("javadoc", facet.getClassifier());
        assertEquals("javadoc", facet.getType());
        artifactMetadata = artifacts.get(3);
        assertEquals("test-artifact-1.0-20100310.014828-2-sources.jar", artifactMetadata.getId());
        facet = (MavenArtifactFacet) artifactMetadata.getFacet(MavenArtifactFacet.FACET_ID);
        assertEquals(2, facet.getBuildNumber());
        assertEquals("20100310.014828", facet.getTimestamp());
        assertEquals("sources", facet.getClassifier());
        assertEquals("java-source", facet.getType());
        artifactMetadata = artifacts.get(4);
        assertEquals("test-artifact-1.0-20100310.014828-2.jar", artifactMetadata.getId());
        facet = (MavenArtifactFacet) artifactMetadata.getFacet(MavenArtifactFacet.FACET_ID);
        assertEquals(2, facet.getBuildNumber());
        assertEquals("20100310.014828", facet.getTimestamp());
        assertNull(facet.getClassifier());
        assertEquals("jar", facet.getType());
        artifactMetadata = artifacts.get(5);
        assertEquals("test-artifact-1.0-20100310.014828-2.pom", artifactMetadata.getId());
        facet = (MavenArtifactFacet) artifactMetadata.getFacet(MavenArtifactFacet.FACET_ID);
        assertEquals(2, facet.getBuildNumber());
        assertEquals("20100310.014828", facet.getTimestamp());
        assertNull(facet.getClassifier());
        assertEquals("pom", facet.getType());
    } finally {
        Files.delete(destFile);
        Files.delete(destFile2);
    }
}
Also used : Path(java.nio.file.Path) ReadMetadataRequest(org.apache.archiva.metadata.repository.storage.ReadMetadataRequest) ArrayList(java.util.ArrayList) MavenArtifactFacet(org.apache.archiva.metadata.model.maven2.MavenArtifactFacet) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata) URL(java.net.URL) Test(org.junit.Test)

Example 3 with MavenArtifactFacet

use of org.apache.archiva.metadata.model.maven2.MavenArtifactFacet in project archiva by apache.

the class RepositoryStatisticsManagerTest method createArtifact.

private ArtifactMetadata createArtifact(String namespace, String projectId, String projectVersion, String type) {
    ArtifactMetadata metadata = new ArtifactMetadata();
    metadata.setRepositoryId(TEST_REPO_ID);
    metadata.setId(projectId + "-" + projectVersion + "." + type);
    metadata.setProject(projectId);
    metadata.setSize(12345L);
    metadata.setProjectVersion(projectVersion);
    metadata.setVersion(projectVersion);
    metadata.setNamespace(namespace);
    MavenArtifactFacet facet = new MavenArtifactFacet();
    facet.setType(type);
    metadata.addFacet(facet);
    return metadata;
}
Also used : MavenArtifactFacet(org.apache.archiva.metadata.model.maven2.MavenArtifactFacet) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata)

Example 4 with MavenArtifactFacet

use of org.apache.archiva.metadata.model.maven2.MavenArtifactFacet in project archiva by apache.

the class DefaultPathParser method toArtifactReference.

/**
 * {@inheritDoc}
 *
 * @see org.apache.archiva.repository.content.PathParser#toArtifactReference(String)
 */
@Override
public ArtifactReference toArtifactReference(String path) throws LayoutException {
    if (StringUtils.isBlank(path)) {
        throw new LayoutException("Unable to convert blank path.");
    }
    ArtifactMetadata metadata;
    try {
        metadata = pathTranslator.getArtifactForPath(null, path);
    } catch (IllegalArgumentException e) {
        throw new LayoutException(e.getMessage(), e);
    }
    ArtifactReference artifact = new ArtifactReference();
    artifact.setGroupId(metadata.getNamespace());
    artifact.setArtifactId(metadata.getProject());
    artifact.setVersion(metadata.getVersion());
    MavenArtifactFacet facet = (MavenArtifactFacet) metadata.getFacet(MavenArtifactFacet.FACET_ID);
    if (facet != null) {
        artifact.setClassifier(facet.getClassifier());
        artifact.setType(facet.getType());
    }
    return artifact;
}
Also used : LayoutException(org.apache.archiva.repository.LayoutException) MavenArtifactFacet(org.apache.archiva.metadata.model.maven2.MavenArtifactFacet) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata) ArtifactReference(org.apache.archiva.model.ArtifactReference)

Example 5 with MavenArtifactFacet

use of org.apache.archiva.metadata.model.maven2.MavenArtifactFacet 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;
}
Also used : ManagedRepository(org.apache.archiva.admin.model.beans.ManagedRepository) MavenArtifactFacet(org.apache.archiva.metadata.model.maven2.MavenArtifactFacet) RepositorySession(org.apache.archiva.metadata.repository.RepositorySession) MetadataRepository(org.apache.archiva.metadata.repository.MetadataRepository) VersionedReference(org.apache.archiva.model.VersionedReference) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata) Path(java.nio.file.Path) RepositoryListener(org.apache.archiva.repository.events.RepositoryListener) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) ContentNotFoundException(org.apache.archiva.repository.ContentNotFoundException) RepositoryException(org.apache.archiva.repository.RepositoryException) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) RepositoryNotFoundException(org.apache.archiva.repository.RepositoryNotFoundException) RepositoryAdminException(org.apache.archiva.admin.model.RepositoryAdminException) Date(java.util.Date) MetadataResolutionException(org.apache.archiva.metadata.repository.MetadataResolutionException) TimeZone(java.util.TimeZone) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata) SimpleDateFormat(java.text.SimpleDateFormat) ArtifactReference(org.apache.archiva.model.ArtifactReference)

Aggregations

MavenArtifactFacet (org.apache.archiva.metadata.model.maven2.MavenArtifactFacet)11 ArtifactMetadata (org.apache.archiva.metadata.model.ArtifactMetadata)9 Path (java.nio.file.Path)3 ArrayList (java.util.ArrayList)3 ReadMetadataRequest (org.apache.archiva.metadata.repository.storage.ReadMetadataRequest)3 ArtifactReference (org.apache.archiva.model.ArtifactReference)3 Test (org.junit.Test)3 URL (java.net.URL)1 DateFormat (java.text.DateFormat)1 DecimalFormat (java.text.DecimalFormat)1 DecimalFormatSymbols (java.text.DecimalFormatSymbols)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 TimeZone (java.util.TimeZone)1 Matcher (java.util.regex.Matcher)1 RepositoryAdminException (org.apache.archiva.admin.model.RepositoryAdminException)1 ManagedRepository (org.apache.archiva.admin.model.beans.ManagedRepository)1 Artifact (org.apache.archiva.maven2.model.Artifact)1 MetadataRepository (org.apache.archiva.metadata.repository.MetadataRepository)1 MetadataRepositoryException (org.apache.archiva.metadata.repository.MetadataRepositoryException)1