From: Olivier Lamy Date: Mon, 26 Mar 2012 09:54:39 +0000 (+0000) Subject: add an Entry object for REST services returning map -> ease json mapping in javascript X-Git-Tag: archiva-1.4-M3~954 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=ef8d2cc9598778263f0a78b251b9a38ffec5f8d0;p=archiva.git add an Entry object for REST services returning map -> ease json mapping in javascript git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1305281 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 5b2be0978..332892b79 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 @@ -22,6 +22,7 @@ import org.apache.archiva.admin.model.beans.ManagedRepository; import org.apache.archiva.metadata.model.ProjectVersionMetadata; import org.apache.archiva.rest.api.model.Artifact; import org.apache.archiva.rest.api.model.BrowseResult; +import org.apache.archiva.rest.api.model.Entry; import org.apache.archiva.rest.api.model.TreeEntry; import org.apache.archiva.rest.api.model.VersionsList; import org.codehaus.plexus.redback.authorization.RedbackAuthorization; @@ -35,7 +36,6 @@ 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 @@ -123,9 +123,8 @@ public interface BrowseService @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 ) + List 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}" ) 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 96c71208f..86182503c 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 @@ -34,6 +34,7 @@ import org.apache.archiva.metadata.repository.storage.maven2.MavenProjectFacet; 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.Entry; import org.apache.archiva.rest.api.model.TreeEntry; import org.apache.archiva.rest.api.model.VersionsList; import org.apache.archiva.rest.api.services.ArchivaRestServiceException; @@ -563,23 +564,30 @@ public class DefaultBrowseService return artifacts; } - public Map getMetadatas( String groupId, String artifactId, String version, String repositoryId ) + public List getMetadatas( String groupId, String artifactId, String version, String repositoryId ) throws ArchivaRestServiceException { ProjectVersionMetadata projectVersionMetadata = getProjectMetadata( groupId, artifactId, version, repositoryId ); if ( projectVersionMetadata == null ) { - return Collections.emptyMap(); + return Collections.emptyList(); } MetadataFacet metadataFacet = projectVersionMetadata.getFacet( GenericMetadataFacet.FACET_ID ); if ( metadataFacet == null ) { - return Collections.emptyMap(); + return Collections.emptyList(); } + Map map = metadataFacet.toProperties(); + List entries = new ArrayList( map.size() ); - return metadataFacet.toProperties(); + for ( Map.Entry entry : map.entrySet() ) + { + entries.add( new Entry( entry.getKey(), entry.getValue() ) ); + } + + return entries; } public Boolean addMetadata( String groupId, String artifactId, String version, String key, String value, diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/BrowseServiceTest.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/BrowseServiceTest.java index a573ba6ce..241646118 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/BrowseServiceTest.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/BrowseServiceTest.java @@ -21,11 +21,14 @@ package org.apache.archiva.rest.services; import org.apache.archiva.metadata.model.ProjectVersionMetadata; import org.apache.archiva.rest.api.model.BrowseResult; import org.apache.archiva.rest.api.model.BrowseResultEntry; +import org.apache.archiva.rest.api.model.Entry; import org.apache.archiva.rest.api.model.VersionsList; import org.apache.archiva.rest.api.services.BrowseService; import org.fest.assertions.MapAssert; import org.junit.Test; +import java.util.HashMap; +import java.util.List; import java.util.Map; import static org.fest.assertions.Assertions.assertThat; @@ -36,6 +39,19 @@ import static org.fest.assertions.Assertions.assertThat; public class BrowseServiceTest extends AbstractArchivaRestTest { + + Map toMap( List entries ) + { + Map map = new HashMap( entries.size() ); + + for ( Entry entry : entries ) + { + map.put( entry.getKey(), entry.getValue() ); + } + + return map; + } + @Test public void metadatagetthenadd() throws Exception @@ -52,13 +68,14 @@ public class BrowseServiceTest BrowseService browseService = getBrowseService( authorizationHeader, false ); - Map metadatas = browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", testRepoId ); + Map metadatas = + toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", testRepoId ) ); assertThat( metadatas ).isNotNull().isEmpty(); browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", testRepoId ); - metadatas = browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", testRepoId ); + metadatas = toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", testRepoId ) ); assertThat( metadatas ).isNotNull().isNotEmpty().includes( MapAssert.entry( "wine", "bordeaux" ) ); @@ -83,19 +100,20 @@ public class BrowseServiceTest BrowseService browseService = getBrowseService( authorizationHeader, false ); - Map metadatas = browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", testRepoId ); + Map metadatas = + toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", testRepoId ) ); assertThat( metadatas ).isNotNull().isEmpty(); browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", testRepoId ); - metadatas = browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", testRepoId ); + metadatas = toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", testRepoId ) ); assertThat( metadatas ).isNotNull().isNotEmpty().includes( MapAssert.entry( "wine", "bordeaux" ) ); browseService.deleteMetadata( "commons-cli", "commons-cli", "1.0", "wine", testRepoId ); - metadatas = browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", testRepoId ); + metadatas = toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", testRepoId ) ); assertThat( metadatas ).isNotNull().isEmpty();