1 package org.apache.archiva.indexer.search;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.archiva.admin.model.RepositoryAdminException;
23 import org.apache.archiva.admin.model.beans.ManagedRepository;
24 import org.apache.archiva.admin.model.beans.ProxyConnector;
25 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
26 import org.apache.archiva.admin.model.proxyconnector.ProxyConnectorAdmin;
27 import org.apache.archiva.common.plexusbridge.MavenIndexerUtils;
28 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
29 import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
30 import org.apache.archiva.indexer.util.SearchUtil;
31 import org.apache.commons.lang.StringUtils;
32 import org.apache.lucene.search.BooleanClause.Occur;
33 import org.apache.lucene.search.BooleanQuery;
34 import org.apache.maven.index.ArtifactInfo;
35 import org.apache.maven.index.FlatSearchRequest;
36 import org.apache.maven.index.FlatSearchResponse;
37 import org.apache.maven.index.MAVEN;
38 import org.apache.maven.index.NexusIndexer;
39 import org.apache.maven.index.OSGI;
40 import org.apache.maven.index.context.IndexCreator;
41 import org.apache.maven.index.context.IndexingContext;
42 import org.apache.maven.index.context.UnsupportedExistingLuceneIndexException;
43 import org.apache.maven.index.expr.StringSearchExpression;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.springframework.stereotype.Service;
48 import javax.inject.Inject;
50 import java.io.IOException;
51 import java.util.ArrayList;
52 import java.util.Collection;
53 import java.util.Collections;
54 import java.util.HashSet;
55 import java.util.List;
60 * RepositorySearch implementation which uses the Nexus Indexer for searching.
62 @Service( "nexusSearch" )
63 public class NexusRepositorySearch
64 implements RepositorySearch
66 private Logger log = LoggerFactory.getLogger(getClass());
68 private NexusIndexer indexer;
70 private ManagedRepositoryAdmin managedRepositoryAdmin;
72 private ProxyConnectorAdmin proxyConnectorAdmin;
74 private MavenIndexerUtils mavenIndexerUtils;
76 protected NexusRepositorySearch()
82 public NexusRepositorySearch(PlexusSisuBridge plexusSisuBridge, ManagedRepositoryAdmin managedRepositoryAdmin,
83 MavenIndexerUtils mavenIndexerUtils, ProxyConnectorAdmin proxyConnectorAdmin)
84 throws PlexusSisuBridgeException
86 this.indexer = plexusSisuBridge.lookup(NexusIndexer.class);
87 this.managedRepositoryAdmin = managedRepositoryAdmin;
88 this.mavenIndexerUtils = mavenIndexerUtils;
89 this.proxyConnectorAdmin = proxyConnectorAdmin;
93 * @see RepositorySearch#search(String, List, String, SearchResultLimits, List)
95 public SearchResults search(String principal, List<String> selectedRepos, String term, SearchResultLimits limits,
96 List<String> previousSearchTerms)
97 throws RepositorySearchException
99 List<String> indexingContextIds = addIndexingContexts( selectedRepos );
101 // since upgrade to nexus 2.0.0, query has changed from g:[QUERIED TERM]* to g:*[QUERIED TERM]*
102 // resulting to more wildcard searches so we need to increase max clause count
103 BooleanQuery.setMaxClauseCount(Integer.MAX_VALUE);
104 BooleanQuery q = new BooleanQuery();
106 if ( previousSearchTerms == null || previousSearchTerms.isEmpty() )
108 constructQuery(term, q);
112 for ( String previousTerm : previousSearchTerms )
114 BooleanQuery iQuery = new BooleanQuery();
115 constructQuery(previousTerm, iQuery);
117 q.add(iQuery, Occur.MUST);
120 BooleanQuery iQuery = new BooleanQuery();
121 constructQuery(term, iQuery);
122 q.add(iQuery, Occur.MUST);
125 // we retun only artifacts without classifier in quick search, olamy cannot find a way to say with this field empty
126 // FIXME cannot find a way currently to setup this in constructQuery !!!
127 return search(limits, q, indexingContextIds, NoClassifierArtifactInfoFiler.LIST, principal, selectedRepos);
132 * @see RepositorySearch#search(String, SearchFields, SearchResultLimits)
134 public SearchResults search(String principal, SearchFields searchFields, SearchResultLimits limits)
135 throws RepositorySearchException
137 if ( searchFields.getRepositories() == null )
139 throw new RepositorySearchException("Repositories cannot be null.");
142 List<String> indexingContextIds = addIndexingContexts(searchFields.getRepositories());
144 BooleanQuery q = new BooleanQuery();
145 if ( StringUtils.isNotBlank(searchFields.getGroupId()) )
147 q.add(indexer.constructQuery(MAVEN.GROUP_ID, new StringSearchExpression(searchFields.getGroupId())),
151 if ( StringUtils.isNotBlank(searchFields.getArtifactId()) )
153 q.add(indexer.constructQuery(MAVEN.ARTIFACT_ID, new StringSearchExpression(searchFields.getArtifactId())),
157 if ( StringUtils.isNotBlank(searchFields.getVersion()) )
159 q.add(indexer.constructQuery(MAVEN.VERSION, new StringSearchExpression(searchFields.getVersion())),
163 if ( StringUtils.isNotBlank(searchFields.getPackaging()) )
165 q.add(indexer.constructQuery(MAVEN.PACKAGING, new StringSearchExpression(searchFields.getPackaging())),
169 if ( StringUtils.isNotBlank(searchFields.getClassName()) )
171 q.add(indexer.constructQuery(MAVEN.CLASSNAMES, new StringSearchExpression(searchFields.getClassName())),
175 if ( StringUtils.isNotBlank(searchFields.getBundleSymbolicName()) )
177 q.add(indexer.constructQuery(OSGI.SYMBOLIC_NAME,
178 new StringSearchExpression(searchFields.getBundleSymbolicName())), Occur.MUST);
181 if ( StringUtils.isNotBlank(searchFields.getBundleVersion()) )
183 q.add(indexer.constructQuery(OSGI.VERSION, new StringSearchExpression(searchFields.getBundleVersion())),
187 if ( StringUtils.isNotBlank(searchFields.getBundleExportPackage()) )
189 q.add(indexer.constructQuery(OSGI.EXPORT_PACKAGE,
190 new StringSearchExpression(searchFields.getBundleExportPackage())),
194 if ( StringUtils.isNotBlank(searchFields.getBundleExportService()) )
196 q.add(indexer.constructQuery(OSGI.EXPORT_SERVICE,
197 new StringSearchExpression(searchFields.getBundleExportService())),
201 if ( StringUtils.isNotBlank(searchFields.getBundleImportPackage()) )
203 q.add(indexer.constructQuery(OSGI.IMPORT_PACKAGE,
204 new StringSearchExpression(searchFields.getBundleImportPackage())),
208 if ( StringUtils.isNotBlank(searchFields.getBundleName()) )
210 q.add(indexer.constructQuery(OSGI.NAME, new StringSearchExpression(searchFields.getBundleName())),
214 if ( StringUtils.isNotBlank(searchFields.getClassifier()) )
216 q.add(indexer.constructQuery(MAVEN.CLASSIFIER, new StringSearchExpression(searchFields.getClassifier())),
220 if ( q.getClauses() == null || q.getClauses().length <= 0 )
222 throw new RepositorySearchException("No search fields set.");
225 return search(limits, q, indexingContextIds, Collections.<ArtifactInfoFiler>emptyList(), principal,
226 searchFields.getRepositories());
229 private SearchResults search(SearchResultLimits limits, BooleanQuery q, List<String> indexingContextIds,
230 List<? extends ArtifactInfoFiler> filters, String principal,
231 List<String> selectedRepos)
232 throws RepositorySearchException
237 FlatSearchRequest request = new FlatSearchRequest(q);
238 request.setContexts(getIndexingContexts(indexingContextIds));
240 FlatSearchResponse response = indexer.searchFlat(request);
242 if ( response == null || response.getTotalHits() == 0 )
244 SearchResults results = new SearchResults();
245 results.setLimits(limits);
249 return convertToSearchResults(response, limits, filters, principal, selectedRepos);
251 catch ( IOException e )
253 throw new RepositorySearchException(e.getMessage(), e);
255 catch ( RepositoryAdminException e )
257 throw new RepositorySearchException(e.getMessage(), e);
262 private List<IndexingContext> getIndexingContexts(List<String> ids)
264 List<IndexingContext> contexts = new ArrayList<IndexingContext>(ids.size());
266 for ( String id : ids )
268 IndexingContext context = indexer.getIndexingContexts().get(id);
269 if ( context != null )
271 contexts.add(context);
275 log.warn("context with id {} not exists", id);
282 private void constructQuery(String term, BooleanQuery q)
284 q.add(indexer.constructQuery(MAVEN.GROUP_ID, new StringSearchExpression(term)), Occur.SHOULD);
285 q.add(indexer.constructQuery(MAVEN.ARTIFACT_ID, new StringSearchExpression(term)), Occur.SHOULD);
286 q.add(indexer.constructQuery(MAVEN.VERSION, new StringSearchExpression(term)), Occur.SHOULD);
287 q.add(indexer.constructQuery(MAVEN.PACKAGING, new StringSearchExpression(term)), Occur.SHOULD);
288 q.add(indexer.constructQuery(MAVEN.CLASSNAMES, new StringSearchExpression(term)), Occur.SHOULD);
291 // new WildcardQuery( new Term( MAVEN.CLASSNAMES.getFieldName(), "*" ) );
292 //q.add( query, Occur.MUST_NOT );
293 // olamy IMHO we could set this option as at least one must match
294 //q.setMinimumNumberShouldMatch( 1 );
299 * @param selectedRepos
300 * @return indexing contextId used
302 private List<String> addIndexingContexts(List<String> selectedRepos)
304 Set<String> indexingContextIds = new HashSet<String>();
305 for ( String repo : selectedRepos )
309 ManagedRepository repoConfig = managedRepositoryAdmin.getManagedRepository(repo);
311 if ( repoConfig != null )
313 String indexDir = repoConfig.getIndexDirectory();
314 File indexDirectory = null;
315 if ( indexDir != null && !"".equals(indexDir) )
317 indexDirectory = new File(repoConfig.getIndexDirectory());
321 indexDirectory = new File(repoConfig.getLocation(), ".indexer");
324 IndexingContext context = indexer.getIndexingContexts().get(repoConfig.getId());
325 if ( context != null )
327 // alreday here so no need to record it again
328 log.debug("index with id {} already exists skip adding it", repoConfig.getId());
329 // set searchable flag
330 context.setSearchable(repoConfig.isScanned());
331 indexingContextIds.add(context.getId());
332 indexingContextIds.addAll(getRemoteIndexingContextIds(repo));
336 context = indexer.addIndexingContext(repoConfig.getId(), repoConfig.getId(),
337 new File(repoConfig.getLocation()), indexDirectory, null, null,
338 getAllIndexCreators());
339 context.setSearchable(repoConfig.isScanned());
340 if ( context.isSearchable() )
342 indexingContextIds.addAll(getRemoteIndexingContextIds(repo));
343 indexingContextIds.add(context.getId());
347 log.warn("indexingContext with id {} not searchable", repoConfig.getId());
353 log.warn("Repository '" + repo + "' not found in configuration.");
356 catch ( UnsupportedExistingLuceneIndexException e )
358 log.warn("Error accessing index of repository '" + repo + "' : " + e.getMessage());
361 catch ( IOException e )
363 log.warn("IO error occured while accessing index of repository '" + repo + "' : " + e.getMessage());
366 catch ( RepositoryAdminException e )
368 log.warn("RepositoryAdminException occured while accessing index of repository '" + repo + "' : "
374 return new ArrayList<String>(indexingContextIds);
378 private Set<String> getRemoteIndexingContextIds(String managedRepoId)
379 throws RepositoryAdminException
381 Set<String> ids = new HashSet<String>();
383 List<ProxyConnector> proxyConnectors = proxyConnectorAdmin.getProxyConnectorAsMap().get(managedRepoId);
385 if ( proxyConnectors == null || proxyConnectors.isEmpty() )
390 for ( ProxyConnector proxyConnector : proxyConnectors )
392 String remoteId = "remote-" + proxyConnector.getTargetRepoId();
393 IndexingContext context = indexer.getIndexingContexts().get(remoteId);
394 if ( context != null && context.isSearchable() )
403 public Collection<String> getAllGroupIds(String principal, List<String> selectedRepos)
404 throws RepositorySearchException
406 List<IndexingContext> indexContexts = getIndexingContexts( selectedRepos );
407 if (indexContexts == null || indexContexts.isEmpty())
409 return Collections.emptyList();
414 Set<String> allGroupIds = new HashSet<String>( );
415 for (IndexingContext indexingContext : indexContexts)
417 allGroupIds.addAll( indexingContext.getAllGroups() );
420 } catch ( IOException e )
422 throw new RepositorySearchException( e.getMessage(), e );
426 protected List<? extends IndexCreator> getAllIndexCreators()
428 return mavenIndexerUtils.getAllIndexCreators();
432 private SearchResults convertToSearchResults(FlatSearchResponse response, SearchResultLimits limits,
433 List<? extends ArtifactInfoFiler> artifactInfoFilers, String principal,
434 List<String> selectedRepos)
435 throws RepositoryAdminException
437 SearchResults results = new SearchResults();
438 Set<ArtifactInfo> artifactInfos = response.getResults();
440 for ( ArtifactInfo artifactInfo : artifactInfos )
442 String id = SearchUtil.getHitId(artifactInfo.groupId, artifactInfo.artifactId, artifactInfo.classifier,
443 artifactInfo.packaging);
444 Map<String, SearchResultHit> hitsMap = results.getHitsMap();
446 if ( !applyArtifactInfoFilters(artifactInfo, artifactInfoFilers, hitsMap) )
451 SearchResultHit hit = hitsMap.get(id);
454 if ( !hit.getVersions().contains(artifactInfo.version) )
456 hit.addVersion(artifactInfo.version);
461 hit = new SearchResultHit();
462 hit.setArtifactId(artifactInfo.artifactId);
463 hit.setGroupId(artifactInfo.groupId);
464 hit.setRepositoryId(artifactInfo.repository);
465 hit.addVersion(artifactInfo.version);
466 hit.setBundleExportPackage(artifactInfo.bundleExportPackage);
467 hit.setBundleExportService(artifactInfo.bundleExportService);
468 hit.setBundleSymbolicName(artifactInfo.bundleSymbolicName);
469 hit.setBundleVersion(artifactInfo.bundleVersion);
470 hit.setBundleDescription(artifactInfo.bundleDescription);
471 hit.setBundleDocUrl(artifactInfo.bundleDocUrl);
472 hit.setBundleRequireBundle(artifactInfo.bundleRequireBundle);
473 hit.setBundleImportPackage(artifactInfo.bundleImportPackage);
474 hit.setBundleLicense(artifactInfo.bundleLicense);
475 hit.setBundleName(artifactInfo.bundleName);
476 hit.setContext(artifactInfo.context);
477 hit.setGoals(artifactInfo.goals);
478 hit.setPrefix(artifactInfo.prefix);
479 hit.setPackaging(artifactInfo.packaging);
480 hit.setClassifier(artifactInfo.classifier);
481 hit.setUrl(getBaseUrl(artifactInfo, selectedRepos));
484 results.addHit(id, hit);
487 results.setTotalHits(response.getTotalHitsCount());
488 results.setTotalHitsMapSize(results.getHitsMap().values().size());
489 results.setReturnedHitsCount(response.getReturnedHitsCount());
490 results.setLimits(limits);
492 if ( limits == null || limits.getSelectedPage() == SearchResultLimits.ALL_PAGES )
498 return paginate(results);
503 * calculate baseUrl without the context and base Archiva Url
505 * @param artifactInfo
508 protected String getBaseUrl(ArtifactInfo artifactInfo, List<String> selectedRepos)
509 throws RepositoryAdminException
511 StringBuilder sb = new StringBuilder();
512 if ( StringUtils.startsWith(artifactInfo.context, "remote-") )
514 // it's a remote index result we search a managed which proxying this remote and on which
515 // current user has read karma
516 String managedRepoId =
517 getManagedRepoId(StringUtils.substringAfter(artifactInfo.context, "remote-"), selectedRepos);
518 if ( managedRepoId != null )
520 sb.append('/').append(managedRepoId);
525 sb.append('/').append(artifactInfo.context);
528 sb.append('/').append(StringUtils.replaceChars(artifactInfo.groupId, '.', '/'));
529 sb.append('/').append(artifactInfo.artifactId);
530 sb.append('/').append(artifactInfo.version);
531 sb.append('/').append(artifactInfo.artifactId);
532 sb.append('-').append(artifactInfo.version);
533 if ( StringUtils.isNotBlank(artifactInfo.classifier) )
535 sb.append('-').append(artifactInfo.classifier);
537 // maven-plugin packaging is a jar
538 if ( StringUtils.equals("maven-plugin", artifactInfo.packaging) )
544 sb.append('.').append(artifactInfo.packaging);
547 return sb.toString();
551 * return a managed repo for a remote result
554 * @param selectedRepos
556 * @throws RepositoryAdminException
558 private String getManagedRepoId(String remoteRepo, List<String> selectedRepos)
559 throws RepositoryAdminException
561 Map<String, List<ProxyConnector>> proxyConnectorMap = proxyConnectorAdmin.getProxyConnectorAsMap();
562 if ( proxyConnectorMap == null || proxyConnectorMap.isEmpty() )
566 if ( selectedRepos != null && !selectedRepos.isEmpty() )
568 for ( Map.Entry<String, List<ProxyConnector>> entry : proxyConnectorMap.entrySet() )
570 if ( selectedRepos.contains(entry.getKey()) )
572 for ( ProxyConnector proxyConnector : entry.getValue() )
574 if ( StringUtils.equals(remoteRepo, proxyConnector.getTargetRepoId()) )
576 return proxyConnector.getSourceRepoId();
583 // we don't find in search selected repos so return the first one
584 for ( Map.Entry<String, List<ProxyConnector>> entry : proxyConnectorMap.entrySet() )
587 for ( ProxyConnector proxyConnector : entry.getValue() )
589 if ( StringUtils.equals(remoteRepo, proxyConnector.getTargetRepoId()) )
591 return proxyConnector.getSourceRepoId();
599 private boolean applyArtifactInfoFilters(ArtifactInfo artifactInfo,
600 List<? extends ArtifactInfoFiler> artifactInfoFilers,
601 Map<String, SearchResultHit> currentResult)
603 if ( artifactInfoFilers == null || artifactInfoFilers.isEmpty() )
608 for ( ArtifactInfoFiler filter : artifactInfoFilers )
610 if ( !filter.addArtifactInResult(artifactInfo, currentResult) )
618 protected SearchResults paginate(SearchResults results)
620 SearchResultLimits limits = results.getLimits();
621 SearchResults paginated = new SearchResults();
623 int fetchCount = limits.getPageSize();
624 int offset = ( limits.getSelectedPage() * limits.getPageSize() );
626 if ( fetchCount > results.getTotalHits() )
628 fetchCount = results.getTotalHits();
632 if ( offset < results.getTotalHits() )
634 // only process if the offset is within the hit count.
635 for ( int i = 0; i < fetchCount; i++ )
637 // Stop fetching if we are past the total # of available hits.
638 if ( offset + i >= results.getHits().size() )
643 SearchResultHit hit = results.getHits().get(( offset + i ));
646 String id = SearchUtil.getHitId(hit.getGroupId(), hit.getArtifactId(), hit.getClassifier(),
648 paginated.addHit(id, hit);
656 paginated.setTotalHits(results.getTotalHits());
657 paginated.setReturnedHitsCount(paginated.getHits().size());
658 paginated.setTotalHitsMapSize( results.getTotalHitsMapSize() );
659 paginated.setLimits(limits);