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.services.ArchivaRestServiceException;
34 import org.apache.archiva.rest.api.services.SearchService;
35 import org.apache.archiva.security.AccessDeniedException;
36 import org.apache.archiva.security.ArchivaSecurityException;
37 import org.apache.archiva.security.PrincipalNotFoundException;
38 import org.apache.archiva.security.UserRepositories;
39 import org.apache.commons.collections.ListUtils;
40 import org.apache.commons.lang.StringUtils;
41 import org.codehaus.plexus.redback.users.UserManager;
42 import org.codehaus.redback.rest.services.RedbackAuthenticationThreadLocal;
43 import org.codehaus.redback.rest.services.RedbackRequestInformation;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.springframework.stereotype.Service;
48 import javax.inject.Inject;
49 import javax.servlet.http.HttpServletRequest;
50 import javax.ws.rs.core.Context;
51 import java.util.ArrayList;
52 import java.util.Collections;
53 import java.util.List;
56 * @author Olivier Lamy
58 @Service( "searchService#rest" )
59 public class DefaultSearchService
60 extends AbstractRestService
61 implements SearchService
65 private RepositorySearch repositorySearch;
67 public List<Artifact> quickSearch( String queryString )
68 throws ArchivaRestServiceException
70 if ( StringUtils.isBlank( queryString ) )
72 return Collections.emptyList();
75 SearchResultLimits limits = new SearchResultLimits( 0 );
78 SearchResults searchResults =
79 repositorySearch.search( getPrincipal(), getObservableRepos(), queryString, limits,
80 Collections.<String>emptyList() );
81 return getArtifacts( searchResults );
84 catch ( RepositorySearchException e )
86 log.error( e.getMessage(), e );
87 throw new ArchivaRestServiceException( e.getMessage() );
91 public List<Artifact> getArtifactVersions( String groupId, String artifactId, String packaging )
92 throws ArchivaRestServiceException
94 if ( StringUtils.isBlank( groupId ) || StringUtils.isBlank( artifactId ) )
96 return Collections.emptyList();
98 SearchFields searchField = new SearchFields();
99 searchField.setGroupId( groupId );
100 searchField.setArtifactId( artifactId );
101 searchField.setPackaging( StringUtils.isBlank( packaging ) ? "jar" : packaging );
102 searchField.setRepositories( getObservableRepos() );
106 SearchResults searchResults = repositorySearch.search( getPrincipal(), searchField, null );
107 return getArtifacts( searchResults );
109 catch ( RepositorySearchException e )
111 log.error( e.getMessage(), e );
112 throw new ArchivaRestServiceException( e.getMessage() );
116 public List<Artifact> searchArtifacts( SearchRequest searchRequest )
117 throws ArchivaRestServiceException
119 if ( searchRequest == null )
121 return Collections.emptyList();
123 SearchFields searchField = new BeanReplicator().replicateBean( searchRequest, SearchFields.class );
124 SearchResultLimits limits = new SearchResultLimits( 0 );
126 // if no repos set we use ones available for the user
127 if ( searchField.getRepositories() == null || searchField.getRepositories().isEmpty() )
129 searchField.setRepositories( getObservableRepos() );
134 SearchResults searchResults = repositorySearch.search( getPrincipal(), searchField, limits );
135 return getArtifacts( searchResults );
137 catch ( RepositorySearchException e )
139 log.error( e.getMessage(), e );
140 throw new ArchivaRestServiceException( e.getMessage() );
144 public GroupIdList getAllGroupIds( List<String> selectedRepos )
145 throws ArchivaRestServiceException
147 List<String> observableRepos = getObservableRepos();
148 List<String> repos = ListUtils.intersection( observableRepos, selectedRepos );
149 if ( repos == null || repos.isEmpty() )
151 return new GroupIdList( Collections.<String>emptyList() );
155 return new GroupIdList( new ArrayList<String>( repositorySearch.getAllGroupIds( getPrincipal(), repos ) ) );
157 catch ( RepositorySearchException e )
159 log.error( e.getMessage(), e );
160 throw new ArchivaRestServiceException( e.getMessage() );
165 public List<Dependency> getDependencies( String groupId, String artifactId, String version )
166 throws ArchivaRestServiceException
168 return null; //To change body of implemented methods use File | Settings | File Templates.
171 public List<Artifact> getArtifactByChecksum( String checksum )
172 throws ArchivaRestServiceException
174 return null; //To change body of implemented methods use File | Settings | File Templates.
177 protected List<Artifact> getArtifacts( SearchResults searchResults )
180 if ( searchResults == null || searchResults.isEmpty() )
182 return Collections.emptyList();
184 List<Artifact> artifacts = new ArrayList<Artifact>( searchResults.getReturnedHitsCount() );
185 for ( SearchResultHit hit : searchResults.getHits() )
187 // duplicate Artifact one per available version
188 if ( hit.getVersions().size() > 0 )
190 for ( String version : hit.getVersions() )
193 Artifact versionned = new BeanReplicator().replicateBean( hit, Artifact.class );
195 if ( StringUtils.isNotBlank( version ) )
197 versionned.setVersion( version );
198 versionned.setUrl( getArtifactUrl( versionned, version ) );
200 artifacts.add( versionned );
210 * TODO add a configuration mechanism to have configured the base archiva url
215 private String getArtifactUrl( Artifact artifact, String version )
218 if ( httpServletRequest == null )
222 if ( StringUtils.isEmpty( artifact.getUrl() ) )
226 StringBuilder sb = new StringBuilder( getBaseUrl( httpServletRequest ) );
228 sb.append( "/repository" );
230 sb.append( '/' ).append( artifact.getContext() );
232 sb.append( '/' ).append( StringUtils.replaceChars( artifact.getGroupId(), '.', '/' ) );
233 sb.append( '/' ).append( artifact.getArtifactId() );
234 sb.append( '/' ).append( artifact.getVersion() );
235 sb.append( '/' ).append( artifact.getArtifactId() );
236 sb.append( '-' ).append( artifact.getVersion() );
237 if ( StringUtils.isNotBlank( artifact.getClassifier() ) )
239 sb.append( '-' ).append( artifact.getClassifier() );
241 // maven-plugin packaging is a jar
242 if ( StringUtils.equals( "maven-plugin", artifact.getPackaging() ) )
248 sb.append( '.' ).append( artifact.getPackaging() );
251 return sb.toString();