Search in sources :

Example 1 with RepositorySearchException

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

the class MavenRepositorySearch method search.

/**
 * @see RepositorySearch#search(String, SearchFields, SearchResultLimits)
 */
@SuppressWarnings("deprecation")
@Override
public SearchResults search(String principal, SearchFields searchFields, SearchResultLimits limits) throws RepositorySearchException {
    if (searchFields.getRepositories() == null) {
        throw new RepositorySearchException("Repositories cannot be null.");
    }
    List<String> indexingContextIds = addIndexingContexts(searchFields.getRepositories());
    // olamy: IMHO doesn't make sense
    if (!searchFields.getRepositories().isEmpty() && (indexingContextIds == null || indexingContextIds.isEmpty())) {
        return new SearchResults();
    }
    BooleanQuery.Builder qb = new BooleanQuery.Builder();
    if (StringUtils.isNotBlank(searchFields.getGroupId())) {
        qb.add(indexer.constructQuery(MAVEN.GROUP_ID, searchFields.isExactSearch() ? new SourcedSearchExpression(searchFields.getGroupId()) : new UserInputSearchExpression(searchFields.getGroupId())), BooleanClause.Occur.MUST);
    }
    if (StringUtils.isNotBlank(searchFields.getArtifactId())) {
        qb.add(indexer.constructQuery(MAVEN.ARTIFACT_ID, searchFields.isExactSearch() ? new SourcedSearchExpression(searchFields.getArtifactId()) : new UserInputSearchExpression(searchFields.getArtifactId())), BooleanClause.Occur.MUST);
    }
    if (StringUtils.isNotBlank(searchFields.getVersion())) {
        qb.add(indexer.constructQuery(MAVEN.VERSION, searchFields.isExactSearch() ? new SourcedSearchExpression(searchFields.getVersion()) : new SourcedSearchExpression(searchFields.getVersion())), BooleanClause.Occur.MUST);
    }
    if (StringUtils.isNotBlank(searchFields.getPackaging())) {
        qb.add(indexer.constructQuery(MAVEN.PACKAGING, searchFields.isExactSearch() ? new SourcedSearchExpression(searchFields.getPackaging()) : new UserInputSearchExpression(searchFields.getPackaging())), BooleanClause.Occur.MUST);
    }
    if (StringUtils.isNotBlank(searchFields.getClassName())) {
        qb.add(indexer.constructQuery(MAVEN.CLASSNAMES, new UserInputSearchExpression(searchFields.getClassName())), BooleanClause.Occur.MUST);
    }
    if (StringUtils.isNotBlank(searchFields.getBundleSymbolicName())) {
        qb.add(indexer.constructQuery(OSGI.SYMBOLIC_NAME, new UserInputSearchExpression(searchFields.getBundleSymbolicName())), BooleanClause.Occur.MUST);
    }
    if (StringUtils.isNotBlank(searchFields.getBundleVersion())) {
        qb.add(indexer.constructQuery(OSGI.VERSION, new UserInputSearchExpression(searchFields.getBundleVersion())), BooleanClause.Occur.MUST);
    }
    if (StringUtils.isNotBlank(searchFields.getBundleExportPackage())) {
        qb.add(indexer.constructQuery(OSGI.EXPORT_PACKAGE, new UserInputSearchExpression(searchFields.getBundleExportPackage())), Occur.MUST);
    }
    if (StringUtils.isNotBlank(searchFields.getBundleExportService())) {
        qb.add(indexer.constructQuery(OSGI.EXPORT_SERVICE, new UserInputSearchExpression(searchFields.getBundleExportService())), Occur.MUST);
    }
    if (StringUtils.isNotBlank(searchFields.getBundleImportPackage())) {
        qb.add(indexer.constructQuery(OSGI.IMPORT_PACKAGE, new UserInputSearchExpression(searchFields.getBundleImportPackage())), Occur.MUST);
    }
    if (StringUtils.isNotBlank(searchFields.getBundleName())) {
        qb.add(indexer.constructQuery(OSGI.NAME, new UserInputSearchExpression(searchFields.getBundleName())), Occur.MUST);
    }
    if (StringUtils.isNotBlank(searchFields.getBundleImportPackage())) {
        qb.add(indexer.constructQuery(OSGI.IMPORT_PACKAGE, new UserInputSearchExpression(searchFields.getBundleImportPackage())), Occur.MUST);
    }
    if (StringUtils.isNotBlank(searchFields.getBundleRequireBundle())) {
        qb.add(indexer.constructQuery(OSGI.REQUIRE_BUNDLE, new UserInputSearchExpression(searchFields.getBundleRequireBundle())), Occur.MUST);
    }
    if (StringUtils.isNotBlank(searchFields.getClassifier())) {
        qb.add(indexer.constructQuery(MAVEN.CLASSIFIER, searchFields.isExactSearch() ? new SourcedSearchExpression(searchFields.getClassifier()) : new UserInputSearchExpression(searchFields.getClassifier())), Occur.MUST);
    } else if (searchFields.isExactSearch()) {
    // TODO improvement in case of exact search and no classifier we must query for classifier with null value
    // currently it's done in DefaultSearchService with some filtering
    }
    BooleanQuery qu = qb.build();
    if (qu.clauses() == null || qu.clauses().size() <= 0) {
        throw new RepositorySearchException("No search fields set.");
    }
    if (qu.clauses() != null) {
        log.debug("CLAUSES ", qu.clauses());
        for (BooleanClause cl : qu.clauses()) {
            log.debug("Clause ", cl);
        }
    }
    return search(limits, qu, indexingContextIds, Collections.<ArtifactInfoFilter>emptyList(), searchFields.getRepositories(), searchFields.isIncludePomArtifacts());
}
Also used : BooleanClause(org.apache.maven.index_shaded.lucene.search.BooleanClause) BooleanQuery(org.apache.maven.index_shaded.lucene.search.BooleanQuery) UserInputSearchExpression(org.apache.maven.index.expr.UserInputSearchExpression) SourcedSearchExpression(org.apache.maven.index.expr.SourcedSearchExpression) RepositorySearchException(org.apache.archiva.indexer.search.RepositorySearchException) SearchResults(org.apache.archiva.indexer.search.SearchResults)

Example 2 with RepositorySearchException

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

the class MavenRepositorySearchTest method testAdvancedSearchSearchFieldsAreBlank.

@Test
public void testAdvancedSearchSearchFieldsAreBlank() throws Exception {
    List<String> selectedRepos = new ArrayList<>();
    selectedRepos.add(TEST_REPO_1);
    SearchFields searchFields = new SearchFields();
    searchFields.setGroupId("");
    searchFields.setArtifactId("");
    searchFields.setVersion("");
    searchFields.setPackaging("");
    searchFields.setClassName("");
    searchFields.setRepositories(selectedRepos);
    try {
        EasyMock.expect(archivaConfig.getDefaultLocale()).andReturn(Locale.getDefault()).anyTimes();
        EasyMock.expect(archivaConfig.getConfiguration()).andReturn(config).anyTimes();
        archivaConfigControl.replay();
        search.search("user", searchFields, null);
        archivaConfigControl.verify();
        fail("A RepositorySearchException should have been thrown.");
    } catch (RepositorySearchException e) {
        assertEquals("No search fields set.", e.getMessage());
    }
}
Also used : SearchFields(org.apache.archiva.indexer.search.SearchFields) ArrayList(java.util.ArrayList) RepositorySearchException(org.apache.archiva.indexer.search.RepositorySearchException) Test(org.junit.Test)

Example 3 with RepositorySearchException

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

the class MavenRepositorySearchTest method testAdvancedSearchNoRepositoriesConfigured.

@Test
public void testAdvancedSearchNoRepositoriesConfigured() throws Exception {
    SearchFields searchFields = new SearchFields();
    searchFields.setArtifactId("archiva");
    searchFields.setRepositories(null);
    try {
        search.search("user", searchFields, null);
        fail("A RepositorySearchExcecption should have been thrown.");
    } catch (RepositorySearchException e) {
        assertEquals("Repositories cannot be null.", e.getMessage());
    }
}
Also used : SearchFields(org.apache.archiva.indexer.search.SearchFields) RepositorySearchException(org.apache.archiva.indexer.search.RepositorySearchException) Test(org.junit.Test)

Example 4 with RepositorySearchException

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

the class MavenRepositorySearchTest method testAdvancedSearchSearchFieldsAreNull.

@Test
public void testAdvancedSearchSearchFieldsAreNull() throws Exception {
    List<String> selectedRepos = new ArrayList<>();
    selectedRepos.add(TEST_REPO_1);
    SearchFields searchFields = new SearchFields();
    searchFields.setRepositories(selectedRepos);
    try {
        EasyMock.expect(archivaConfig.getDefaultLocale()).andReturn(Locale.getDefault()).anyTimes();
        EasyMock.expect(archivaConfig.getConfiguration()).andReturn(config).anyTimes();
        archivaConfigControl.replay();
        search.search("user", searchFields, null);
        archivaConfigControl.verify();
        fail("A RepositorySearchExcecption should have been thrown.");
    } catch (RepositorySearchException e) {
        assertEquals("No search fields set.", e.getMessage());
    }
}
Also used : SearchFields(org.apache.archiva.indexer.search.SearchFields) ArrayList(java.util.ArrayList) RepositorySearchException(org.apache.archiva.indexer.search.RepositorySearchException) Test(org.junit.Test)

Example 5 with RepositorySearchException

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

Aggregations

RepositorySearchException (org.apache.archiva.indexer.search.RepositorySearchException)12 SearchFields (org.apache.archiva.indexer.search.SearchFields)5 SearchResults (org.apache.archiva.indexer.search.SearchResults)5 ArchivaRestServiceException (org.apache.archiva.rest.api.services.ArchivaRestServiceException)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 IndexingContext (org.apache.maven.index.context.IndexingContext)3 Test (org.junit.Test)3 IOException (java.io.IOException)2 RepositoryAdminException (org.apache.archiva.admin.model.RepositoryAdminException)2 UnsupportedBaseContextException (org.apache.archiva.indexer.UnsupportedBaseContextException)2 SearchResultLimits (org.apache.archiva.indexer.search.SearchResultLimits)2 RemoteRepository (org.apache.archiva.repository.RemoteRepository)2 Path (java.nio.file.Path)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 HttpSession (javax.servlet.http.HttpSession)1 ProxyConnector (org.apache.archiva.admin.model.beans.ProxyConnector)1 IndexMergerException (org.apache.archiva.indexer.merger.IndexMergerException)1