Search in sources :

Example 11 with MetadataResolutionException

use of org.apache.archiva.metadata.repository.MetadataResolutionException 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)

Example 12 with MetadataResolutionException

use of org.apache.archiva.metadata.repository.MetadataResolutionException in project archiva by apache.

the class DefaultBrowseService method browseGroupId.

@Override
public BrowseResult browseGroupId(String groupId, String repositoryId) throws ArchivaRestServiceException {
    List<String> selectedRepos = getSelectedRepos(repositoryId);
    Set<String> projects = new LinkedHashSet<>();
    RepositorySession repositorySession = repositorySessionFactory.createSession();
    Set<String> namespaces;
    try {
        MetadataResolver metadataResolver = repositorySession.getResolver();
        Set<String> namespacesToCollapse = new LinkedHashSet<>();
        for (String repoId : selectedRepos) {
            namespacesToCollapse.addAll(metadataResolver.resolveNamespaces(repositorySession, repoId, groupId));
            projects.addAll(metadataResolver.resolveProjects(repositorySession, repoId, groupId));
        }
        // TODO: this logic should be optional, particularly remembering we want to keep this code simple
        // it is located here to avoid the content repository implementation needing to do too much for what
        // is essentially presentation code
        namespaces = new LinkedHashSet<>();
        for (String n : namespacesToCollapse) {
            // TODO: check performance of this
            namespaces.add(collapseNamespaces(repositorySession, metadataResolver, selectedRepos, groupId + "." + n));
        }
    } catch (MetadataResolutionException e) {
        throw new ArchivaRestServiceException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
    } finally {
        repositorySession.close();
    }
    List<BrowseResultEntry> browseGroupResultEntries = new ArrayList<>(namespaces.size() + projects.size());
    for (String namespace : namespaces) {
        browseGroupResultEntries.add(new BrowseResultEntry(namespace, false).groupId(namespace));
    }
    for (String project : projects) {
        browseGroupResultEntries.add(new BrowseResultEntry(groupId + '.' + project, true).groupId(groupId).artifactId(project));
    }
    Collections.sort(browseGroupResultEntries);
    return new BrowseResult(browseGroupResultEntries);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) BrowseResultEntry(org.apache.archiva.rest.api.model.BrowseResultEntry) ArrayList(java.util.ArrayList) MetadataResolver(org.apache.archiva.metadata.repository.MetadataResolver) RepositorySession(org.apache.archiva.metadata.repository.RepositorySession) MetadataResolutionException(org.apache.archiva.metadata.repository.MetadataResolutionException) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) BrowseResult(org.apache.archiva.rest.api.model.BrowseResult)

Example 13 with MetadataResolutionException

use of org.apache.archiva.metadata.repository.MetadataResolutionException in project archiva by apache.

the class DefaultBrowseService method getRootGroups.

@Override
public BrowseResult getRootGroups(String repositoryId) throws ArchivaRestServiceException {
    List<String> selectedRepos = getSelectedRepos(repositoryId);
    Set<String> namespaces = new LinkedHashSet<String>();
    // TODO: this logic should be optional, particularly remembering we want to keep this code simple
    // it is located here to avoid the content repository implementation needing to do too much for what
    // is essentially presentation code
    Set<String> namespacesToCollapse = new LinkedHashSet<String>();
    RepositorySession repositorySession = repositorySessionFactory.createSession();
    try {
        MetadataResolver metadataResolver = repositorySession.getResolver();
        for (String repoId : selectedRepos) {
            namespacesToCollapse.addAll(metadataResolver.resolveRootNamespaces(repositorySession, repoId));
        }
        for (String n : namespacesToCollapse) {
            // TODO: check performance of this
            namespaces.add(collapseNamespaces(repositorySession, metadataResolver, selectedRepos, n));
        }
    } catch (MetadataResolutionException e) {
        throw new ArchivaRestServiceException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
    } finally {
        repositorySession.close();
    }
    List<BrowseResultEntry> browseGroupResultEntries = new ArrayList<>(namespaces.size());
    for (String namespace : namespaces) {
        browseGroupResultEntries.add(new BrowseResultEntry(namespace, false));
    }
    Collections.sort(browseGroupResultEntries);
    return new BrowseResult(browseGroupResultEntries);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) BrowseResultEntry(org.apache.archiva.rest.api.model.BrowseResultEntry) ArrayList(java.util.ArrayList) MetadataResolver(org.apache.archiva.metadata.repository.MetadataResolver) BrowseResult(org.apache.archiva.rest.api.model.BrowseResult) RepositorySession(org.apache.archiva.metadata.repository.RepositorySession) MetadataResolutionException(org.apache.archiva.metadata.repository.MetadataResolutionException)

Example 14 with MetadataResolutionException

use of org.apache.archiva.metadata.repository.MetadataResolutionException in project archiva by apache.

the class NewVersionsOfArtifactRssFeedProcessor method processNewVersionsOfArtifact.

private SyndFeed processNewVersionsOfArtifact(String groupId, String artifactId, MetadataRepository metadataRepository) throws FeedException {
    List<ArtifactMetadata> artifacts = new ArrayList<>();
    try {
        for (String repoId : metadataRepository.getRepositories()) {
            Collection<String> versions = metadataRepository.getProjectVersions(repoId, groupId, artifactId);
            for (String version : versions) {
                artifacts.addAll(metadataRepository.getArtifacts(repoId, groupId, artifactId, version));
            }
        }
    } catch (MetadataRepositoryException e) {
        throw new FeedException("Unable to construct feed, metadata could not be retrieved: " + e.getMessage(), e);
    } catch (MetadataResolutionException 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();
        if (tmp != whenGathered) {
            if (entry != null) {
                entry.setDescription(description);
                entries.add(entry);
                entry = null;
            }
            entry = new RssFeedEntry(this.getTitle() + "\'" + groupId + ":" + artifactId + "\'" + " as of " + new Date(whenGathered));
            entry.setPublishedDate(artifact.getWhenGathered());
            description = this.getDescription() + "\'" + groupId + ":" + artifactId + "\'" + ": \n" + artifact.getId() + " | ";
        } else {
            description = description + artifact.getId() + " | ";
        }
        if (idx == (artifacts.size() - 1)) {
            entry.setDescription(description);
            entries.add(entry);
        }
        tmp = whenGathered;
        idx++;
    }
    String key = groupId + ":" + artifactId;
    return generator.generateFeed(getTitle() + "\'" + key + "\'", "New versions of artifact " + "\'" + key + "\' found during repository scan.", entries);
}
Also used : MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) FeedException(com.sun.syndication.io.FeedException) ArrayList(java.util.ArrayList) RssFeedEntry(org.apache.archiva.rss.RssFeedEntry) MetadataResolutionException(org.apache.archiva.metadata.repository.MetadataResolutionException) Date(java.util.Date) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata)

Example 15 with MetadataResolutionException

use of org.apache.archiva.metadata.repository.MetadataResolutionException 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)

Aggregations

MetadataResolutionException (org.apache.archiva.metadata.repository.MetadataResolutionException)26 ArrayList (java.util.ArrayList)14 MetadataRepositoryException (org.apache.archiva.metadata.repository.MetadataRepositoryException)13 Path (java.nio.file.Path)10 ArtifactMetadata (org.apache.archiva.metadata.model.ArtifactMetadata)10 IOException (java.io.IOException)9 HashMap (java.util.HashMap)8 Properties (java.util.Properties)8 RepositorySession (org.apache.archiva.metadata.repository.RepositorySession)8 LinkedHashSet (java.util.LinkedHashSet)7 MetadataRepository (org.apache.archiva.metadata.repository.MetadataRepository)7 ArchivaRestServiceException (org.apache.archiva.rest.api.services.ArchivaRestServiceException)7 Date (java.util.Date)6 MetadataFacet (org.apache.archiva.metadata.model.MetadataFacet)6 MetadataFacetFactory (org.apache.archiva.metadata.model.MetadataFacetFactory)6 ProjectMetadata (org.apache.archiva.metadata.model.ProjectMetadata)6 ProjectVersionMetadata (org.apache.archiva.metadata.model.ProjectVersionMetadata)6 ProjectVersionReference (org.apache.archiva.metadata.model.ProjectVersionReference)6 MetadataResolver (org.apache.archiva.metadata.repository.MetadataResolver)6 Map (java.util.Map)5