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.expr.SourcedSearchExpression;
43 import org.apache.maven.index.expr.UserInputSearchExpression;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.springframework.stereotype.Service;
48 import javax.inject.Inject;
49 import java.io.IOException;
50 import java.util.ArrayList;
51 import java.util.Collection;
52 import java.util.Collections;
53 import java.util.HashSet;
54 import java.util.List;
59 * RepositorySearch implementation which uses the Maven Indexer for searching.
61 @Service("repositorySearch#maven")
62 public class MavenRepositorySearch
63 implements RepositorySearch
65 private Logger log = LoggerFactory.getLogger( getClass() );
67 private NexusIndexer indexer;
69 private ManagedRepositoryAdmin managedRepositoryAdmin;
71 private ProxyConnectorAdmin proxyConnectorAdmin;
73 private MavenIndexerUtils mavenIndexerUtils;
75 protected MavenRepositorySearch()
81 public MavenRepositorySearch( PlexusSisuBridge plexusSisuBridge, ManagedRepositoryAdmin managedRepositoryAdmin,
82 MavenIndexerUtils mavenIndexerUtils, ProxyConnectorAdmin proxyConnectorAdmin )
83 throws PlexusSisuBridgeException
85 this.indexer = plexusSisuBridge.lookup( NexusIndexer.class );
86 this.managedRepositoryAdmin = managedRepositoryAdmin;
87 this.mavenIndexerUtils = mavenIndexerUtils;
88 this.proxyConnectorAdmin = proxyConnectorAdmin;
92 * @see RepositorySearch#search(String, List, String, SearchResultLimits, List)
94 public SearchResults search( String principal, List<String> selectedRepos, String term, SearchResultLimits limits,
95 List<String> previousSearchTerms )
96 throws RepositorySearchException
98 List<String> indexingContextIds = addIndexingContexts( selectedRepos );
100 // since upgrade to nexus 2.0.0, query has changed from g:[QUERIED TERM]* to g:*[QUERIED TERM]*
101 // resulting to more wildcard searches so we need to increase max clause count
102 BooleanQuery.setMaxClauseCount( Integer.MAX_VALUE );
103 BooleanQuery q = new BooleanQuery();
105 if ( previousSearchTerms == null || previousSearchTerms.isEmpty() )
107 constructQuery( term, q );
111 for ( String previousTerm : previousSearchTerms )
113 BooleanQuery iQuery = new BooleanQuery();
114 constructQuery( previousTerm, iQuery );
116 q.add( iQuery, Occur.MUST );
119 BooleanQuery iQuery = new BooleanQuery();
120 constructQuery( term, iQuery );
121 q.add( iQuery, Occur.MUST );
124 // we retun only artifacts without classifier in quick search, olamy cannot find a way to say with this field empty
125 // FIXME cannot find a way currently to setup this in constructQuery !!!
126 return search( limits, q, indexingContextIds, NoClassifierArtifactInfoFilter.LIST, selectedRepos, true );
131 * @see RepositorySearch#search(String, SearchFields, SearchResultLimits)
133 public SearchResults search( String principal, SearchFields searchFields, SearchResultLimits limits )
134 throws RepositorySearchException
136 if ( searchFields.getRepositories() == null )
138 throw new RepositorySearchException( "Repositories cannot be null." );
141 List<String> indexingContextIds = addIndexingContexts( searchFields.getRepositories() );
143 // if no index found in the specified ones return an empty search result instead of doing a search on all index
144 // olamy: IMHO doesn't make sense
145 if ( !searchFields.getRepositories().isEmpty() && ( indexingContextIds == null
146 || indexingContextIds.isEmpty() ) )
148 return new SearchResults();
151 BooleanQuery q = new BooleanQuery();
152 if ( StringUtils.isNotBlank( searchFields.getGroupId() ) )
154 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new UserInputSearchExpression( searchFields.getGroupId() ) ),
158 if ( StringUtils.isNotBlank( searchFields.getArtifactId() ) )
160 q.add( indexer.constructQuery( MAVEN.ARTIFACT_ID,
161 new UserInputSearchExpression( searchFields.getArtifactId() ) ),
165 if ( StringUtils.isNotBlank( searchFields.getVersion() ) )
167 q.add( indexer.constructQuery( MAVEN.VERSION, new SourcedSearchExpression( searchFields.getVersion() ) ),
171 if ( StringUtils.isNotBlank( searchFields.getPackaging() ) )
174 indexer.constructQuery( MAVEN.PACKAGING, new UserInputSearchExpression( searchFields.getPackaging() ) ),
178 if ( StringUtils.isNotBlank( searchFields.getClassName() ) )
180 q.add( indexer.constructQuery( MAVEN.CLASSNAMES,
181 new UserInputSearchExpression( searchFields.getClassName() ) ), Occur.MUST );
184 if ( StringUtils.isNotBlank( searchFields.getBundleSymbolicName() ) )
186 q.add( indexer.constructQuery( OSGI.SYMBOLIC_NAME,
187 new UserInputSearchExpression( searchFields.getBundleSymbolicName() ) ),
191 if ( StringUtils.isNotBlank( searchFields.getBundleVersion() ) )
193 q.add( indexer.constructQuery( OSGI.VERSION,
194 new UserInputSearchExpression( searchFields.getBundleVersion() ) ),
198 if ( StringUtils.isNotBlank( searchFields.getBundleExportPackage() ) )
200 q.add( indexer.constructQuery( OSGI.EXPORT_PACKAGE,
201 new UserInputSearchExpression( searchFields.getBundleExportPackage() ) ),
205 if ( StringUtils.isNotBlank( searchFields.getBundleExportService() ) )
207 q.add( indexer.constructQuery( OSGI.EXPORT_SERVICE,
208 new UserInputSearchExpression( searchFields.getBundleExportService() ) ),
212 if ( StringUtils.isNotBlank( searchFields.getBundleImportPackage() ) )
214 q.add( indexer.constructQuery( OSGI.IMPORT_PACKAGE,
215 new UserInputSearchExpression( searchFields.getBundleImportPackage() ) ),
219 if ( StringUtils.isNotBlank( searchFields.getBundleName() ) )
221 q.add( indexer.constructQuery( OSGI.NAME, new UserInputSearchExpression( searchFields.getBundleName() ) ),
225 if ( StringUtils.isNotBlank( searchFields.getBundleImportPackage() ) )
227 q.add( indexer.constructQuery( OSGI.IMPORT_PACKAGE,
228 new UserInputSearchExpression( searchFields.getBundleImportPackage() ) ),
232 if ( StringUtils.isNotBlank( searchFields.getBundleRequireBundle() ) )
234 q.add( indexer.constructQuery( OSGI.REQUIRE_BUNDLE,
235 new UserInputSearchExpression( searchFields.getBundleRequireBundle() ) ),
239 if ( StringUtils.isNotBlank( searchFields.getClassifier() ) )
241 q.add( indexer.constructQuery( MAVEN.CLASSIFIER,
242 new UserInputSearchExpression( searchFields.getClassifier() ) ),
246 if ( q.getClauses() == null || q.getClauses().length <= 0 )
248 throw new RepositorySearchException( "No search fields set." );
251 return search( limits, q, indexingContextIds, Collections.<ArtifactInfoFilter>emptyList(),
252 searchFields.getRepositories(), searchFields.isIncludePomArtifacts() );
255 private SearchResults search( SearchResultLimits limits, BooleanQuery q, List<String> indexingContextIds,
256 List<? extends ArtifactInfoFilter> filters, List<String> selectedRepos,
257 boolean includePoms )
258 throws RepositorySearchException
263 FlatSearchRequest request = new FlatSearchRequest( q );
265 request.setContexts( getIndexingContexts( indexingContextIds ) );
267 FlatSearchResponse response = indexer.searchFlat( request );
269 if ( response == null || response.getTotalHits() == 0 )
271 SearchResults results = new SearchResults();
272 results.setLimits( limits );
276 return convertToSearchResults( response, limits, filters, selectedRepos, includePoms );
278 catch ( IOException e )
280 throw new RepositorySearchException( e.getMessage(), e );
282 catch ( RepositoryAdminException e )
284 throw new RepositorySearchException( e.getMessage(), e );
289 private List<IndexingContext> getIndexingContexts( List<String> ids )
291 List<IndexingContext> contexts = new ArrayList<>( ids.size() );
293 for ( String id : ids )
295 IndexingContext context = indexer.getIndexingContexts().get( id );
296 if ( context != null )
298 contexts.add( context );
302 log.warn( "context with id {} not exists", id );
309 private void constructQuery( String term, BooleanQuery q )
311 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new UserInputSearchExpression( term ) ), Occur.SHOULD );
312 q.add( indexer.constructQuery( MAVEN.ARTIFACT_ID, new UserInputSearchExpression( term ) ), Occur.SHOULD );
313 q.add( indexer.constructQuery( MAVEN.VERSION, new UserInputSearchExpression( term ) ), Occur.SHOULD );
314 q.add( indexer.constructQuery( MAVEN.PACKAGING, new UserInputSearchExpression( term ) ), Occur.SHOULD );
315 q.add( indexer.constructQuery( MAVEN.CLASSNAMES, new UserInputSearchExpression( term ) ), Occur.SHOULD );
318 // new WildcardQuery( new Term( MAVEN.CLASSNAMES.getFieldName(), "*" ) );
319 //q.add( query, Occur.MUST_NOT );
320 // olamy IMHO we could set this option as at least one must match
321 //q.setMinimumNumberShouldMatch( 1 );
326 * @param selectedRepos
327 * @return indexing contextId used
329 private List<String> addIndexingContexts( List<String> selectedRepos )
331 Set<String> indexingContextIds = new HashSet<String>();
332 for ( String repo : selectedRepos )
336 ManagedRepository repoConfig = managedRepositoryAdmin.getManagedRepository( repo );
338 if ( repoConfig != null )
341 IndexingContext context = managedRepositoryAdmin.createIndexContext( repoConfig );
342 if ( context.isSearchable() )
344 indexingContextIds.addAll( getRemoteIndexingContextIds( repo ) );
345 indexingContextIds.add( context.getId() );
349 log.warn( "indexingContext with id {} not searchable", repoConfig.getId() );
355 log.warn( "Repository '{}' not found in configuration.", repo );
358 catch ( RepositoryAdminException e )
360 log.warn( "RepositoryAdminException occured while accessing index of repository '{}' : {}", repo,
366 return new ArrayList<>( indexingContextIds );
370 public Set<String> getRemoteIndexingContextIds( String managedRepoId )
371 throws RepositoryAdminException
373 Set<String> ids = new HashSet<String>();
375 List<ProxyConnector> proxyConnectors = proxyConnectorAdmin.getProxyConnectorAsMap().get( managedRepoId );
377 if ( proxyConnectors == null || proxyConnectors.isEmpty() )
382 for ( ProxyConnector proxyConnector : proxyConnectors )
384 String remoteId = "remote-" + proxyConnector.getTargetRepoId();
385 IndexingContext context = indexer.getIndexingContexts().get( remoteId );
386 if ( context != null && context.isSearchable() )
395 public Collection<String> getAllGroupIds( String principal, List<String> selectedRepos )
396 throws RepositorySearchException
398 List<IndexingContext> indexContexts = getIndexingContexts( selectedRepos );
400 if ( indexContexts == null || indexContexts.isEmpty() )
402 return Collections.emptyList();
407 Set<String> allGroupIds = new HashSet<String>();
408 for ( IndexingContext indexingContext : indexContexts )
410 allGroupIds.addAll( indexingContext.getAllGroups() );
414 catch ( IOException e )
416 throw new RepositorySearchException( e.getMessage(), e );
422 protected List<? extends IndexCreator> getAllIndexCreators()
424 return mavenIndexerUtils.getAllIndexCreators();
428 private SearchResults convertToSearchResults( FlatSearchResponse response, SearchResultLimits limits,
429 List<? extends ArtifactInfoFilter> artifactInfoFilters,
430 List<String> selectedRepos, boolean includePoms )
431 throws RepositoryAdminException
433 SearchResults results = new SearchResults();
434 Set<ArtifactInfo> artifactInfos = response.getResults();
436 for ( ArtifactInfo artifactInfo : artifactInfos )
438 if ( StringUtils.equalsIgnoreCase( "pom", artifactInfo.fextension ) && !includePoms )
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, artifactInfoFilters, 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.setFileExtension( artifactInfo.fextension );
482 hit.setUrl( getBaseUrl( artifactInfo, selectedRepos ) );
485 results.addHit( id, hit );
488 results.setTotalHits( response.getTotalHitsCount() );
489 results.setTotalHitsMapSize( results.getHitsMap().values().size() );
490 results.setReturnedHitsCount( response.getReturnedHitsCount() );
491 results.setLimits( limits );
493 if ( limits == null || limits.getSelectedPage() == SearchResultLimits.ALL_PAGES )
499 return paginate( results );
504 * calculate baseUrl without the context and base Archiva Url
506 * @param artifactInfo
509 protected String getBaseUrl( ArtifactInfo artifactInfo, List<String> selectedRepos )
510 throws RepositoryAdminException
512 StringBuilder sb = new StringBuilder();
513 if ( StringUtils.startsWith( artifactInfo.context, "remote-" ) )
515 // it's a remote index result we search a managed which proxying this remote and on which
516 // current user has read karma
517 String managedRepoId =
518 getManagedRepoId( StringUtils.substringAfter( artifactInfo.context, "remote-" ), selectedRepos );
519 if ( managedRepoId != null )
521 sb.append( '/' ).append( managedRepoId );
522 artifactInfo.context = managedRepoId;
527 sb.append( '/' ).append( artifactInfo.context );
530 sb.append( '/' ).append( StringUtils.replaceChars( artifactInfo.groupId, '.', '/' ) );
531 sb.append( '/' ).append( artifactInfo.artifactId );
532 sb.append( '/' ).append( artifactInfo.version );
533 sb.append( '/' ).append( artifactInfo.artifactId );
534 sb.append( '-' ).append( artifactInfo.version );
535 if ( StringUtils.isNotBlank( artifactInfo.classifier ) )
537 sb.append( '-' ).append( artifactInfo.classifier );
539 // maven-plugin packaging is a jar
540 if ( StringUtils.equals( "maven-plugin", artifactInfo.packaging ) )
546 sb.append( '.' ).append( artifactInfo.packaging );
549 return sb.toString();
553 * return a managed repo for a remote result
556 * @param selectedRepos
558 * @throws RepositoryAdminException
560 private String getManagedRepoId( String remoteRepo, List<String> selectedRepos )
561 throws RepositoryAdminException
563 Map<String, List<ProxyConnector>> proxyConnectorMap = proxyConnectorAdmin.getProxyConnectorAsMap();
564 if ( proxyConnectorMap == null || proxyConnectorMap.isEmpty() )
568 if ( selectedRepos != null && !selectedRepos.isEmpty() )
570 for ( Map.Entry<String, List<ProxyConnector>> entry : proxyConnectorMap.entrySet() )
572 if ( selectedRepos.contains( entry.getKey() ) )
574 for ( ProxyConnector proxyConnector : entry.getValue() )
576 if ( StringUtils.equals( remoteRepo, proxyConnector.getTargetRepoId() ) )
578 return proxyConnector.getSourceRepoId();
585 // we don't find in search selected repos so return the first one
586 for ( Map.Entry<String, List<ProxyConnector>> entry : proxyConnectorMap.entrySet() )
589 for ( ProxyConnector proxyConnector : entry.getValue() )
591 if ( StringUtils.equals( remoteRepo, proxyConnector.getTargetRepoId() ) )
593 return proxyConnector.getSourceRepoId();
601 private boolean applyArtifactInfoFilters( ArtifactInfo artifactInfo,
602 List<? extends ArtifactInfoFilter> artifactInfoFilters,
603 Map<String, SearchResultHit> currentResult )
605 if ( artifactInfoFilters == null || artifactInfoFilters.isEmpty() )
610 for ( ArtifactInfoFilter filter : artifactInfoFilters )
612 if ( !filter.addArtifactInResult( artifactInfo, currentResult ) )
620 protected SearchResults paginate( SearchResults results )
622 SearchResultLimits limits = results.getLimits();
623 SearchResults paginated = new SearchResults();
625 int fetchCount = limits.getPageSize();
626 int offset = ( limits.getSelectedPage() * limits.getPageSize() );
628 if ( fetchCount > results.getTotalHits() )
630 fetchCount = results.getTotalHits();
634 if ( offset < results.getTotalHits() )
636 // only process if the offset is within the hit count.
637 for ( int i = 0; i < fetchCount; i++ )
639 // Stop fetching if we are past the total # of available hits.
640 if ( offset + i >= results.getHits().size() )
645 SearchResultHit hit = results.getHits().get( ( offset + i ) );
648 String id = SearchUtil.getHitId( hit.getGroupId(), hit.getArtifactId(), hit.getClassifier(),
649 hit.getPackaging() );
650 paginated.addHit( id, hit );
658 paginated.setTotalHits( results.getTotalHits() );
659 paginated.setReturnedHitsCount( paginated.getHits().size() );
660 paginated.setTotalHitsMapSize( results.getTotalHitsMapSize() );
661 paginated.setLimits( limits );