]> source.dussan.org Git - archiva.git/commitdiff
fix quick search which returns artifacts with classifier too : hackhish solution...
authorOlivier Lamy <olamy@apache.org>
Thu, 15 Sep 2011 15:45:49 +0000 (15:45 +0000)
committerOlivier Lamy <olamy@apache.org>
Thu, 15 Sep 2011 15:45:49 +0000 (15:45 +0000)
git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1171151 13f79535-47bb-0310-9956-ffa450edef68

archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/ArtifactInfoFiler.java [new file with mode: 0644]
archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/NexusRepositorySearch.java
archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/NoClassifierArtifactInfoFiler.java [new file with mode: 0644]
archiva-modules/archiva-base/archiva-indexer/src/test/java/org/apache/archiva/indexer/search/NexusRepositorySearchTest.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/main/java/org/apache/archiva/rest/services/searchfilter/ArtifactFiler.java [deleted file]
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/searchfilter/NoClassifierArtifactFiler.java [deleted file]

diff --git a/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/ArtifactInfoFiler.java b/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/ArtifactInfoFiler.java
new file mode 100644 (file)
index 0000000..4325800
--- /dev/null
@@ -0,0 +1,32 @@
+package org.apache.archiva.indexer.search;
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.maven.index.ArtifactInfo;
+
+import java.util.Map;
+
+/**
+ * @author Olivier Lamy
+ * @since 1.4
+ */
+public interface ArtifactInfoFiler
+{
+    boolean addArtifactInResult( ArtifactInfo artifact, Map<String, SearchResultHit> currentResult );
+}
index 5ffba7f8865c71b81d9a6dc3a3ae9a76087a8bfa..cdc906c22d36d07668b53e4fad7a2b08fffb0579 100644 (file)
@@ -47,6 +47,7 @@ import javax.inject.Inject;
 import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -110,7 +111,10 @@ public class NexusRepositorySearch
             q.add( iQuery, Occur.MUST );
         }
 
-        return search( limits, q, indexingContextIds );
+        // we retun only artifacts without classifier in quick search, olamy cannot find a way to say with this field empty
+        // FIXME  cannot find a way currently to setup this in constructQuery !!!
+        return search( limits, q, indexingContextIds, NoClassifierArtifactInfoFiler.LIST );
+
     }
 
     /**
@@ -199,10 +203,11 @@ public class NexusRepositorySearch
             throw new RepositorySearchException( "No search fields set." );
         }
 
-        return search( limits, q, indexingContextIds );
+        return search( limits, q, indexingContextIds, Collections.<ArtifactInfoFiler>emptyList() );
     }
 
-    private SearchResults search( SearchResultLimits limits, BooleanQuery q, List<String> indexingContextIds )
+    private SearchResults search( SearchResultLimits limits, BooleanQuery q, List<String> indexingContextIds,
+                                  List<? extends ArtifactInfoFiler> filters )
         throws RepositorySearchException
     {
 
@@ -219,7 +224,7 @@ public class NexusRepositorySearch
                 return results;
             }
 
-            return convertToSearchResults( response, limits );
+            return convertToSearchResults( response, limits, filters );
         }
         catch ( IOException e )
         {
@@ -275,6 +280,10 @@ public class NexusRepositorySearch
         q.add( indexer.constructQuery( MAVEN.VERSION, new StringSearchExpression( term ) ), Occur.SHOULD );
         q.add( indexer.constructQuery( MAVEN.PACKAGING, new StringSearchExpression( term ) ), Occur.SHOULD );
         q.add( indexer.constructQuery( MAVEN.CLASSNAMES, new StringSearchExpression( term ) ), Occur.SHOULD );
+
+        //Query query =
+        //    new WildcardQuery( new Term( MAVEN.CLASSNAMES.getFieldName(), "*" ) );
+        //q.add( query, Occur.MUST_NOT );
         // olamy IMHO we could set this option as at least one must match
         //q.setMinimumNumberShouldMatch( 1 );
     }
@@ -363,7 +372,8 @@ public class NexusRepositorySearch
     }
 
 
-    private SearchResults convertToSearchResults( FlatSearchResponse response, SearchResultLimits limits )
+    private SearchResults convertToSearchResults( FlatSearchResponse response, SearchResultLimits limits,
+                                                  List<? extends ArtifactInfoFiler> artifactInfoFilers )
     {
         SearchResults results = new SearchResults();
         Set<ArtifactInfo> artifactInfos = response.getResults();
@@ -374,6 +384,11 @@ public class NexusRepositorySearch
                                              artifactInfo.packaging );
             Map<String, SearchResultHit> hitsMap = results.getHitsMap();
 
+            if ( !applyArtifactInfoFilters( artifactInfo, artifactInfoFilers, hitsMap ) )
+            {
+                continue;
+            }
+
             SearchResultHit hit = hitsMap.get( id );
             if ( hit != null )
             {
@@ -428,6 +443,25 @@ public class NexusRepositorySearch
         }
     }
 
+    private boolean applyArtifactInfoFilters( ArtifactInfo artifactInfo,
+                                              List<? extends ArtifactInfoFiler> artifactInfoFilers,
+                                              Map<String, SearchResultHit> currentResult )
+    {
+        if ( artifactInfoFilers == null || artifactInfoFilers.isEmpty() )
+        {
+            return true;
+        }
+
+        for ( ArtifactInfoFiler filter : artifactInfoFilers )
+        {
+            if ( !filter.addArtifactInResult( artifactInfo, currentResult ) )
+            {
+                return false;
+            }
+        }
+        return true;
+    }
+
     private SearchResults paginate( SearchResults results )
     {
         SearchResultLimits limits = results.getLimits();
diff --git a/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/NoClassifierArtifactInfoFiler.java b/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/NoClassifierArtifactInfoFiler.java
new file mode 100644 (file)
index 0000000..0789702
--- /dev/null
@@ -0,0 +1,42 @@
+package org.apache.archiva.indexer.search;
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.maven.index.ArtifactInfo;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author Olivier Lamy
+ */
+public class NoClassifierArtifactInfoFiler
+    implements ArtifactInfoFiler
+{
+    public static final NoClassifierArtifactInfoFiler INSTANCE = new NoClassifierArtifactInfoFiler();
+
+    public static final List<? extends ArtifactInfoFiler> LIST = Arrays.asList( INSTANCE );
+
+    public boolean addArtifactInResult( ArtifactInfo artifact, Map<String, SearchResultHit> currentResult )
+    {
+        return StringUtils.isBlank( artifact.classifier );
+    }
+}
index fb6f4ae0d6c779bfd3f1e644e5ae845014cbc07a..2f7da7585f8e1992ac1fb75f52fd2be044a860b7 100644 (file)
@@ -131,6 +131,49 @@ public class NexusRepositorySearchTest
         //TODO: search for class & package names
     }
 
+    @Test
+    public void testQuickSearchNotWithClassifier()
+        throws Exception
+    {
+        createSimpleIndex( true );
+
+        List<String> selectedRepos = Arrays.asList( TEST_REPO_1 );
+
+        // search artifactId
+        archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
+
+        archivaConfigControl.replay();
+
+        SearchResults results = search.search( "user", selectedRepos, "archiva-search", null, null );
+
+        archivaConfigControl.verify();
+
+        assertNotNull( results );
+
+        SearchResultHit hit =
+            results.getSearchResultHit( SearchUtil.getHitId( "org.apache.archiva", "archiva-search", null, "jar" ) );
+        assertNotNull( "hit null in result " + results.getHits(), hit );
+        assertEquals( "org.apache.archiva", hit.getGroupId() );
+        assertEquals( "archiva-search", hit.getArtifactId() );
+        assertEquals( "1.0", hit.getVersions().get( 0 ) );
+
+        archivaConfigControl.reset();
+
+        // search groupId
+        archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
+
+        archivaConfigControl.replay();
+
+        results = search.search( "user", selectedRepos, "archiva-search", null, null );
+
+        archivaConfigControl.verify();
+
+        assertNotNull( results );
+        assertEquals( "total hints not 3 hits " + results.getHits(), 3, results.getTotalHits() );
+
+        //TODO: search for class & package names
+    }
+
     @Test
     public void testQuickSearchMultipleArtifactsSameVersion()
         throws Exception
index 74484238940f1ca3c18a6ef497ae11fe509bfeca..3f79041d17fa13fca2ae025f10f76be31bfd95a6 100644 (file)
@@ -30,8 +30,6 @@ 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;
@@ -80,7 +78,7 @@ public class DefaultSearchService
             SearchResults searchResults =
                 repositorySearch.search( getPrincipal(), getObservableRepos(), queryString, limits,
                                          Collections.<String>emptyList() );
-            return getArtifacts( searchResults, new ArrayList<ArtifactFiler>( NoClassifierArtifactFiler.LIST ) );
+            return getArtifacts( searchResults);
 
         }
         catch ( RepositorySearchException e )
@@ -105,7 +103,7 @@ public class DefaultSearchService
         try
         {
             SearchResults searchResults = repositorySearch.search( getPrincipal(), searchField, null );
-            return getArtifacts( searchResults, Collections.<ArtifactFiler>emptyList() );
+            return getArtifacts( searchResults );
         }
         catch ( RepositorySearchException e )
         {
@@ -127,7 +125,7 @@ public class DefaultSearchService
         try
         {
             SearchResults searchResults = repositorySearch.search( getPrincipal(), searchField, limits );
-            return getArtifacts( searchResults, Collections.<ArtifactFiler>emptyList() );
+            return getArtifacts( searchResults );
         }
         catch ( RepositorySearchException e )
         {
@@ -182,7 +180,7 @@ public class DefaultSearchService
                 : redbackRequestInformation.getUser().getUsername() );
     }
 
-    protected List<Artifact> getArtifacts( SearchResults searchResults, List<ArtifactFiler> artifactFilers )
+    protected List<Artifact> getArtifacts( SearchResults searchResults )
     {
         if ( searchResults == null || searchResults.isEmpty() )
         {
@@ -226,34 +224,13 @@ public class DefaultSearchService
                     if ( StringUtils.isNotBlank( version ) )
                     {
                         versionned.setVersion( version );
-                        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;
-    }
 }
diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/searchfilter/ArtifactFiler.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/searchfilter/ArtifactFiler.java
deleted file mode 100644 (file)
index b07071e..0000000
+++ /dev/null
@@ -1,32 +0,0 @@
-package org.apache.archiva.rest.services.searchfilter;
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import org.apache.archiva.rest.api.model.Artifact;
-
-import java.util.List;
-
-/**
- * @author Olivier Lamy
- * @since 1.4
- */
-public interface ArtifactFiler
-{
-    boolean addArtifactInResult( Artifact artifact, List<Artifact> currentResult );
-}
diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/searchfilter/NoClassifierArtifactFiler.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/searchfilter/NoClassifierArtifactFiler.java
deleted file mode 100644 (file)
index 7a92b12..0000000
+++ /dev/null
@@ -1,42 +0,0 @@
-package org.apache.archiva.rest.services.searchfilter;
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import com.sun.org.apache.bcel.internal.generic.INSTANCEOF;
-import org.apache.archiva.rest.api.model.Artifact;
-import org.apache.commons.lang.StringUtils;
-
-import java.util.Arrays;
-import java.util.List;
-
-/**
- * @author Olivier Lamy
- */
-public class NoClassifierArtifactFiler
-    implements ArtifactFiler
-{
-    public static final NoClassifierArtifactFiler INSTANCE = new NoClassifierArtifactFiler();
-
-    public static final List<? extends ArtifactFiler> LIST = Arrays.asList( INSTANCE );
-
-    public boolean addArtifactInResult( Artifact artifact, List<Artifact> currentResult )
-    {
-        return StringUtils.isBlank( artifact.getClassifier() );
-    }
-}