1 package org.apache.archiva.rest.services;
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.indexer.search.RepositorySearch;
23 import org.apache.archiva.indexer.search.RepositorySearchException;
24 import org.apache.archiva.indexer.search.SearchFields;
25 import org.apache.archiva.indexer.search.SearchResultHit;
26 import org.apache.archiva.indexer.search.SearchResultLimits;
27 import org.apache.archiva.indexer.search.SearchResults;
28 import org.apache.archiva.maven2.model.Artifact;
29 import org.apache.archiva.rest.api.model.Dependency;
30 import org.apache.archiva.rest.api.model.GroupIdList;
31 import org.apache.archiva.rest.api.model.SearchRequest;
32 import org.apache.archiva.rest.api.model.StringList;
33 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
34 import org.apache.archiva.rest.api.services.SearchService;
35 import org.apache.commons.collections.ListUtils;
36 import org.apache.commons.lang.StringUtils;
37 import org.springframework.stereotype.Service;
39 import javax.inject.Inject;
40 import java.util.ArrayList;
41 import java.util.Collections;
42 import java.util.List;
45 * @author Olivier Lamy
47 @Service( "searchService#rest" )
48 public class DefaultSearchService
49 extends AbstractRestService
50 implements SearchService
54 private RepositorySearch repositorySearch;
57 public List<Artifact> quickSearch( String queryString )
58 throws ArchivaRestServiceException
60 if ( StringUtils.isBlank( queryString ) )
62 return Collections.emptyList();
65 SearchResultLimits limits = new SearchResultLimits( 0 );
68 SearchResults searchResults =
69 repositorySearch.search( getPrincipal(), getObservableRepos(), queryString, limits,
70 Collections.<String>emptyList() );
71 return getArtifacts( searchResults );
74 catch ( RepositorySearchException e )
76 log.error( e.getMessage(), e );
77 throw new ArchivaRestServiceException( e.getMessage(), e );
82 public List<Artifact> quickSearchWithRepositories( SearchRequest searchRequest )
83 throws ArchivaRestServiceException
85 String queryString = searchRequest.getQueryTerms();
86 if ( StringUtils.isBlank( queryString ) )
88 return Collections.emptyList();
90 List<String> repositories = searchRequest.getRepositories();
91 if ( repositories == null || repositories.isEmpty() )
93 repositories = getObservableRepos();
95 SearchResultLimits limits =
96 new SearchResultLimits( searchRequest.getPageSize(), searchRequest.getSelectedPage() );
99 SearchResults searchResults = repositorySearch.search( getPrincipal(), repositories, queryString, limits,
100 Collections.<String>emptyList() );
101 return getArtifacts( searchResults );
104 catch ( RepositorySearchException e )
106 log.error( e.getMessage(), e );
107 throw new ArchivaRestServiceException( e.getMessage(), e );
112 public List<Artifact> getArtifactVersions( String groupId, String artifactId, String packaging )
113 throws ArchivaRestServiceException
115 if ( StringUtils.isBlank( groupId ) || StringUtils.isBlank( artifactId ) )
117 return Collections.emptyList();
119 SearchFields searchField = new SearchFields();
120 searchField.setGroupId( groupId );
121 searchField.setArtifactId( artifactId );
122 searchField.setPackaging( StringUtils.isBlank( packaging ) ? "jar" : packaging );
123 searchField.setRepositories( getObservableRepos() );
127 SearchResults searchResults = repositorySearch.search( getPrincipal(), searchField, null );
128 return getArtifacts( searchResults );
130 catch ( RepositorySearchException e )
132 log.error( e.getMessage(), e );
133 throw new ArchivaRestServiceException( e.getMessage(), e );
138 public List<Artifact> searchArtifacts( SearchRequest searchRequest )
139 throws ArchivaRestServiceException
141 if ( searchRequest == null )
143 return Collections.emptyList();
145 SearchFields searchField = getModelMapper().map( searchRequest, SearchFields.class );
146 SearchResultLimits limits = new SearchResultLimits( 0 );
148 // if no repos set we use ones available for the user
149 if ( searchField.getRepositories() == null || searchField.getRepositories().isEmpty() )
151 searchField.setRepositories( getObservableRepos() );
156 SearchResults searchResults = repositorySearch.search( getPrincipal(), searchField, limits );
157 return getArtifacts( searchResults );
159 catch ( RepositorySearchException e )
161 log.error( e.getMessage(), e );
162 throw new ArchivaRestServiceException( e.getMessage(), e );
167 public GroupIdList getAllGroupIds( List<String> selectedRepos )
168 throws ArchivaRestServiceException
170 List<String> observableRepos = getObservableRepos();
171 List<String> repos = ListUtils.intersection( observableRepos, selectedRepos );
172 if ( repos == null || repos.isEmpty() )
174 return new GroupIdList( Collections.<String>emptyList() );
178 return new GroupIdList( new ArrayList<>( repositorySearch.getAllGroupIds( getPrincipal(), repos ) ) );
180 catch ( RepositorySearchException e )
182 log.error( e.getMessage(), e );
183 throw new ArchivaRestServiceException( e.getMessage(), e );
188 public List<Dependency> getDependencies( String groupId, String artifactId, String version )
189 throws ArchivaRestServiceException
191 return null; //To change body of implemented methods use File | Settings | File Templates.
194 public List<Artifact> getArtifactByChecksum( String checksum )
195 throws ArchivaRestServiceException
197 return null; //To change body of implemented methods use File | Settings | File Templates.
201 public StringList getObservablesRepoIds()
202 throws ArchivaRestServiceException
204 return new StringList( getObservableRepos() );
207 //-------------------------------------
209 //-------------------------------------
210 protected List<Artifact> getArtifacts( SearchResults searchResults )
211 throws ArchivaRestServiceException
214 if ( searchResults == null || searchResults.isEmpty() )
216 return Collections.emptyList();
218 List<Artifact> artifacts = new ArrayList<>( searchResults.getReturnedHitsCount() );
219 for ( SearchResultHit hit : searchResults.getHits() )
221 // duplicate Artifact one per available version
222 if ( hit.getVersions().size() > 0 )
224 for ( String version : hit.getVersions() )
227 Artifact versionned = getModelMapper().map( hit, Artifact.class );
229 if ( StringUtils.isNotBlank( version ) )
231 versionned.setVersion( version );
232 versionned.setUrl( getArtifactUrl( versionned ) );
234 artifacts.add( versionned );