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 javax.ws.rs.core.Response;
42 import java.util.ArrayList;
43 import java.util.Arrays;
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;
60 public List<Artifact> quickSearch( String queryString )
61 throws ArchivaRestServiceException
63 if ( StringUtils.isBlank( queryString ) )
65 return Collections.emptyList();
68 SearchResultLimits limits = new SearchResultLimits( 0 );
71 SearchResults searchResults =
72 repositorySearch.search( getPrincipal(), getObservableRepos(), queryString, limits,
73 Collections.<String>emptyList() );
74 return getArtifacts( searchResults );
77 catch ( RepositorySearchException e )
79 log.error( e.getMessage(), e );
80 throw new ArchivaRestServiceException( e.getMessage(), e );
85 public List<Artifact> quickSearchWithRepositories( SearchRequest searchRequest )
86 throws ArchivaRestServiceException
88 String queryString = searchRequest.getQueryTerms();
89 if ( StringUtils.isBlank( queryString ) )
91 return Collections.emptyList();
93 List<String> repositories = searchRequest.getRepositories();
94 if ( repositories == null || repositories.isEmpty() )
96 repositories = getObservableRepos();
98 SearchResultLimits limits =
99 new SearchResultLimits( searchRequest.getPageSize(), searchRequest.getSelectedPage() );
102 SearchResults searchResults = repositorySearch.search( getPrincipal(), repositories, queryString, limits,
103 Collections.<String>emptyList() );
104 return getArtifacts( searchResults );
107 catch ( RepositorySearchException e )
109 log.error( e.getMessage(), e );
110 throw new ArchivaRestServiceException( e.getMessage(), e );
115 public List<Artifact> getArtifactVersions( String groupId, String artifactId, String packaging )
116 throws ArchivaRestServiceException
118 if ( StringUtils.isBlank( groupId ) || StringUtils.isBlank( artifactId ) )
120 return Collections.emptyList();
122 SearchFields searchField = new SearchFields();
123 searchField.setGroupId( groupId );
124 searchField.setArtifactId( artifactId );
125 searchField.setPackaging( StringUtils.isBlank( packaging ) ? "jar" : packaging );
126 searchField.setRepositories( getObservableRepos() );
130 SearchResults searchResults = repositorySearch.search( getPrincipal(), searchField, null );
131 return getArtifacts( searchResults );
133 catch ( RepositorySearchException e )
135 log.error( e.getMessage(), e );
136 throw new ArchivaRestServiceException( e.getMessage(), e );
141 public List<Artifact> searchArtifacts( SearchRequest searchRequest )
142 throws ArchivaRestServiceException
144 if ( searchRequest == null )
146 return Collections.emptyList();
148 SearchFields searchField = getModelMapper().map( searchRequest, SearchFields.class );
149 SearchResultLimits limits = new SearchResultLimits( 0 );
150 limits.setPageSize( searchRequest.getPageSize() );
152 // if no repos set we use ones available for the user
153 if ( searchField.getRepositories() == null || searchField.getRepositories().isEmpty() )
155 searchField.setRepositories( getObservableRepos() );
160 SearchResults searchResults = repositorySearch.search( getPrincipal(), searchField, limits );
161 return getArtifacts( searchResults );
163 catch ( RepositorySearchException e )
165 log.error( e.getMessage(), e );
166 throw new ArchivaRestServiceException( e.getMessage(), e );
171 public GroupIdList getAllGroupIds( List<String> selectedRepos )
172 throws ArchivaRestServiceException
174 List<String> observableRepos = getObservableRepos();
175 List<String> repos = ListUtils.intersection( observableRepos, selectedRepos );
176 if ( repos == null || repos.isEmpty() )
178 return new GroupIdList( Collections.<String>emptyList() );
182 return new GroupIdList( new ArrayList<>( repositorySearch.getAllGroupIds( getPrincipal(), repos ) ) );
184 catch ( RepositorySearchException e )
186 log.error( e.getMessage(), e );
187 throw new ArchivaRestServiceException( e.getMessage(), e );
192 public List<Dependency> getDependencies( String groupId, String artifactId, String version )
193 throws ArchivaRestServiceException
195 return null; //To change body of implemented methods use File | Settings | File Templates.
198 public List<Artifact> getArtifactByChecksum( String checksum )
199 throws ArchivaRestServiceException
201 return null; //To change body of implemented methods use File | Settings | File Templates.
205 public StringList getObservablesRepoIds()
206 throws ArchivaRestServiceException
208 return new StringList( getObservableRepos() );
212 public Response redirectToArtifactFile( String repositoryId, String groupId, String artifactId, String version,
213 String packaging, String classifier )
214 throws ArchivaRestServiceException
219 SearchFields searchField = new SearchFields();
220 searchField.setGroupId( groupId );
221 searchField.setArtifactId( artifactId );
222 searchField.setPackaging( StringUtils.isBlank( packaging ) ? "jar" : packaging );
223 searchField.setVersion( version );
224 searchField.setClassifier( classifier );
225 searchField.setRepositories( Arrays.asList( repositoryId ) );
226 searchField.setExactSearch( true );
227 SearchResults searchResults = repositorySearch.search( getPrincipal(), searchField, null );
228 List<Artifact> artifacts = getArtifacts( searchResults );
230 // TODO improve that with querying lucene with null value for classifier
231 // so simple loop and retain only artifact with null classifier
232 if ( classifier == null )
234 List<Artifact> filteredArtifacts = new ArrayList<>( artifacts.size() );
235 for ( Artifact artifact : artifacts )
237 if ( artifact.getClassifier() == null )
239 filteredArtifacts.add( artifact );
243 artifacts = filteredArtifacts;
246 String artifactUrl = getArtifactUrl( artifacts.get( 0 ), repositoryId );
248 return Response.temporaryRedirect( new URI( artifactUrl ) ).build();
250 catch ( Exception e )
252 throw new ArchivaRestServiceException( e.getMessage(), e );
256 //-------------------------------------
258 //-------------------------------------
259 protected List<Artifact> getArtifacts( SearchResults searchResults )
260 throws ArchivaRestServiceException
263 if ( searchResults == null || searchResults.isEmpty() )
265 return Collections.emptyList();
267 List<Artifact> artifacts = new ArrayList<>( searchResults.getReturnedHitsCount() );
268 for ( SearchResultHit hit : searchResults.getHits() )
270 // duplicate Artifact one per available version
271 if ( hit.getVersions().size() > 0 )
273 for ( String version : hit.getVersions() )
276 Artifact versionned = getModelMapper().map( hit, Artifact.class );
278 if ( StringUtils.isNotBlank( version ) )
280 versionned.setVersion( version );
281 versionned.setUrl( getArtifactUrl( versionned ) );
283 artifacts.add( versionned );