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());
}
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());
}
}
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());
}
}
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());
}
}
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);
}
}
Aggregations