]> source.dussan.org Git - archiva.git/commitdiff
improve MetadataRepository api to be able to remove an artifact with MetadataFacet...
authorOlivier Lamy <olamy@apache.org>
Tue, 5 Jun 2012 21:42:02 +0000 (21:42 +0000)
committerOlivier Lamy <olamy@apache.org>
Tue, 5 Jun 2012 21:42:02 +0000 (21:42 +0000)
git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1346609 13f79535-47bb-0310-9956-ffa450edef68

13 files changed:
archiva-modules/archiva-base/archiva-indexer/src/test/java/org/apache/archiva/indexer/search/mock/MockRepositorySessionFactory.java
archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-default/src/test/java/org/apache/archiva/admin/mock/MockRepositorySessionFactory.java
archiva-modules/archiva-scheduler/archiva-scheduler-indexing/src/test/java/org/apache/archiva/scheduler/indexing/mock/MockRepositorySessionFactory.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/RepositoriesService.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultRepositoriesService.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/RepositoriesServiceTest.java
archiva-modules/metadata/metadata-model/src/main/java/org/apache/archiva/metadata/model/FacetedMetadata.java
archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/MetadataRepository.java
archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/MavenArtifactFacet.java
archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/metadata/repository/storage/maven2/MavenProjectFacet.java
archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/metadata/repository/TestMetadataRepository.java
archiva-modules/plugins/metadata-store-file/src/main/java/org/apache/archiva/metadata/repository/file/FileMetadataRepository.java
archiva-modules/plugins/metadata-store-jcr/src/main/java/org/apache/archiva/metadata/repository/jcr/JcrMetadataRepository.java

index fa175d67001dd95ddfdb93d1088503f7f90c4448..5a6c40042e1a6ba7eead2f58d1e7ea41f10edf56 100644 (file)
@@ -63,6 +63,14 @@ public class MockRepositorySessionFactory
             {
                 return new MetadataRepository( )
                 {
+
+                    public void removeArtifact( String repositoryId, String namespace, String project,
+                                                String projectVersion, String projectId, MetadataFacet metadataFacet )
+                        throws MetadataRepositoryException
+                    {
+
+                    }
+
                     public void updateProject( String repositoryId, ProjectMetadata project )
                         throws MetadataRepositoryException
                     {
index f753ce9ead87de18d05e213f4814540b02bd9830..649343209d60e81a45619717285c80b8b941d9c5 100644 (file)
@@ -63,6 +63,14 @@ public class MockRepositorySessionFactory
             {
                 return new MetadataRepository( )
                 {
+
+                    public void removeArtifact( String repositoryId, String namespace, String project,
+                                                String projectVersion, String projectId, MetadataFacet metadataFacet )
+                        throws MetadataRepositoryException
+                    {
+                        //To change body of implemented methods use File | Settings | File Templates.
+                    }
+
                     public void updateProject( String repositoryId, ProjectMetadata project )
                         throws MetadataRepositoryException
                     {
index 063fa4b4bf8c60dbb43c7d115eedcbae39fcdf85..37290708abaf1ad8a01138f353290c913ae6191c 100644 (file)
@@ -63,30 +63,38 @@ public class MockRepositorySessionFactory
             {
                 return new MetadataRepository()
                 {
+
+                    public void removeArtifact( String repositoryId, String namespace, String project,
+                                                String projectVersion, String projectId, MetadataFacet metadataFacet )
+                        throws MetadataRepositoryException
+                    {
+
+                    }
+
                     public void updateProject( String repositoryId, ProjectMetadata project )
                         throws MetadataRepositoryException
                     {
-                        //To change body of implemented methods use File | Settings | File Templates.
+
                     }
 
                     public void updateArtifact( String repositoryId, String namespace, String projectId,
                                                 String projectVersion, ArtifactMetadata artifactMeta )
                         throws MetadataRepositoryException
                     {
-                        //To change body of implemented methods use File | Settings | File Templates.
+
                     }
 
                     public void updateProjectVersion( String repositoryId, String namespace, String projectId,
                                                       ProjectVersionMetadata versionMetadata )
                         throws MetadataRepositoryException
                     {
-                        //To change body of implemented methods use File | Settings | File Templates.
+
                     }
 
                     public void updateNamespace( String repositoryId, String namespace )
                         throws MetadataRepositoryException
                     {
-                        //To change body of implemented methods use File | Settings | File Templates.
+
                     }
 
                     public List<String> getMetadataFacets( String repositoryId, String facetId )
@@ -104,7 +112,7 @@ public class MockRepositorySessionFactory
                     public void addMetadataFacet( String repositoryId, MetadataFacet metadataFacet )
                         throws MetadataRepositoryException
                     {
-                        //To change body of implemented methods use File | Settings | File Templates.
+
                     }
 
                     public void removeMetadataFacets( String repositoryId, String facetId )
index b82ee5d16d6e86c83f483fbcde67fa18871d4089..06509af11c97dbb44c21871f211a2c78d0cbcb75 100644 (file)
@@ -129,4 +129,15 @@ public interface RepositoriesService
     Boolean isAuthorizedToDeleteArtifacts( @PathParam( "repositoryId" ) String repoId )
         throws ArchivaRestServiceException;
 
+    @Path( "deleteGroupId" )
+    @GET
+    @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN } )
+    @RedbackAuthorization( noPermission = true )
+    /**
+     * <b>permissions are checked in impl</b>
+     * @since 1.4-M3
+     */
+    Boolean deleteGroupId( @QueryParam( "groupId" ) String groupId )
+        throws ArchivaRestServiceException;
+
 }
index 6ee2cec2a49fd24543dcd7428596618c573797c5..d49f6efcaec6b172632bb878ba3df3208b423a8f 100644 (file)
@@ -37,6 +37,7 @@ import org.apache.archiva.metadata.repository.MetadataRepositoryException;
 import org.apache.archiva.metadata.repository.MetadataResolutionException;
 import org.apache.archiva.metadata.repository.RepositorySession;
 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
+import org.apache.archiva.metadata.repository.storage.maven2.MavenArtifactFacet;
 import org.apache.archiva.model.ArchivaRepositoryMetadata;
 import org.apache.archiva.model.ArtifactReference;
 import org.apache.archiva.model.VersionedReference;
@@ -669,6 +670,10 @@ public class DefaultRepositoriesService
 
             ManagedRepositoryContent repository = repositoryFactory.getManagedRepositoryContent( repositoryId );
 
+            MetadataRepository metadataRepository = repositorySession.getRepository();
+
+            String path = repository.toMetadataPath( ref );
+
             if ( StringUtils.isNotBlank( artifact.getClassifier() ) )
             {
                 if ( StringUtils.isBlank( artifact.getPackaging() ) )
@@ -684,45 +689,73 @@ public class DefaultRepositoriesService
                 artifactReference.setType( artifact.getPackaging() );
                 repository.deleteArtifact( artifactReference );
 
-                // TODO cleanup facet which contains classifier information
-                return Boolean.TRUE;
             }
-
-            String path = repository.toMetadataPath( ref );
-            int index = path.lastIndexOf( '/' );
-            path = path.substring( 0, index );
-            File targetPath = new File( repoConfig.getLocation(), path );
-
-            if ( !targetPath.exists() )
+            else
             {
-                throw new ContentNotFoundException(
-                    artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion() );
-            }
 
-            // TODO: this should be in the storage mechanism so that it is all tied together
-            // delete from file system
-            repository.deleteVersion( ref );
+                int index = path.lastIndexOf( '/' );
+                path = path.substring( 0, index );
+                File targetPath = new File( repoConfig.getLocation(), path );
 
-            File metadataFile = getMetadata( targetPath.getAbsolutePath() );
-            ArchivaRepositoryMetadata metadata = getMetadata( metadataFile );
+                if ( !targetPath.exists() )
+                {
+                    throw new ContentNotFoundException(
+                        artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion() );
+                }
 
-            updateMetadata( metadata, metadataFile, lastUpdatedTimestamp, artifact );
+                // TODO: this should be in the storage mechanism so that it is all tied together
+                // delete from file system
+                repository.deleteVersion( ref );
 
-            MetadataRepository metadataRepository = repositorySession.getRepository();
+                File metadataFile = getMetadata( targetPath.getAbsolutePath() );
+                ArchivaRepositoryMetadata metadata = getMetadata( metadataFile );
 
+                updateMetadata( metadata, metadataFile, lastUpdatedTimestamp, artifact );
+            }
             Collection<ArtifactMetadata> artifacts =
                 metadataRepository.getArtifacts( repositoryId, artifact.getGroupId(), artifact.getArtifactId(),
                                                  artifact.getVersion() );
 
             for ( ArtifactMetadata artifactMetadata : artifacts )
             {
+
                 // TODO: mismatch between artifact (snapshot) version and project (base) version here
                 if ( artifactMetadata.getVersion().equals( artifact.getVersion() ) )
                 {
-                    metadataRepository.removeArtifact( artifactMetadata.getRepositoryId(),
-                                                       artifactMetadata.getNamespace(), artifactMetadata.getProject(),
-                                                       artifact.getVersion(), artifactMetadata.getId() );
+                    if ( StringUtils.isNotBlank( artifact.getClassifier() ) )
+                    {
+                        if ( StringUtils.isBlank( artifact.getPackaging() ) )
+                        {
+                            throw new ArchivaRestServiceException(
+                                "You must configure a type/packaging when using classifier", 400, null );
+                        }
+                        // cleanup facet which contains classifier information
+                        MavenArtifactFacet mavenArtifactFacet =
+                            (MavenArtifactFacet) artifactMetadata.getFacet( MavenArtifactFacet.FACET_ID );
+
+                        if ( StringUtils.equals( artifact.getClassifier(), mavenArtifactFacet.getClassifier() ) )
+                        {
+                            artifactMetadata.removeFacet( MavenArtifactFacet.FACET_ID );
+                            String groupId = artifact.getGroupId(), artifactId = artifact.getArtifactId(), version =
+                                artifact.getVersion();
+                            //metadataRepository.updateArtifact( repositoryId, groupId, artifactId, version,
+                            //                                   artifactMetadata );
+                            // String repositoryId, String namespace, String project, String version, String projectId, MetadataFacet metadataFacet
+                            MavenArtifactFacet mavenArtifactFacetToCompare = new MavenArtifactFacet();
+                            mavenArtifactFacetToCompare.setClassifier( artifact.getClassifier() );
+                            metadataRepository.removeArtifact( repositoryId, groupId, artifactId, version,
+                                                               mavenArtifactFacetToCompare );
+                            metadataRepository.save();
+                        }
 
+                    }
+                    else
+                    {
+                        metadataRepository.removeArtifact( artifactMetadata.getRepositoryId(),
+                                                           artifactMetadata.getNamespace(),
+                                                           artifactMetadata.getProject(), artifact.getVersion(),
+                                                           artifactMetadata.getId() );
+                    }
                     // TODO: move into the metadata repository proper - need to differentiate attachment of
                     //       repository metadata to an artifact
                     for ( RepositoryListener listener : listeners )
@@ -736,11 +769,8 @@ public class DefaultRepositoriesService
                 }
             }
 
-            repositorySession.save();
 
-            repositorySession.close();
         }
-
         catch ( ContentNotFoundException e )
         {
             throw new ArchivaRestServiceException( "Artifact does not exist: " + e.getMessage(), 400, e );
@@ -766,13 +796,20 @@ public class DefaultRepositoriesService
             throw new ArchivaRestServiceException( "RepositoryAdmin exception: " + e.getMessage(), 500, e );
         }
         finally
-
         {
+            repositorySession.save();
+
             repositorySession.close();
         }
         return Boolean.TRUE;
     }
 
+    public Boolean deleteGroupId( String groupId )
+        throws ArchivaRestServiceException
+    {
+        return true;
+    }
+
     public Boolean isAuthorizedToDeleteArtifacts( String repoId )
         throws ArchivaRestServiceException
     {
index 4ecdddbe7918724cff0ce06b452615c37b255f67..59fcbbd65fade030dc3d0cadb28a6fd54e0f9ae7 100644 (file)
@@ -22,13 +22,14 @@ package org.apache.archiva.rest.services;
 import org.apache.archiva.admin.model.beans.ManagedRepository;
 import org.apache.archiva.common.utils.FileUtil;
 import org.apache.archiva.rest.api.model.Artifact;
+import org.apache.archiva.rest.api.model.BrowseResult;
+import org.apache.archiva.rest.api.model.BrowseResultEntry;
 import org.apache.archiva.rest.api.model.VersionsList;
 import org.apache.archiva.rest.api.services.BrowseService;
 import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
 import org.apache.archiva.rest.api.services.RepositoriesService;
 import org.apache.cxf.jaxrs.client.ServerWebApplicationException;
 import org.fest.assertions.Assertions;
-import org.junit.Ignore;
 import org.junit.Test;
 
 import java.io.File;
@@ -189,7 +190,6 @@ public class RepositoriesServiceTest
     }
 
     @Test
-    @Ignore
     public void deleteArtifactWithClassifier()
         throws Exception
     {
@@ -232,9 +232,15 @@ public class RepositoriesServiceTest
             artifacts =
                 browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.0.1", SOURCE_REPO_ID );
 
-            Assertions.assertThat( artifacts ).isNotNull().isEmpty();
+            log.info( "artifact: {}", artifacts );
+
+            Assertions.assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 2 );
+
+            versionsList = browseService.getVersionsList( "commons-logging", "commons-logging", SOURCE_REPO_ID );
 
-            Assertions.assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 5 );
+            log.info( "versionsList: {}", versionsList );
+
+            Assertions.assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 6 );
 
         }
         finally
@@ -244,6 +250,31 @@ public class RepositoriesServiceTest
     }
 
 
+    @Test
+    public void deleteGroupId()
+        throws Exception
+    {
+        initSourceTargetRepo();
+        try
+        {
+            BrowseService browseService = getBrowseService( authorizationHeader, false );
+
+            BrowseResult browseResult = browseService.browseGroupId( "org.apache.karaf.features", SOURCE_REPO_ID );
+
+            assertNotNull( browseResult );
+
+            Assertions.assertThat( browseResult.getBrowseResultEntries() ).isNotNull().isNotEmpty().contains(
+                new BrowseResultEntry( "org.apache.karaf.features.org.apache.karaf.features.command", true ),
+                new BrowseResultEntry( "org.apache.karaf.features.org.apache.karaf.features.core", true ) );
+
+            log.info( "browseResult: {}", browseResult );
+        }
+        finally
+        {
+            cleanRepos();
+        }
+    }
+
     @Test
     public void authorizedToDeleteArtifacts()
         throws Exception
index 387813e36f09b3a3e7eee97ecbc09677505d6f04..e2261a199349cbdb364b27f1a8cb6b8cadb4b490 100644 (file)
@@ -19,7 +19,6 @@ package org.apache.archiva.metadata.model;
  * under the License.
  */
 
-import javax.xml.bind.annotation.XmlElement;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
@@ -56,6 +55,11 @@ public abstract class FacetedMetadata
         return this.facets.get( facetId );
     }
 
+    public MetadataFacet removeFacet( String facetId )
+    {
+        return this.facets.remove( facetId );
+    }
+
     /**
      * Get all the facets available on this metadata.
      *
index 4ab65c6427286c06bf08c2c1167e1ecd077338e3..60e536357886f999eaa343fc37dc4b70298bd76d 100644 (file)
@@ -79,6 +79,19 @@ public interface MetadataRepository
     void removeArtifact( String repositoryId, String namespace, String project, String version, String id )
         throws MetadataRepositoryException;
 
+    /**
+     * @param repositoryId
+     * @param namespace
+     * @param project
+     * @param version
+     * @param metadataFacet will remove artifacts which have this {@link MetadataFacet} using equals
+     * @throws MetadataRepositoryException
+     * @since 1.4-M3
+     */
+    void removeArtifact( String repositoryId, String namespace, String project, String version,
+                         MetadataFacet metadataFacet )
+        throws MetadataRepositoryException;
+
     /**
      * Delete a repository's metadata. This includes all associated metadata facets.
      *
index aa330385d991eb7a55526768fd203dbaaf99f719..6e72fdcd7d9eca16dc31bb79d3a6c4148305d570 100644 (file)
@@ -20,6 +20,7 @@ package org.apache.archiva.metadata.repository.storage.maven2;
  */
 
 import org.apache.archiva.metadata.model.MetadataFacet;
+import org.apache.commons.lang.StringUtils;
 
 import java.util.HashMap;
 import java.util.Map;
@@ -118,4 +119,22 @@ public class MavenArtifactFacet
             this.buildNumber = Integer.parseInt( buildNumber );
         }
     }
+
+    @Override
+    public boolean equals( Object o )
+    {
+        if ( this == o )
+        {
+            return true;
+        }
+        if ( !( o instanceof MavenArtifactFacet ) )
+        {
+            return false;
+        }
+
+        MavenArtifactFacet that = (MavenArtifactFacet) o;
+
+        return StringUtils.equals( that.getClassifier(), this.classifier );
+    }
+
 }
index 83fe1693e710d9ce275b4d8c10fee880b24c3fd2..93826d8d1b037f61dd6303fb11f07547f68d3ac4 100644 (file)
@@ -118,4 +118,43 @@ public class MavenProjectFacet
             this.parent = parent;
         }
     }
+
+    @Override
+    public boolean equals( Object o )
+    {
+        if ( this == o )
+        {
+            return true;
+        }
+        if ( !( o instanceof MavenProjectFacet ) )
+        {
+            return false;
+        }
+
+        MavenProjectFacet that = (MavenProjectFacet) o;
+
+        if ( !artifactId.equals( that.artifactId ) )
+        {
+            return false;
+        }
+        if ( !groupId.equals( that.groupId ) )
+        {
+            return false;
+        }
+        if ( !packaging.equals( that.packaging ) )
+        {
+            return false;
+        }
+
+        return true;
+    }
+
+    @Override
+    public int hashCode()
+    {
+        int result = groupId.hashCode();
+        result = 31 * result + artifactId.hashCode();
+        result = 31 * result + packaging.hashCode();
+        return result;
+    }
 }
index 3d5760765cb0fd4b4e7062054a8b4c971e40e038..24a0a5d44e7dbf58340d63a7c85939972e4897d2 100644 (file)
@@ -198,4 +198,12 @@ public class TestMetadataRepository
     {
         return null;  //To change body of implemented methods use File | Settings | File Templates.
     }
+
+
+    public void removeArtifact( String repositoryId, String namespace, String project, String projectVersion,
+                                String projectId, MetadataFacet metadataFacet )
+        throws MetadataRepositoryException
+    {
+        //To change body of implemented methods use File | Settings | File Templates.
+    }
 }
\ No newline at end of file
index ad483d5ecec6e521390c13006ae0086be0a6f3ac..e3de5fd400124d3e6d9379e9053d615b14cd5eef 100644 (file)
@@ -35,8 +35,10 @@ import org.apache.archiva.metadata.model.ProjectVersionMetadata;
 import org.apache.archiva.metadata.model.ProjectVersionReference;
 import org.apache.archiva.metadata.model.Scm;
 import org.apache.archiva.metadata.repository.MetadataRepository;
+import org.apache.archiva.metadata.repository.MetadataRepositoryException;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang.NotImplementedException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -635,6 +637,23 @@ public class FileMetadataRepository
         }
     }
 
+    /**
+     * FIXME implements this !!!!
+     * @param repositoryId
+     * @param namespace
+     * @param project
+     * @param projectVersion
+     * @param projectId
+     * @param metadataFacet  will remove artifacts which have this {@link MetadataFacet} using equals
+     * @throws MetadataRepositoryException
+     */
+    public void removeArtifact( String repositoryId, String namespace, String project, String projectVersion,
+                                String projectId, MetadataFacet metadataFacet )
+        throws MetadataRepositoryException
+    {
+        throw new NotImplementedException( "not implemented" );
+    }
+
     public void removeRepository( String repoId )
     {
         File dir = getDirectory( repoId );
index b0acf2950e2b473f0dff697dfb2c2d12763c188c..ab1b1cbd640b2d9ab64b814bf9855f2b2dd2c000 100644 (file)
@@ -194,6 +194,27 @@ public class JcrMetadataRepository
 
             node.setProperty( "version", artifactMeta.getVersion() );
 
+            // iterate over available facets to update/add/remove from the artifactMetadata
+            for ( String facetId : metadataFacetFactories.keySet() )
+            {
+                MetadataFacet metadataFacet = artifactMeta.getFacet( facetId );
+                if ( node.hasNode( facetId ) )
+                {
+                    node.getNode( facetId ).remove();
+                }
+                if ( metadataFacet != null )
+                {
+                    // recreate, to ensure properties are removed
+                    Node n = node.addNode( facetId );
+                    n.addMixin( FACET_NODE_TYPE );
+
+                    for ( Map.Entry<String, String> entry : metadataFacet.toProperties().entrySet() )
+                    {
+                        n.setProperty( entry.getKey(), entry.getValue() );
+                    }
+                }
+            }
+            /*
             for ( MetadataFacet facet : artifactMeta.getFacetList() )
             {
                 if ( node.hasNode( facet.getFacetId() ) )
@@ -210,6 +231,7 @@ public class JcrMetadataRepository
                     n.setProperty( entry.getKey(), entry.getValue() );
                 }
             }
+            */
         }
         catch ( RepositoryException e )
         {
@@ -1011,6 +1033,40 @@ public class JcrMetadataRepository
         }
     }
 
+    public void removeArtifact( String repositoryId, String namespace, String project, String projectVersion,
+                                MetadataFacet metadataFacet )
+        throws MetadataRepositoryException
+    {
+        try
+        {
+            Node root = getJcrSession().getRootNode();
+            String path = getProjectVersionPath( repositoryId, namespace, project, projectVersion );
+
+            if ( root.hasNode( path ) )
+            {
+                Node node = root.getNode( path );
+
+                for ( Node n : JcrUtils.getChildNodes( node ) )
+                {
+                    if ( n.isNodeType( ARTIFACT_NODE_TYPE ) )
+                    {
+                        ArtifactMetadata artifactMetadata = getArtifactFromNode( repositoryId, n );
+                        log.debug( "artifactMetadata: {}", artifactMetadata );
+                        MetadataFacet metadataFacetToRemove = artifactMetadata.getFacet( metadataFacet.getFacetId() );
+                        if ( metadataFacetToRemove != null && metadataFacet.equals( metadataFacetToRemove ) )
+                        {
+                            n.remove();
+                        }
+                    }
+                }
+            }
+        }
+        catch ( RepositoryException e )
+        {
+            throw new MetadataRepositoryException( e.getMessage(), e );
+        }
+    }
+
     public Collection<ArtifactMetadata> getArtifacts( String repositoryId, String namespace, String projectId,
                                                       String projectVersion )
         throws MetadataResolutionException