]> source.dussan.org Git - archiva.git/commitdiff
add a rest service to get ArtifactDownloadInfo
authorOlivier Lamy <olamy@apache.org>
Sat, 19 May 2012 13:33:06 +0000 (13:33 +0000)
committerOlivier Lamy <olamy@apache.org>
Sat, 19 May 2012 13:33:06 +0000 (13:33 +0000)
git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1340479 13f79535-47bb-0310-9956-ffa450edef68

archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/model/ArtifactDownloadInfo.java
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/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/BrowseServiceTest.java

index ce699d0b3fcbe6b8d24fabb3bddb9e77bf2bef32..612281a44f8061538d7c31cf43dd26af5cbb3340 100644 (file)
@@ -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();
     }
+
 }
index a0d9275b1133d08d367d748d3aa505e370a2f902..7859c35357b0884d952fbd5cbaa910b49b3dca16 100644 (file)
@@ -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<ArtifactDownloadInfo> getArtifactDownloadInfos( @PathParam( "g" ) String groupId,
+                                                         @PathParam( "a" ) String artifactId,
+                                                         @PathParam( "v" ) String version,
+                                                         @QueryParam( "repositoryId" ) String repositoryId )
+        throws ArchivaRestServiceException;
 }
index d94f2923108d248dbf266af7e4e085bbf1ac01e5..36f84f1a7588d09d2d3969a95315ab9c35cc8dc6 100644 (file)
@@ -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<ArtifactDownloadInfo> getArtifactDownloadInfos( String groupId, String artifactId, String version,
+                                                                String repositoryId )
+        throws ArchivaRestServiceException
+    {
+        List<String> selectedRepos = getSelectedRepos( repositoryId );
+
+        List<ArtifactDownloadInfo> artifactDownloadInfos = new ArrayList<ArtifactDownloadInfo>();
+
+        RepositorySession session = repositorySessionFactory.createSession();
+
+        MetadataResolver metadataResolver = session.getResolver();
+
+        try
+        {
+            for ( String repoId : selectedRepos )
+            {
+                List<ArtifactMetadata> artifacts = new ArrayList<ArtifactMetadata>(
+                    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 ) )
index 21ecc85402229ad31b16626aa58e62f9eb03c001..bbce2905663102a2d9f3f908d2b817bb1f7296bc 100644 (file)
@@ -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<ArtifactDownloadInfo> artifactDownloadInfos =
+            browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.1", testRepoId );
+
+        log.info( "artifactDownloadInfos {}", artifactDownloadInfos );
+        assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 3 );
+        deleteTestRepo( testRepoId );
+    }
 
 }