Search in sources :

Example 11 with SearchFields

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

the class MavenRepositorySearchTest method testAdvancedSearchClassNameInWar.

@Test
public void testAdvancedSearchClassNameInWar() throws Exception {
    createIndexContainingMoreArtifacts(true);
    List<String> selectedRepos = Arrays.asList(TEST_REPO_1);
    SearchFields searchFields = new SearchFields();
    searchFields.setClassName("SomeClass");
    searchFields.setRepositories(selectedRepos);
    EasyMock.expect(archivaConfig.getDefaultLocale()).andReturn(Locale.getDefault()).anyTimes();
    EasyMock.expect(archivaConfig.getConfiguration()).andReturn(config).anyTimes();
    archivaConfigControl.replay();
    SearchResults results = search.search("user", searchFields, null);
    archivaConfigControl.verify();
    assertNotNull(results);
    assertEquals(1, results.getHits().size());
    assertEquals("test-webapp", results.getHits().get(0).getArtifactId());
}
Also used : SearchFields(org.apache.archiva.indexer.search.SearchFields) SearchResults(org.apache.archiva.indexer.search.SearchResults) Test(org.junit.Test)

Example 12 with SearchFields

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

the class MavenRepositorySearchTest method testAdvancedSearch.

// tests for advanced search
@Test
public void testAdvancedSearch() throws Exception {
    List<Path> files = new ArrayList<>();
    files.add(Paths.get(org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos/" + TEST_REPO_2 + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar"));
    files.add(Paths.get(org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/repos/" + TEST_REPO_2 + "/org/apache/archiva/archiva-search/1.1/archiva-search-1.1.jar"));
    createIndex(TEST_REPO_2, files, false);
    List<String> selectedRepos = new ArrayList<>();
    selectedRepos.add(TEST_REPO_2);
    SearchFields searchFields = new SearchFields();
    searchFields.setGroupId("org.apache.archiva");
    searchFields.setVersion("1.0");
    searchFields.setRepositories(selectedRepos);
    EasyMock.expect(archivaConfig.getDefaultLocale()).andReturn(Locale.getDefault()).anyTimes();
    EasyMock.expect(archivaConfig.getConfiguration()).andReturn(config).anyTimes();
    archivaConfigControl.replay();
    SearchResults results = search.search("user", searchFields, null);
    archivaConfigControl.verify();
    assertNotNull(results);
    assertEquals(1, results.getTotalHits());
    SearchResultHit hit = results.getHits().get(0);
    assertEquals("org.apache.archiva", hit.getGroupId());
    assertEquals("archiva-search", hit.getArtifactId());
    assertEquals("1.0", hit.getVersions().get(0));
}
Also used : Path(java.nio.file.Path) SearchFields(org.apache.archiva.indexer.search.SearchFields) SearchResultHit(org.apache.archiva.indexer.search.SearchResultHit) ArrayList(java.util.ArrayList) SearchResults(org.apache.archiva.indexer.search.SearchResults) Test(org.junit.Test)

Example 13 with SearchFields

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

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

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

SearchFields (org.apache.archiva.indexer.search.SearchFields)19 SearchResults (org.apache.archiva.indexer.search.SearchResults)16 Test (org.junit.Test)16 ArrayList (java.util.ArrayList)11 RepositorySearchException (org.apache.archiva.indexer.search.RepositorySearchException)6 SearchResultHit (org.apache.archiva.indexer.search.SearchResultHit)5 Path (java.nio.file.Path)3 SearchResultLimits (org.apache.archiva.indexer.search.SearchResultLimits)3 ArchivaRestServiceException (org.apache.archiva.rest.api.services.ArchivaRestServiceException)3 URI (java.net.URI)1 TreeMap (java.util.TreeMap)1 Artifact (org.apache.archiva.maven2.model.Artifact)1 MetadataRepositoryException (org.apache.archiva.metadata.repository.MetadataRepositoryException)1