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, selectedRepos, false );
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 // if no index found in the specified ones return an empty search result instead of doing a search on all index
145 // olamy: IMHO doesn't make sense
146 if ( !searchFields.getRepositories().isEmpty() && ( indexingContextIds == null
147 || indexingContextIds.isEmpty() ) )
149 return new SearchResults();
152 BooleanQuery q = new BooleanQuery();
153 if ( StringUtils.isNotBlank( searchFields.getGroupId() ) )
155 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( searchFields.getGroupId() ) ),
159 if ( StringUtils.isNotBlank( searchFields.getArtifactId() ) )
162 indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( searchFields.getArtifactId() ) ),
166 if ( StringUtils.isNotBlank( searchFields.getVersion() ) )
168 q.add( indexer.constructQuery( MAVEN.VERSION, new StringSearchExpression( searchFields.getVersion() ) ),
172 if ( StringUtils.isNotBlank( searchFields.getPackaging() ) )
174 q.add( indexer.constructQuery( MAVEN.PACKAGING, new StringSearchExpression( searchFields.getPackaging() ) ),
178 if ( StringUtils.isNotBlank( searchFields.getClassName() ) )
181 indexer.constructQuery( MAVEN.CLASSNAMES, new StringSearchExpression( searchFields.getClassName() ) ),
185 if ( StringUtils.isNotBlank( searchFields.getBundleSymbolicName() ) )
187 q.add( indexer.constructQuery( OSGI.SYMBOLIC_NAME,
188 new StringSearchExpression( searchFields.getBundleSymbolicName() ) ),
192 if ( StringUtils.isNotBlank( searchFields.getBundleVersion() ) )
195 indexer.constructQuery( OSGI.VERSION, new StringSearchExpression( searchFields.getBundleVersion() ) ),
199 if ( StringUtils.isNotBlank( searchFields.getBundleExportPackage() ) )
201 q.add( indexer.constructQuery( OSGI.EXPORT_PACKAGE,
202 new StringSearchExpression( searchFields.getBundleExportPackage() ) ),
206 if ( StringUtils.isNotBlank( searchFields.getBundleExportService() ) )
208 q.add( indexer.constructQuery( OSGI.EXPORT_SERVICE,
209 new StringSearchExpression( searchFields.getBundleExportService() ) ),
213 if ( StringUtils.isNotBlank( searchFields.getBundleImportPackage() ) )
215 q.add( indexer.constructQuery( OSGI.IMPORT_PACKAGE,
216 new StringSearchExpression( searchFields.getBundleImportPackage() ) ),
220 if ( StringUtils.isNotBlank( searchFields.getBundleName() ) )
222 q.add( indexer.constructQuery( OSGI.NAME, new StringSearchExpression( searchFields.getBundleName() ) ),
226 if ( StringUtils.isNotBlank( searchFields.getClassifier() ) )
229 indexer.constructQuery( MAVEN.CLASSIFIER, new StringSearchExpression( searchFields.getClassifier() ) ),
233 if ( q.getClauses() == null || q.getClauses().length <= 0 )
235 throw new RepositorySearchException( "No search fields set." );
238 return search( limits, q, indexingContextIds, Collections.<ArtifactInfoFiler>emptyList(),
239 searchFields.getRepositories(), searchFields.isIncludePomArtifacts() );
242 private SearchResults search( SearchResultLimits limits, BooleanQuery q, List<String> indexingContextIds,
243 List<? extends ArtifactInfoFiler> filters,
244 List<String> selectedRepos, boolean includePoms)
245 throws RepositorySearchException
250 FlatSearchRequest request = new FlatSearchRequest( q );
251 request.setContexts( getIndexingContexts( indexingContextIds ) );
253 FlatSearchResponse response = indexer.searchFlat( request );
255 if ( response == null || response.getTotalHits() == 0 )
257 SearchResults results = new SearchResults();
258 results.setLimits( limits );
262 return convertToSearchResults( response, limits, filters, selectedRepos, includePoms );
264 catch ( IOException e )
266 throw new RepositorySearchException( e.getMessage(), e );
268 catch ( RepositoryAdminException e )
270 throw new RepositorySearchException( e.getMessage(), e );
275 private List<IndexingContext> getIndexingContexts( List<String> ids )
277 List<IndexingContext> contexts = new ArrayList<IndexingContext>( ids.size() );
279 for ( String id : ids )
281 IndexingContext context = indexer.getIndexingContexts().get( id );
282 if ( context != null )
284 contexts.add( context );
288 log.warn( "context with id {} not exists", id );
295 private void constructQuery( String term, BooleanQuery q )
297 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( term ) ), Occur.SHOULD );
298 q.add( indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( term ) ), Occur.SHOULD );
299 q.add( indexer.constructQuery( MAVEN.VERSION, new StringSearchExpression( term ) ), Occur.SHOULD );
300 q.add( indexer.constructQuery( MAVEN.PACKAGING, new StringSearchExpression( term ) ), Occur.SHOULD );
301 q.add( indexer.constructQuery( MAVEN.CLASSNAMES, new StringSearchExpression( term ) ), Occur.SHOULD );
304 // new WildcardQuery( new Term( MAVEN.CLASSNAMES.getFieldName(), "*" ) );
305 //q.add( query, Occur.MUST_NOT );
306 // olamy IMHO we could set this option as at least one must match
307 //q.setMinimumNumberShouldMatch( 1 );
312 * @param selectedRepos
313 * @return indexing contextId used
315 private List<String> addIndexingContexts( List<String> selectedRepos )
317 Set<String> indexingContextIds = new HashSet<String>();
318 for ( String repo : selectedRepos )
322 ManagedRepository repoConfig = managedRepositoryAdmin.getManagedRepository( repo );
324 if ( repoConfig != null )
326 String indexDir = repoConfig.getIndexDirectory();
327 File indexDirectory = null;
328 if ( indexDir != null && !"".equals( indexDir ) )
330 indexDirectory = new File( repoConfig.getIndexDirectory() );
331 if ( !indexDirectory.isAbsolute() )
333 indexDirectory = new File( repoConfig.getLocation(), repoConfig.getIndexDirectory() );
338 indexDirectory = new File( repoConfig.getLocation(), ".indexer" );
341 IndexingContext context = indexer.getIndexingContexts().get( repoConfig.getId() );
342 if ( context != null )
344 // alreday here so no need to record it again
345 log.debug( "index with id {} already exists skip adding it", repoConfig.getId() );
346 // set searchable flag
347 context.setSearchable( repoConfig.isScanned() );
348 indexingContextIds.add( context.getId() );
349 indexingContextIds.addAll( getRemoteIndexingContextIds( repo ) );
353 context = indexer.addIndexingContext( repoConfig.getId(), repoConfig.getId(),
354 new File( repoConfig.getLocation() ), indexDirectory, null,
355 null, getAllIndexCreators() );
356 context.setSearchable( repoConfig.isScanned() );
357 if ( context.isSearchable() )
359 indexingContextIds.addAll( getRemoteIndexingContextIds( repo ) );
360 indexingContextIds.add( context.getId() );
364 log.warn( "indexingContext with id {} not searchable", repoConfig.getId() );
370 log.warn( "Repository '" + repo + "' not found in configuration." );
373 catch ( UnsupportedExistingLuceneIndexException e )
375 log.warn( "Error accessing index of repository '" + repo + "' : " + e.getMessage() );
378 catch ( IOException e )
380 log.warn( "IO error occured while accessing index of repository '" + repo + "' : " + e.getMessage() );
383 catch ( RepositoryAdminException e )
385 log.warn( "RepositoryAdminException occured while accessing index of repository '" + repo + "' : "
391 return new ArrayList<String>( indexingContextIds );
395 public Set<String> getRemoteIndexingContextIds( String managedRepoId )
396 throws RepositoryAdminException
398 Set<String> ids = new HashSet<String>();
400 List<ProxyConnector> proxyConnectors = proxyConnectorAdmin.getProxyConnectorAsMap().get( managedRepoId );
402 if ( proxyConnectors == null || proxyConnectors.isEmpty() )
407 for ( ProxyConnector proxyConnector : proxyConnectors )
409 String remoteId = "remote-" + proxyConnector.getTargetRepoId();
410 IndexingContext context = indexer.getIndexingContexts().get( remoteId );
411 if ( context != null && context.isSearchable() )
420 public Collection<String> getAllGroupIds( String principal, List<String> selectedRepos )
421 throws RepositorySearchException
423 List<IndexingContext> indexContexts = getIndexingContexts( selectedRepos );
424 if ( indexContexts == null || indexContexts.isEmpty() )
426 return Collections.emptyList();
431 Set<String> allGroupIds = new HashSet<String>();
432 for ( IndexingContext indexingContext : indexContexts )
434 allGroupIds.addAll( indexingContext.getAllGroups() );
438 catch ( IOException e )
440 throw new RepositorySearchException( e.getMessage(), e );
444 protected List<? extends IndexCreator> getAllIndexCreators()
446 return mavenIndexerUtils.getAllIndexCreators();
450 private SearchResults convertToSearchResults( FlatSearchResponse response, SearchResultLimits limits,
451 List<? extends ArtifactInfoFiler> artifactInfoFilers,
452 List<String>selectedRepos, boolean includePoms)
453 throws RepositoryAdminException
455 SearchResults results = new SearchResults();
456 Set<ArtifactInfo> artifactInfos = response.getResults();
458 for ( ArtifactInfo artifactInfo : artifactInfos )
460 if ( StringUtils.equalsIgnoreCase( "pom", artifactInfo.fextension ) && !includePoms )
464 String id = SearchUtil.getHitId( artifactInfo.groupId, artifactInfo.artifactId, artifactInfo.classifier,
465 artifactInfo.packaging );
466 Map<String, SearchResultHit> hitsMap = results.getHitsMap();
468 if ( !applyArtifactInfoFilters( artifactInfo, artifactInfoFilers, hitsMap ) )
473 SearchResultHit hit = hitsMap.get( id );
476 if ( !hit.getVersions().contains( artifactInfo.version ) )
478 hit.addVersion( artifactInfo.version );
483 hit = new SearchResultHit();
484 hit.setArtifactId( artifactInfo.artifactId );
485 hit.setGroupId( artifactInfo.groupId );
486 hit.setRepositoryId( artifactInfo.repository );
487 hit.addVersion( artifactInfo.version );
488 hit.setBundleExportPackage( artifactInfo.bundleExportPackage );
489 hit.setBundleExportService( artifactInfo.bundleExportService );
490 hit.setBundleSymbolicName( artifactInfo.bundleSymbolicName );
491 hit.setBundleVersion( artifactInfo.bundleVersion );
492 hit.setBundleDescription( artifactInfo.bundleDescription );
493 hit.setBundleDocUrl( artifactInfo.bundleDocUrl );
494 hit.setBundleRequireBundle( artifactInfo.bundleRequireBundle );
495 hit.setBundleImportPackage( artifactInfo.bundleImportPackage );
496 hit.setBundleLicense( artifactInfo.bundleLicense );
497 hit.setBundleName( artifactInfo.bundleName );
498 hit.setContext( artifactInfo.context );
499 hit.setGoals( artifactInfo.goals );
500 hit.setPrefix( artifactInfo.prefix );
501 hit.setPackaging( artifactInfo.packaging );
502 hit.setClassifier( artifactInfo.classifier );
503 hit.setFileExtension( artifactInfo.fextension );
504 hit.setUrl( getBaseUrl( artifactInfo, selectedRepos ) );
507 results.addHit( id, hit );
510 results.setTotalHits( response.getTotalHitsCount() );
511 results.setTotalHitsMapSize( results.getHitsMap().values().size() );
512 results.setReturnedHitsCount( response.getReturnedHitsCount() );
513 results.setLimits( limits );
515 if ( limits == null || limits.getSelectedPage() == SearchResultLimits.ALL_PAGES )
521 return paginate( results );
526 * calculate baseUrl without the context and base Archiva Url
528 * @param artifactInfo
531 protected String getBaseUrl( ArtifactInfo artifactInfo, List<String> selectedRepos )
532 throws RepositoryAdminException
534 StringBuilder sb = new StringBuilder();
535 if ( StringUtils.startsWith( artifactInfo.context, "remote-" ) )
537 // it's a remote index result we search a managed which proxying this remote and on which
538 // current user has read karma
539 String managedRepoId =
540 getManagedRepoId( StringUtils.substringAfter( artifactInfo.context, "remote-" ), selectedRepos );
541 if ( managedRepoId != null )
543 sb.append( '/' ).append( managedRepoId );
548 sb.append( '/' ).append( artifactInfo.context );
551 sb.append( '/' ).append( StringUtils.replaceChars( artifactInfo.groupId, '.', '/' ) );
552 sb.append( '/' ).append( artifactInfo.artifactId );
553 sb.append( '/' ).append( artifactInfo.version );
554 sb.append( '/' ).append( artifactInfo.artifactId );
555 sb.append( '-' ).append( artifactInfo.version );
556 if ( StringUtils.isNotBlank( artifactInfo.classifier ) )
558 sb.append( '-' ).append( artifactInfo.classifier );
560 // maven-plugin packaging is a jar
561 if ( StringUtils.equals( "maven-plugin", artifactInfo.packaging ) )
567 sb.append( '.' ).append( artifactInfo.packaging );
570 return sb.toString();
574 * return a managed repo for a remote result
577 * @param selectedRepos
579 * @throws RepositoryAdminException
581 private String getManagedRepoId( String remoteRepo, List<String> selectedRepos )
582 throws RepositoryAdminException
584 Map<String, List<ProxyConnector>> proxyConnectorMap = proxyConnectorAdmin.getProxyConnectorAsMap();
585 if ( proxyConnectorMap == null || proxyConnectorMap.isEmpty() )
589 if ( selectedRepos != null && !selectedRepos.isEmpty() )
591 for ( Map.Entry<String, List<ProxyConnector>> entry : proxyConnectorMap.entrySet() )
593 if ( selectedRepos.contains( entry.getKey() ) )
595 for ( ProxyConnector proxyConnector : entry.getValue() )
597 if ( StringUtils.equals( remoteRepo, proxyConnector.getTargetRepoId() ) )
599 return proxyConnector.getSourceRepoId();
606 // we don't find in search selected repos so return the first one
607 for ( Map.Entry<String, List<ProxyConnector>> entry : proxyConnectorMap.entrySet() )
610 for ( ProxyConnector proxyConnector : entry.getValue() )
612 if ( StringUtils.equals( remoteRepo, proxyConnector.getTargetRepoId() ) )
614 return proxyConnector.getSourceRepoId();
622 private boolean applyArtifactInfoFilters( ArtifactInfo artifactInfo,
623 List<? extends ArtifactInfoFiler> artifactInfoFilers,
624 Map<String, SearchResultHit> currentResult )
626 if ( artifactInfoFilers == null || artifactInfoFilers.isEmpty() )
631 for ( ArtifactInfoFiler filter : artifactInfoFilers )
633 if ( !filter.addArtifactInResult( artifactInfo, currentResult ) )
641 protected SearchResults paginate( SearchResults results )
643 SearchResultLimits limits = results.getLimits();
644 SearchResults paginated = new SearchResults();
646 int fetchCount = limits.getPageSize();
647 int offset = ( limits.getSelectedPage() * limits.getPageSize() );
649 if ( fetchCount > results.getTotalHits() )
651 fetchCount = results.getTotalHits();
655 if ( offset < results.getTotalHits() )
657 // only process if the offset is within the hit count.
658 for ( int i = 0; i < fetchCount; i++ )
660 // Stop fetching if we are past the total # of available hits.
661 if ( offset + i >= results.getHits().size() )
666 SearchResultHit hit = results.getHits().get( ( offset + i ) );
669 String id = SearchUtil.getHitId( hit.getGroupId(), hit.getArtifactId(), hit.getClassifier(),
670 hit.getPackaging() );
671 paginated.addHit( id, hit );
679 paginated.setTotalHits( results.getTotalHits() );
680 paginated.setReturnedHitsCount( paginated.getHits().size() );
681 paginated.setTotalHitsMapSize( results.getTotalHitsMapSize() );
682 paginated.setLimits( limits );