]> source.dussan.org Git - archiva.git/commitdiff
rest services for artifact generic metadatas
authorOlivier Lamy <olamy@apache.org>
Sat, 24 Mar 2012 18:23:43 +0000 (18:23 +0000)
committerOlivier Lamy <olamy@apache.org>
Sat, 24 Mar 2012 18:23:43 +0000 (18:23 +0000)
git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1304880 13f79535-47bb-0310-9956-ffa450edef68

archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/BrowseService.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/pom.xml
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultBrowseService.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/AbstractArchivaRestTest.java

index d4a38e4073797aa484e0242ae4cba5c4ac2bc9e5..7a47163ba82f7e16c73d71bb66facba953193de0 100644 (file)
@@ -26,13 +26,16 @@ import org.apache.archiva.rest.api.model.TreeEntry;
 import org.apache.archiva.rest.api.model.VersionsList;
 import org.codehaus.plexus.redback.authorization.RedbackAuthorization;
 
+import javax.ws.rs.DELETE;
 import javax.ws.rs.GET;
+import javax.ws.rs.PUT;
 import javax.ws.rs.Path;
 import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
 import javax.ws.rs.QueryParam;
 import javax.ws.rs.core.MediaType;
 import java.util.List;
+import java.util.Map;
 
 /**
  * @author Olivier Lamy
@@ -103,6 +106,33 @@ public interface BrowseService
     @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } )
     @RedbackAuthorization( noPermission = true, noRestriction = true )
     List<Artifact> getDependees( @PathParam( "g" ) String groupId, @PathParam( "a" ) String artifactId,
-                              @PathParam( "v" ) String version, @QueryParam( "repositoryId" ) String repositoryId )
+                                 @PathParam( "v" ) String version, @QueryParam( "repositoryId" ) String repositoryId )
+        throws ArchivaRestServiceException;
+
+    @Path( "metadatas/{g}/{a}/{v}" )
+    @GET
+    @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } )
+    @RedbackAuthorization( noPermission = true, noRestriction = true )
+    Map<String, String> getMetadatas( @PathParam( "g" ) String groupId, @PathParam( "a" ) String artifactId,
+                                      @PathParam( "v" ) String version,
+                                      @QueryParam( "repositoryId" ) String repositoryId )
+        throws ArchivaRestServiceException;
+
+    @Path( "metadata/{g}/{a}/{v}/{key}/{value}" )
+    @PUT
+    @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } )
+    @RedbackAuthorization( noPermission = false, noRestriction = false, permissions = "archiva-add-metadata" )
+    Boolean addMetadata( @PathParam( "g" ) String groupId, @PathParam( "a" ) String artifactId,
+                         @PathParam( "v" ) String version, @PathParam( "key" ) String key,
+                         @PathParam( "value" ) String value, @QueryParam( "repositoryId" ) String repositoryId )
+        throws ArchivaRestServiceException;
+
+    @Path( "metadata/{g}/{a}/{v}/{key}" )
+    @DELETE
+    @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } )
+    @RedbackAuthorization( noPermission = false, noRestriction = false, permissions = "archiva-add-metadata" )
+    Boolean deleteMetadata( @PathParam( "g" ) String groupId, @PathParam( "a" ) String artifactId,
+                            @PathParam( "v" ) String version, @PathParam( "key" ) String key,
+                            @QueryParam( "repositoryId" ) String repositoryId )
         throws ArchivaRestServiceException;
 }
index 3311f8c94a74f7eaaee7accb18888ad87c869731..8afc5fa5f54db33cfbae82cc0c71880af650da98 100644 (file)
       <groupId>org.apache.archiva</groupId>
       <artifactId>audit</artifactId>
     </dependency>
+    <dependency>
+      <groupId>org.apache.archiva</groupId>
+      <artifactId>generic-metadata-support</artifactId>
+    </dependency>
     <dependency>
       <groupId>org.apache.archiva</groupId>
       <artifactId>archiva-scheduler-repository</artifactId>
       <scope>test</scope>
     </dependency>
 
+    <dependency>
+      <groupId>org.easytesting</groupId>
+      <artifactId>fest-assert</artifactId>
+      <version>1.4</version>
+      <scope>test</scope>
+    </dependency>
+
   </dependencies>
 
   <build>
index f15a113c76df5e7e069ca0858dee021c11685b92..96c71208f72711b479307a435eff5e7e1d61076a 100644 (file)
@@ -18,12 +18,15 @@ package org.apache.archiva.rest.services;
  * under the License.
  */
 
-import net.sf.beanlib.provider.replicator.BeanReplicator;
 import org.apache.archiva.admin.model.beans.ManagedRepository;
 import org.apache.archiva.common.utils.VersionComparator;
 import org.apache.archiva.dependency.tree.maven2.DependencyTreeBuilder;
+import org.apache.archiva.metadata.generic.GenericMetadataFacet;
+import org.apache.archiva.metadata.model.MetadataFacet;
 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
 import org.apache.archiva.metadata.model.ProjectVersionReference;
+import org.apache.archiva.metadata.repository.MetadataRepository;
+import org.apache.archiva.metadata.repository.MetadataRepositoryException;
 import org.apache.archiva.metadata.repository.MetadataResolutionException;
 import org.apache.archiva.metadata.repository.MetadataResolver;
 import org.apache.archiva.metadata.repository.RepositorySession;
@@ -35,12 +38,11 @@ import org.apache.archiva.rest.api.model.TreeEntry;
 import org.apache.archiva.rest.api.model.VersionsList;
 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
 import org.apache.archiva.rest.api.services.BrowseService;
+import org.apache.archiva.rest.services.utils.TreeDependencyNodeVisitor;
 import org.apache.archiva.security.ArchivaSecurityException;
 import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.lang.StringUtils;
-import org.apache.maven.shared.dependency.tree.DependencyNode;
 import org.apache.maven.shared.dependency.tree.DependencyTreeBuilderException;
-import org.apache.maven.shared.dependency.tree.traversal.DependencyNodeVisitor;
 import org.springframework.stereotype.Service;
 
 import javax.inject.Inject;
@@ -48,8 +50,10 @@ import javax.ws.rs.core.Response;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.LinkedHashSet;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 
 /**
@@ -508,52 +512,6 @@ public class DefaultBrowseService
         return treeEntries;
     }
 
-    private static class TreeDependencyNodeVisitor
-        implements DependencyNodeVisitor
-    {
-
-        final List<TreeEntry> treeEntries;
-
-        private TreeEntry currentEntry;
-
-
-        private DependencyNode firstNode;
-
-
-        boolean firstChild = true;
-
-        private TreeDependencyNodeVisitor( List<TreeEntry> treeEntries )
-        {
-            this.treeEntries = treeEntries;
-        }
-
-        public boolean visit( DependencyNode node )
-        {
-            TreeEntry entry = new TreeEntry( new BeanReplicator().replicateBean( node.getArtifact(), Artifact.class ) );
-            entry.setParent( currentEntry );
-            currentEntry = entry;
-
-            if ( firstNode == null )
-            {
-                firstNode = node;
-                treeEntries.add( currentEntry );
-            }
-            else
-            {
-                currentEntry.getParent().getChilds().add( currentEntry );
-            }
-            return true;
-        }
-
-        public boolean endVisit( DependencyNode node )
-        {
-            currentEntry = currentEntry.getParent();
-            firstChild = false;
-            return true;
-        }
-
-    }
-
     public List<ManagedRepository> getUserRepositories()
         throws ArchivaRestServiceException
     {
@@ -605,6 +563,127 @@ public class DefaultBrowseService
         return artifacts;
     }
 
+    public Map<String, String> getMetadatas( String groupId, String artifactId, String version, String repositoryId )
+        throws ArchivaRestServiceException
+    {
+        ProjectVersionMetadata projectVersionMetadata =
+            getProjectMetadata( groupId, artifactId, version, repositoryId );
+        if ( projectVersionMetadata == null )
+        {
+            return Collections.emptyMap();
+        }
+        MetadataFacet metadataFacet = projectVersionMetadata.getFacet( GenericMetadataFacet.FACET_ID );
+
+        if ( metadataFacet == null )
+        {
+            return Collections.emptyMap();
+        }
+
+        return metadataFacet.toProperties();
+    }
+
+    public Boolean addMetadata( String groupId, String artifactId, String version, String key, String value,
+                                String repositoryId )
+        throws ArchivaRestServiceException
+    {
+        ProjectVersionMetadata projectVersionMetadata =
+            getProjectMetadata( groupId, artifactId, version, repositoryId );
+
+        if ( projectVersionMetadata == null )
+        {
+            return Boolean.FALSE;
+        }
+
+        Map<String, String> properties = new HashMap<String, String>();
+
+        MetadataFacet metadataFacet = projectVersionMetadata.getFacet( GenericMetadataFacet.FACET_ID );
+
+        if ( metadataFacet != null && metadataFacet.toProperties() != null )
+        {
+            properties.putAll( metadataFacet.toProperties() );
+        }
+        else
+        {
+            metadataFacet = new GenericMetadataFacet();
+        }
+
+        properties.put( key, value );
+
+        metadataFacet.fromProperties( properties );
+
+        projectVersionMetadata.addFacet( metadataFacet );
+
+        RepositorySession repositorySession = repositorySessionFactory.createSession();
+
+        try
+        {
+            MetadataRepository metadataRepository = repositorySession.getRepository();
+
+            metadataRepository.updateProjectVersion( repositoryId, groupId, artifactId, projectVersionMetadata );
+
+            repositorySession.save();
+        }
+        catch ( MetadataRepositoryException e )
+        {
+            log.error( e.getMessage(), e );
+            throw new ArchivaRestServiceException( e.getMessage(),
+                                                   Response.Status.INTERNAL_SERVER_ERROR.getStatusCode() );
+        }
+        finally
+        {
+            repositorySession.close();
+        }
+        return Boolean.TRUE;
+    }
+
+    public Boolean deleteMetadata( String groupId, String artifactId, String version, String key, String repositoryId )
+        throws ArchivaRestServiceException
+    {
+        ProjectVersionMetadata projectVersionMetadata =
+            getProjectMetadata( groupId, artifactId, version, repositoryId );
+
+        if ( projectVersionMetadata == null )
+        {
+            return Boolean.FALSE;
+        }
+
+        GenericMetadataFacet metadataFacet =
+            (GenericMetadataFacet) projectVersionMetadata.getFacet( GenericMetadataFacet.FACET_ID );
+
+        if ( metadataFacet != null && metadataFacet.toProperties() != null )
+        {
+            Map<String, String> properties = metadataFacet.toProperties();
+            properties.remove( key );
+            metadataFacet.setAdditionalProperties( properties );
+        }
+        else
+        {
+            return Boolean.TRUE;
+        }
+
+        RepositorySession repositorySession = repositorySessionFactory.createSession();
+
+        try
+        {
+            MetadataRepository metadataRepository = repositorySession.getRepository();
+
+            metadataRepository.updateProjectVersion( repositoryId, groupId, artifactId, projectVersionMetadata );
+
+            repositorySession.save();
+        }
+        catch ( MetadataRepositoryException e )
+        {
+            log.error( e.getMessage(), e );
+            throw new ArchivaRestServiceException( e.getMessage(),
+                                                   Response.Status.INTERNAL_SERVER_ERROR.getStatusCode() );
+        }
+        finally
+        {
+            repositorySession.close();
+        }
+        return Boolean.TRUE;
+    }
+
     //---------------------------
     // internals
     //---------------------------
index d2304c508f74bb7cd254a232595236752674ecbc..5afb4a980905cd33e3548a293c442c5394c4d7ae 100644 (file)
@@ -22,6 +22,7 @@ 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.services.ArchivaAdministrationService;
+import org.apache.archiva.rest.api.services.BrowseService;
 import org.apache.archiva.rest.api.services.CommonServices;
 import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
 import org.apache.archiva.rest.api.services.NetworkProxyService;
@@ -200,6 +201,26 @@ public abstract class AbstractArchivaRestTest
         return service;
     }
 
+    protected BrowseService getBrowseService( String authzHeader )
+    {
+        BrowseService service =
+            JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
+                                       BrowseService.class,
+                                       Collections.singletonList( new JacksonJaxbJsonProvider() ) );
+        // to add authentification
+        if ( authzHeader != null )
+        {
+            WebClient.client( service ).header( "Authorization", authzHeader );
+        }
+
+        WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
+
+        WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
+        WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
+        return service;
+
+    }
+
     protected SearchService getSearchService( String authzHeader )
     {
         // START SNIPPET: cxf-searchservice-creation