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.Collections;
53 import java.util.List;
56 * @author Olivier Lamy
58 @Service( "searchService#rest" )
59 public class DefaultSearchService
60 implements SearchService
63 private Logger log = LoggerFactory.getLogger( getClass() );
66 private RepositorySearch repositorySearch;
69 private UserRepositories userRepositories;
71 public List<Artifact> quickSearch( String queryString )
72 throws ArchivaRestServiceException
74 if ( StringUtils.isBlank( queryString ) )
76 return Collections.emptyList();
79 SearchResultLimits limits = new SearchResultLimits( 0 );
80 List<String> observableRepoIds = getObservableRepos();
83 SearchResults searchResults =
84 repositorySearch.search( getPrincipal(), getObservableRepos(), queryString, limits,
85 Collections.<String>emptyList() );
86 return getArtifacts( searchResults );
89 catch ( RepositorySearchException e )
91 log.error( e.getMessage(), e );
92 throw new ArchivaRestServiceException( e.getMessage() );
96 public List<Artifact> getArtifactVersions( String groupId, String artifactId, String packaging )
97 throws ArchivaRestServiceException
99 if ( StringUtils.isBlank( groupId ) || StringUtils.isBlank( artifactId ) )
101 return Collections.emptyList();
103 SearchFields searchField = new SearchFields();
104 searchField.setGroupId( groupId );
105 searchField.setArtifactId( artifactId );
106 searchField.setPackaging( StringUtils.isBlank( packaging ) ? "jar" : packaging );
107 searchField.setRepositories( getObservableRepos() );
111 SearchResults searchResults = repositorySearch.search( getPrincipal(), searchField, null );
112 return getArtifacts( searchResults );
114 catch ( RepositorySearchException e )
116 log.error( e.getMessage(), e );
117 throw new ArchivaRestServiceException( e.getMessage() );
121 public List<Artifact> searchArtifacts( SearchRequest searchRequest )
122 throws ArchivaRestServiceException
124 if ( searchRequest == null )
126 return Collections.emptyList();
128 SearchFields searchField = new BeanReplicator().replicateBean( searchRequest, SearchFields.class );
129 SearchResultLimits limits = new SearchResultLimits( 0 );
133 SearchResults searchResults = repositorySearch.search( getPrincipal(), searchField, limits );
134 return getArtifacts( searchResults );
136 catch ( RepositorySearchException e )
138 log.error( e.getMessage(), e );
139 throw new ArchivaRestServiceException( e.getMessage() );
143 public GroupIdList getAllGroupIds( List<String> selectedRepos )
144 throws ArchivaRestServiceException
146 List<String> observableRepos = getObservableRepos();
147 List<String> repos = ListUtils.intersection( observableRepos, selectedRepos );
148 if ( repos == null || repos.isEmpty() )
150 return new GroupIdList( Collections.<String>emptyList() );
154 return new GroupIdList( new ArrayList<String>( repositorySearch.getAllGroupIds( getPrincipal(), repos ) ) );
156 catch ( RepositorySearchException e )
158 log.error( e.getMessage(), e );
159 throw new ArchivaRestServiceException( e.getMessage() );
164 public List<Dependency> getDependencies( String groupId, String artifactId, String version )
165 throws ArchivaRestServiceException
167 return null; //To change body of implemented methods use File | Settings | File Templates.
170 public List<Artifact> getArtifactByChecksum( String checksum )
171 throws ArchivaRestServiceException
173 return null; //To change body of implemented methods use File | Settings | File Templates.
177 protected List<String> getObservableRepos()
181 List<String> ids = userRepositories.getObservableRepositoryIds( getPrincipal() );
182 return ids == null ? Collections.<String>emptyList() : ids;
184 catch ( PrincipalNotFoundException e )
186 log.warn( e.getMessage(), e );
188 catch ( AccessDeniedException e )
190 log.warn( e.getMessage(), e );
192 catch ( ArchivaSecurityException e )
194 log.warn( e.getMessage(), e );
196 return Collections.emptyList();
199 protected String getPrincipal()
201 RedbackRequestInformation redbackRequestInformation = RedbackAuthenticationThreadLocal.get();
203 return redbackRequestInformation == null
204 ? UserManager.GUEST_USERNAME
205 : ( redbackRequestInformation.getUser() == null
206 ? UserManager.GUEST_USERNAME
207 : redbackRequestInformation.getUser().getUsername() );
210 protected List<Artifact> getArtifacts( SearchResults searchResults )
213 HttpContext httpContext = HttpContextThreadLocal.get();
214 if ( searchResults == null || searchResults.isEmpty() )
216 return Collections.emptyList();
218 List<Artifact> artifacts = new ArrayList<Artifact>( searchResults.getReturnedHitsCount() );
219 for ( SearchResultHit hit : searchResults.getHits() )
221 // duplicate Artifact one per available version
222 if ( hit.getVersions().size() > 0 )
224 for ( String version : hit.getVersions() )
227 Artifact versionned = new Artifact( );
228 versionned.setArtifactId( hit.getArtifactId());
229 versionned.setGroupId( hit.getGroupId() );
230 versionned.setRepositoryId(hit.getRepositoryId() );
233 versionned.setBundleExportPackage( hit.getBundleExportPackage() );
234 versionned.setBundleExportService( hit.getBundleExportService());
235 versionned.setBundleSymbolicName(hit.getBundleSymbolicName() );
236 versionned.setBundleVersion( artifactInfo.bundleVersion );
237 versionned.setBundleDescription( artifactInfo.bundleDescription );
238 versionned.setBundleDocUrl( artifactInfo.bundleDocUrl );
240 versionned.setBundleRequireBundle( artifactInfo.bundleRequireBundle );
241 versionned.setBundleImportPackage( artifactInfo.bundleImportPackage );
242 versionned.setBundleLicense( artifactInfo.bundleLicense );
243 versionned.setBundleName( artifactInfo.bundleName );
244 versionned.setContext( artifactInfo.context );
245 versionned.setGoals( artifactInfo.goals );
246 versionned.setPrefix( artifactInfo.prefix );
248 versionned.setUrl( artifactInfo.remoteUrl );
250 // FIXME archiva url ??
252 Artifact versionned = new BeanReplicator().replicateBean( hit, Artifact.class );
254 if ( StringUtils.isNotBlank( version ) )
256 versionned.setVersion( version );
257 versionned.setUrl( getArtifactUrl( httpContext, versionned ) );
259 artifacts.add( versionned );
268 private String getArtifactUrl( HttpContext httpContext, Artifact artifact )
270 if ( httpContext == null )
274 if ( httpContext.getHttpServletRequest() == null )
278 if ( StringUtils.isEmpty( artifact.getUrl() ) )
282 StringBuilder sb = new StringBuilder( getBaseUrl( httpContext.getHttpServletRequest() ) );
284 sb.append( "/repository" );
285 if ( !StringUtils.startsWith( artifact.getUrl(), "/" ) )
289 sb.append( artifact.getUrl() );
290 return sb.toString();
293 protected String getBaseUrl( HttpServletRequest req )
295 return req.getScheme() + "://" + req.getServerName() + ( req.getServerPort() == 80
297 : ":" + req.getServerPort() ) + req.getContextPath();