1 package org.apache.archiva.rest.services;
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import net.sf.beanlib.provider.replicator.BeanReplicator;
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.rest.api.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.services.ArchivaRestServiceException;
33 import org.apache.archiva.rest.api.services.SearchService;
34 import org.apache.archiva.rest.services.interceptors.HttpContext;
35 import org.apache.archiva.rest.services.interceptors.HttpContextThreadLocal;
36 import org.apache.archiva.security.AccessDeniedException;
37 import org.apache.archiva.security.ArchivaSecurityException;
38 import org.apache.archiva.security.PrincipalNotFoundException;
39 import org.apache.archiva.security.UserRepositories;
40 import org.apache.commons.collections.ListUtils;
41 import org.apache.commons.lang.StringUtils;
42 import org.codehaus.plexus.redback.users.UserManager;
43 import org.codehaus.redback.rest.services.RedbackAuthenticationThreadLocal;
44 import org.codehaus.redback.rest.services.RedbackRequestInformation;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47 import org.springframework.stereotype.Service;
49 import javax.inject.Inject;
50 import javax.servlet.http.HttpServletRequest;
51 import javax.ws.rs.core.Context;
52 import java.util.ArrayList;
53 import java.util.Collections;
54 import java.util.List;
57 * @author Olivier Lamy
59 @Service( "searchService#rest" )
60 public class DefaultSearchService
61 implements SearchService
64 private Logger log = LoggerFactory.getLogger( getClass() );
67 private RepositorySearch repositorySearch;
70 private UserRepositories userRepositories;
73 private HttpServletRequest httpServletRequest;
75 public List<Artifact> quickSearch( String queryString )
76 throws ArchivaRestServiceException
78 if ( StringUtils.isBlank( queryString ) )
80 return Collections.emptyList();
83 SearchResultLimits limits = new SearchResultLimits( 0 );
84 List<String> observableRepoIds = getObservableRepos();
87 SearchResults searchResults =
88 repositorySearch.search( getPrincipal(), getObservableRepos(), queryString, limits,
89 Collections.<String>emptyList() );
90 return getArtifacts( searchResults );
93 catch ( RepositorySearchException e )
95 log.error( e.getMessage(), e );
96 throw new ArchivaRestServiceException( e.getMessage() );
100 public List<Artifact> getArtifactVersions( String groupId, String artifactId, String packaging )
101 throws ArchivaRestServiceException
103 if ( StringUtils.isBlank( groupId ) || StringUtils.isBlank( artifactId ) )
105 return Collections.emptyList();
107 SearchFields searchField = new SearchFields();
108 searchField.setGroupId( groupId );
109 searchField.setArtifactId( artifactId );
110 searchField.setPackaging( StringUtils.isBlank( packaging ) ? "jar" : packaging );
111 searchField.setRepositories( getObservableRepos() );
115 SearchResults searchResults = repositorySearch.search( getPrincipal(), searchField, null );
116 return getArtifacts( searchResults );
118 catch ( RepositorySearchException e )
120 log.error( e.getMessage(), e );
121 throw new ArchivaRestServiceException( e.getMessage() );
125 public List<Artifact> searchArtifacts( SearchRequest searchRequest )
126 throws ArchivaRestServiceException
128 if ( searchRequest == null )
130 return Collections.emptyList();
132 SearchFields searchField = new BeanReplicator().replicateBean( searchRequest, SearchFields.class );
133 SearchResultLimits limits = new SearchResultLimits( 0 );
135 // if no repos set we use ones available for the user
136 if ( searchField.getRepositories() == null || searchField.getRepositories().isEmpty() )
138 searchField.setRepositories( getObservableRepos() );
143 SearchResults searchResults = repositorySearch.search( getPrincipal(), searchField, limits );
144 return getArtifacts( searchResults );
146 catch ( RepositorySearchException e )
148 log.error( e.getMessage(), e );
149 throw new ArchivaRestServiceException( e.getMessage() );
153 public GroupIdList getAllGroupIds( List<String> selectedRepos )
154 throws ArchivaRestServiceException
156 List<String> observableRepos = getObservableRepos();
157 List<String> repos = ListUtils.intersection( observableRepos, selectedRepos );
158 if ( repos == null || repos.isEmpty() )
160 return new GroupIdList( Collections.<String>emptyList() );
164 return new GroupIdList( new ArrayList<String>( repositorySearch.getAllGroupIds( getPrincipal(), repos ) ) );
166 catch ( RepositorySearchException e )
168 log.error( e.getMessage(), e );
169 throw new ArchivaRestServiceException( e.getMessage() );
174 public List<Dependency> getDependencies( String groupId, String artifactId, String version )
175 throws ArchivaRestServiceException
177 return null; //To change body of implemented methods use File | Settings | File Templates.
180 public List<Artifact> getArtifactByChecksum( String checksum )
181 throws ArchivaRestServiceException
183 return null; //To change body of implemented methods use File | Settings | File Templates.
187 protected List<String> getObservableRepos()
191 List<String> ids = userRepositories.getObservableRepositoryIds( getPrincipal() );
192 return ids == null ? Collections.<String>emptyList() : ids;
194 catch ( PrincipalNotFoundException e )
196 log.warn( e.getMessage(), e );
198 catch ( AccessDeniedException e )
200 log.warn( e.getMessage(), e );
202 catch ( ArchivaSecurityException e )
204 log.warn( e.getMessage(), e );
206 return Collections.emptyList();
209 protected String getPrincipal()
211 RedbackRequestInformation redbackRequestInformation = RedbackAuthenticationThreadLocal.get();
213 return redbackRequestInformation == null
214 ? UserManager.GUEST_USERNAME
215 : ( redbackRequestInformation.getUser() == null
216 ? UserManager.GUEST_USERNAME
217 : redbackRequestInformation.getUser().getUsername() );
220 protected List<Artifact> getArtifacts( SearchResults searchResults )
223 if ( searchResults == null || searchResults.isEmpty() )
225 return Collections.emptyList();
227 List<Artifact> artifacts = new ArrayList<Artifact>( searchResults.getReturnedHitsCount() );
228 for ( SearchResultHit hit : searchResults.getHits() )
230 // duplicate Artifact one per available version
231 if ( hit.getVersions().size() > 0 )
233 for ( String version : hit.getVersions() )
236 Artifact versionned = new BeanReplicator().replicateBean( hit, Artifact.class );
238 if ( StringUtils.isNotBlank( version ) )
240 versionned.setVersion( version );
241 versionned.setUrl( getArtifactUrl( versionned ) );
243 artifacts.add( versionned );
253 * TODO add a configuration mechanism to have configured the base archiva url
257 private String getArtifactUrl( Artifact artifact )
260 if ( httpServletRequest == null )
264 if ( StringUtils.isEmpty( artifact.getUrl() ) )
268 StringBuilder sb = new StringBuilder( getBaseUrl( httpServletRequest ) );
270 sb.append( "/repository" );
271 if ( !StringUtils.startsWith( artifact.getUrl(), "/" ) )
275 sb.append( artifact.getUrl() );
276 return sb.toString();
279 protected String getBaseUrl( HttpServletRequest req )
281 return req.getScheme() + "://" + req.getServerName() + ( req.getServerPort() == 80
283 : ":" + req.getServerPort() ) + req.getContextPath();