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 net.sf.beanlib.provider.replicator.BeanReplicator;
23 import org.apache.archiva.indexer.search.RepositorySearch;
24 import org.apache.archiva.indexer.search.RepositorySearchException;
25 import org.apache.archiva.indexer.search.SearchFields;
26 import org.apache.archiva.indexer.search.SearchResultHit;
27 import org.apache.archiva.indexer.search.SearchResultLimits;
28 import org.apache.archiva.indexer.search.SearchResults;
29 import org.apache.archiva.rest.api.model.Artifact;
30 import org.apache.archiva.rest.api.model.Dependency;
31 import org.apache.archiva.rest.api.model.GroupIdList;
32 import org.apache.archiva.rest.api.model.SearchRequest;
33 import org.apache.archiva.rest.api.model.StringList;
34 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
35 import org.apache.archiva.rest.api.services.SearchService;
36 import org.apache.commons.collections.ListUtils;
37 import org.apache.commons.lang.StringUtils;
38 import org.springframework.stereotype.Service;
40 import javax.inject.Inject;
41 import java.util.ArrayList;
42 import java.util.Collections;
43 import java.util.List;
46 * @author Olivier Lamy
48 @Service( "searchService#rest" )
49 public class DefaultSearchService
50 extends AbstractRestService
51 implements SearchService
55 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 );
81 public List<Artifact> quickSearchWithRepositories( SearchRequest searchRequest )
82 throws ArchivaRestServiceException
84 String queryString = searchRequest.getQueryTerms();
85 if ( StringUtils.isBlank( queryString ) )
87 return Collections.emptyList();
89 List<String> repositories = searchRequest.getRepositories();
90 if ( repositories == null || repositories.isEmpty() )
92 repositories = getObservableRepos();
94 SearchResultLimits limits = new SearchResultLimits( 0 );
97 SearchResults searchResults = repositorySearch.search( getPrincipal(), repositories, queryString, limits,
98 Collections.<String>emptyList() );
99 return getArtifacts( searchResults );
102 catch ( RepositorySearchException e )
104 log.error( e.getMessage(), e );
105 throw new ArchivaRestServiceException( e.getMessage(), e );
109 public List<Artifact> getArtifactVersions( String groupId, String artifactId, String packaging )
110 throws ArchivaRestServiceException
112 if ( StringUtils.isBlank( groupId ) || StringUtils.isBlank( artifactId ) )
114 return Collections.emptyList();
116 SearchFields searchField = new SearchFields();
117 searchField.setGroupId( groupId );
118 searchField.setArtifactId( artifactId );
119 searchField.setPackaging( StringUtils.isBlank( packaging ) ? "jar" : packaging );
120 searchField.setRepositories( getObservableRepos() );
124 SearchResults searchResults = repositorySearch.search( getPrincipal(), searchField, null );
125 return getArtifacts( searchResults );
127 catch ( RepositorySearchException e )
129 log.error( e.getMessage(), e );
130 throw new ArchivaRestServiceException( e.getMessage(), e );
134 public List<Artifact> searchArtifacts( SearchRequest searchRequest )
135 throws ArchivaRestServiceException
137 if ( searchRequest == null )
139 return Collections.emptyList();
141 SearchFields searchField = new BeanReplicator().replicateBean( searchRequest, SearchFields.class );
142 SearchResultLimits limits = new SearchResultLimits( 0 );
144 // if no repos set we use ones available for the user
145 if ( searchField.getRepositories() == null || searchField.getRepositories().isEmpty() )
147 searchField.setRepositories( getObservableRepos() );
152 SearchResults searchResults = repositorySearch.search( getPrincipal(), searchField, limits );
153 return getArtifacts( searchResults );
155 catch ( RepositorySearchException e )
157 log.error( e.getMessage(), e );
158 throw new ArchivaRestServiceException( e.getMessage(), e );
162 public GroupIdList getAllGroupIds( List<String> selectedRepos )
163 throws ArchivaRestServiceException
165 List<String> observableRepos = getObservableRepos();
166 List<String> repos = ListUtils.intersection( observableRepos, selectedRepos );
167 if ( repos == null || repos.isEmpty() )
169 return new GroupIdList( Collections.<String>emptyList() );
173 return new GroupIdList( new ArrayList<String>( repositorySearch.getAllGroupIds( getPrincipal(), repos ) ) );
175 catch ( RepositorySearchException e )
177 log.error( e.getMessage(), e );
178 throw new ArchivaRestServiceException( e.getMessage(), e );
183 public List<Dependency> getDependencies( String groupId, String artifactId, String version )
184 throws ArchivaRestServiceException
186 return null; //To change body of implemented methods use File | Settings | File Templates.
189 public List<Artifact> getArtifactByChecksum( String checksum )
190 throws ArchivaRestServiceException
192 return null; //To change body of implemented methods use File | Settings | File Templates.
195 public StringList getObservablesRepoIds()
196 throws ArchivaRestServiceException
198 return new StringList( getObservableRepos() );
201 //-------------------------------------
203 //-------------------------------------
204 protected List<Artifact> getArtifacts( SearchResults searchResults )
205 throws ArchivaRestServiceException
208 if ( searchResults == null || searchResults.isEmpty() )
210 return Collections.emptyList();
212 List<Artifact> artifacts = new ArrayList<Artifact>( searchResults.getReturnedHitsCount() );
213 for ( SearchResultHit hit : searchResults.getHits() )
215 // duplicate Artifact one per available version
216 if ( hit.getVersions().size() > 0 )
218 for ( String version : hit.getVersions() )
221 Artifact versionned = new BeanReplicator().replicateBean( hit, Artifact.class );
223 if ( StringUtils.isNotBlank( version ) )
225 versionned.setVersion( version );
226 versionned.setUrl( getArtifactUrl( versionned ) );
228 artifacts.add( versionned );