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 java.util.ArrayList;
52 import java.util.Collection;
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;
72 public List<Artifact> quickSearch( String queryString )
73 throws ArchivaRestServiceException
75 if ( StringUtils.isBlank( queryString ) )
77 return Collections.emptyList();
80 SearchResultLimits limits = new SearchResultLimits( 0 );
81 List<String> observableRepoIds = getObservableRepos();
84 SearchResults searchResults =
85 repositorySearch.search( getPrincipal(), getObservableRepos(), queryString, limits,
86 Collections.<String>emptyList() );
87 return getArtifacts( searchResults );
90 catch ( RepositorySearchException e )
92 log.error( e.getMessage(), e );
93 throw new ArchivaRestServiceException( e.getMessage() );
97 public List<Artifact> getArtifactVersions( String groupId, String artifactId, String packaging )
98 throws ArchivaRestServiceException
100 if ( StringUtils.isBlank( groupId ) || StringUtils.isBlank( artifactId ) )
102 return Collections.emptyList();
104 SearchFields searchField = new SearchFields();
105 searchField.setGroupId( groupId );
106 searchField.setArtifactId( artifactId );
107 searchField.setPackaging( StringUtils.isBlank( packaging ) ? "jar" : packaging );
108 searchField.setRepositories( getObservableRepos() );
112 SearchResults searchResults = repositorySearch.search( getPrincipal(), searchField, null );
113 return getArtifacts( searchResults );
115 catch ( RepositorySearchException e )
117 log.error( e.getMessage(), e );
118 throw new ArchivaRestServiceException( e.getMessage() );
122 public List<Artifact> searchArtifacts( SearchRequest searchRequest )
123 throws ArchivaRestServiceException
125 if ( searchRequest == null )
127 return Collections.emptyList();
129 SearchFields searchField = new BeanReplicator().replicateBean( searchRequest, SearchFields.class );
130 SearchResultLimits limits = new SearchResultLimits( 0 );
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.
178 protected List<String> getObservableRepos()
182 List<String> ids = userRepositories.getObservableRepositoryIds( getPrincipal() );
183 return ids == null ? Collections.<String>emptyList() : ids;
185 catch ( PrincipalNotFoundException e )
187 log.warn( e.getMessage(), e );
189 catch ( AccessDeniedException e )
191 log.warn( e.getMessage(), e );
193 catch ( ArchivaSecurityException e )
195 log.warn( e.getMessage(), e );
197 return Collections.emptyList();
200 protected String getPrincipal()
202 RedbackRequestInformation redbackRequestInformation = RedbackAuthenticationThreadLocal.get();
204 return redbackRequestInformation == null
205 ? UserManager.GUEST_USERNAME
206 : ( redbackRequestInformation.getUser() == null
207 ? UserManager.GUEST_USERNAME
208 : redbackRequestInformation.getUser().getUsername() );
211 protected List<Artifact> getArtifacts( SearchResults searchResults )
214 HttpContext httpContext = HttpContextThreadLocal.get();
215 if ( searchResults == null || searchResults.isEmpty() )
217 return Collections.emptyList();
219 List<Artifact> artifacts = new ArrayList<Artifact>( searchResults.getReturnedHitsCount() );
220 for ( SearchResultHit hit : searchResults.getHits() )
222 // duplicate Artifact one per available version
223 if ( hit.getVersions().size() > 0 )
225 for ( String version : hit.getVersions() )
228 Artifact versionned = new Artifact( );
229 versionned.setArtifactId( hit.getArtifactId());
230 versionned.setGroupId( hit.getGroupId() );
231 versionned.setRepositoryId(hit.getRepositoryId() );
234 versionned.setBundleExportPackage( hit.getBundleExportPackage() );
235 versionned.setBundleExportService( hit.getBundleExportService());
236 versionned.setBundleSymbolicName(hit.getBundleSymbolicName() );
237 versionned.setBundleVersion( artifactInfo.bundleVersion );
238 versionned.setBundleDescription( artifactInfo.bundleDescription );
239 versionned.setBundleDocUrl( artifactInfo.bundleDocUrl );
241 versionned.setBundleRequireBundle( artifactInfo.bundleRequireBundle );
242 versionned.setBundleImportPackage( artifactInfo.bundleImportPackage );
243 versionned.setBundleLicense( artifactInfo.bundleLicense );
244 versionned.setBundleName( artifactInfo.bundleName );
245 versionned.setContext( artifactInfo.context );
246 versionned.setGoals( artifactInfo.goals );
247 versionned.setPrefix( artifactInfo.prefix );
249 versionned.setUrl( artifactInfo.remoteUrl );
251 // FIXME archiva url ??
253 Artifact versionned = new BeanReplicator().replicateBean( hit, Artifact.class );
255 if ( StringUtils.isNotBlank( version ) )
257 versionned.setVersion( version );
258 versionned.setUrl( getArtifactUrl( httpContext, versionned ) );
260 artifacts.add( versionned );
269 private String getArtifactUrl( HttpContext httpContext, Artifact artifact )
271 if ( httpContext == null )
275 if ( httpContext.getHttpServletRequest() == null )
279 if ( StringUtils.isEmpty( artifact.getUrl() ) )
283 StringBuilder sb = new StringBuilder( getBaseUrl( httpContext.getHttpServletRequest() ) );
285 sb.append( "/repository" );
286 if ( !StringUtils.startsWith( artifact.getUrl(), "/" ) )
290 sb.append( artifact.getUrl() );
291 return sb.toString();
294 protected String getBaseUrl( HttpServletRequest req )
296 return req.getScheme() + "://" + req.getServerName()
297 + ( req.getServerPort() == 80 ? "" : ":" + req.getServerPort() ) + req.getContextPath();