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
220 if ( StringUtils.isEmpty( groupId ) )
222 return Response.status( new Response.StatusType()
225 public int getStatusCode()
227 return Response.Status.BAD_REQUEST.getStatusCode();
231 public Response.Status.Family getFamily()
233 return Response.Status.BAD_REQUEST.getFamily();
237 public String getReasonPhrase()
239 return "groupId mandatory";
244 if ( StringUtils.isEmpty( artifactId ) )
246 return Response.status( new Response.StatusType()
249 public int getStatusCode()
251 return Response.Status.BAD_REQUEST.getStatusCode();
255 public Response.Status.Family getFamily()
257 return Response.Status.BAD_REQUEST.getFamily();
261 public String getReasonPhrase()
263 return "artifactId mandatory";
268 if ( StringUtils.isEmpty( version ) )
270 return Response.status( new Response.StatusType()
273 public int getStatusCode()
275 return Response.Status.BAD_REQUEST.getStatusCode();
279 public Response.Status.Family getFamily()
281 return Response.Status.BAD_REQUEST.getFamily();
285 public String getReasonPhrase()
287 return "version mandatory";
292 SearchFields searchField = new SearchFields();
293 searchField.setGroupId( groupId );
294 searchField.setArtifactId( artifactId );
295 searchField.setPackaging( StringUtils.isBlank( packaging ) ? "jar" : packaging );
296 searchField.setVersion( version );
297 searchField.setClassifier( classifier );
298 List<String> userRepos = getObservablesRepoIds().getStrings();
299 searchField.setRepositories(
300 StringUtils.isEmpty( repositoryId ) ? userRepos : Arrays.asList( repositoryId ) );
301 searchField.setExactSearch( true );
302 SearchResults searchResults = repositorySearch.search( getPrincipal(), searchField, null );
303 List<Artifact> artifacts = getArtifacts( searchResults );
305 if ( artifacts.isEmpty() )
307 return Response.status( new Response.StatusType()
310 public int getStatusCode()
312 return Response.Status.NO_CONTENT.getStatusCode();
316 public Response.Status.Family getFamily()
318 return Response.Status.NO_CONTENT.getFamily();
322 public String getReasonPhrase()
324 return "your query doesn't return any artifact";
329 // TODO improve that with querying lucene with null value for classifier
330 // so simple loop and retain only artifact with null classifier
331 if ( classifier == null )
333 List<Artifact> filteredArtifacts = new ArrayList<>( artifacts.size() );
334 for ( Artifact artifact : artifacts )
336 if ( artifact.getClassifier() == null )
338 filteredArtifacts.add( artifact );
342 artifacts = filteredArtifacts;
345 // TODO return json result of the query ?
346 if ( artifacts.size() > 1 )
348 return Response.status( new Response.StatusType()
351 public int getStatusCode()
353 return Response.Status.BAD_REQUEST.getStatusCode();
357 public Response.Status.Family getFamily()
359 return Response.Status.BAD_REQUEST.getFamily();
363 public String getReasonPhrase()
365 return "your query return more than one artifact";
370 Artifact artifact = artifacts.get( 0 );
372 return Response.temporaryRedirect( new URI( artifact.getUrl() ) ).build();
374 catch ( Exception e )
376 throw new ArchivaRestServiceException( e.getMessage(), e );
380 //-------------------------------------
382 //-------------------------------------
383 protected List<Artifact> getArtifacts( SearchResults searchResults )
384 throws ArchivaRestServiceException
387 if ( searchResults == null || searchResults.isEmpty() )
389 return Collections.emptyList();
391 List<Artifact> artifacts = new ArrayList<>( searchResults.getReturnedHitsCount() );
392 for ( SearchResultHit hit : searchResults.getHits() )
394 // duplicate Artifact one per available version
395 if ( hit.getVersions().size() > 0 )
397 for ( String version : hit.getVersions() )
400 Artifact versionned = getModelMapper().map( hit, Artifact.class );
402 if ( StringUtils.isNotBlank( version ) )
404 versionned.setVersion( version );
405 versionned.setUrl( getArtifactUrl( versionned ) );
407 artifacts.add( versionned );