From bcc39e49aea8cdacc1984d6d02298493373da4d8 Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Mon, 22 Oct 2012 20:52:14 +0000 Subject: [PATCH] fix deletion of project version when it's snapshot version, timestamped was not deleted git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1401075 13f79535-47bb-0310-9956-ffa450edef68 --- .../api/services/RepositoriesService.java | 23 +++- .../services/DefaultRepositoriesService.java | 106 +++++++++++++++++- .../src/main/webapp/js/archiva/search.js | 12 +- 3 files changed, 129 insertions(+), 12 deletions(-) diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/RepositoriesService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/RepositoriesService.java index 1e8cc7643..eb4529626 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/RepositoriesService.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/RepositoriesService.java @@ -123,6 +123,19 @@ public interface RepositoriesService Boolean deleteArtifact( Artifact artifact ) throws ArchivaRestServiceException; + @Path ("projectVersion/{repositoryId}/{namespace}/{projectId}/{version}") + @DELETE + @Produces ({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN }) + @RedbackAuthorization (noPermission = true) + /** + * permissions are checked in impl + * @since 1.4-M4 + */ + Boolean removeProjectVersion( @PathParam ( "repositoryId" ) String repositoryId, + @PathParam ( "namespace" ) String namespace, @PathParam ( "projectId" ) String projectId, + @PathParam ( "version" ) String version ) + throws ArchivaRestServiceException; + @Path ("isAuthorizedToDeleteArtifacts/{repositoryId}") @GET @Produces ({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN }) @@ -141,16 +154,16 @@ public interface RepositoriesService Boolean deleteGroupId( @QueryParam ("groupId") String groupId, @QueryParam ("repositoryId") String repositoryId ) throws ArchivaRestServiceException; - @Path ( "project/{repositoryId}/{groupId}/{projectId}" ) + @Path ("project/{repositoryId}/{groupId}/{projectId}") @DELETE - @Produces ( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN } ) - @RedbackAuthorization ( noPermission = true ) + @Produces ({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN }) + @RedbackAuthorization (noPermission = true) /** * permissions are checked in impl * @since 1.4-M4 */ - Boolean deleteProject( @PathParam ( "groupId" ) String groupId, @PathParam ( "projectId" ) String projectId, - @PathParam ( "repositoryId" ) String repositoryId ) + Boolean deleteProject( @PathParam ("groupId") String groupId, @PathParam ("projectId") String projectId, + @PathParam ("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/DefaultRepositoriesService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultRepositoriesService.java index a32e25603..24986c5ae 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultRepositoriesService.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultRepositoriesService.java @@ -600,6 +600,109 @@ public class DefaultRepositoriesService } } + public Boolean removeProjectVersion( String repositoryId, String namespace, String projectId, String version ) + throws ArchivaRestServiceException + { + // if not a generic we can use the standard way to delete artifact + if ( !VersionUtil.isGenericSnapshot( version ) ) + { + Artifact artifact = new Artifact( namespace, projectId, version ); + return deleteArtifact( artifact ); + } + + if ( StringUtils.isEmpty( repositoryId ) ) + { + throw new ArchivaRestServiceException( "repositoryId cannot be null", 400, null ); + } + + if ( !isAuthorizedToDeleteArtifacts( repositoryId ) ) + { + throw new ArchivaRestServiceException( "not authorized to delete artifacts", 403, null ); + } + + if ( StringUtils.isEmpty( namespace ) ) + { + throw new ArchivaRestServiceException( "groupId cannot be null", 400, null ); + } + + if ( StringUtils.isEmpty( projectId ) ) + { + throw new ArchivaRestServiceException( "artifactId cannot be null", 400, null ); + } + + if ( StringUtils.isEmpty( version ) ) + { + throw new ArchivaRestServiceException( "version cannot be null", 400, null ); + } + + RepositorySession repositorySession = repositorySessionFactory.createSession(); + + try + { + ManagedRepositoryContent repository = repositoryFactory.getManagedRepositoryContent( repositoryId ); + + VersionedReference ref = new VersionedReference(); + ref.setArtifactId( projectId ); + ref.setGroupId( namespace ); + ref.setVersion( version ); + + repository.deleteVersion( ref ); + + /* + ProjectReference projectReference = new ProjectReference(); + projectReference.setGroupId( namespace ); + projectReference.setArtifactId( projectId ); + + repository.getVersions( ) + */ + + ArtifactReference artifactReference = new ArtifactReference(); + artifactReference.setGroupId( namespace ); + artifactReference.setArtifactId( projectId ); + artifactReference.setVersion( version ); + + MetadataRepository metadataRepository = repositorySession.getRepository(); + + Set related = repository.getRelatedArtifacts( artifactReference ); + log.debug( "related: {}", related ); + for ( ArtifactReference artifactRef : related ) + { + repository.deleteArtifact( artifactRef ); + } + + Collection artifacts = + metadataRepository.getArtifacts( repositoryId, namespace, projectId, version ); + + for ( ArtifactMetadata artifactMetadata : artifacts ) + { + metadataRepository.removeArtifact( artifactMetadata, version ); + } + + metadataRepository.removeProjectVersion( repositoryId, namespace, projectId, version ); + } + catch ( MetadataRepositoryException e ) + { + throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e ); + } + catch ( MetadataResolutionException e ) + { + throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e ); + } + catch ( RepositoryException e ) + { + throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e ); + } + finally + { + + repositorySession.save(); + + repositorySession.close(); + } + + return Boolean.TRUE; + } + public Boolean deleteArtifact( Artifact artifact ) throws ArchivaRestServiceException { @@ -632,7 +735,8 @@ public class DefaultRepositoriesService // TODO more control on artifact fields - boolean snapshotVersion = VersionUtil.isSnapshot( artifact.getVersion() ); + boolean snapshotVersion = + VersionUtil.isSnapshot( artifact.getVersion() ) | VersionUtil.isGenericSnapshot( artifact.getVersion() ); RepositorySession repositorySession = repositorySessionFactory.createSession(); try diff --git a/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/archiva/search.js b/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/archiva/search.js index 552a698fd..71211b861 100644 --- a/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/archiva/search.js +++ b/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/archiva/search.js @@ -270,17 +270,17 @@ define("archiva.search",["jquery","i18n","jquery.tmpl","choosen","knockout","kno }); return; } - $.log("deleteVersion:"+version+',repoId:'+repoId); + clearUserMessages(); var artifact = new Artifact(repoId,null,self.groupId,self.artifactId,repoId,version); openDialogConfirm(function(){ + var url = "restServices/archivaServices/repositoriesService/projectVersion/"+repoId; + url+="/"+encodeURIComponent(self.groupId)+"/"+encodeURIComponent(self.artifactId); + url+="/"+encodeURIComponent(version); $("#dialog-confirm-modal-ok").button('loading'); $.ajax({ - url:"restServices/archivaServices/repositoriesService/deleteArtifact", - type:"POST", - dataType:"json", - contentType: 'application/json', - data: ko.toJSON(artifact), + url:url, + type:"DELETE", success:function(data){ self.versions.remove(version); refreshContent(); -- 2.39.5