From: Olivier Lamy Date: Sat, 24 Mar 2012 18:23:43 +0000 (+0000) Subject: rest services for artifact generic metadatas X-Git-Tag: archiva-1.4-M3~963 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=af5c53ec1cc7bc82a461c91d8be76c19364fa699;p=archiva.git rest services for artifact generic metadatas git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1304880 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/BrowseService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/BrowseService.java index d4a38e407..7a47163ba 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/BrowseService.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/BrowseService.java @@ -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 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 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; } diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/pom.xml b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/pom.xml index 3311f8c94..8afc5fa5f 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/pom.xml +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/pom.xml @@ -61,6 +61,10 @@ org.apache.archiva audit + + org.apache.archiva + generic-metadata-support + org.apache.archiva archiva-scheduler-repository @@ -301,6 +305,13 @@ test + + org.easytesting + fest-assert + 1.4 + test + + diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultBrowseService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultBrowseService.java index f15a113c7..96c71208f 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultBrowseService.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultBrowseService.java @@ -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 treeEntries; - - private TreeEntry currentEntry; - - - private DependencyNode firstNode; - - - boolean firstChild = true; - - private TreeDependencyNodeVisitor( List 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 getUserRepositories() throws ArchivaRestServiceException { @@ -605,6 +563,127 @@ public class DefaultBrowseService return artifacts; } + public Map 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 properties = new HashMap(); + + 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 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 //--------------------------- diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/AbstractArchivaRestTest.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/AbstractArchivaRestTest.java index d2304c508..5afb4a980 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/AbstractArchivaRestTest.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/AbstractArchivaRestTest.java @@ -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