add rest service to get artifacts number per repository

git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1388174 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Olivier Lamy 2012-09-20 19:42:21 +00:00
parent 18ef59174e
commit 8f64c48ccf
3 changed files with 151 additions and 88 deletions

View File

@ -43,154 +43,167 @@ import java.util.List;
* @author Olivier Lamy * @author Olivier Lamy
* @since 1.4-M3 * @since 1.4-M3
*/ */
@Path ( "/browseService/" ) @Path ("/browseService/")
public interface BrowseService public interface BrowseService
{ {
@Path ( "rootGroups" ) @Path ("rootGroups")
@GET @GET
@Produces ( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) @Produces ({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@RedbackAuthorization ( noPermission = true, noRestriction = true ) @RedbackAuthorization (noPermission = true, noRestriction = true)
BrowseResult getRootGroups( @QueryParam ( "repositoryId" ) String repositoryId ) BrowseResult getRootGroups( @QueryParam ("repositoryId") String repositoryId )
throws ArchivaRestServiceException; throws ArchivaRestServiceException;
@Path ( "browseGroupId/{groupId}" ) @Path ("browseGroupId/{groupId}")
@GET @GET
@Produces ( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) @Produces ({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@RedbackAuthorization ( noPermission = true, noRestriction = true ) @RedbackAuthorization (noPermission = true, noRestriction = true)
BrowseResult browseGroupId( @PathParam ( "groupId" ) String groupId, BrowseResult browseGroupId( @PathParam ("groupId") String groupId,
@QueryParam ( "repositoryId" ) String repositoryId ) @QueryParam ("repositoryId") String repositoryId )
throws ArchivaRestServiceException; throws ArchivaRestServiceException;
@Path ( "versionsList/{g}/{a}" ) @Path ("versionsList/{g}/{a}")
@GET @GET
@Produces ( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) @Produces ({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@RedbackAuthorization ( noPermission = true, noRestriction = true ) @RedbackAuthorization (noPermission = true, noRestriction = true)
VersionsList getVersionsList( @PathParam ( "g" ) String groupId, @PathParam ( "a" ) String artifactId, VersionsList getVersionsList( @PathParam ("g") String groupId, @PathParam ("a") String artifactId,
@QueryParam ( "repositoryId" ) String repositoryId ) @QueryParam ("repositoryId") String repositoryId )
throws ArchivaRestServiceException; throws ArchivaRestServiceException;
@Path ( "projectVersionMetadata/{g}/{a}" ) @Path ("projectVersionMetadata/{g}/{a}")
@GET @GET
@Produces ( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) @Produces ({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@RedbackAuthorization ( noPermission = true, noRestriction = true ) @RedbackAuthorization (noPermission = true, noRestriction = true)
ProjectVersionMetadata getProjectVersionMetadata( @PathParam ( "g" ) String groupId, ProjectVersionMetadata getProjectVersionMetadata( @PathParam ("g") String groupId,
@PathParam ( "a" ) String artifactId, @PathParam ("a") String artifactId,
@QueryParam ( "repositoryId" ) String repositoryId ) @QueryParam ("repositoryId") String repositoryId )
throws ArchivaRestServiceException; throws ArchivaRestServiceException;
@Path ( "projectVersionMetadata/{g}/{a}/{v}" ) @Path ("projectVersionMetadata/{g}/{a}/{v}")
@GET @GET
@Produces ( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) @Produces ({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@RedbackAuthorization ( noPermission = true, noRestriction = true ) @RedbackAuthorization (noPermission = true, noRestriction = true)
ProjectVersionMetadata getProjectMetadata( @PathParam ( "g" ) String groupId, @PathParam ( "a" ) String artifactId, ProjectVersionMetadata getProjectMetadata( @PathParam ("g") String groupId, @PathParam ("a") String artifactId,
@PathParam ( "v" ) String version, @PathParam ("v") String version,
@QueryParam ( "repositoryId" ) String repositoryId ) @QueryParam ("repositoryId") String repositoryId )
throws ArchivaRestServiceException; throws ArchivaRestServiceException;
@Path ( "userRepositories" ) @Path ("userRepositories")
@GET @GET
@Produces ( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) @Produces ({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@RedbackAuthorization ( noPermission = true, noRestriction = true ) @RedbackAuthorization (noPermission = true, noRestriction = true)
/** /**
* @return List of managed repositories current user can read * @return List of managed repositories current user can read
*/ */
List<ManagedRepository> getUserRepositories() List<ManagedRepository> getUserRepositories()
throws ArchivaRestServiceException; throws ArchivaRestServiceException;
@Path ( "treeEntries/{g}/{a}/{v}" ) @Path ("treeEntries/{g}/{a}/{v}")
@GET @GET
@Produces ( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) @Produces ({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@RedbackAuthorization ( noPermission = true, noRestriction = true ) @RedbackAuthorization (noPermission = true, noRestriction = true)
/** /**
* return the dependency Tree for an artifacts * return the dependency Tree for an artifacts
* <b>the List result has only one entry</b> * <b>the List result has only one entry</b>
*/ */
List<TreeEntry> getTreeEntries( @PathParam ( "g" ) String groupId, @PathParam ( "a" ) String artifactId, List<TreeEntry> getTreeEntries( @PathParam ("g") String groupId, @PathParam ("a") String artifactId,
@PathParam ( "v" ) String version, @PathParam ("v") String version, @QueryParam ("repositoryId") String repositoryId )
@QueryParam ( "repositoryId" ) String repositoryId )
throws ArchivaRestServiceException; throws ArchivaRestServiceException;
@Path ( "dependees/{g}/{a}/{v}" ) @Path ("dependees/{g}/{a}/{v}")
@GET @GET
@Produces ( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) @Produces ({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@RedbackAuthorization ( noPermission = true, noRestriction = true ) @RedbackAuthorization (noPermission = true, noRestriction = true)
/** /**
* List of artifacts using the artifact passed in parameter. * List of artifacts using the artifact passed in parameter.
*/ */
List<Artifact> getDependees( @PathParam ( "g" ) String groupId, @PathParam ( "a" ) String artifactId, 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; throws ArchivaRestServiceException;
@Path ( "metadatas/{g}/{a}/{v}" ) @Path ("metadatas/{g}/{a}/{v}")
@GET @GET
@Produces ( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) @Produces ({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@RedbackAuthorization ( noPermission = true, noRestriction = true ) @RedbackAuthorization (noPermission = true, noRestriction = true)
List<Entry> getMetadatas( @PathParam ( "g" ) String groupId, @PathParam ( "a" ) String artifactId, List<Entry> getMetadatas( @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; throws ArchivaRestServiceException;
@Path ( "metadata/{g}/{a}/{v}/{key}/{value}" ) @Path ("metadata/{g}/{a}/{v}/{key}/{value}")
@PUT @PUT
@Produces ( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) @Produces ({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@RedbackAuthorization ( noPermission = false, noRestriction = false, permissions = "archiva-add-metadata" ) @RedbackAuthorization (noPermission = false, noRestriction = false, permissions = "archiva-add-metadata")
Boolean addMetadata( @PathParam ( "g" ) String groupId, @PathParam ( "a" ) String artifactId, Boolean addMetadata( @PathParam ("g") String groupId, @PathParam ("a") String artifactId,
@PathParam ( "v" ) String version, @PathParam ( "key" ) String key, @PathParam ("v") String version, @PathParam ("key") String key,
@PathParam ( "value" ) String value, @QueryParam ( "repositoryId" ) String repositoryId ) @PathParam ("value") String value, @QueryParam ("repositoryId") String repositoryId )
throws ArchivaRestServiceException; throws ArchivaRestServiceException;
@Path ( "metadata/{g}/{a}/{v}/{key}" ) @Path ("metadata/{g}/{a}/{v}/{key}")
@DELETE @DELETE
@Produces ( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) @Produces ({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@RedbackAuthorization ( noPermission = false, noRestriction = false, permissions = "archiva-add-metadata" ) @RedbackAuthorization (noPermission = false, noRestriction = false, permissions = "archiva-add-metadata")
Boolean deleteMetadata( @PathParam ( "g" ) String groupId, @PathParam ( "a" ) String artifactId, Boolean deleteMetadata( @PathParam ("g") String groupId, @PathParam ("a") String artifactId,
@PathParam ( "v" ) String version, @PathParam ( "key" ) String key, @PathParam ("v") String version, @PathParam ("key") String key,
@QueryParam ( "repositoryId" ) String repositoryId ) @QueryParam ("repositoryId") String repositoryId )
throws ArchivaRestServiceException; throws ArchivaRestServiceException;
@Path ( "artifactContentEntries/{g}/{a}/{v}" ) @Path ("artifactContentEntries/{g}/{a}/{v}")
@GET @GET
@Produces ( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) @Produces ({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@RedbackAuthorization ( noPermission = true, noRestriction = true ) @RedbackAuthorization (noPermission = true, noRestriction = true)
List<ArtifactContentEntry> getArtifactContentEntries( @PathParam ( "g" ) String groupId, List<ArtifactContentEntry> getArtifactContentEntries( @PathParam ("g") String groupId,
@PathParam ( "a" ) String artifactId, @PathParam ("a") String artifactId,
@PathParam ( "v" ) String version, @PathParam ("v") String version,
@QueryParam ( "c" ) String classifier, @QueryParam ("c") String classifier,
@QueryParam ( "t" ) String type, @QueryParam ("t") String type, @QueryParam ("p") String path,
@QueryParam ( "p" ) String path, @QueryParam ("repositoryId") String repositoryId )
@QueryParam ( "repositoryId" ) String repositoryId )
throws ArchivaRestServiceException; throws ArchivaRestServiceException;
@Path ( "artifactDownloadInfos/{g}/{a}/{v}" ) @Path ("artifactDownloadInfos/{g}/{a}/{v}")
@GET @GET
@Produces ( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) @Produces ({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@RedbackAuthorization ( noPermission = true, noRestriction = true ) @RedbackAuthorization (noPermission = true, noRestriction = true)
List<Artifact> getArtifactDownloadInfos( @PathParam ( "g" ) String groupId, @PathParam ( "a" ) String artifactId, List<Artifact> getArtifactDownloadInfos( @PathParam ("g") String groupId, @PathParam ("a") String artifactId,
@PathParam ( "v" ) String version, @PathParam ("v") String version,
@QueryParam ( "repositoryId" ) String repositoryId ) @QueryParam ("repositoryId") String repositoryId )
throws ArchivaRestServiceException; throws ArchivaRestServiceException;
@Path ( "artifactContentText/{g}/{a}/{v}" ) @Path ("artifactContentText/{g}/{a}/{v}")
@GET @GET
@Produces ( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) @Produces ({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@RedbackAuthorization ( noPermission = true, noRestriction = true ) @RedbackAuthorization (noPermission = true, noRestriction = true)
/** /**
* if path is empty content of the file is returned (for pom view) * if path is empty content of the file is returned (for pom view)
*/ */
ArtifactContent getArtifactContentText( @PathParam ( "g" ) String groupId, @PathParam ( "a" ) String artifactId, ArtifactContent getArtifactContentText( @PathParam ("g") String groupId, @PathParam ("a") String artifactId,
@PathParam ( "v" ) String version, @QueryParam ( "c" ) String classifier, @PathParam ("v") String version, @QueryParam ("c") String classifier,
@QueryParam ( "t" ) String type, @QueryParam ( "p" ) String path, @QueryParam ("t") String type, @QueryParam ("p") String path,
@QueryParam ( "repositoryId" ) String repositoryId ) @QueryParam ("repositoryId") String repositoryId )
throws ArchivaRestServiceException; throws ArchivaRestServiceException;
@Path ( "artifactAvailable/{g}/{a}/{v}" ) @Path ("artifactAvailable/{g}/{a}/{v}")
@GET @GET
@Produces ( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) @Produces ({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@RedbackAuthorization ( noPermission = true, noRestriction = true ) @RedbackAuthorization (noPermission = true, noRestriction = true)
/** /**
* verify if an artifact is available locally if not download from proxies will be try * verify if an artifact is available locally if not download from proxies will be try
* @since 1.4-M3 * @since 1.4-M3
*/ */
Boolean artifactAvailable( @PathParam ( "g" ) String groupId, @PathParam ( "a" ) String artifactId, Boolean artifactAvailable( @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 ("artifactsNumber/{r}")
@GET
@Produces ({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@RedbackAuthorization (noPermission = true, noRestriction = true)
/**
*
* return artifacts number in a repository
* @param repositoryId
* @return
* @throws ArchivaRestServiceException
* @since 1.4-M3
*/
Integer getArtifactsNumber( @PathParam ( "r" ) String repositoryId )
throws ArchivaRestServiceException; throws ArchivaRestServiceException;
} }

View File

@ -51,7 +51,6 @@ import org.apache.archiva.rest.api.model.Entry;
import org.apache.archiva.rest.api.model.VersionsList; import org.apache.archiva.rest.api.model.VersionsList;
import org.apache.archiva.rest.api.services.ArchivaRestServiceException; import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
import org.apache.archiva.rest.api.services.BrowseService; import org.apache.archiva.rest.api.services.BrowseService;
import org.apache.archiva.rest.services.utils.ArtifactBuilder;
import org.apache.archiva.rest.services.utils.ArtifactContentEntryComparator; import org.apache.archiva.rest.services.utils.ArtifactContentEntryComparator;
import org.apache.archiva.security.ArchivaSecurityException; import org.apache.archiva.security.ArchivaSecurityException;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
@ -62,6 +61,7 @@ import org.springframework.stereotype.Service;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -830,6 +830,24 @@ public class DefaultBrowseService
return false; return false;
} }
public Integer getArtifactsNumber( String repositoryId )
throws ArchivaRestServiceException
{
RepositorySession repositorySession = repositorySessionFactory.createSession();
try
{
return repositorySession.getRepository().getArtifacts( repositoryId ).size();
}
catch ( MetadataRepositoryException e )
{
throw new ArchivaRestServiceException( e.getMessage(), e );
}
finally
{
repositorySession.close();
}
}
//--------------------------- //---------------------------
// internals // internals
//--------------------------- //---------------------------

View File

@ -18,8 +18,8 @@ package org.apache.archiva.rest.services;
* under the License. * under the License.
*/ */
import org.apache.archiva.metadata.model.ProjectVersionMetadata;
import org.apache.archiva.maven2.model.Artifact; import org.apache.archiva.maven2.model.Artifact;
import org.apache.archiva.metadata.model.ProjectVersionMetadata;
import org.apache.archiva.rest.api.model.ArtifactContentEntry; import org.apache.archiva.rest.api.model.ArtifactContentEntry;
import org.apache.archiva.rest.api.model.BrowseResult; import org.apache.archiva.rest.api.model.BrowseResult;
import org.apache.archiva.rest.api.model.BrowseResultEntry; import org.apache.archiva.rest.api.model.BrowseResultEntry;
@ -428,4 +428,36 @@ public class BrowseServiceTest
} }
@Test
public void artifactsNumber()
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 );
//WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
try
{
int number = browseService.getArtifactsNumber( testRepoId );
log.info( "getArtifactsNumber: {}", number );
assertTrue( number > 1 );
}
catch ( Exception e )
{
log.error( e.getMessage(), e );
throw e;
}
}
} }