Search in sources :

Example 16 with SearchResults

use of org.apache.archiva.indexer.search.SearchResults in project archiva by apache.

the class MavenRepositorySearchPaginateTest method nonPaginatedResult.

@Test
public void nonPaginatedResult() throws Exception {
    MavenRepositorySearch search = new MavenRepositorySearch();
    SearchResults searchResults = build(10, new SearchResultLimits(0));
    searchResults = search.paginate(searchResults);
    assertEquals(10, searchResults.getReturnedHitsCount());
}
Also used : SearchResultLimits(org.apache.archiva.indexer.search.SearchResultLimits) SearchResults(org.apache.archiva.indexer.search.SearchResults) Test(org.junit.Test)

Example 17 with SearchResults

use of org.apache.archiva.indexer.search.SearchResults in project archiva by apache.

the class DefaultSearchService method searchArtifacts.

@Override
public List<Artifact> searchArtifacts(SearchRequest searchRequest) throws ArchivaRestServiceException {
    if (searchRequest == null) {
        return Collections.emptyList();
    }
    SearchFields searchField = getModelMapper().map(searchRequest, SearchFields.class);
    SearchResultLimits limits = new SearchResultLimits(0);
    limits.setPageSize(searchRequest.getPageSize());
    // if no repos set we use ones available for the user
    if (searchField.getRepositories() == null || searchField.getRepositories().isEmpty()) {
        searchField.setRepositories(getObservableRepos());
    }
    try {
        SearchResults searchResults = repositorySearch.search(getPrincipal(), searchField, limits);
        return getArtifacts(searchResults);
    } catch (RepositorySearchException e) {
        log.error(e.getMessage(), e);
        throw new ArchivaRestServiceException(e.getMessage(), e);
    }
}
Also used : SearchResultLimits(org.apache.archiva.indexer.search.SearchResultLimits) SearchFields(org.apache.archiva.indexer.search.SearchFields) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) RepositorySearchException(org.apache.archiva.indexer.search.RepositorySearchException) SearchResults(org.apache.archiva.indexer.search.SearchResults)

Example 18 with SearchResults

use of org.apache.archiva.indexer.search.SearchResults 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 19 with SearchResults

use of org.apache.archiva.indexer.search.SearchResults in project archiva by apache.

the class DefaultSearchService method quickSearchWithRepositories.

@Override
public List<Artifact> quickSearchWithRepositories(SearchRequest searchRequest) throws ArchivaRestServiceException {
    String queryString = searchRequest.getQueryTerms();
    if (StringUtils.isBlank(queryString)) {
        return Collections.emptyList();
    }
    List<String> repositories = searchRequest.getRepositories();
    if (repositories == null || repositories.isEmpty()) {
        repositories = getObservableRepos();
    }
    SearchResultLimits limits = new SearchResultLimits(searchRequest.getPageSize(), searchRequest.getSelectedPage());
    try {
        SearchResults searchResults = repositorySearch.search(getPrincipal(), repositories, queryString, limits, Collections.<String>emptyList());
        return getArtifacts(searchResults);
    } catch (RepositorySearchException e) {
        log.error(e.getMessage(), e);
        throw new ArchivaRestServiceException(e.getMessage(), e);
    }
}
Also used : SearchResultLimits(org.apache.archiva.indexer.search.SearchResultLimits) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) RepositorySearchException(org.apache.archiva.indexer.search.RepositorySearchException) SearchResults(org.apache.archiva.indexer.search.SearchResults)

Example 20 with SearchResults

use of org.apache.archiva.indexer.search.SearchResults in project archiva by apache.

the class DefaultSearchService method getArtifactVersions.

@Override
public List<Artifact> getArtifactVersions(String groupId, String artifactId, String packaging) throws ArchivaRestServiceException {
    if (StringUtils.isBlank(groupId) || StringUtils.isBlank(artifactId)) {
        return Collections.emptyList();
    }
    SearchFields searchField = new SearchFields();
    searchField.setGroupId(groupId);
    searchField.setArtifactId(artifactId);
    searchField.setPackaging(StringUtils.isBlank(packaging) ? "jar" : packaging);
    searchField.setRepositories(getObservableRepos());
    try {
        SearchResults searchResults = repositorySearch.search(getPrincipal(), searchField, null);
        return getArtifacts(searchResults);
    } catch (RepositorySearchException e) {
        log.error(e.getMessage(), e);
        throw new ArchivaRestServiceException(e.getMessage(), e);
    }
}
Also used : SearchFields(org.apache.archiva.indexer.search.SearchFields) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) RepositorySearchException(org.apache.archiva.indexer.search.RepositorySearchException) SearchResults(org.apache.archiva.indexer.search.SearchResults)

Aggregations

SearchResults (org.apache.archiva.indexer.search.SearchResults)35 Test (org.junit.Test)26 ArrayList (java.util.ArrayList)17 SearchFields (org.apache.archiva.indexer.search.SearchFields)16 SearchResultHit (org.apache.archiva.indexer.search.SearchResultHit)13 SearchResultLimits (org.apache.archiva.indexer.search.SearchResultLimits)9 RepositorySearchException (org.apache.archiva.indexer.search.RepositorySearchException)6 Path (java.nio.file.Path)4 ArchivaRestServiceException (org.apache.archiva.rest.api.services.ArchivaRestServiceException)4 IOException (java.io.IOException)1 URI (java.net.URI)1 TreeMap (java.util.TreeMap)1 RepositoryAdminException (org.apache.archiva.admin.model.RepositoryAdminException)1 Artifact (org.apache.archiva.maven2.model.Artifact)1 MetadataRepositoryException (org.apache.archiva.metadata.repository.MetadataRepositoryException)1 ArtifactInfo (org.apache.maven.index.ArtifactInfo)1 FlatSearchRequest (org.apache.maven.index.FlatSearchRequest)1 FlatSearchResponse (org.apache.maven.index.FlatSearchResponse)1 SourcedSearchExpression (org.apache.maven.index.expr.SourcedSearchExpression)1 UserInputSearchExpression (org.apache.maven.index.expr.UserInputSearchExpression)1