您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

DefaultSearchService.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. package org.apache.archiva.rest.services;
  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.indexer.search.RepositorySearch;
  21. import org.apache.archiva.indexer.search.RepositorySearchException;
  22. import org.apache.archiva.indexer.search.SearchFields;
  23. import org.apache.archiva.indexer.search.SearchResultHit;
  24. import org.apache.archiva.indexer.search.SearchResultLimits;
  25. import org.apache.archiva.indexer.search.SearchResults;
  26. import org.apache.archiva.maven2.model.Artifact;
  27. import org.apache.archiva.rest.api.model.Dependency;
  28. import org.apache.archiva.rest.api.model.GroupIdList;
  29. import org.apache.archiva.rest.api.model.SearchRequest;
  30. import org.apache.archiva.rest.api.model.StringList;
  31. import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
  32. import org.apache.archiva.rest.api.services.SearchService;
  33. import org.apache.commons.collections.ListUtils;
  34. import org.apache.commons.lang.StringUtils;
  35. import org.springframework.stereotype.Service;
  36. import javax.inject.Inject;
  37. import java.util.ArrayList;
  38. import java.util.Collections;
  39. import java.util.List;
  40. /**
  41. * @author Olivier Lamy
  42. */
  43. @Service( "searchService#rest" )
  44. public class DefaultSearchService
  45. extends AbstractRestService
  46. implements SearchService
  47. {
  48. @Inject
  49. private RepositorySearch repositorySearch;
  50. public List<Artifact> quickSearch( String queryString )
  51. throws ArchivaRestServiceException
  52. {
  53. if ( StringUtils.isBlank( queryString ) )
  54. {
  55. return Collections.emptyList();
  56. }
  57. SearchResultLimits limits = new SearchResultLimits( 0 );
  58. try
  59. {
  60. SearchResults searchResults =
  61. repositorySearch.search( getPrincipal(), getObservableRepos(), queryString, limits,
  62. Collections.<String>emptyList() );
  63. return getArtifacts( searchResults );
  64. }
  65. catch ( RepositorySearchException e )
  66. {
  67. log.error( e.getMessage(), e );
  68. throw new ArchivaRestServiceException( e.getMessage(), e );
  69. }
  70. }
  71. public List<Artifact> quickSearchWithRepositories( SearchRequest searchRequest )
  72. throws ArchivaRestServiceException
  73. {
  74. String queryString = searchRequest.getQueryTerms();
  75. if ( StringUtils.isBlank( queryString ) )
  76. {
  77. return Collections.emptyList();
  78. }
  79. List<String> repositories = searchRequest.getRepositories();
  80. if ( repositories == null || repositories.isEmpty() )
  81. {
  82. repositories = getObservableRepos();
  83. }
  84. SearchResultLimits limits =
  85. new SearchResultLimits( searchRequest.getPageSize(), searchRequest.getSelectedPage() );
  86. try
  87. {
  88. SearchResults searchResults = repositorySearch.search( getPrincipal(), repositories, queryString, limits,
  89. Collections.<String>emptyList() );
  90. return getArtifacts( searchResults );
  91. }
  92. catch ( RepositorySearchException e )
  93. {
  94. log.error( e.getMessage(), e );
  95. throw new ArchivaRestServiceException( e.getMessage(), e );
  96. }
  97. }
  98. public List<Artifact> getArtifactVersions( String groupId, String artifactId, String packaging )
  99. throws ArchivaRestServiceException
  100. {
  101. if ( StringUtils.isBlank( groupId ) || StringUtils.isBlank( artifactId ) )
  102. {
  103. return Collections.emptyList();
  104. }
  105. SearchFields searchField = new SearchFields();
  106. searchField.setGroupId( groupId );
  107. searchField.setArtifactId( artifactId );
  108. searchField.setPackaging( StringUtils.isBlank( packaging ) ? "jar" : packaging );
  109. searchField.setRepositories( getObservableRepos() );
  110. try
  111. {
  112. SearchResults searchResults = repositorySearch.search( getPrincipal(), searchField, null );
  113. return getArtifacts( searchResults );
  114. }
  115. catch ( RepositorySearchException e )
  116. {
  117. log.error( e.getMessage(), e );
  118. throw new ArchivaRestServiceException( e.getMessage(), e );
  119. }
  120. }
  121. public List<Artifact> searchArtifacts( SearchRequest searchRequest )
  122. throws ArchivaRestServiceException
  123. {
  124. if ( searchRequest == null )
  125. {
  126. return Collections.emptyList();
  127. }
  128. SearchFields searchField = getModelMapper().map( searchRequest, SearchFields.class );
  129. SearchResultLimits limits = new SearchResultLimits( 0 );
  130. // if no repos set we use ones available for the user
  131. if ( searchField.getRepositories() == null || searchField.getRepositories().isEmpty() )
  132. {
  133. searchField.setRepositories( getObservableRepos() );
  134. }
  135. try
  136. {
  137. SearchResults searchResults = repositorySearch.search( getPrincipal(), searchField, limits );
  138. return getArtifacts( searchResults );
  139. }
  140. catch ( RepositorySearchException e )
  141. {
  142. log.error( e.getMessage(), e );
  143. throw new ArchivaRestServiceException( e.getMessage(), e );
  144. }
  145. }
  146. public GroupIdList getAllGroupIds( List<String> selectedRepos )
  147. throws ArchivaRestServiceException
  148. {
  149. List<String> observableRepos = getObservableRepos();
  150. List<String> repos = ListUtils.intersection( observableRepos, selectedRepos );
  151. if ( repos == null || repos.isEmpty() )
  152. {
  153. return new GroupIdList( Collections.<String>emptyList() );
  154. }
  155. try
  156. {
  157. return new GroupIdList( new ArrayList<>( repositorySearch.getAllGroupIds( getPrincipal(), repos ) ) );
  158. }
  159. catch ( RepositorySearchException e )
  160. {
  161. log.error( e.getMessage(), e );
  162. throw new ArchivaRestServiceException( e.getMessage(), e );
  163. }
  164. }
  165. public List<Dependency> getDependencies( String groupId, String artifactId, String version )
  166. throws ArchivaRestServiceException
  167. {
  168. return null; //To change body of implemented methods use File | Settings | File Templates.
  169. }
  170. public List<Artifact> getArtifactByChecksum( String checksum )
  171. throws ArchivaRestServiceException
  172. {
  173. return null; //To change body of implemented methods use File | Settings | File Templates.
  174. }
  175. public StringList getObservablesRepoIds()
  176. throws ArchivaRestServiceException
  177. {
  178. return new StringList( getObservableRepos() );
  179. }
  180. //-------------------------------------
  181. // internal
  182. //-------------------------------------
  183. protected List<Artifact> getArtifacts( SearchResults searchResults )
  184. throws ArchivaRestServiceException
  185. {
  186. if ( searchResults == null || searchResults.isEmpty() )
  187. {
  188. return Collections.emptyList();
  189. }
  190. List<Artifact> artifacts = new ArrayList<>( searchResults.getReturnedHitsCount() );
  191. for ( SearchResultHit hit : searchResults.getHits() )
  192. {
  193. // duplicate Artifact one per available version
  194. if ( hit.getVersions().size() > 0 )
  195. {
  196. for ( String version : hit.getVersions() )
  197. {
  198. Artifact versionned = getModelMapper().map( hit, Artifact.class );
  199. if ( StringUtils.isNotBlank( version ) )
  200. {
  201. versionned.setVersion( version );
  202. versionned.setUrl( getArtifactUrl( versionned ) );
  203. artifacts.add( versionned );
  204. }
  205. }
  206. }
  207. }
  208. return artifacts;
  209. }
  210. }