From 5f5a4accbd4a9fb03480eac183f1c2ca88b81604 Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Sat, 19 May 2012 13:33:06 +0000 Subject: [PATCH] add a rest service to get ArtifactDownloadInfo git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1340479 13f79535-47bb-0310-9956-ffa450edef68 --- .../rest/api/model/ArtifactDownloadInfo.java | 6 ++ .../rest/api/services/BrowseService.java | 13 +++- .../rest/services/DefaultBrowseService.java | 60 ++++++++++++++++++- .../rest/services/BrowseServiceTest.java | 23 +++++++ 4 files changed, 100 insertions(+), 2 deletions(-) diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/model/ArtifactDownloadInfo.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/model/ArtifactDownloadInfo.java index ce699d0b3..612281a44 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/model/ArtifactDownloadInfo.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/model/ArtifactDownloadInfo.java @@ -52,6 +52,11 @@ public class ArtifactDownloadInfo private String classifier; + public ArtifactDownloadInfo() + { + // no op + } + public ArtifactDownloadInfo( ArtifactMetadata artifact, String path, String type, String classifier ) { this.repositoryId = artifact.getRepositoryId(); @@ -197,4 +202,5 @@ public class ArtifactDownloadInfo sb.append( '}' ); return sb.toString(); } + } 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 a0d9275b1..7859c3535 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 @@ -20,13 +20,14 @@ package org.apache.archiva.rest.api.services; import org.apache.archiva.admin.model.beans.ManagedRepository; import org.apache.archiva.metadata.model.ProjectVersionMetadata; +import org.apache.archiva.redback.authorization.RedbackAuthorization; import org.apache.archiva.rest.api.model.Artifact; import org.apache.archiva.rest.api.model.ArtifactContentEntry; +import org.apache.archiva.rest.api.model.ArtifactDownloadInfo; 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.apache.archiva.redback.authorization.RedbackAuthorization; import javax.ws.rs.DELETE; import javax.ws.rs.GET; @@ -158,4 +159,14 @@ public interface BrowseService @QueryParam( "p" ) String path, @QueryParam( "repositoryId" ) String repositoryId ) throws ArchivaRestServiceException; + + @Path( "artifactDownloadInfos/{g}/{a}/{v}" ) + @GET + @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) + @RedbackAuthorization( noPermission = true, noRestriction = true ) + List getArtifactDownloadInfos( @PathParam( "g" ) String groupId, + @PathParam( "a" ) String artifactId, + @PathParam( "v" ) String version, + @QueryParam( "repositoryId" ) String repositoryId ) + throws ArchivaRestServiceException; } 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 d94f29231..36f84f1a7 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 @@ -22,6 +22,7 @@ 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.ArtifactMetadata; import org.apache.archiva.metadata.model.MetadataFacet; import org.apache.archiva.metadata.model.ProjectVersionMetadata; import org.apache.archiva.metadata.model.ProjectVersionReference; @@ -30,6 +31,7 @@ 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; +import org.apache.archiva.metadata.repository.storage.maven2.ArtifactMetadataVersionComparator; import org.apache.archiva.metadata.repository.storage.maven2.MavenProjectFacet; import org.apache.archiva.model.ArchivaArtifact; import org.apache.archiva.repository.ManagedRepositoryContent; @@ -38,6 +40,7 @@ import org.apache.archiva.repository.RepositoryException; import org.apache.archiva.repository.RepositoryNotFoundException; import org.apache.archiva.rest.api.model.Artifact; import org.apache.archiva.rest.api.model.ArtifactContentEntry; +import org.apache.archiva.rest.api.model.ArtifactDownloadInfo; import org.apache.archiva.rest.api.model.BrowseResult; import org.apache.archiva.rest.api.model.BrowseResultEntry; import org.apache.archiva.rest.api.model.Entry; @@ -46,6 +49,7 @@ 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.ArtifactContentEntryComparator; +import org.apache.archiva.rest.services.utils.ArtifactDownloadInfoBuilder; import org.apache.archiva.rest.services.utils.TreeDependencyNodeVisitor; import org.apache.archiva.security.ArchivaSecurityException; import org.apache.commons.collections.CollectionUtils; @@ -651,6 +655,60 @@ public class DefaultBrowseService return Collections.emptyList(); } + public List getArtifactDownloadInfos( String groupId, String artifactId, String version, + String repositoryId ) + throws ArchivaRestServiceException + { + List selectedRepos = getSelectedRepos( repositoryId ); + + List artifactDownloadInfos = new ArrayList(); + + RepositorySession session = repositorySessionFactory.createSession(); + + MetadataResolver metadataResolver = session.getResolver(); + + try + { + for ( String repoId : selectedRepos ) + { + List artifacts = new ArrayList( + metadataResolver.resolveArtifacts( session, repoId, groupId, artifactId, version ) ); + Collections.sort( artifacts, ArtifactMetadataVersionComparator.INSTANCE ); + + for ( ArtifactMetadata artifact : artifacts ) + { + + ArtifactDownloadInfoBuilder builder = + new ArtifactDownloadInfoBuilder().forArtifactMetadata( artifact ).withManagedRepositoryContent( + repositoryContentFactory.getManagedRepositoryContent( repositoryId ) ); + artifactDownloadInfos.add( builder.build() ); + } + + } + } + catch ( RepositoryException e ) + { + log.error( e.getMessage(), e ); + throw new ArchivaRestServiceException( e.getMessage(), + Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e ); + } + catch ( MetadataResolutionException e ) + { + log.error( e.getMessage(), e ); + throw new ArchivaRestServiceException( e.getMessage(), + Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e ); + } + finally + { + if ( session != null ) + { + session.close(); + } + } + + return artifactDownloadInfos; + } + //--------------------------- // internals //--------------------------- @@ -781,7 +839,7 @@ public class DefaultBrowseService if ( CollectionUtils.isEmpty( selectedRepos ) ) { // FIXME 403 ??? - return null; + return Collections.emptyList(); } if ( StringUtils.isNotEmpty( repositoryId ) ) 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 21ecc8540..bbce29056 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 @@ -20,6 +20,7 @@ package org.apache.archiva.rest.services; import org.apache.archiva.metadata.model.ProjectVersionMetadata; import org.apache.archiva.rest.api.model.ArtifactContentEntry; +import org.apache.archiva.rest.api.model.ArtifactDownloadInfo; import org.apache.archiva.rest.api.model.BrowseResult; import org.apache.archiva.rest.api.model.BrowseResultEntry; import org.apache.archiva.rest.api.model.Entry; @@ -303,5 +304,27 @@ public class BrowseServiceTest deleteTestRepo( testRepoId ); } + @Test + public void getArtifactDownloadInfos() + throws Exception + { + String testRepoId = "test-repo"; + // force guest user creation if not exists + if ( getUserService( authorizationHeader ).getGuestUser() == null ) + { + assertNotNull( getUserService( authorizationHeader ).createGuestUser() ); + } + + createAndIndexRepo( testRepoId, new File( getBasedir(), "src/test/repo-with-osgi" ).getAbsolutePath(), false ); + + BrowseService browseService = getBrowseService( authorizationHeader, true ); + + List artifactDownloadInfos = + browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.1", testRepoId ); + + log.info( "artifactDownloadInfos {}", artifactDownloadInfos ); + assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 3 ); + deleteTestRepo( testRepoId ); + } } -- 2.39.5