]> source.dussan.org Git - archiva.git/blob
81b76ee5fc1d464d0e6efc6c76f865002500c19f
[archiva.git] /
1 package org.apache.archiva.indexer.search;
2
3 /*
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
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
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;
47
48 import javax.inject.Inject;
49 import java.io.File;
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;
56 import java.util.Map;
57 import java.util.Set;
58
59 /**
60  * RepositorySearch implementation which uses the Nexus Indexer for searching.
61  */
62 @Service( "nexusSearch" )
63 public class NexusRepositorySearch
64     implements RepositorySearch
65 {
66     private Logger log = LoggerFactory.getLogger( getClass() );
67
68     private NexusIndexer indexer;
69
70     private ManagedRepositoryAdmin managedRepositoryAdmin;
71
72     private ProxyConnectorAdmin proxyConnectorAdmin;
73
74     private MavenIndexerUtils mavenIndexerUtils;
75
76     protected NexusRepositorySearch()
77     {
78         // for test purpose
79     }
80
81     @Inject
82     public NexusRepositorySearch( PlexusSisuBridge plexusSisuBridge, ManagedRepositoryAdmin managedRepositoryAdmin,
83                                   MavenIndexerUtils mavenIndexerUtils, ProxyConnectorAdmin proxyConnectorAdmin )
84         throws PlexusSisuBridgeException
85     {
86         this.indexer = plexusSisuBridge.lookup( NexusIndexer.class );
87         this.managedRepositoryAdmin = managedRepositoryAdmin;
88         this.mavenIndexerUtils = mavenIndexerUtils;
89         this.proxyConnectorAdmin = proxyConnectorAdmin;
90     }
91
92     /**
93      * @see RepositorySearch#search(String, List, String, SearchResultLimits, List)
94      */
95     public SearchResults search( String principal, List<String> selectedRepos, String term, SearchResultLimits limits,
96                                  List<String> previousSearchTerms )
97         throws RepositorySearchException
98     {
99         List<String> indexingContextIds = addIndexingContexts( selectedRepos );
100
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();
105
106         if ( previousSearchTerms == null || previousSearchTerms.isEmpty() )
107         {
108             constructQuery( term, q );
109         }
110         else
111         {
112             for ( String previousTerm : previousSearchTerms )
113             {
114                 BooleanQuery iQuery = new BooleanQuery();
115                 constructQuery( previousTerm, iQuery );
116
117                 q.add( iQuery, Occur.MUST );
118             }
119
120             BooleanQuery iQuery = new BooleanQuery();
121             constructQuery( term, iQuery );
122             q.add( iQuery, Occur.MUST );
123         }
124
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 );
128
129     }
130
131     /**
132      * @see RepositorySearch#search(String, SearchFields, SearchResultLimits)
133      */
134     public SearchResults search( String principal, SearchFields searchFields, SearchResultLimits limits )
135         throws RepositorySearchException
136     {
137         if ( searchFields.getRepositories() == null )
138         {
139             throw new RepositorySearchException( "Repositories cannot be null." );
140         }
141
142         List<String> indexingContextIds = addIndexingContexts( searchFields.getRepositories() );
143
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() ) )
148         {
149             return new SearchResults();
150         }
151
152         BooleanQuery q = new BooleanQuery();
153         if ( StringUtils.isNotBlank( searchFields.getGroupId() ) )
154         {
155             q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( searchFields.getGroupId() ) ),
156                    Occur.MUST );
157         }
158
159         if ( StringUtils.isNotBlank( searchFields.getArtifactId() ) )
160         {
161             q.add(
162                 indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( searchFields.getArtifactId() ) ),
163                 Occur.MUST );
164         }
165
166         if ( StringUtils.isNotBlank( searchFields.getVersion() ) )
167         {
168             q.add( indexer.constructQuery( MAVEN.VERSION, new StringSearchExpression( searchFields.getVersion() ) ),
169                    Occur.MUST );
170         }
171
172         if ( StringUtils.isNotBlank( searchFields.getPackaging() ) )
173         {
174             q.add( indexer.constructQuery( MAVEN.PACKAGING, new StringSearchExpression( searchFields.getPackaging() ) ),
175                    Occur.MUST );
176         }
177
178         if ( StringUtils.isNotBlank( searchFields.getClassName() ) )
179         {
180             q.add(
181                 indexer.constructQuery( MAVEN.CLASSNAMES, new StringSearchExpression( searchFields.getClassName() ) ),
182                 Occur.MUST );
183         }
184
185         if ( StringUtils.isNotBlank( searchFields.getBundleSymbolicName() ) )
186         {
187             q.add( indexer.constructQuery( OSGI.SYMBOLIC_NAME,
188                                            new StringSearchExpression( searchFields.getBundleSymbolicName() ) ),
189                    Occur.MUST );
190         }
191
192         if ( StringUtils.isNotBlank( searchFields.getBundleVersion() ) )
193         {
194             q.add(
195                 indexer.constructQuery( OSGI.VERSION, new StringSearchExpression( searchFields.getBundleVersion() ) ),
196                 Occur.MUST );
197         }
198
199         if ( StringUtils.isNotBlank( searchFields.getBundleExportPackage() ) )
200         {
201             q.add( indexer.constructQuery( OSGI.EXPORT_PACKAGE,
202                                            new StringSearchExpression( searchFields.getBundleExportPackage() ) ),
203                    Occur.MUST );
204         }
205
206         if ( StringUtils.isNotBlank( searchFields.getBundleExportService() ) )
207         {
208             q.add( indexer.constructQuery( OSGI.EXPORT_SERVICE,
209                                            new StringSearchExpression( searchFields.getBundleExportService() ) ),
210                    Occur.MUST );
211         }
212
213         if ( StringUtils.isNotBlank( searchFields.getBundleImportPackage() ) )
214         {
215             q.add( indexer.constructQuery( OSGI.IMPORT_PACKAGE,
216                                            new StringSearchExpression( searchFields.getBundleImportPackage() ) ),
217                    Occur.MUST );
218         }
219
220         if ( StringUtils.isNotBlank( searchFields.getBundleName() ) )
221         {
222             q.add( indexer.constructQuery( OSGI.NAME, new StringSearchExpression( searchFields.getBundleName() ) ),
223                    Occur.MUST );
224         }
225
226         if ( StringUtils.isNotBlank( searchFields.getClassifier() ) )
227         {
228             q.add(
229                 indexer.constructQuery( MAVEN.CLASSIFIER, new StringSearchExpression( searchFields.getClassifier() ) ),
230                 Occur.MUST );
231         }
232
233         if ( q.getClauses() == null || q.getClauses().length <= 0 )
234         {
235             throw new RepositorySearchException( "No search fields set." );
236         }
237
238         return search( limits, q, indexingContextIds, Collections.<ArtifactInfoFiler>emptyList(),
239                        searchFields.getRepositories(), searchFields.isIncludePomArtifacts() );
240     }
241
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
246     {
247
248         try
249         {
250             FlatSearchRequest request = new FlatSearchRequest( q );
251             request.setContexts( getIndexingContexts( indexingContextIds ) );
252
253             FlatSearchResponse response = indexer.searchFlat( request );
254
255             if ( response == null || response.getTotalHits() == 0 )
256             {
257                 SearchResults results = new SearchResults();
258                 results.setLimits( limits );
259                 return results;
260             }
261
262             return convertToSearchResults( response, limits, filters, selectedRepos, includePoms );
263         }
264         catch ( IOException e )
265         {
266             throw new RepositorySearchException( e.getMessage(), e );
267         }
268         catch ( RepositoryAdminException e )
269         {
270             throw new RepositorySearchException( e.getMessage(), e );
271         }
272
273     }
274
275     private List<IndexingContext> getIndexingContexts( List<String> ids )
276     {
277         List<IndexingContext> contexts = new ArrayList<IndexingContext>( ids.size() );
278
279         for ( String id : ids )
280         {
281             IndexingContext context = indexer.getIndexingContexts().get( id );
282             if ( context != null )
283             {
284                 contexts.add( context );
285             }
286             else
287             {
288                 log.warn( "context with id {} not exists", id );
289             }
290         }
291
292         return contexts;
293     }
294
295     private void constructQuery( String term, BooleanQuery q )
296     {
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 );
302
303         //Query query =
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 );
308     }
309
310
311     /**
312      * @param selectedRepos
313      * @return indexing contextId used
314      */
315     private List<String> addIndexingContexts( List<String> selectedRepos )
316     {
317         Set<String> indexingContextIds = new HashSet<String>();
318         for ( String repo : selectedRepos )
319         {
320             try
321             {
322                 ManagedRepository repoConfig = managedRepositoryAdmin.getManagedRepository( repo );
323
324                 if ( repoConfig != null )
325                 {
326                     String indexDir = repoConfig.getIndexDirectory();
327                     File indexDirectory = null;
328                     if ( indexDir != null && !"".equals( indexDir ) )
329                     {
330                         indexDirectory = new File( repoConfig.getIndexDirectory() );
331                         if ( !indexDirectory.isAbsolute() )
332                         {
333                             indexDirectory = new File( repoConfig.getLocation(), repoConfig.getIndexDirectory() );
334                         }
335                     }
336                     else
337                     {
338                         indexDirectory = new File( repoConfig.getLocation(), ".indexer" );
339                     }
340
341                     IndexingContext context = indexer.getIndexingContexts().get( repoConfig.getId() );
342                     if ( context != null )
343                     {
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 ) );
350                         continue;
351                     }
352
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() )
358                     {
359                         indexingContextIds.addAll( getRemoteIndexingContextIds( repo ) );
360                         indexingContextIds.add( context.getId() );
361                     }
362                     else
363                     {
364                         log.warn( "indexingContext with id {} not searchable", repoConfig.getId() );
365                     }
366
367                 }
368                 else
369                 {
370                     log.warn( "Repository '" + repo + "' not found in configuration." );
371                 }
372             }
373             catch ( UnsupportedExistingLuceneIndexException e )
374             {
375                 log.warn( "Error accessing index of repository '" + repo + "' : " + e.getMessage() );
376                 continue;
377             }
378             catch ( IOException e )
379             {
380                 log.warn( "IO error occured while accessing index of repository '" + repo + "' : " + e.getMessage() );
381                 continue;
382             }
383             catch ( RepositoryAdminException e )
384             {
385                 log.warn( "RepositoryAdminException occured while accessing index of repository '" + repo + "' : "
386                               + e.getMessage() );
387                 continue;
388             }
389         }
390
391         return new ArrayList<String>( indexingContextIds );
392     }
393
394
395     public Set<String> getRemoteIndexingContextIds( String managedRepoId )
396         throws RepositoryAdminException
397     {
398         Set<String> ids = new HashSet<String>();
399
400         List<ProxyConnector> proxyConnectors = proxyConnectorAdmin.getProxyConnectorAsMap().get( managedRepoId );
401
402         if ( proxyConnectors == null || proxyConnectors.isEmpty() )
403         {
404             return ids;
405         }
406
407         for ( ProxyConnector proxyConnector : proxyConnectors )
408         {
409             String remoteId = "remote-" + proxyConnector.getTargetRepoId();
410             IndexingContext context = indexer.getIndexingContexts().get( remoteId );
411             if ( context != null && context.isSearchable() )
412             {
413                 ids.add( remoteId );
414             }
415         }
416
417         return ids;
418     }
419
420     public Collection<String> getAllGroupIds( String principal, List<String> selectedRepos )
421         throws RepositorySearchException
422     {
423         List<IndexingContext> indexContexts = getIndexingContexts( selectedRepos );
424         if ( indexContexts == null || indexContexts.isEmpty() )
425         {
426             return Collections.emptyList();
427         }
428
429         try
430         {
431             Set<String> allGroupIds = new HashSet<String>();
432             for ( IndexingContext indexingContext : indexContexts )
433             {
434                 allGroupIds.addAll( indexingContext.getAllGroups() );
435             }
436             return allGroupIds;
437         }
438         catch ( IOException e )
439         {
440             throw new RepositorySearchException( e.getMessage(), e );
441         }
442     }
443
444     protected List<? extends IndexCreator> getAllIndexCreators()
445     {
446         return mavenIndexerUtils.getAllIndexCreators();
447     }
448
449
450     private SearchResults convertToSearchResults( FlatSearchResponse response, SearchResultLimits limits,
451                                                   List<? extends ArtifactInfoFiler> artifactInfoFilers,
452                                                   List<String>selectedRepos, boolean includePoms)
453         throws RepositoryAdminException
454     {
455         SearchResults results = new SearchResults();
456         Set<ArtifactInfo> artifactInfos = response.getResults();
457
458         for ( ArtifactInfo artifactInfo : artifactInfos )
459         {
460             if ( StringUtils.equalsIgnoreCase( "pom", artifactInfo.fextension ) && !includePoms )
461             {
462                 continue;
463             }
464             String id = SearchUtil.getHitId( artifactInfo.groupId, artifactInfo.artifactId, artifactInfo.classifier,
465                                              artifactInfo.packaging );
466             Map<String, SearchResultHit> hitsMap = results.getHitsMap();
467
468             if ( !applyArtifactInfoFilters( artifactInfo, artifactInfoFilers, hitsMap ) )
469             {
470                 continue;
471             }
472
473             SearchResultHit hit = hitsMap.get( id );
474             if ( hit != null )
475             {
476                 if ( !hit.getVersions().contains( artifactInfo.version ) )
477                 {
478                     hit.addVersion( artifactInfo.version );
479                 }
480             }
481             else
482             {
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 ) );
505             }
506
507             results.addHit( id, hit );
508         }
509
510         results.setTotalHits( response.getTotalHitsCount() );
511         results.setTotalHitsMapSize( results.getHitsMap().values().size() );
512         results.setReturnedHitsCount( response.getReturnedHitsCount() );
513         results.setLimits( limits );
514
515         if ( limits == null || limits.getSelectedPage() == SearchResultLimits.ALL_PAGES )
516         {
517             return results;
518         }
519         else
520         {
521             return paginate( results );
522         }
523     }
524
525     /**
526      * calculate baseUrl without the context and base Archiva Url
527      *
528      * @param artifactInfo
529      * @return
530      */
531     protected String getBaseUrl( ArtifactInfo artifactInfo, List<String> selectedRepos )
532         throws RepositoryAdminException
533     {
534         StringBuilder sb = new StringBuilder();
535         if ( StringUtils.startsWith( artifactInfo.context, "remote-" ) )
536         {
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 )
542             {
543                 sb.append( '/' ).append( managedRepoId );
544             }
545         }
546         else
547         {
548             sb.append( '/' ).append( artifactInfo.context );
549         }
550
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 ) )
557         {
558             sb.append( '-' ).append( artifactInfo.classifier );
559         }
560         // maven-plugin packaging is a jar
561         if ( StringUtils.equals( "maven-plugin", artifactInfo.packaging ) )
562         {
563             sb.append( "jar" );
564         }
565         else
566         {
567             sb.append( '.' ).append( artifactInfo.packaging );
568         }
569
570         return sb.toString();
571     }
572
573     /**
574      * return a managed repo for a remote result
575      *
576      * @param remoteRepo
577      * @param selectedRepos
578      * @return
579      * @throws RepositoryAdminException
580      */
581     private String getManagedRepoId( String remoteRepo, List<String> selectedRepos )
582         throws RepositoryAdminException
583     {
584         Map<String, List<ProxyConnector>> proxyConnectorMap = proxyConnectorAdmin.getProxyConnectorAsMap();
585         if ( proxyConnectorMap == null || proxyConnectorMap.isEmpty() )
586         {
587             return null;
588         }
589         if ( selectedRepos != null && !selectedRepos.isEmpty() )
590         {
591             for ( Map.Entry<String, List<ProxyConnector>> entry : proxyConnectorMap.entrySet() )
592             {
593                 if ( selectedRepos.contains( entry.getKey() ) )
594                 {
595                     for ( ProxyConnector proxyConnector : entry.getValue() )
596                     {
597                         if ( StringUtils.equals( remoteRepo, proxyConnector.getTargetRepoId() ) )
598                         {
599                             return proxyConnector.getSourceRepoId();
600                         }
601                     }
602                 }
603             }
604         }
605
606         // we don't find in search selected repos so return the first one
607         for ( Map.Entry<String, List<ProxyConnector>> entry : proxyConnectorMap.entrySet() )
608         {
609
610             for ( ProxyConnector proxyConnector : entry.getValue() )
611             {
612                 if ( StringUtils.equals( remoteRepo, proxyConnector.getTargetRepoId() ) )
613                 {
614                     return proxyConnector.getSourceRepoId();
615                 }
616             }
617
618         }
619         return null;
620     }
621
622     private boolean applyArtifactInfoFilters( ArtifactInfo artifactInfo,
623                                               List<? extends ArtifactInfoFiler> artifactInfoFilers,
624                                               Map<String, SearchResultHit> currentResult )
625     {
626         if ( artifactInfoFilers == null || artifactInfoFilers.isEmpty() )
627         {
628             return true;
629         }
630
631         for ( ArtifactInfoFiler filter : artifactInfoFilers )
632         {
633             if ( !filter.addArtifactInResult( artifactInfo, currentResult ) )
634             {
635                 return false;
636             }
637         }
638         return true;
639     }
640
641     protected SearchResults paginate( SearchResults results )
642     {
643         SearchResultLimits limits = results.getLimits();
644         SearchResults paginated = new SearchResults();
645
646         int fetchCount = limits.getPageSize();
647         int offset = ( limits.getSelectedPage() * limits.getPageSize() );
648
649         if ( fetchCount > results.getTotalHits() )
650         {
651             fetchCount = results.getTotalHits();
652         }
653
654         // Goto offset.
655         if ( offset < results.getTotalHits() )
656         {
657             // only process if the offset is within the hit count.
658             for ( int i = 0; i < fetchCount; i++ )
659             {
660                 // Stop fetching if we are past the total # of available hits.
661                 if ( offset + i >= results.getHits().size() )
662                 {
663                     break;
664                 }
665
666                 SearchResultHit hit = results.getHits().get( ( offset + i ) );
667                 if ( hit != null )
668                 {
669                     String id = SearchUtil.getHitId( hit.getGroupId(), hit.getArtifactId(), hit.getClassifier(),
670                                                      hit.getPackaging() );
671                     paginated.addHit( id, hit );
672                 }
673                 else
674                 {
675                     break;
676                 }
677             }
678         }
679         paginated.setTotalHits( results.getTotalHits() );
680         paginated.setReturnedHitsCount( paginated.getHits().size() );
681         paginated.setTotalHitsMapSize( results.getTotalHitsMapSize() );
682         paginated.setLimits( limits );
683
684         return paginated;
685     }
686
687
688 }