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() );
81 public List<Artifact> getArtifactVersions( String groupId, String artifactId, String packaging )
82 throws ArchivaRestServiceException
84 if ( StringUtils.isBlank( groupId ) || StringUtils.isBlank( artifactId ) )
86 return Collections.emptyList();
88 SearchFields searchField = new SearchFields();
89 searchField.setGroupId( groupId );
90 searchField.setArtifactId( artifactId );
91 searchField.setPackaging( StringUtils.isBlank( packaging ) ? "jar" : packaging );
92 searchField.setRepositories( getObservableRepos() );
96 SearchResults searchResults = repositorySearch.search( getPrincipal(), searchField, null );
97 return getArtifacts( searchResults );
99 catch ( RepositorySearchException e )
101 log.error( e.getMessage(), e );
102 throw new ArchivaRestServiceException( e.getMessage() );
106 public List<Artifact> searchArtifacts( SearchRequest searchRequest )
107 throws ArchivaRestServiceException
109 if ( searchRequest == null )
111 return Collections.emptyList();
113 SearchFields searchField = new BeanReplicator().replicateBean( searchRequest, SearchFields.class );
114 SearchResultLimits limits = new SearchResultLimits( 0 );
116 // if no repos set we use ones available for the user
117 if ( searchField.getRepositories() == null || searchField.getRepositories().isEmpty() )
119 searchField.setRepositories( getObservableRepos() );
124 SearchResults searchResults = repositorySearch.search( getPrincipal(), searchField, limits );
125 return getArtifacts( searchResults );
127 catch ( RepositorySearchException e )
129 log.error( e.getMessage(), e );
130 throw new ArchivaRestServiceException( e.getMessage() );
134 public GroupIdList getAllGroupIds( List<String> selectedRepos )
135 throws ArchivaRestServiceException
137 List<String> observableRepos = getObservableRepos();
138 List<String> repos = ListUtils.intersection( observableRepos, selectedRepos );
139 if ( repos == null || repos.isEmpty() )
141 return new GroupIdList( Collections.<String>emptyList() );
145 return new GroupIdList( new ArrayList<String>( repositorySearch.getAllGroupIds( getPrincipal(), repos ) ) );
147 catch ( RepositorySearchException e )
149 log.error( e.getMessage(), e );
150 throw new ArchivaRestServiceException( e.getMessage() );
155 public List<Dependency> getDependencies( String groupId, String artifactId, String version )
156 throws ArchivaRestServiceException
158 return null; //To change body of implemented methods use File | Settings | File Templates.
161 public List<Artifact> getArtifactByChecksum( String checksum )
162 throws ArchivaRestServiceException
164 return null; //To change body of implemented methods use File | Settings | File Templates.
167 public StringList getObservablesRepoIds()
168 throws ArchivaRestServiceException
170 return new StringList( getObservableRepos() );
173 //-------------------------------------
175 //-------------------------------------
176 protected List<Artifact> getArtifacts( SearchResults searchResults )
179 if ( searchResults == null || searchResults.isEmpty() )
181 return Collections.emptyList();
183 List<Artifact> artifacts = new ArrayList<Artifact>( searchResults.getReturnedHitsCount() );
184 for ( SearchResultHit hit : searchResults.getHits() )
186 // duplicate Artifact one per available version
187 if ( hit.getVersions().size() > 0 )
189 for ( String version : hit.getVersions() )
192 Artifact versionned = new BeanReplicator().replicateBean( hit, Artifact.class );
194 if ( StringUtils.isNotBlank( version ) )
196 versionned.setVersion( version );
197 versionned.setUrl( getArtifactUrl( versionned, version ) );
199 artifacts.add( versionned );
209 * TODO add a configuration mechanism to have configured the base archiva url
214 private String getArtifactUrl( Artifact artifact, String version )
217 if ( httpServletRequest == null )
221 if ( StringUtils.isEmpty( artifact.getUrl() ) )
225 StringBuilder sb = new StringBuilder( getBaseUrl( httpServletRequest ) );
227 sb.append( "/repository" );
229 sb.append( '/' ).append( artifact.getContext() );
231 sb.append( '/' ).append( StringUtils.replaceChars( artifact.getGroupId(), '.', '/' ) );
232 sb.append( '/' ).append( artifact.getArtifactId() );
233 sb.append( '/' ).append( artifact.getVersion() );
234 sb.append( '/' ).append( artifact.getArtifactId() );
235 sb.append( '-' ).append( artifact.getVersion() );
236 if ( StringUtils.isNotBlank( artifact.getClassifier() ) )
238 sb.append( '-' ).append( artifact.getClassifier() );
240 // maven-plugin packaging is a jar
241 if ( StringUtils.equals( "maven-plugin", artifact.getPackaging() ) )
247 sb.append( '.' ).append( artifact.getPackaging() );
250 return sb.toString();