Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

MavenRepositorySearch.java 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. package org.apache.archiva.indexer.search;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import org.apache.archiva.admin.model.RepositoryAdminException;
  21. import org.apache.archiva.admin.model.beans.ManagedRepository;
  22. import org.apache.archiva.admin.model.beans.ProxyConnector;
  23. import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
  24. import org.apache.archiva.admin.model.proxyconnector.ProxyConnectorAdmin;
  25. import org.apache.archiva.common.plexusbridge.MavenIndexerUtils;
  26. import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
  27. import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
  28. import org.apache.archiva.indexer.util.SearchUtil;
  29. import org.apache.commons.lang.StringUtils;
  30. import org.apache.lucene.search.BooleanClause.Occur;
  31. import org.apache.lucene.search.BooleanQuery;
  32. import org.apache.maven.index.ArtifactInfo;
  33. import org.apache.maven.index.FlatSearchRequest;
  34. import org.apache.maven.index.FlatSearchResponse;
  35. import org.apache.maven.index.MAVEN;
  36. import org.apache.maven.index.NexusIndexer;
  37. import org.apache.maven.index.OSGI;
  38. import org.apache.maven.index.context.IndexCreator;
  39. import org.apache.maven.index.context.IndexingContext;
  40. import org.apache.maven.index.expr.SourcedSearchExpression;
  41. import org.apache.maven.index.expr.UserInputSearchExpression;
  42. import org.slf4j.Logger;
  43. import org.slf4j.LoggerFactory;
  44. import org.springframework.stereotype.Service;
  45. import javax.inject.Inject;
  46. import java.io.IOException;
  47. import java.util.ArrayList;
  48. import java.util.Collection;
  49. import java.util.Collections;
  50. import java.util.HashSet;
  51. import java.util.List;
  52. import java.util.Map;
  53. import java.util.Set;
  54. /**
  55. * RepositorySearch implementation which uses the Maven Indexer for searching.
  56. */
  57. @Service("repositorySearch#maven")
  58. public class MavenRepositorySearch
  59. implements RepositorySearch
  60. {
  61. private Logger log = LoggerFactory.getLogger( getClass() );
  62. private NexusIndexer indexer;
  63. private ManagedRepositoryAdmin managedRepositoryAdmin;
  64. private ProxyConnectorAdmin proxyConnectorAdmin;
  65. private MavenIndexerUtils mavenIndexerUtils;
  66. protected MavenRepositorySearch()
  67. {
  68. // for test purpose
  69. }
  70. @Inject
  71. public MavenRepositorySearch( PlexusSisuBridge plexusSisuBridge, ManagedRepositoryAdmin managedRepositoryAdmin,
  72. MavenIndexerUtils mavenIndexerUtils, ProxyConnectorAdmin proxyConnectorAdmin )
  73. throws PlexusSisuBridgeException
  74. {
  75. this.indexer = plexusSisuBridge.lookup( NexusIndexer.class );
  76. this.managedRepositoryAdmin = managedRepositoryAdmin;
  77. this.mavenIndexerUtils = mavenIndexerUtils;
  78. this.proxyConnectorAdmin = proxyConnectorAdmin;
  79. }
  80. /**
  81. * @see RepositorySearch#search(String, List, String, SearchResultLimits, List)
  82. */
  83. public SearchResults search( String principal, List<String> selectedRepos, String term, SearchResultLimits limits,
  84. List<String> previousSearchTerms )
  85. throws RepositorySearchException
  86. {
  87. List<String> indexingContextIds = addIndexingContexts( selectedRepos );
  88. // since upgrade to nexus 2.0.0, query has changed from g:[QUERIED TERM]* to g:*[QUERIED TERM]*
  89. // resulting to more wildcard searches so we need to increase max clause count
  90. BooleanQuery.setMaxClauseCount( Integer.MAX_VALUE );
  91. BooleanQuery q = new BooleanQuery();
  92. if ( previousSearchTerms == null || previousSearchTerms.isEmpty() )
  93. {
  94. constructQuery( term, q );
  95. }
  96. else
  97. {
  98. for ( String previousTerm : previousSearchTerms )
  99. {
  100. BooleanQuery iQuery = new BooleanQuery();
  101. constructQuery( previousTerm, iQuery );
  102. q.add( iQuery, Occur.MUST );
  103. }
  104. BooleanQuery iQuery = new BooleanQuery();
  105. constructQuery( term, iQuery );
  106. q.add( iQuery, Occur.MUST );
  107. }
  108. // we retun only artifacts without classifier in quick search, olamy cannot find a way to say with this field empty
  109. // FIXME cannot find a way currently to setup this in constructQuery !!!
  110. return search( limits, q, indexingContextIds, NoClassifierArtifactInfoFilter.LIST, selectedRepos, true );
  111. }
  112. /**
  113. * @see RepositorySearch#search(String, SearchFields, SearchResultLimits)
  114. */
  115. public SearchResults search( String principal, SearchFields searchFields, SearchResultLimits limits )
  116. throws RepositorySearchException
  117. {
  118. if ( searchFields.getRepositories() == null )
  119. {
  120. throw new RepositorySearchException( "Repositories cannot be null." );
  121. }
  122. List<String> indexingContextIds = addIndexingContexts( searchFields.getRepositories() );
  123. // if no index found in the specified ones return an empty search result instead of doing a search on all index
  124. // olamy: IMHO doesn't make sense
  125. if ( !searchFields.getRepositories().isEmpty() && ( indexingContextIds == null
  126. || indexingContextIds.isEmpty() ) )
  127. {
  128. return new SearchResults();
  129. }
  130. BooleanQuery q = new BooleanQuery();
  131. if ( StringUtils.isNotBlank( searchFields.getGroupId() ) )
  132. {
  133. q.add( indexer.constructQuery( MAVEN.GROUP_ID, new UserInputSearchExpression( searchFields.getGroupId() ) ),
  134. Occur.MUST );
  135. }
  136. if ( StringUtils.isNotBlank( searchFields.getArtifactId() ) )
  137. {
  138. q.add( indexer.constructQuery( MAVEN.ARTIFACT_ID,
  139. new UserInputSearchExpression( searchFields.getArtifactId() ) ),
  140. Occur.MUST );
  141. }
  142. if ( StringUtils.isNotBlank( searchFields.getVersion() ) )
  143. {
  144. q.add( indexer.constructQuery( MAVEN.VERSION, new SourcedSearchExpression( searchFields.getVersion() ) ),
  145. Occur.MUST );
  146. }
  147. if ( StringUtils.isNotBlank( searchFields.getPackaging() ) )
  148. {
  149. q.add(
  150. indexer.constructQuery( MAVEN.PACKAGING, new UserInputSearchExpression( searchFields.getPackaging() ) ),
  151. Occur.MUST );
  152. }
  153. if ( StringUtils.isNotBlank( searchFields.getClassName() ) )
  154. {
  155. q.add( indexer.constructQuery( MAVEN.CLASSNAMES,
  156. new UserInputSearchExpression( searchFields.getClassName() ) ), Occur.MUST );
  157. }
  158. if ( StringUtils.isNotBlank( searchFields.getBundleSymbolicName() ) )
  159. {
  160. q.add( indexer.constructQuery( OSGI.SYMBOLIC_NAME,
  161. new UserInputSearchExpression( searchFields.getBundleSymbolicName() ) ),
  162. Occur.MUST );
  163. }
  164. if ( StringUtils.isNotBlank( searchFields.getBundleVersion() ) )
  165. {
  166. q.add( indexer.constructQuery( OSGI.VERSION,
  167. new UserInputSearchExpression( searchFields.getBundleVersion() ) ),
  168. Occur.MUST );
  169. }
  170. if ( StringUtils.isNotBlank( searchFields.getBundleExportPackage() ) )
  171. {
  172. q.add( indexer.constructQuery( OSGI.EXPORT_PACKAGE,
  173. new UserInputSearchExpression( searchFields.getBundleExportPackage() ) ),
  174. Occur.MUST );
  175. }
  176. if ( StringUtils.isNotBlank( searchFields.getBundleExportService() ) )
  177. {
  178. q.add( indexer.constructQuery( OSGI.EXPORT_SERVICE,
  179. new UserInputSearchExpression( searchFields.getBundleExportService() ) ),
  180. Occur.MUST );
  181. }
  182. if ( StringUtils.isNotBlank( searchFields.getBundleImportPackage() ) )
  183. {
  184. q.add( indexer.constructQuery( OSGI.IMPORT_PACKAGE,
  185. new UserInputSearchExpression( searchFields.getBundleImportPackage() ) ),
  186. Occur.MUST );
  187. }
  188. if ( StringUtils.isNotBlank( searchFields.getBundleName() ) )
  189. {
  190. q.add( indexer.constructQuery( OSGI.NAME, new UserInputSearchExpression( searchFields.getBundleName() ) ),
  191. Occur.MUST );
  192. }
  193. if ( StringUtils.isNotBlank( searchFields.getBundleImportPackage() ) )
  194. {
  195. q.add( indexer.constructQuery( OSGI.IMPORT_PACKAGE,
  196. new UserInputSearchExpression( searchFields.getBundleImportPackage() ) ),
  197. Occur.MUST );
  198. }
  199. if ( StringUtils.isNotBlank( searchFields.getBundleRequireBundle() ) )
  200. {
  201. q.add( indexer.constructQuery( OSGI.REQUIRE_BUNDLE,
  202. new UserInputSearchExpression( searchFields.getBundleRequireBundle() ) ),
  203. Occur.MUST );
  204. }
  205. if ( StringUtils.isNotBlank( searchFields.getClassifier() ) )
  206. {
  207. q.add( indexer.constructQuery( MAVEN.CLASSIFIER,
  208. new UserInputSearchExpression( searchFields.getClassifier() ) ),
  209. Occur.MUST );
  210. }
  211. if ( q.getClauses() == null || q.getClauses().length <= 0 )
  212. {
  213. throw new RepositorySearchException( "No search fields set." );
  214. }
  215. return search( limits, q, indexingContextIds, Collections.<ArtifactInfoFilter>emptyList(),
  216. searchFields.getRepositories(), searchFields.isIncludePomArtifacts() );
  217. }
  218. private SearchResults search( SearchResultLimits limits, BooleanQuery q, List<String> indexingContextIds,
  219. List<? extends ArtifactInfoFilter> filters, List<String> selectedRepos,
  220. boolean includePoms )
  221. throws RepositorySearchException
  222. {
  223. try
  224. {
  225. FlatSearchRequest request = new FlatSearchRequest( q );
  226. request.setContexts( getIndexingContexts( indexingContextIds ) );
  227. FlatSearchResponse response = indexer.searchFlat( request );
  228. if ( response == null || response.getTotalHits() == 0 )
  229. {
  230. SearchResults results = new SearchResults();
  231. results.setLimits( limits );
  232. return results;
  233. }
  234. return convertToSearchResults( response, limits, filters, selectedRepos, includePoms );
  235. }
  236. catch ( IOException e )
  237. {
  238. throw new RepositorySearchException( e.getMessage(), e );
  239. }
  240. catch ( RepositoryAdminException e )
  241. {
  242. throw new RepositorySearchException( e.getMessage(), e );
  243. }
  244. }
  245. private List<IndexingContext> getIndexingContexts( List<String> ids )
  246. {
  247. List<IndexingContext> contexts = new ArrayList<>( ids.size() );
  248. for ( String id : ids )
  249. {
  250. IndexingContext context = indexer.getIndexingContexts().get( id );
  251. if ( context != null )
  252. {
  253. contexts.add( context );
  254. }
  255. else
  256. {
  257. log.warn( "context with id {} not exists", id );
  258. }
  259. }
  260. return contexts;
  261. }
  262. private void constructQuery( String term, BooleanQuery q )
  263. {
  264. q.add( indexer.constructQuery( MAVEN.GROUP_ID, new UserInputSearchExpression( term ) ), Occur.SHOULD );
  265. q.add( indexer.constructQuery( MAVEN.ARTIFACT_ID, new UserInputSearchExpression( term ) ), Occur.SHOULD );
  266. q.add( indexer.constructQuery( MAVEN.VERSION, new UserInputSearchExpression( term ) ), Occur.SHOULD );
  267. q.add( indexer.constructQuery( MAVEN.PACKAGING, new UserInputSearchExpression( term ) ), Occur.SHOULD );
  268. q.add( indexer.constructQuery( MAVEN.CLASSNAMES, new UserInputSearchExpression( term ) ), Occur.SHOULD );
  269. //Query query =
  270. // new WildcardQuery( new Term( MAVEN.CLASSNAMES.getFieldName(), "*" ) );
  271. //q.add( query, Occur.MUST_NOT );
  272. // olamy IMHO we could set this option as at least one must match
  273. //q.setMinimumNumberShouldMatch( 1 );
  274. }
  275. /**
  276. * @param selectedRepos
  277. * @return indexing contextId used
  278. */
  279. private List<String> addIndexingContexts( List<String> selectedRepos )
  280. {
  281. Set<String> indexingContextIds = new HashSet<String>();
  282. for ( String repo : selectedRepos )
  283. {
  284. try
  285. {
  286. ManagedRepository repoConfig = managedRepositoryAdmin.getManagedRepository( repo );
  287. if ( repoConfig != null )
  288. {
  289. IndexingContext context = managedRepositoryAdmin.createIndexContext( repoConfig );
  290. if ( context.isSearchable() )
  291. {
  292. indexingContextIds.addAll( getRemoteIndexingContextIds( repo ) );
  293. indexingContextIds.add( context.getId() );
  294. }
  295. else
  296. {
  297. log.warn( "indexingContext with id {} not searchable", repoConfig.getId() );
  298. }
  299. }
  300. else
  301. {
  302. log.warn( "Repository '{}' not found in configuration.", repo );
  303. }
  304. }
  305. catch ( RepositoryAdminException e )
  306. {
  307. log.warn( "RepositoryAdminException occured while accessing index of repository '{}' : {}", repo,
  308. e.getMessage() );
  309. continue;
  310. }
  311. }
  312. return new ArrayList<>( indexingContextIds );
  313. }
  314. public Set<String> getRemoteIndexingContextIds( String managedRepoId )
  315. throws RepositoryAdminException
  316. {
  317. Set<String> ids = new HashSet<String>();
  318. List<ProxyConnector> proxyConnectors = proxyConnectorAdmin.getProxyConnectorAsMap().get( managedRepoId );
  319. if ( proxyConnectors == null || proxyConnectors.isEmpty() )
  320. {
  321. return ids;
  322. }
  323. for ( ProxyConnector proxyConnector : proxyConnectors )
  324. {
  325. String remoteId = "remote-" + proxyConnector.getTargetRepoId();
  326. IndexingContext context = indexer.getIndexingContexts().get( remoteId );
  327. if ( context != null && context.isSearchable() )
  328. {
  329. ids.add( remoteId );
  330. }
  331. }
  332. return ids;
  333. }
  334. public Collection<String> getAllGroupIds( String principal, List<String> selectedRepos )
  335. throws RepositorySearchException
  336. {
  337. List<IndexingContext> indexContexts = getIndexingContexts( selectedRepos );
  338. if ( indexContexts == null || indexContexts.isEmpty() )
  339. {
  340. return Collections.emptyList();
  341. }
  342. try
  343. {
  344. Set<String> allGroupIds = new HashSet<String>();
  345. for ( IndexingContext indexingContext : indexContexts )
  346. {
  347. allGroupIds.addAll( indexingContext.getAllGroups() );
  348. }
  349. return allGroupIds;
  350. }
  351. catch ( IOException e )
  352. {
  353. throw new RepositorySearchException( e.getMessage(), e );
  354. }
  355. }
  356. protected List<? extends IndexCreator> getAllIndexCreators()
  357. {
  358. return mavenIndexerUtils.getAllIndexCreators();
  359. }
  360. private SearchResults convertToSearchResults( FlatSearchResponse response, SearchResultLimits limits,
  361. List<? extends ArtifactInfoFilter> artifactInfoFilters,
  362. List<String> selectedRepos, boolean includePoms )
  363. throws RepositoryAdminException
  364. {
  365. SearchResults results = new SearchResults();
  366. Set<ArtifactInfo> artifactInfos = response.getResults();
  367. for ( ArtifactInfo artifactInfo : artifactInfos )
  368. {
  369. if ( StringUtils.equalsIgnoreCase( "pom", artifactInfo.fextension ) && !includePoms )
  370. {
  371. continue;
  372. }
  373. String id = SearchUtil.getHitId( artifactInfo.groupId, artifactInfo.artifactId, artifactInfo.classifier,
  374. artifactInfo.packaging );
  375. Map<String, SearchResultHit> hitsMap = results.getHitsMap();
  376. if ( !applyArtifactInfoFilters( artifactInfo, artifactInfoFilters, hitsMap ) )
  377. {
  378. continue;
  379. }
  380. SearchResultHit hit = hitsMap.get( id );
  381. if ( hit != null )
  382. {
  383. if ( !hit.getVersions().contains( artifactInfo.version ) )
  384. {
  385. hit.addVersion( artifactInfo.version );
  386. }
  387. }
  388. else
  389. {
  390. hit = new SearchResultHit();
  391. hit.setArtifactId( artifactInfo.artifactId );
  392. hit.setGroupId( artifactInfo.groupId );
  393. hit.setRepositoryId( artifactInfo.repository );
  394. hit.addVersion( artifactInfo.version );
  395. hit.setBundleExportPackage( artifactInfo.bundleExportPackage );
  396. hit.setBundleExportService( artifactInfo.bundleExportService );
  397. hit.setBundleSymbolicName( artifactInfo.bundleSymbolicName );
  398. hit.setBundleVersion( artifactInfo.bundleVersion );
  399. hit.setBundleDescription( artifactInfo.bundleDescription );
  400. hit.setBundleDocUrl( artifactInfo.bundleDocUrl );
  401. hit.setBundleRequireBundle( artifactInfo.bundleRequireBundle );
  402. hit.setBundleImportPackage( artifactInfo.bundleImportPackage );
  403. hit.setBundleLicense( artifactInfo.bundleLicense );
  404. hit.setBundleName( artifactInfo.bundleName );
  405. hit.setContext( artifactInfo.context );
  406. hit.setGoals( artifactInfo.goals );
  407. hit.setPrefix( artifactInfo.prefix );
  408. hit.setPackaging( artifactInfo.packaging );
  409. hit.setClassifier( artifactInfo.classifier );
  410. hit.setFileExtension( artifactInfo.fextension );
  411. hit.setUrl( getBaseUrl( artifactInfo, selectedRepos ) );
  412. }
  413. results.addHit( id, hit );
  414. }
  415. results.setTotalHits( response.getTotalHitsCount() );
  416. results.setTotalHitsMapSize( results.getHitsMap().values().size() );
  417. results.setReturnedHitsCount( response.getReturnedHitsCount() );
  418. results.setLimits( limits );
  419. if ( limits == null || limits.getSelectedPage() == SearchResultLimits.ALL_PAGES )
  420. {
  421. return results;
  422. }
  423. else
  424. {
  425. return paginate( results );
  426. }
  427. }
  428. /**
  429. * calculate baseUrl without the context and base Archiva Url
  430. *
  431. * @param artifactInfo
  432. * @return
  433. */
  434. protected String getBaseUrl( ArtifactInfo artifactInfo, List<String> selectedRepos )
  435. throws RepositoryAdminException
  436. {
  437. StringBuilder sb = new StringBuilder();
  438. if ( StringUtils.startsWith( artifactInfo.context, "remote-" ) )
  439. {
  440. // it's a remote index result we search a managed which proxying this remote and on which
  441. // current user has read karma
  442. String managedRepoId =
  443. getManagedRepoId( StringUtils.substringAfter( artifactInfo.context, "remote-" ), selectedRepos );
  444. if ( managedRepoId != null )
  445. {
  446. sb.append( '/' ).append( managedRepoId );
  447. artifactInfo.context = managedRepoId;
  448. }
  449. }
  450. else
  451. {
  452. sb.append( '/' ).append( artifactInfo.context );
  453. }
  454. sb.append( '/' ).append( StringUtils.replaceChars( artifactInfo.groupId, '.', '/' ) );
  455. sb.append( '/' ).append( artifactInfo.artifactId );
  456. sb.append( '/' ).append( artifactInfo.version );
  457. sb.append( '/' ).append( artifactInfo.artifactId );
  458. sb.append( '-' ).append( artifactInfo.version );
  459. if ( StringUtils.isNotBlank( artifactInfo.classifier ) )
  460. {
  461. sb.append( '-' ).append( artifactInfo.classifier );
  462. }
  463. // maven-plugin packaging is a jar
  464. if ( StringUtils.equals( "maven-plugin", artifactInfo.packaging ) )
  465. {
  466. sb.append( "jar" );
  467. }
  468. else
  469. {
  470. sb.append( '.' ).append( artifactInfo.packaging );
  471. }
  472. return sb.toString();
  473. }
  474. /**
  475. * return a managed repo for a remote result
  476. *
  477. * @param remoteRepo
  478. * @param selectedRepos
  479. * @return
  480. * @throws RepositoryAdminException
  481. */
  482. private String getManagedRepoId( String remoteRepo, List<String> selectedRepos )
  483. throws RepositoryAdminException
  484. {
  485. Map<String, List<ProxyConnector>> proxyConnectorMap = proxyConnectorAdmin.getProxyConnectorAsMap();
  486. if ( proxyConnectorMap == null || proxyConnectorMap.isEmpty() )
  487. {
  488. return null;
  489. }
  490. if ( selectedRepos != null && !selectedRepos.isEmpty() )
  491. {
  492. for ( Map.Entry<String, List<ProxyConnector>> entry : proxyConnectorMap.entrySet() )
  493. {
  494. if ( selectedRepos.contains( entry.getKey() ) )
  495. {
  496. for ( ProxyConnector proxyConnector : entry.getValue() )
  497. {
  498. if ( StringUtils.equals( remoteRepo, proxyConnector.getTargetRepoId() ) )
  499. {
  500. return proxyConnector.getSourceRepoId();
  501. }
  502. }
  503. }
  504. }
  505. }
  506. // we don't find in search selected repos so return the first one
  507. for ( Map.Entry<String, List<ProxyConnector>> entry : proxyConnectorMap.entrySet() )
  508. {
  509. for ( ProxyConnector proxyConnector : entry.getValue() )
  510. {
  511. if ( StringUtils.equals( remoteRepo, proxyConnector.getTargetRepoId() ) )
  512. {
  513. return proxyConnector.getSourceRepoId();
  514. }
  515. }
  516. }
  517. return null;
  518. }
  519. private boolean applyArtifactInfoFilters( ArtifactInfo artifactInfo,
  520. List<? extends ArtifactInfoFilter> artifactInfoFilters,
  521. Map<String, SearchResultHit> currentResult )
  522. {
  523. if ( artifactInfoFilters == null || artifactInfoFilters.isEmpty() )
  524. {
  525. return true;
  526. }
  527. for ( ArtifactInfoFilter filter : artifactInfoFilters )
  528. {
  529. if ( !filter.addArtifactInResult( artifactInfo, currentResult ) )
  530. {
  531. return false;
  532. }
  533. }
  534. return true;
  535. }
  536. protected SearchResults paginate( SearchResults results )
  537. {
  538. SearchResultLimits limits = results.getLimits();
  539. SearchResults paginated = new SearchResults();
  540. int fetchCount = limits.getPageSize();
  541. int offset = ( limits.getSelectedPage() * limits.getPageSize() );
  542. if ( fetchCount > results.getTotalHits() )
  543. {
  544. fetchCount = results.getTotalHits();
  545. }
  546. // Goto offset.
  547. if ( offset < results.getTotalHits() )
  548. {
  549. // only process if the offset is within the hit count.
  550. for ( int i = 0; i < fetchCount; i++ )
  551. {
  552. // Stop fetching if we are past the total # of available hits.
  553. if ( offset + i >= results.getHits().size() )
  554. {
  555. break;
  556. }
  557. SearchResultHit hit = results.getHits().get( ( offset + i ) );
  558. if ( hit != null )
  559. {
  560. String id = SearchUtil.getHitId( hit.getGroupId(), hit.getArtifactId(), hit.getClassifier(),
  561. hit.getPackaging() );
  562. paginated.addHit( id, hit );
  563. }
  564. else
  565. {
  566. break;
  567. }
  568. }
  569. }
  570. paginated.setTotalHits( results.getTotalHits() );
  571. paginated.setReturnedHitsCount( paginated.getHits().size() );
  572. paginated.setTotalHitsMapSize( results.getTotalHitsMapSize() );
  573. paginated.setLimits( limits );
  574. return paginated;
  575. }
  576. }