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.admin.model.RepositoryAdminException;
24 import org.apache.archiva.indexer.search.RepositorySearch;
25 import org.apache.archiva.indexer.search.RepositorySearchException;
26 import org.apache.archiva.indexer.search.SearchFields;
27 import org.apache.archiva.indexer.search.SearchResultHit;
28 import org.apache.archiva.indexer.search.SearchResultLimits;
29 import org.apache.archiva.indexer.search.SearchResults;
30 import org.apache.archiva.rest.api.model.Artifact;
31 import org.apache.archiva.rest.api.model.Dependency;
32 import org.apache.archiva.rest.api.model.GroupIdList;
33 import org.apache.archiva.rest.api.model.SearchRequest;
34 import org.apache.archiva.rest.api.model.StringList;
35 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
36 import org.apache.archiva.rest.api.services.SearchService;
37 import org.apache.commons.collections.ListUtils;
38 import org.apache.commons.lang.StringUtils;
39 import org.springframework.stereotype.Service;
41 import javax.inject.Inject;
42 import javax.ws.rs.core.Response;
43 import java.util.ArrayList;
44 import java.util.Collections;
45 import java.util.List;
48 * @author Olivier Lamy
50 @Service( "searchService#rest" )
51 public class DefaultSearchService
52 extends AbstractRestService
53 implements SearchService
57 private RepositorySearch repositorySearch;
59 public List<Artifact> quickSearch( String queryString )
60 throws ArchivaRestServiceException
62 if ( StringUtils.isBlank( queryString ) )
64 return Collections.emptyList();
67 SearchResultLimits limits = new SearchResultLimits( 0 );
70 SearchResults searchResults =
71 repositorySearch.search( getPrincipal(), getObservableRepos(), queryString, limits,
72 Collections.<String>emptyList() );
73 return getArtifacts( searchResults );
76 catch ( RepositorySearchException e )
78 log.error( e.getMessage(), e );
79 throw new ArchivaRestServiceException( e.getMessage(), e );
83 public List<Artifact> quickSearchWithRepositories( SearchRequest searchRequest )
84 throws ArchivaRestServiceException
86 String queryString = searchRequest.getQueryTerms();
87 if ( StringUtils.isBlank( queryString ) )
89 return Collections.emptyList();
91 List<String> repositories = searchRequest.getRepositories();
92 if ( repositories == null || repositories.isEmpty() )
94 repositories = getObservableRepos();
96 SearchResultLimits limits = new SearchResultLimits( 0 );
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 );
111 public List<Artifact> getArtifactVersions( String groupId, String artifactId, String packaging )
112 throws ArchivaRestServiceException
114 if ( StringUtils.isBlank( groupId ) || StringUtils.isBlank( artifactId ) )
116 return Collections.emptyList();
118 SearchFields searchField = new SearchFields();
119 searchField.setGroupId( groupId );
120 searchField.setArtifactId( artifactId );
121 searchField.setPackaging( StringUtils.isBlank( packaging ) ? "jar" : packaging );
122 searchField.setRepositories( getObservableRepos() );
126 SearchResults searchResults = repositorySearch.search( getPrincipal(), searchField, null );
127 return getArtifacts( searchResults );
129 catch ( RepositorySearchException e )
131 log.error( e.getMessage(), e );
132 throw new ArchivaRestServiceException( e.getMessage(), e );
136 public List<Artifact> searchArtifacts( SearchRequest searchRequest )
137 throws ArchivaRestServiceException
139 if ( searchRequest == null )
141 return Collections.emptyList();
143 SearchFields searchField = new BeanReplicator().replicateBean( searchRequest, SearchFields.class );
144 SearchResultLimits limits = new SearchResultLimits( 0 );
146 // if no repos set we use ones available for the user
147 if ( searchField.getRepositories() == null || searchField.getRepositories().isEmpty() )
149 searchField.setRepositories( getObservableRepos() );
154 SearchResults searchResults = repositorySearch.search( getPrincipal(), searchField, limits );
155 return getArtifacts( searchResults );
157 catch ( RepositorySearchException e )
159 log.error( e.getMessage(), e );
160 throw new ArchivaRestServiceException( e.getMessage(), e );
164 public GroupIdList getAllGroupIds( List<String> selectedRepos )
165 throws ArchivaRestServiceException
167 List<String> observableRepos = getObservableRepos();
168 List<String> repos = ListUtils.intersection( observableRepos, selectedRepos );
169 if ( repos == null || repos.isEmpty() )
171 return new GroupIdList( Collections.<String>emptyList() );
175 return new GroupIdList( new ArrayList<String>( repositorySearch.getAllGroupIds( getPrincipal(), repos ) ) );
177 catch ( RepositorySearchException e )
179 log.error( e.getMessage(), e );
180 throw new ArchivaRestServiceException( e.getMessage(), e );
185 public List<Dependency> getDependencies( String groupId, String artifactId, String version )
186 throws ArchivaRestServiceException
188 return null; //To change body of implemented methods use File | Settings | File Templates.
191 public List<Artifact> getArtifactByChecksum( String checksum )
192 throws ArchivaRestServiceException
194 return null; //To change body of implemented methods use File | Settings | File Templates.
197 public StringList getObservablesRepoIds()
198 throws ArchivaRestServiceException
200 return new StringList( getObservableRepos() );
203 //-------------------------------------
205 //-------------------------------------
206 protected List<Artifact> getArtifacts( SearchResults searchResults )
207 throws ArchivaRestServiceException
210 if ( searchResults == null || searchResults.isEmpty() )
212 return Collections.emptyList();
214 List<Artifact> artifacts = new ArrayList<Artifact>( searchResults.getReturnedHitsCount() );
215 for ( SearchResultHit hit : searchResults.getHits() )
217 // duplicate Artifact one per available version
218 if ( hit.getVersions().size() > 0 )
220 for ( String version : hit.getVersions() )
223 Artifact versionned = new BeanReplicator().replicateBean( hit, Artifact.class );
225 if ( StringUtils.isNotBlank( version ) )
227 versionned.setVersion( version );
228 versionned.setUrl( getArtifactUrl( versionned, version ) );
230 artifacts.add( versionned );