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.Collection;
24 import java.util.Date;
25 import java.util.List;
27 import org.apache.archiva.indexer.search.RepositorySearch;
28 import org.apache.archiva.indexer.search.SearchResultHit;
29 import org.apache.archiva.indexer.search.SearchResultLimits;
30 import org.apache.archiva.indexer.search.SearchResults;
31 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
32 import org.apache.archiva.metadata.model.ProjectVersionReference;
33 import org.apache.archiva.metadata.repository.MetadataResolver;
34 import org.apache.archiva.metadata.repository.storage.maven2.MavenProjectFacet;
35 import org.apache.archiva.web.xmlrpc.api.SearchService;
36 import org.apache.archiva.web.xmlrpc.api.beans.Artifact;
37 import org.apache.archiva.web.xmlrpc.api.beans.Dependency;
38 import org.apache.archiva.web.xmlrpc.security.XmlRpcUserRepositories;
39 import org.apache.maven.archiva.common.utils.VersionUtil;
40 import org.apache.maven.archiva.database.ArchivaDAO;
41 import org.apache.maven.archiva.database.ArtifactDAO;
42 import org.apache.maven.archiva.database.constraints.ArtifactsByChecksumConstraint;
43 import org.apache.maven.archiva.database.constraints.UniqueVersionConstraint;
44 import org.apache.maven.archiva.model.ArchivaArtifact;
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 MetadataResolver metadataResolver;
74 public SearchServiceImpl( XmlRpcUserRepositories xmlRpcUserRepositories, ArchivaDAO archivaDAO,
75 MetadataResolver metadataResolver, RepositorySearch search )
77 this.xmlRpcUserRepositories = xmlRpcUserRepositories;
78 this.archivaDAO = archivaDAO;
80 this.metadataResolver = metadataResolver;
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 = (List<String>) archivaDAO.query(
98 new UniqueVersionConstraint( observableRepos, resultHit.getGroupId(), resultHit.getArtifactId() ) );
99 if ( versions != null && !versions.isEmpty() )
101 resultHit.setVersion( null );
102 resultHit.setVersions( filterTimestampedSnapshots( versions ) );
105 List<String> resultHitVersions = resultHit.getVersions();
106 if ( resultHitVersions != null )
108 for ( String version : resultHitVersions )
110 Artifact artifact = null;
111 for ( String repoId : observableRepos )
113 // slight behaviour change to previous implementation: instead of allocating "jar" when not
114 // found in the database, we can rely on the metadata repository to create it on the fly. We
115 // just allocate the default packaging if the Maven facet is not found.
116 ProjectVersionMetadata model =
117 metadataResolver.getProjectVersion( repoId, resultHit.getGroupId(),
118 resultHit.getArtifactId(), version );
122 String packaging = "jar";
124 MavenProjectFacet facet = (MavenProjectFacet) model.getFacet( MavenProjectFacet.FACET_ID );
125 if ( facet != null && facet.getPackaging() != null )
127 packaging = facet.getPackaging();
129 artifact = new Artifact( repoId, resultHit.getGroupId(), resultHit.getArtifactId(), version,
135 if ( artifact != null )
137 artifacts.add( artifact );
147 * Remove timestamped snapshots from versions
149 private static List<String> filterTimestampedSnapshots( List<String> versions )
151 final List<String> filtered = new ArrayList<String>();
152 for ( final String version : versions )
154 final String baseVersion = VersionUtil.getBaseVersion( version );
155 if ( !filtered.contains( baseVersion ) )
157 filtered.add( baseVersion );
163 public List<Artifact> getArtifactByChecksum( String checksum )
166 // 1. get ArtifactDAO from ArchivaDAO
167 // 2. create ArtifactsByChecksumConstraint( "queryTerm" )
168 // 3. query artifacts using constraint
169 // 4. convert results to list of Artifact objects
171 List<Artifact> results = new ArrayList<Artifact>();
172 ArtifactDAO artifactDAO = archivaDAO.getArtifactDAO();
174 ArtifactsByChecksumConstraint constraint = new ArtifactsByChecksumConstraint( checksum );
175 List<ArchivaArtifact> artifacts = artifactDAO.queryArtifacts( constraint );
177 for ( ArchivaArtifact archivaArtifact : artifacts )
180 new Artifact( archivaArtifact.getModel().getRepositoryId(), archivaArtifact.getModel().getGroupId(),
181 archivaArtifact.getModel().getArtifactId(), archivaArtifact.getModel().getVersion(),
182 archivaArtifact.getType() );
183 //archivaArtifact.getModel().getWhenGathered() );
184 results.add( artifact );
190 public List<Artifact> getArtifactVersions( String groupId, String artifactId )
193 List<Artifact> artifacts = new ArrayList<Artifact>();
194 List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories();
196 for ( String repoId : observableRepos )
198 Collection<String> results = metadataResolver.getProjectVersions( repoId, groupId, artifactId );
200 for ( final String version : results )
202 final Artifact artifact = new Artifact( repoId, groupId, artifactId, version, "pom" );
204 artifacts.add( artifact );
211 public List<Artifact> getArtifactVersionsByDate( String groupId, String artifactId, String version, Date since )
214 List<Artifact> artifacts = new ArrayList<Artifact>();
216 // 1. get observable repositories
217 // 2. use RepositoryBrowsing method to query uniqueVersions? (but with date)
222 public List<Dependency> getDependencies( String groupId, String artifactId, String version )
225 List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories();
227 for ( String repoId : observableRepos )
229 ProjectVersionMetadata model = metadataResolver.getProjectVersion( repoId, groupId, artifactId, version );
232 List<Dependency> dependencies = new ArrayList<Dependency>();
233 List<org.apache.archiva.metadata.model.Dependency> modelDeps = model.getDependencies();
234 for ( org.apache.archiva.metadata.model.Dependency dep : modelDeps )
236 Dependency dependency =
237 new Dependency( dep.getGroupId(), dep.getArtifactId(), dep.getVersion(), dep.getClassifier(),
238 dep.getType(), dep.getScope() );
239 dependencies.add( dependency );
244 throw new Exception( "Artifact does not exist." );
247 public List<Artifact> getDependencyTree( String groupId, String artifactId, String version )
250 List<Artifact> a = new ArrayList<Artifact>();
255 public List<Artifact> getDependees( String groupId, String artifactId, String version )
258 List<Artifact> artifacts = new ArrayList<Artifact>();
259 List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories();
261 for ( String repoId : observableRepos )
263 Collection<ProjectVersionReference> refs =
264 metadataResolver.getProjectReferences( repoId, groupId, artifactId, version );
265 for ( ProjectVersionReference ref : refs )
268 new Artifact( repoId, ref.getNamespace(), ref.getProjectId(), ref.getProjectVersion(), "" ) );