From 979fee6e57a1d391dcc423c9868dba2a1ff7116c Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Thu, 15 Sep 2011 08:59:28 +0000 Subject: [PATCH] [MRM-1490] REST services : search now returns classifier : fix new services based on this git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1171015 13f79535-47bb-0310-9956-ffa450edef68 --- .../indexer/search/NexusRepositorySearch.java | 1 + .../archiva/rest/api/model/Artifact.java | 15 +++++++ .../rest/api/services/SearchService.java | 3 +- .../rest/services/DefaultSearchService.java | 42 +++++++++++++++---- .../rest/services/SearchServiceTest.java | 24 +++++++---- 5 files changed, 68 insertions(+), 17 deletions(-) diff --git a/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/NexusRepositorySearch.java b/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/NexusRepositorySearch.java index 86c9230d8..5ffba7f88 100644 --- a/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/NexusRepositorySearch.java +++ b/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/NexusRepositorySearch.java @@ -406,6 +406,7 @@ public class NexusRepositorySearch hit.setGoals( artifactInfo.goals ); hit.setPrefix( artifactInfo.prefix ); hit.setPackaging( artifactInfo.packaging ); + hit.setClassifier( artifactInfo.classifier ); // sure ?? hit.setUrl( artifactInfo.remoteUrl ); } diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/model/Artifact.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/model/Artifact.java index 999728bec..cb45a4a60 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/model/Artifact.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/model/Artifact.java @@ -125,6 +125,8 @@ public class Artifact private String classifier; + private String packaging; + public Artifact() { @@ -321,6 +323,18 @@ public class Artifact this.classifier = classifier; } + + public String getPackaging() + { + return packaging; + } + + public void setPackaging( String packaging ) + { + this.packaging = packaging; + } + + @Override public String toString() { @@ -345,6 +359,7 @@ public class Artifact sb.append( ", bundleImportPackage='" ).append( bundleImportPackage ).append( '\'' ); sb.append( ", bundleRequireBundle='" ).append( bundleRequireBundle ).append( '\'' ); sb.append( ", classifier='" ).append( classifier ).append( '\'' ); + sb.append( ", packaging='" ).append( packaging ).append( '\'' ); sb.append( '}' ); return sb.toString(); } diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/SearchService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/SearchService.java index 0575d7bb4..7f77edbda 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/SearchService.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/SearchService.java @@ -57,7 +57,8 @@ public interface SearchService @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN } ) @RedbackAuthorization( noPermission = true, noRestriction = true ) List getArtifactVersions( @QueryParam( "groupId" ) String groupId, - @QueryParam( "artifactId" ) String artifactId ) + @QueryParam( "artifactId" ) String artifactId, + @QueryParam( "packaging" ) String packaging ) throws ArchivaRestServiceException; @Path( "searchArtifacts" ) diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultSearchService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultSearchService.java index 79ce2fa7e..744842389 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultSearchService.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultSearchService.java @@ -30,6 +30,8 @@ import org.apache.archiva.rest.api.model.Dependency; import org.apache.archiva.rest.api.model.SearchRequest; import org.apache.archiva.rest.api.services.ArchivaRestServiceException; import org.apache.archiva.rest.api.services.SearchService; +import org.apache.archiva.rest.services.searchfilter.ArtifactFiler; +import org.apache.archiva.rest.services.searchfilter.NoClassifierArtifactFiler; import org.apache.archiva.security.AccessDeniedException; import org.apache.archiva.security.ArchivaSecurityException; import org.apache.archiva.security.PrincipalNotFoundException; @@ -78,7 +80,8 @@ public class DefaultSearchService SearchResults searchResults = repositorySearch.search( getPrincipal(), getObservableRepos(), queryString, limits, Collections.emptyList() ); - return getArtifacts( searchResults ); + return getArtifacts( searchResults, new ArrayList( NoClassifierArtifactFiler.LIST ) ); + } catch ( RepositorySearchException e ) { @@ -87,7 +90,7 @@ public class DefaultSearchService } } - public List getArtifactVersions( String groupId, String artifactId ) + public List getArtifactVersions( String groupId, String artifactId, String packaging ) throws ArchivaRestServiceException { if ( StringUtils.isBlank( groupId ) || StringUtils.isBlank( artifactId ) ) @@ -97,12 +100,12 @@ public class DefaultSearchService SearchFields searchField = new SearchFields(); searchField.setGroupId( groupId ); searchField.setArtifactId( artifactId ); - SearchResultLimits limits = new SearchResultLimits( 0 ); + searchField.setPackaging( StringUtils.isBlank( packaging ) ? "jar" : packaging ); try { - SearchResults searchResults = repositorySearch.search( getPrincipal(), searchField, limits ); - return getArtifacts( searchResults ); + SearchResults searchResults = repositorySearch.search( getPrincipal(), searchField, null ); + return getArtifacts( searchResults, Collections.emptyList() ); } catch ( RepositorySearchException e ) { @@ -124,7 +127,7 @@ public class DefaultSearchService try { SearchResults searchResults = repositorySearch.search( getPrincipal(), searchField, limits ); - return getArtifacts( searchResults ); + return getArtifacts( searchResults, Collections.emptyList() ); } catch ( RepositorySearchException e ) { @@ -179,7 +182,7 @@ public class DefaultSearchService : redbackRequestInformation.getUser().getUsername() ); } - protected List getArtifacts( SearchResults searchResults ) + protected List getArtifacts( SearchResults searchResults, List artifactFilers ) { if ( searchResults == null || searchResults.isEmpty() ) { @@ -223,11 +226,34 @@ public class DefaultSearchService if ( StringUtils.isNotBlank( version ) ) { versionned.setVersion( version ); - artifacts.add( versionned ); + if ( applyFiltering( versionned, artifactFilers, artifacts ) ) + { + artifacts.add( versionned ); + } } } } } return artifacts; } + + protected boolean applyFiltering( Artifact artifact, List artifactFilers, List artifacts ) + { + if ( artifact == null ) + { + return false; + } + if ( artifactFilers == null || artifactFilers.isEmpty() ) + { + return true; + } + for ( ArtifactFiler filter : artifactFilers ) + { + if ( !filter.addArtifactInResult( artifact, artifacts ) ) + { + return false; + } + } + return true; + } } diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/SearchServiceTest.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/SearchServiceTest.java index 4878ff3f1..38ecd35df 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/SearchServiceTest.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/SearchServiceTest.java @@ -27,6 +27,7 @@ import org.apache.commons.io.FileUtils; import org.junit.Test; import java.io.File; +import java.util.Date; import java.util.List; /** @@ -56,7 +57,8 @@ public class SearchServiceTest List artifacts = searchService.quickSearch( "commons-logging" ); assertNotNull( artifacts ); - assertTrue( " empty results for commons-logging search", artifacts.size() == 6 ); + assertTrue( " not 6 results for commons-logging search but " + artifacts.size() + ":" + artifacts, + artifacts.size() == 6 ); log.info( "artifacts for commons-logging size {} search {}", artifacts.size(), artifacts ); deleteTestRepo( testRepoId, targetRepo ); @@ -78,17 +80,18 @@ public class SearchServiceTest SearchService searchService = getSearchService( authorizationHeader ); - List artifacts = searchService.getArtifactVersions( "commons-logging", "commons-logging" ); + List artifacts = searchService.getArtifactVersions( "commons-logging", "commons-logging", "jar" ); assertNotNull( artifacts ); - assertTrue( " empty results for commons-logging search", artifacts.size() == 6 ); + assertTrue( " not 3 results for commons-logging search but " + artifacts.size() + ":" + artifacts, + artifacts.size() == 13 ); log.info( "artifacts for commons-logging size {} search {}", artifacts.size(), artifacts ); deleteTestRepo( testRepoId, targetRepo ); } @Test - public void searchWithSearchRequestGroupIdAndArtifactId() + public void searchWithSearchRequestGroupIdAndArtifactIdAndClassifier() throws Exception { @@ -103,7 +106,7 @@ public class SearchServiceTest SearchService searchService = getSearchService( authorizationHeader ); - SearchRequest searchRequest = new SearchRequest( ); + SearchRequest searchRequest = new SearchRequest(); searchRequest.setGroupId( "commons-logging" ); searchRequest.setArtifactId( "commons-logging" ); searchRequest.setClassifier( "sources" ); @@ -111,7 +114,8 @@ public class SearchServiceTest List artifacts = searchService.searchArtifacts( searchRequest ); assertNotNull( artifacts ); - assertTrue( " empty results for commons-logging search", artifacts.size() == 6 ); + assertTrue( " not 2 results for commons-logging search but " + artifacts.size() + ":" + artifacts, + artifacts.size() == 2 ); log.info( "artifacts for commons-logging size {} search {}", artifacts.size(), artifacts ); deleteTestRepo( testRepoId, targetRepo ); @@ -120,6 +124,10 @@ public class SearchServiceTest private File createAndIndexRepo( String testRepoId ) throws Exception { + if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( testRepoId ) != null ) + { + getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( testRepoId, true ); + } File targetRepo = new File( System.getProperty( "targetDir", "./target" ), "test-repo" ); cleanupFiles( targetRepo ); @@ -132,11 +140,11 @@ public class SearchServiceTest managedRepository.setName( "test repo" ); managedRepository.setLocation( targetRepo.getPath() ); + managedRepository.setIndexDirectory( targetRepo.getPath() + "/index-" + Long.toString( new Date().getTime() ) ); ManagedRepositoriesService service = getManagedRepositoriesService( authorizationHeader ); service.addManagedRepository( managedRepository ); - getRepositoriesService( authorizationHeader ).scanRepositoryNow( testRepoId, true ); return targetRepo; @@ -153,7 +161,7 @@ public class SearchServiceTest } - private void cleanupFiles(File targetRepo) + private void cleanupFiles( File targetRepo ) throws Exception {