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 );
147 limits.setPageSize( searchRequest.getPageSize() );
149 // if no repos set we use ones available for the user
150 if ( searchField.getRepositories() == null || searchField.getRepositories().isEmpty() )
152 searchField.setRepositories( getObservableRepos() );
157 SearchResults searchResults = repositorySearch.search( getPrincipal(), searchField, limits );
158 return getArtifacts( searchResults );
160 catch ( RepositorySearchException e )
162 log.error( e.getMessage(), e );
163 throw new ArchivaRestServiceException( e.getMessage(), e );
168 public GroupIdList getAllGroupIds( List<String> selectedRepos )
169 throws ArchivaRestServiceException
171 List<String> observableRepos = getObservableRepos();
172 List<String> repos = ListUtils.intersection( observableRepos, selectedRepos );
173 if ( repos == null || repos.isEmpty() )
175 return new GroupIdList( Collections.<String>emptyList() );
179 return new GroupIdList( new ArrayList<>( repositorySearch.getAllGroupIds( getPrincipal(), repos ) ) );
181 catch ( RepositorySearchException e )
183 log.error( e.getMessage(), e );
184 throw new ArchivaRestServiceException( e.getMessage(), e );
189 public List<Dependency> getDependencies( String groupId, String artifactId, String version )
190 throws ArchivaRestServiceException
192 return null; //To change body of implemented methods use File | Settings | File Templates.
195 public List<Artifact> getArtifactByChecksum( String checksum )
196 throws ArchivaRestServiceException
198 return null; //To change body of implemented methods use File | Settings | File Templates.
202 public StringList getObservablesRepoIds()
203 throws ArchivaRestServiceException
205 return new StringList( getObservableRepos() );
208 //-------------------------------------
210 //-------------------------------------
211 protected List<Artifact> getArtifacts( SearchResults searchResults )
212 throws ArchivaRestServiceException
215 if ( searchResults == null || searchResults.isEmpty() )
217 return Collections.emptyList();
219 List<Artifact> artifacts = new ArrayList<>( searchResults.getReturnedHitsCount() );
220 for ( SearchResultHit hit : searchResults.getHits() )
222 // duplicate Artifact one per available version
223 if ( hit.getVersions().size() > 0 )
225 for ( String version : hit.getVersions() )
228 Artifact versionned = getModelMapper().map( hit, Artifact.class );
230 if ( StringUtils.isNotBlank( version ) )
232 versionned.setVersion( version );
233 versionned.setUrl( getArtifactUrl( versionned ) );
235 artifacts.add( versionned );