1 package org.apache.archiva.web.xmlrpc.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 java.util.ArrayList;
23 import java.util.Date;
24 import java.util.List;
26 import org.apache.archiva.indexer.search.RepositorySearch;
27 import org.apache.archiva.indexer.search.SearchResultHit;
28 import org.apache.archiva.indexer.search.SearchResultLimits;
29 import org.apache.archiva.indexer.search.SearchResults;
30 import org.apache.archiva.web.xmlrpc.api.SearchService;
31 import org.apache.archiva.web.xmlrpc.api.beans.Artifact;
32 import org.apache.archiva.web.xmlrpc.api.beans.Dependency;
33 import org.apache.archiva.web.xmlrpc.security.XmlRpcUserRepositories;
34 import org.apache.maven.archiva.common.utils.VersionUtil;
35 import org.apache.maven.archiva.database.ArchivaDAO;
36 import org.apache.maven.archiva.database.ArchivaDatabaseException;
37 import org.apache.maven.archiva.database.ArtifactDAO;
38 import org.apache.maven.archiva.database.ObjectNotFoundException;
39 import org.apache.maven.archiva.database.browsing.BrowsingResults;
40 import org.apache.maven.archiva.database.browsing.RepositoryBrowsing;
41 import org.apache.maven.archiva.database.constraints.ArtifactsByChecksumConstraint;
42 import org.apache.maven.archiva.database.constraints.UniqueVersionConstraint;
43 import org.apache.maven.archiva.model.ArchivaArtifact;
44 import org.apache.maven.archiva.model.ArchivaProjectModel;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
51 * quick/general text search which returns a list of artifacts
52 * query for an artifact based on a checksum
53 * query for all available versions of an artifact, sorted in version significance order
54 * query for all available versions of an artifact since a given date
55 * query for an artifact's direct dependencies
56 * query for an artifact's dependency tree (as with mvn dependency:tree - no duplicates should be included)
57 * query for all artifacts that depend on a given artifact
59 * @version $Id: SearchServiceImpl.java
61 public class SearchServiceImpl
62 implements SearchService
64 private Logger log = LoggerFactory.getLogger( SearchServiceImpl.class );
66 private RepositorySearch search;
68 private XmlRpcUserRepositories xmlRpcUserRepositories;
70 private ArchivaDAO archivaDAO;
72 private RepositoryBrowsing repoBrowsing;
74 public SearchServiceImpl( XmlRpcUserRepositories xmlRpcUserRepositories, ArchivaDAO archivaDAO,
75 RepositoryBrowsing repoBrowsing, RepositorySearch search )
77 this.xmlRpcUserRepositories = xmlRpcUserRepositories;
78 this.archivaDAO = archivaDAO;
79 this.repoBrowsing = repoBrowsing;
83 @SuppressWarnings( "unchecked" )
84 public List<Artifact> quickSearch( String queryString )
87 List<Artifact> artifacts = new ArrayList<Artifact>();
88 List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories();
89 SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES );
90 SearchResults results = null;
92 results = search.search( "", observableRepos, queryString, limits, null );
94 for ( SearchResultHit resultHit : results.getHits() )
96 // double-check all versions as done in SearchAction
97 final List<String> versions =
98 (List<String>) archivaDAO.query( new UniqueVersionConstraint( observableRepos, resultHit.getGroupId(),
99 resultHit.getArtifactId() ) );
100 if ( versions != null && !versions.isEmpty() )
102 resultHit.setVersion( null );
103 resultHit.setVersions( filterTimestampedSnapshots( versions ) );
106 List<String> resultHitVersions = resultHit.getVersions();
107 if( resultHitVersions != null )
109 for( String version : resultHitVersions )
113 ArchivaProjectModel model = repoBrowsing.selectVersion( "", observableRepos, resultHit.getGroupId(), resultHit.getArtifactId(), version );
115 String repoId = repoBrowsing.getRepositoryId( "", observableRepos, resultHit.getGroupId(), resultHit.getArtifactId(), version );
117 Artifact artifact = null;
120 artifact = new Artifact( repoId, resultHit.getGroupId(), resultHit.getArtifactId(), version, "jar" );
124 artifact = new Artifact( repoId, model.getGroupId(), model.getArtifactId(), version, model.getPackaging() );
126 artifacts.add( artifact );
128 catch( ObjectNotFoundException e )
130 log.debug( "Unable to find pom artifact : " + e.getMessage() );
132 catch( ArchivaDatabaseException e )
134 log.debug( "Error occurred while getting pom artifact from database : " + e.getMessage() );
144 * Remove timestamped snapshots from versions
146 private static List<String> filterTimestampedSnapshots(List<String> versions)
148 final List<String> filtered = new ArrayList<String>();
149 for (final String version : versions)
151 final String baseVersion = VersionUtil.getBaseVersion(version);
152 if (!filtered.contains(baseVersion))
154 filtered.add(baseVersion);
160 public List<Artifact> getArtifactByChecksum( String checksum )
163 // 1. get ArtifactDAO from ArchivaDAO
164 // 2. create ArtifactsByChecksumConstraint( "queryTerm" )
165 // 3. query artifacts using constraint
166 // 4. convert results to list of Artifact objects
168 List<Artifact> results = new ArrayList<Artifact>();
169 ArtifactDAO artifactDAO = archivaDAO.getArtifactDAO();
171 ArtifactsByChecksumConstraint constraint = new ArtifactsByChecksumConstraint( checksum );
172 List<ArchivaArtifact> artifacts = artifactDAO.queryArtifacts( constraint );
174 for( ArchivaArtifact archivaArtifact : artifacts )
176 Artifact artifact = new Artifact( archivaArtifact.getModel().getRepositoryId(), archivaArtifact.getModel().getGroupId(),
177 archivaArtifact.getModel().getArtifactId(), archivaArtifact.getModel().getVersion(), archivaArtifact.getType() );
178 //archivaArtifact.getModel().getWhenGathered() );
179 results.add( artifact );
185 public List<Artifact> getArtifactVersions( String groupId, String artifactId )
188 final List<Artifact> artifacts = new ArrayList<Artifact>();
189 final List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories();
191 final BrowsingResults results = repoBrowsing.selectArtifactId( "", observableRepos, groupId, artifactId );
193 for( final String version : results.getVersions() )
195 final Artifact artifact = new Artifact( "", groupId, artifactId, version, "pom" );
196 //ArchivaArtifact pomArtifact = artifactDAO.getArtifact( groupId, artifactId, version, "", "pom", );
197 //Artifact artifact = new Artifact( "", groupId, artifactId, version, pomArtifact.getType() );
198 //pomArtifact.getModel().getWhenGathered() );
200 artifacts.add( artifact );
203 // 1. get observable repositories
204 // 2. use RepositoryBrowsing method to query uniqueVersions?
208 public List<Artifact> getArtifactVersionsByDate( String groupId, String artifactId, String version, Date since )
211 List<Artifact> artifacts = new ArrayList<Artifact>();
213 // 1. get observable repositories
214 // 2. use RepositoryBrowsing method to query uniqueVersions? (but with date)
219 public List<Dependency> getDependencies( String groupId, String artifactId, String version )
222 List<Dependency> dependencies = new ArrayList<Dependency>();
223 List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories();
227 ArchivaProjectModel model = repoBrowsing.selectVersion( "", observableRepos, groupId, artifactId, version );
228 List<org.apache.maven.archiva.model.Dependency> modelDeps = model.getDependencies();
229 for( org.apache.maven.archiva.model.Dependency dep : modelDeps )
231 Dependency dependency = new Dependency(
232 dep.getGroupId(), dep.getArtifactId(), dep.getVersion(), dep.getClassifier(), dep.getType(), dep.getScope() );
233 dependencies.add( dependency );
236 catch ( ObjectNotFoundException oe )
238 throw new Exception( "Artifact does not exist." );
244 public List<Artifact> getDependencyTree( String groupId, String artifactId, String version )
247 List<Artifact> a = new ArrayList<Artifact>();
252 //get artifacts that depend on a given artifact
253 public List<Artifact> getDependees( String groupId, String artifactId, String version )
256 List<Artifact> artifacts = new ArrayList<Artifact>();
257 List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories();
259 List<ArchivaProjectModel> dependees = repoBrowsing.getUsedBy( "", observableRepos, groupId, artifactId, version );
260 for( ArchivaProjectModel model : dependees )
263 new Artifact( "", model.getGroupId(), model.getArtifactId(), model.getVersion(), "" );
264 //model.getWhenIndexed() );
265 artifacts.add( artifact );