]> source.dussan.org Git - archiva.git/commitdiff
[MRM-1490] REST services : search now returns classifier : fix new services based...
authorOlivier Lamy <olamy@apache.org>
Thu, 15 Sep 2011 08:59:28 +0000 (08:59 +0000)
committerOlivier Lamy <olamy@apache.org>
Thu, 15 Sep 2011 08:59:28 +0000 (08:59 +0000)
git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1171015 13f79535-47bb-0310-9956-ffa450edef68

archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/NexusRepositorySearch.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/model/Artifact.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/SearchService.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultSearchService.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/SearchServiceTest.java

index 86c9230d86b5cbe03171883f8716d530b2a9dfdb..5ffba7f8865c71b81d9a6dc3a3ae9a76087a8bfa 100644 (file)
@@ -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 );
             }
index 999728becfd51a0baddadb288a45a588e9175fb4..cb45a4a605424cc123fed375b102f0dc9b3233b3 100644 (file)
@@ -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();
     }
index 0575d7bb4ef37bb3bd02c3694e3cf45e061f68c0..7f77edbdaa8db6791f60f304b2e32f6ecc0f98ec 100644 (file)
@@ -57,7 +57,8 @@ public interface SearchService
     @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN } )
     @RedbackAuthorization( noPermission = true, noRestriction = true )
     List<Artifact> getArtifactVersions( @QueryParam( "groupId" ) String groupId,
-                                        @QueryParam( "artifactId" ) String artifactId )
+                                        @QueryParam( "artifactId" ) String artifactId,
+                                        @QueryParam( "packaging" ) String packaging )
         throws ArchivaRestServiceException;
 
     @Path( "searchArtifacts" )
index 79ce2fa7e5fccc520d3e86f23eba67bded776daf..74484238940f1ca3c18a6ef497ae11fe509bfeca 100644 (file)
@@ -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.<String>emptyList() );
-            return getArtifacts( searchResults );
+            return getArtifacts( searchResults, new ArrayList<ArtifactFiler>( NoClassifierArtifactFiler.LIST ) );
+
         }
         catch ( RepositorySearchException e )
         {
@@ -87,7 +90,7 @@ public class DefaultSearchService
         }
     }
 
-    public List<Artifact> getArtifactVersions( String groupId, String artifactId )
+    public List<Artifact> 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.<ArtifactFiler>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.<ArtifactFiler>emptyList() );
         }
         catch ( RepositorySearchException e )
         {
@@ -179,7 +182,7 @@ public class DefaultSearchService
                 : redbackRequestInformation.getUser().getUsername() );
     }
 
-    protected List<Artifact> getArtifacts( SearchResults searchResults )
+    protected List<Artifact> getArtifacts( SearchResults searchResults, List<ArtifactFiler> 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<ArtifactFiler> artifactFilers, List<Artifact> 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;
+    }
 }
index 4878ff3f1974b0c096eac8dbd0813fbae409f55d..38ecd35df7423670665bbfe204335ce63b52b917 100644 (file)
@@ -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<Artifact> 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<Artifact> artifacts = searchService.getArtifactVersions( "commons-logging", "commons-logging" );
+        List<Artifact> 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<Artifact> 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
     {