Search in sources :

Example 31 with BaseRepositoryContentLayout

use of org.apache.archiva.repository.content.BaseRepositoryContentLayout in project archiva by apache.

the class DefaultRepositoriesService method deleteArtifact.

@Override
public ActionStatus 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 (!getPermissionStatus(repositoryId).isAuthorizedToDeleteArtifacts()) {
        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());
    String baseVersion = VersionUtil.getBaseVersion(artifact.getVersion());
    RepositorySession repositorySession = null;
    try {
        repositorySession = repositorySessionFactory.createSession();
    } catch (MetadataRepositoryException e) {
        e.printStackTrace();
    }
    try {
        Date lastUpdatedTimestamp = Calendar.getInstance().getTime();
        TimeZone timezone = TimeZone.getTimeZone("UTC");
        DateFormat fmt = new SimpleDateFormat("yyyyMMdd.HHmmss");
        fmt.setTimeZone(timezone);
        ManagedRepository repo = repositoryRegistry.getManagedRepository(repositoryId);
        ManagedRepositoryContent repository = getManagedRepositoryContent(repositoryId);
        BaseRepositoryContentLayout layout = repository.getLayout(BaseRepositoryContentLayout.class);
        ArchivaItemSelector versionSelector = ArchivaItemSelector.builder().withNamespace(artifact.getGroupId()).withProjectId(artifact.getArtifactId()).withVersion(baseVersion).build();
        Version version1 = layout.getVersion(versionSelector);
        String path = repository.toPath(version1);
        ArchivaItemSelector selector = ArchivaItemSelector.builder().withNamespace(artifact.getGroupId()).withProjectId(artifact.getArtifactId()).withVersion(baseVersion).withClassifier(artifact.getClassifier()).withArtifactId(artifact.getArtifactId()).withType(artifact.getType()).includeRelatedArtifacts().build();
        MetadataRepository metadataRepository = repositorySession.getRepository();
        if (StringUtils.isNotBlank(artifact.getClassifier())) {
            if (StringUtils.isBlank(artifact.getPackaging())) {
                throw new ArchivaRestServiceException("You must configure a type/packaging when using classifier", 400, null);
            }
            List<? extends org.apache.archiva.repository.content.Artifact> artifactItems = layout.getArtifacts(selector);
            for (org.apache.archiva.repository.content.Artifact aRef : artifactItems) {
                try {
                    repository.deleteItem(aRef);
                } catch (ItemNotFoundException e) {
                    log.error("Could not delete item, seems to be deleted by other thread. {}, {} ", aRef, e.getMessage());
                }
            }
        } else {
            int index = path.lastIndexOf('/');
            path = path.substring(0, index);
            StorageAsset targetPath = repo.getAsset(path);
            if (!targetPath.exists()) {
                // throw new ContentNotFoundException(
                // artifact.getNamespace() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion() );
                log.warn("targetPath {} not found skip file deletion", targetPath);
                return ActionStatus.FAIL;
            }
            // delete from file system
            if (!snapshotVersion && version1.exists()) {
                try {
                    repository.deleteItem(version1);
                } catch (ItemNotFoundException e) {
                    log.error("Could not delete version item {}", e.getMessage());
                }
            } else {
                // We are deleting all version related artifacts for a snapshot version
                for (org.apache.archiva.repository.content.Artifact delArtifact : layout.getArtifacts(selector)) {
                    try {
                        repository.deleteItem(delArtifact);
                    } catch (ItemNotFoundException e) {
                        log.warn("Artifact that should be deleted, was not found: {}", delArtifact);
                    }
                }
                StorageAsset metadataFile = getMetadata(repo, targetPath.getPath());
                ArchivaRepositoryMetadata metadata = getMetadata(repository.getRepository().getType(), metadataFile);
                updateMetadata(metadata, metadataFile, lastUpdatedTimestamp, artifact);
            }
        }
        Collection<ArtifactMetadata> artifacts = Collections.emptyList();
        if (snapshotVersion) {
            artifacts = metadataRepository.getArtifacts(repositorySession, repositoryId, artifact.getGroupId(), artifact.getArtifactId(), baseVersion);
        } else {
            artifacts = metadataRepository.getArtifacts(repositorySession, 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(repositorySession, 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(repositorySession, 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.removeFacetFromArtifact(repositorySession, repositoryId, groupId, artifactId, version, mavenArtifactFacetToCompare);
                        repositorySession.save();
                    }
                } else {
                    if (snapshotVersion) {
                        metadataRepository.removeTimestampedArtifact(repositorySession, artifactMetadata, VersionUtil.getBaseVersion(artifact.getVersion()));
                    } else {
                        metadataRepository.removeArtifact(repositorySession, 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 | MetadataSessionException | MetadataRepositoryException | LayoutException e) {
        throw new ArchivaRestServiceException("Repository exception: " + e.getMessage(), 500, e);
    } finally {
        try {
            repositorySession.save();
        } catch (MetadataSessionException e) {
            log.error("Could not save sesion {}", e.getMessage());
        }
        repositorySession.close();
    }
    return ActionStatus.SUCCESS;
}
Also used : ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) ManagedRepository(org.apache.archiva.repository.ManagedRepository) MavenArtifactFacet(org.apache.archiva.maven.metadata.model.MavenArtifactFacet) RepositorySession(org.apache.archiva.metadata.repository.RepositorySession) MetadataSessionException(org.apache.archiva.metadata.repository.MetadataSessionException) MetadataRepository(org.apache.archiva.metadata.repository.MetadataRepository) Version(org.apache.archiva.repository.content.Version) LayoutException(org.apache.archiva.repository.content.LayoutException) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) RepositoryListener(org.apache.archiva.metadata.audit.RepositoryListener) ContentNotFoundException(org.apache.archiva.repository.content.ContentNotFoundException) RepositoryException(org.apache.archiva.repository.RepositoryException) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) RepositoryNotFoundException(org.apache.archiva.repository.RepositoryNotFoundException) Date(java.util.Date) MetadataResolutionException(org.apache.archiva.metadata.repository.MetadataResolutionException) TimeZone(java.util.TimeZone) BaseRepositoryContentLayout(org.apache.archiva.repository.content.BaseRepositoryContentLayout) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata) SimpleDateFormat(java.text.SimpleDateFormat) ItemNotFoundException(org.apache.archiva.repository.content.ItemNotFoundException)

Example 32 with BaseRepositoryContentLayout

use of org.apache.archiva.repository.content.BaseRepositoryContentLayout in project archiva by apache.

the class ArtifactBuilder method build.

public Artifact build() {
    String type = null, classifier = null;
    MavenArtifactFacet facet = (MavenArtifactFacet) artifactMetadata.getFacet(MavenArtifactFacet.FACET_ID);
    if (facet != null) {
        type = facet.getType();
        classifier = facet.getClassifier();
    }
    ArchivaItemSelector.Builder selectorBuilder = ArchivaItemSelector.builder().withNamespace(artifactMetadata.getNamespace()).withProjectId(artifactMetadata.getProject()).withVersion(artifactMetadata.getProjectVersion()).withArtifactId(artifactMetadata.getProject()).withArtifactVersion(artifactMetadata.getVersion());
    if (StringUtils.isNotEmpty(type)) {
        selectorBuilder.withType(type);
    }
    if (StringUtils.isNotEmpty(classifier)) {
        selectorBuilder.withClassifier(classifier);
    }
    BaseRepositoryContentLayout layout;
    try {
        layout = managedRepositoryContent.getLayout(BaseRepositoryContentLayout.class);
    } catch (LayoutException e) {
        throw new RuntimeException("Could not convert to layout " + e.getMessage());
    }
    org.apache.archiva.repository.content.Artifact repoArtifact = layout.getArtifact(selectorBuilder.build());
    String extension = repoArtifact.getExtension();
    Artifact artifact = new Artifact(repoArtifact.getVersion().getProject().getNamespace().getId(), repoArtifact.getId(), repoArtifact.getArtifactVersion());
    artifact.setRepositoryId(artifactMetadata.getRepositoryId());
    artifact.setClassifier(classifier);
    artifact.setPackaging(type);
    artifact.setType(type);
    artifact.setFileExtension(extension);
    artifact.setPath(managedRepositoryContent.toPath(repoArtifact));
    // TODO: find a reusable formatter for this
    double s = this.artifactMetadata.getSize();
    String symbol = "b";
    if (s > 1024) {
        symbol = "K";
        s /= 1024;
        if (s > 1024) {
            symbol = "M";
            s /= 1024;
            if (s > 1024) {
                symbol = "G";
                s /= 1024;
            }
        }
    }
    artifact.setContext(managedRepositoryContent.getId());
    DecimalFormat df = new DecimalFormat("#,###.##", new DecimalFormatSymbols(Locale.US));
    artifact.setSize(df.format(s) + " " + symbol);
    artifact.setId(repoArtifact.getId() + "-" + repoArtifact.getArtifactVersion() + "." + repoArtifact.getType());
    return artifact;
}
Also used : ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat) MavenArtifactFacet(org.apache.archiva.maven.metadata.model.MavenArtifactFacet) Artifact(org.apache.archiva.maven.model.Artifact) LayoutException(org.apache.archiva.repository.content.LayoutException) BaseRepositoryContentLayout(org.apache.archiva.repository.content.BaseRepositoryContentLayout)

Example 33 with BaseRepositoryContentLayout

use of org.apache.archiva.repository.content.BaseRepositoryContentLayout in project archiva by apache.

the class RetentionCountRepositoryPurge method process.

@Override
public void process(String path) throws RepositoryPurgeException {
    try {
        ContentItem item = repository.toItem(path);
        BaseRepositoryContentLayout layout = repository.getLayout(BaseRepositoryContentLayout.class);
        Artifact artifact = layout.adaptItem(Artifact.class, item);
        if (!artifact.exists()) {
            return;
        }
        if (VersionUtil.isSnapshot(artifact.getVersion().getId())) {
            ArchivaItemSelector selector = ArchivaItemSelector.builder().withNamespace(artifact.getVersion().getProject().getNamespace().getId()).withProjectId(artifact.getVersion().getProject().getId()).withArtifactId(artifact.getId()).withVersion(artifact.getVersion().getId()).withClassifier("*").includeRelatedArtifacts().build();
            List<String> versions;
            try (Stream<? extends Artifact> stream = repository.getLayout(BaseRepositoryContentLayout.class).newArtifactStream(selector)) {
                versions = stream.map(a -> a.getArtifactVersion()).filter(StringUtils::isNotEmpty).distinct().collect(Collectors.toList());
            }
            Collections.sort(versions, VersionComparator.getInstance());
            if (retentionCount > versions.size()) {
                log.trace("No deletion, because retention count is higher than actual number of artifacts.");
                // Done. nothing to do here. skip it.
                return;
            }
            ArchivaItemSelector.Builder selectorBuilder = ArchivaItemSelector.builder().withNamespace(artifact.getVersion().getProject().getNamespace().getId()).withProjectId(artifact.getVersion().getProject().getId()).withArtifactId(artifact.getId()).withClassifier("*").includeRelatedArtifacts().withVersion(artifact.getVersion().getId());
            int countToPurge = versions.size() - retentionCount;
            Set<Artifact> artifactsToDelete = new HashSet<>();
            for (String version : versions) {
                if (countToPurge-- <= 0) {
                    break;
                }
                List<? extends Artifact> delArtifacts = repository.getLayout(BaseRepositoryContentLayout.class).getArtifacts(selectorBuilder.withArtifactVersion(version).build());
                if (delArtifacts != null && delArtifacts.size() > 0) {
                    artifactsToDelete.addAll(delArtifacts);
                }
            }
            purge(artifactsToDelete);
        }
    } catch (LayoutException le) {
        throw new RepositoryPurgeException(le.getMessage(), le);
    } catch (ContentAccessException e) {
        log.error("Error while accessing the repository data: {}", e.getMessage(), e);
        throw new RepositoryPurgeException(e.getMessage(), e);
    }
}
Also used : ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) Artifact(org.apache.archiva.repository.content.Artifact) LayoutException(org.apache.archiva.repository.content.LayoutException) BaseRepositoryContentLayout(org.apache.archiva.repository.content.BaseRepositoryContentLayout) ContentItem(org.apache.archiva.repository.content.ContentItem) HashSet(java.util.HashSet) ContentAccessException(org.apache.archiva.repository.content.ContentAccessException)

Example 34 with BaseRepositoryContentLayout

use of org.apache.archiva.repository.content.BaseRepositoryContentLayout in project archiva by apache.

the class SimpleArtifactConsumer method processFile.

public void processFile(String path, boolean executeOnEntireRepo) throws ConsumerException {
    log.info("Processing entry [{}] from repository [{}]", path, this.repository.getId());
    try {
        ManagedRepositoryContent repositoryContent = repository.getContent();
        BaseRepositoryContentLayout layout = repositoryContent.getLayout(BaseRepositoryContentLayout.class);
        Artifact artifact = layout.getArtifact(path);
        repositorySession.getRepository().getArtifacts(repositorySession, repository.getId(), artifact.getNamespace().getId(), artifact.getId(), artifact.getVersion().getId());
    } catch (LayoutException | MetadataResolutionException e) {
        throw new ConsumerException(e.getLocalizedMessage(), e);
    }
}
Also used : LayoutException(org.apache.archiva.repository.content.LayoutException) BaseRepositoryContentLayout(org.apache.archiva.repository.content.BaseRepositoryContentLayout) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) ConsumerException(org.apache.archiva.consumers.ConsumerException) Artifact(org.apache.archiva.repository.content.Artifact) MetadataResolutionException(org.apache.archiva.metadata.repository.MetadataResolutionException)

Example 35 with BaseRepositoryContentLayout

use of org.apache.archiva.repository.content.BaseRepositoryContentLayout in project archiva by apache.

the class ManagedDefaultRepositoryContentTest method testAddArtifact.

@Test
public void testAddArtifact() throws IOException, URISyntaxException, LayoutException {
    ManagedRepository repo = createManagedRepoWithContent("delete-repository");
    ManagedRepositoryContent myRepoContent = repo.getContent();
    BaseRepositoryContentLayout layout = myRepoContent.getLayout(BaseRepositoryContentLayout.class);
    Path repoRoot = repo.getRoot().getFilePath();
    Path tmpFile = Files.createTempFile("archiva-mvn-repotest", "jar");
    try (OutputStream outputStream = Files.newOutputStream(tmpFile)) {
        for (int i = 0; i < 255; i++) {
            outputStream.write("test.test.test\n".getBytes(Charset.forName("UTF-8")));
        }
    }
    Path file = repoRoot.resolve("org/apache/maven/samplejar/2.0/samplejar-2.0.jar");
    FileTime lmt = Files.getLastModifiedTime(file);
    ArchivaItemSelector selector = ArchivaItemSelector.builder().withNamespace("org.apache.maven").withProjectId("samplejar").withVersion("2.0").withArtifactId("samplejar").withArtifactVersion("2.0").withExtension("jar").build();
    Artifact artifact = layout.getArtifact(selector);
    layout.addArtifact(tmpFile, artifact);
    FileTime lmtAfter = Files.getLastModifiedTime(file);
    assertNotEquals(lmtAfter, lmt);
    Reader ln = Files.newBufferedReader(file, Charset.forName("UTF-8"));
    char[] content = new char[50];
    ln.read(content);
    assertTrue(new String(content).startsWith("test.test.test"));
    tmpFile = Files.createTempFile("archiva-mvn-repotest", "jar");
    try (OutputStream outputStream = Files.newOutputStream(tmpFile)) {
        for (int i = 0; i < 255; i++) {
            outputStream.write("test.test.test\n".getBytes(Charset.forName("UTF-8")));
        }
    }
    file = repoRoot.resolve("org/apache/maven/samplejar/2.0/samplejar-2.0.test");
    assertFalse(Files.exists(file));
    assertTrue(Files.exists(tmpFile));
    selector = ArchivaItemSelector.builder().withNamespace("org.apache.maven").withProjectId("samplejar").withVersion("2.0").withArtifactId("samplejar").withArtifactVersion("2.0").withExtension("test").build();
    artifact = layout.getArtifact(selector);
    layout.addArtifact(tmpFile, artifact);
    ln = Files.newBufferedReader(file, Charset.forName("UTF-8"));
    ln.read(content);
    assertTrue(new String(content).startsWith("test.test.test"));
}
Also used : Path(java.nio.file.Path) ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) MavenManagedRepository(org.apache.archiva.maven.repository.MavenManagedRepository) ManagedRepository(org.apache.archiva.repository.ManagedRepository) EditableManagedRepository(org.apache.archiva.repository.EditableManagedRepository) BaseRepositoryContentLayout(org.apache.archiva.repository.content.BaseRepositoryContentLayout) OutputStream(java.io.OutputStream) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) MavenMetadataReader(org.apache.archiva.maven.metadata.MavenMetadataReader) Reader(java.io.Reader) FileTime(java.nio.file.attribute.FileTime) Artifact(org.apache.archiva.repository.content.Artifact) Test(org.junit.Test)

Aggregations

BaseRepositoryContentLayout (org.apache.archiva.repository.content.BaseRepositoryContentLayout)62 StorageAsset (org.apache.archiva.repository.storage.StorageAsset)55 Artifact (org.apache.archiva.repository.content.Artifact)50 Path (java.nio.file.Path)49 Test (org.junit.Test)45 LayoutException (org.apache.archiva.repository.content.LayoutException)10 ArchivaItemSelector (org.apache.archiva.repository.content.base.ArchivaItemSelector)9 ContentItem (org.apache.archiva.repository.content.ContentItem)7 ManagedRepositoryContent (org.apache.archiva.repository.ManagedRepositoryContent)5 Project (org.apache.archiva.repository.content.Project)5 Version (org.apache.archiva.repository.content.Version)5 MetadataRepositoryException (org.apache.archiva.metadata.repository.MetadataRepositoryException)4 ContentAccessException (org.apache.archiva.repository.content.ContentAccessException)4 ItemSelector (org.apache.archiva.repository.content.ItemSelector)4 ResourceDoesNotExistException (org.apache.maven.wagon.ResourceDoesNotExistException)4 IOException (java.io.IOException)3 MetadataRepository (org.apache.archiva.metadata.repository.MetadataRepository)3 MetadataResolutionException (org.apache.archiva.metadata.repository.MetadataResolutionException)3 ManagedRepository (org.apache.archiva.repository.ManagedRepository)3 RepositoryException (org.apache.archiva.repository.RepositoryException)3