Search in sources :

Example 6 with ArchivaRestServiceException

use of org.apache.archiva.rest.api.services.ArchivaRestServiceException in project archiva by apache.

the class DefaultSearchService method redirectToArtifactFile.

@Override
public Response redirectToArtifactFile(String repositoryId, String groupId, String artifactId, String version, String packaging, String classifier) throws ArchivaRestServiceException {
    try {
        if (StringUtils.isEmpty(groupId)) {
            return Response.status(new Response.StatusType() {

                @Override
                public int getStatusCode() {
                    return Response.Status.BAD_REQUEST.getStatusCode();
                }

                @Override
                public Response.Status.Family getFamily() {
                    return Response.Status.BAD_REQUEST.getFamily();
                }

                @Override
                public String getReasonPhrase() {
                    return "groupId mandatory";
                }
            }).build();
        }
        if (StringUtils.isEmpty(version)) {
            return Response.status(new Response.StatusType() {

                @Override
                public int getStatusCode() {
                    return Response.Status.BAD_REQUEST.getStatusCode();
                }

                @Override
                public Response.Status.Family getFamily() {
                    return Response.Status.BAD_REQUEST.getFamily();
                }

                @Override
                public String getReasonPhrase() {
                    return "version mandatory";
                }
            }).build();
        }
        if (StringUtils.isEmpty(artifactId)) {
            return Response.status(new Response.StatusType() {

                @Override
                public int getStatusCode() {
                    return Response.Status.BAD_REQUEST.getStatusCode();
                }

                @Override
                public Response.Status.Family getFamily() {
                    return Response.Status.BAD_REQUEST.getFamily();
                }

                @Override
                public String getReasonPhrase() {
                    return "artifactId mandatory";
                }
            }).build();
        }
        SearchFields searchField = new SearchFields();
        searchField.setGroupId(groupId);
        searchField.setArtifactId(artifactId);
        searchField.setPackaging(StringUtils.isBlank(packaging) ? "jar" : packaging);
        if (!StringUtils.equals(version, LATEST_KEYWORD)) {
            searchField.setVersion(version);
        }
        searchField.setClassifier(classifier);
        List<String> userRepos = getObservablesRepoIds().getStrings();
        searchField.setRepositories(StringUtils.isEmpty(repositoryId) ? userRepos : Arrays.asList(repositoryId));
        searchField.setExactSearch(true);
        SearchResults searchResults = repositorySearch.search(getPrincipal(), searchField, null);
        List<Artifact> artifacts = getArtifacts(searchResults);
        if (artifacts.isEmpty()) {
            return Response.status(new Response.StatusType() {

                @Override
                public int getStatusCode() {
                    return Response.Status.NO_CONTENT.getStatusCode();
                }

                @Override
                public Response.Status.Family getFamily() {
                    return Response.Status.NO_CONTENT.getFamily();
                }

                @Override
                public String getReasonPhrase() {
                    return "your query doesn't return any artifact";
                }
            }).build();
        }
        // so simple loop and retain only artifact with null classifier
        if (classifier == null) {
            List<Artifact> filteredArtifacts = new ArrayList<>(artifacts.size());
            for (Artifact artifact : artifacts) {
                if (artifact.getClassifier() == null) {
                    filteredArtifacts.add(artifact);
                }
            }
            artifacts = filteredArtifacts;
        }
        // TODO return json result of the query ?
        if (artifacts.size() > 1 && !StringUtils.equals(version, LATEST_KEYWORD)) {
            return Response.status(new Response.StatusType() {

                @Override
                public int getStatusCode() {
                    return Response.Status.BAD_REQUEST.getStatusCode();
                }

                @Override
                public Response.Status.Family getFamily() {
                    return Response.Status.BAD_REQUEST.getFamily();
                }

                @Override
                public String getReasonPhrase() {
                    return "your query return more than one artifact";
                }
            }).build();
        }
        // version is LATEST so we have to find the latest one from the result
        if (artifacts.size() > 1 && StringUtils.equals(version, LATEST_KEYWORD)) {
            TreeMap<String, Artifact> artifactPerVersion = new TreeMap<>(VersionComparator.getInstance());
            for (Artifact artifact : artifacts) {
                artifactPerVersion.put(artifact.getVersion(), artifact);
            }
            return Response.temporaryRedirect(new URI(artifactPerVersion.lastEntry().getValue().getUrl())).build();
        }
        Artifact artifact = artifacts.get(0);
        return Response.temporaryRedirect(new URI(artifact.getUrl())).build();
    } catch (Exception e) {
        throw new ArchivaRestServiceException(e.getMessage(), e);
    }
}
Also used : SearchFields(org.apache.archiva.indexer.search.SearchFields) ArrayList(java.util.ArrayList) SearchResults(org.apache.archiva.indexer.search.SearchResults) TreeMap(java.util.TreeMap) URI(java.net.URI) Artifact(org.apache.archiva.maven2.model.Artifact) RepositorySearchException(org.apache.archiva.indexer.search.RepositorySearchException) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException)

Example 7 with ArchivaRestServiceException

use of org.apache.archiva.rest.api.services.ArchivaRestServiceException in project archiva by apache.

the class DefaultSearchService method getAllGroupIds.

@Override
public GroupIdList getAllGroupIds(List<String> selectedRepos) throws ArchivaRestServiceException {
    List<String> observableRepos = getObservableRepos();
    List<String> repos = ListUtils.intersection(observableRepos, selectedRepos);
    if (repos == null || repos.isEmpty()) {
        return new GroupIdList(Collections.<String>emptyList());
    }
    try {
        return new GroupIdList(new ArrayList<>(repositorySearch.getAllGroupIds(getPrincipal(), repos)));
    } catch (RepositorySearchException e) {
        log.error(e.getMessage(), e);
        throw new ArchivaRestServiceException(e.getMessage(), e);
    }
}
Also used : GroupIdList(org.apache.archiva.rest.api.model.GroupIdList) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) RepositorySearchException(org.apache.archiva.indexer.search.RepositorySearchException)

Example 8 with ArchivaRestServiceException

use of org.apache.archiva.rest.api.services.ArchivaRestServiceException in project archiva by apache.

the class DefaultSearchService method getArtifactByChecksum.

public List<Artifact> getArtifactByChecksum(ChecksumSearch checksumSearch) throws ArchivaRestServiceException {
    // if no repos set we use ones available for the user
    if (checksumSearch.getRepositories() == null || checksumSearch.getRepositories().isEmpty()) {
        checksumSearch.setRepositories(getObservableRepos());
    }
    RepositorySession repositorySession = repositorySessionFactory.createSession();
    MetadataRepository metadataRepository = repositorySession.getRepository();
    Set<Artifact> artifactSet = new HashSet<>();
    try {
        for (String repoId : checksumSearch.getRepositories()) {
            Collection<ArtifactMetadata> artifactMetadatas = metadataRepository.getArtifactsByChecksum(repoId, checksumSearch.getChecksum());
            artifactSet.addAll(buildArtifacts(artifactMetadatas, repoId));
        }
        return new ArrayList<>(artifactSet);
    } catch (MetadataRepositoryException e) {
        log.error(e.getMessage(), e);
        throw new ArchivaRestServiceException(e.getMessage(), e);
    } finally {
        repositorySession.closeQuietly();
    }
}
Also used : MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) MetadataRepository(org.apache.archiva.metadata.repository.MetadataRepository) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) ArrayList(java.util.ArrayList) RepositorySession(org.apache.archiva.metadata.repository.RepositorySession) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata) Artifact(org.apache.archiva.maven2.model.Artifact) HashSet(java.util.HashSet)

Example 9 with ArchivaRestServiceException

use of org.apache.archiva.rest.api.services.ArchivaRestServiceException 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 10 with ArchivaRestServiceException

use of org.apache.archiva.rest.api.services.ArchivaRestServiceException in project archiva by apache.

the class DefaultBrowseService method addMetadata.

@Override
public Boolean addMetadata(String groupId, String artifactId, String version, String key, String value, String repositoryId) throws ArchivaRestServiceException {
    ProjectVersionMetadata projectVersionMetadata = getProjectMetadata(groupId, artifactId, version, repositoryId);
    if (projectVersionMetadata == null) {
        return Boolean.FALSE;
    }
    Map<String, String> properties = new HashMap<>();
    MetadataFacet metadataFacet = projectVersionMetadata.getFacet(GenericMetadataFacet.FACET_ID);
    if (metadataFacet != null && metadataFacet.toProperties() != null) {
        properties.putAll(metadataFacet.toProperties());
    } else {
        metadataFacet = new GenericMetadataFacet();
    }
    properties.put(key, value);
    metadataFacet.fromProperties(properties);
    projectVersionMetadata.addFacet(metadataFacet);
    RepositorySession repositorySession = repositorySessionFactory.createSession();
    try {
        MetadataRepository metadataRepository = repositorySession.getRepository();
        metadataRepository.updateProjectVersion(repositoryId, groupId, artifactId, projectVersionMetadata);
        repositorySession.save();
    } catch (MetadataRepositoryException e) {
        log.error(e.getMessage(), e);
        throw new ArchivaRestServiceException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
    } finally {
        repositorySession.close();
    }
    return Boolean.TRUE;
}
Also used : MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) MetadataRepository(org.apache.archiva.metadata.repository.MetadataRepository) HashMap(java.util.HashMap) ProjectVersionMetadata(org.apache.archiva.metadata.model.ProjectVersionMetadata) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) GenericMetadataFacet(org.apache.archiva.metadata.generic.GenericMetadataFacet) MetadataFacet(org.apache.archiva.metadata.model.MetadataFacet) GenericMetadataFacet(org.apache.archiva.metadata.generic.GenericMetadataFacet) RepositorySession(org.apache.archiva.metadata.repository.RepositorySession)

Aggregations

ArchivaRestServiceException (org.apache.archiva.rest.api.services.ArchivaRestServiceException)44 RepositoryAdminException (org.apache.archiva.admin.model.RepositoryAdminException)18 MetadataRepositoryException (org.apache.archiva.metadata.repository.MetadataRepositoryException)16 RepositorySession (org.apache.archiva.metadata.repository.RepositorySession)15 RepositoryException (org.apache.archiva.repository.RepositoryException)13 ManagedRepositoryContent (org.apache.archiva.repository.ManagedRepositoryContent)11 MetadataRepository (org.apache.archiva.metadata.repository.MetadataRepository)10 ArrayList (java.util.ArrayList)9 IOException (java.io.IOException)8 Path (java.nio.file.Path)8 ManagedRepository (org.apache.archiva.admin.model.beans.ManagedRepository)8 MetadataResolutionException (org.apache.archiva.metadata.repository.MetadataResolutionException)8 ArtifactMetadata (org.apache.archiva.metadata.model.ArtifactMetadata)7 Artifact (org.apache.archiva.maven2.model.Artifact)6 RepositorySearchException (org.apache.archiva.indexer.search.RepositorySearchException)5 MetadataResolver (org.apache.archiva.metadata.repository.MetadataResolver)5 ArtifactReference (org.apache.archiva.model.ArtifactReference)5 SearchResults (org.apache.archiva.indexer.search.SearchResults)4 ArchivaArtifact (org.apache.archiva.model.ArchivaArtifact)4 RepositoryNotFoundException (org.apache.archiva.repository.RepositoryNotFoundException)4