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 org.apache.archiva.indexer.search.RepositorySearch;
23 import org.apache.archiva.indexer.search.SearchResultHit;
24 import org.apache.archiva.indexer.search.SearchResultLimits;
25 import org.apache.archiva.indexer.search.SearchResults;
26 import org.apache.archiva.metadata.model.ArtifactMetadata;
27 import org.apache.archiva.metadata.model.FacetedMetadata;
28 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
29 import org.apache.archiva.metadata.model.ProjectVersionReference;
30 import org.apache.archiva.metadata.repository.MetadataRepository;
31 import org.apache.archiva.metadata.repository.MetadataResolver;
32 import org.apache.archiva.metadata.repository.storage.maven2.MavenArtifactFacet;
33 import org.apache.archiva.metadata.repository.storage.maven2.MavenProjectFacet;
34 import org.apache.archiva.web.xmlrpc.api.SearchService;
35 import org.apache.archiva.web.xmlrpc.api.beans.Artifact;
36 import org.apache.archiva.web.xmlrpc.api.beans.Dependency;
37 import org.apache.archiva.web.xmlrpc.security.XmlRpcUserRepositories;
39 import java.util.ArrayList;
40 import java.util.Collection;
41 import java.util.Date;
42 import java.util.List;
44 public class SearchServiceImpl
45 implements SearchService
47 private RepositorySearch search;
49 private XmlRpcUserRepositories xmlRpcUserRepositories;
51 private MetadataResolver metadataResolver;
53 private MetadataRepository metadataRepository;
55 public SearchServiceImpl( XmlRpcUserRepositories xmlRpcUserRepositories, MetadataResolver metadataResolver,
56 MetadataRepository metadataRepository, RepositorySearch search )
58 this.xmlRpcUserRepositories = xmlRpcUserRepositories;
60 this.metadataResolver = metadataResolver;
61 this.metadataRepository = metadataRepository;
64 @SuppressWarnings( "unchecked" )
65 public List<Artifact> quickSearch( String queryString )
68 List<Artifact> artifacts = new ArrayList<Artifact>();
69 List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories();
70 SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES );
71 SearchResults results;
73 results = search.search( "", observableRepos, queryString, limits, null );
75 for ( SearchResultHit resultHit : results.getHits() )
77 List<String> resultHitVersions = resultHit.getVersions();
78 if ( resultHitVersions != null )
80 for ( String version : resultHitVersions )
82 Artifact artifact = null;
83 for ( String repoId : observableRepos )
85 // slight behaviour change to previous implementation: instead of allocating "jar" when not
86 // found in the database, we can rely on the metadata repository to create it on the fly. We
87 // just allocate the default packaging if the Maven facet is not found.
88 FacetedMetadata model = metadataResolver.resolveProjectVersion( repoId, resultHit.getGroupId(),
89 resultHit.getArtifactId(),
94 String packaging = "jar";
96 MavenProjectFacet facet = (MavenProjectFacet) model.getFacet( MavenProjectFacet.FACET_ID );
97 if ( facet != null && facet.getPackaging() != null )
99 packaging = facet.getPackaging();
101 artifact = new Artifact( repoId, resultHit.getGroupId(), resultHit.getArtifactId(), version,
107 if ( artifact != null )
109 artifacts.add( artifact );
118 public List<Artifact> getArtifactByChecksum( String checksum )
121 List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories();
123 List<Artifact> results = new ArrayList<Artifact>();
124 for ( String repoId : observableRepos )
126 for ( ArtifactMetadata artifact : metadataRepository.getArtifactsByChecksum( repoId, checksum ) )
128 // TODO: customise XMLRPC to handle non-Maven artifacts
129 MavenArtifactFacet facet = (MavenArtifactFacet) artifact.getFacet( MavenArtifactFacet.FACET_ID );
131 results.add( new Artifact( artifact.getRepositoryId(), artifact.getNamespace(), artifact.getProject(),
132 artifact.getVersion(), facet != null ? facet.getType() : null ) );
138 public List<Artifact> getArtifactVersions( String groupId, String artifactId )
141 List<Artifact> artifacts = new ArrayList<Artifact>();
142 List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories();
144 for ( String repoId : observableRepos )
146 Collection<String> results = metadataResolver.resolveProjectVersions( repoId, groupId, artifactId );
148 for ( final String version : results )
150 final Artifact artifact = new Artifact( repoId, groupId, artifactId, version, "pom" );
152 artifacts.add( artifact );
159 public List<Artifact> getArtifactVersionsByDate( String groupId, String artifactId, String version, Date since )
162 // List<Artifact> artifacts = new ArrayList<Artifact>();
164 // 1. get observable repositories
165 // 2. use RepositoryBrowsing method to query uniqueVersions? (but with date)
167 throw new UnsupportedOperationException( "getArtifactVersionsByDate not yet implemented" );
172 public List<Dependency> getDependencies( String groupId, String artifactId, String version )
175 List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories();
177 for ( String repoId : observableRepos )
179 ProjectVersionMetadata model = metadataResolver.resolveProjectVersion( repoId, groupId, artifactId,
183 List<Dependency> dependencies = new ArrayList<Dependency>();
184 List<org.apache.archiva.metadata.model.Dependency> modelDeps = model.getDependencies();
185 for ( org.apache.archiva.metadata.model.Dependency dep : modelDeps )
187 Dependency dependency = new Dependency( dep.getGroupId(), dep.getArtifactId(), dep.getVersion(),
188 dep.getClassifier(), dep.getType(), dep.getScope() );
189 dependencies.add( dependency );
194 throw new Exception( "Artifact does not exist." );
197 public List<Artifact> getDependencyTree( String groupId, String artifactId, String version )
200 // List<Artifact> a = new ArrayList<Artifact>();
202 throw new UnsupportedOperationException( "getDependencyTree not yet implemented" );
206 public List<Artifact> getDependees( String groupId, String artifactId, String version )
209 List<Artifact> artifacts = new ArrayList<Artifact>();
210 List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories();
212 for ( String repoId : observableRepos )
214 Collection<ProjectVersionReference> refs = metadataResolver.resolveProjectReferences( repoId, groupId,
215 artifactId, version );
216 for ( ProjectVersionReference ref : refs )
218 artifacts.add( new Artifact( repoId, ref.getNamespace(), ref.getProjectId(), ref.getProjectVersion(),