VersionedReference ref = new VersionedReference();
ref.setArtifactId( artifact.getArtifactId() );
ref.setGroupId( artifact.getGroupId() );
ref.setVersion( artifact.getVersion() );
ManagedRepositoryContent repository = repositoryFactory.getManagedRepositoryContent( repositoryId );
ArtifactReference artifactReference = new ArtifactReference();
artifactReference.setArtifactId( artifact.getArtifactId() );
artifactReference.setGroupId( artifact.getGroupId() );
artifactReference.setVersion( artifact.getVersion() );
artifactReference.setClassifier( artifact.getClassifier() );
artifactReference.setType( artifact.getPackaging() );
MetadataRepository metadataRepository = repositorySession.getRepository();
String path = repository.toMetadataPath( ref );
if ( StringUtils.isNotBlank( artifact.getClassifier() ) )
{
if ( StringUtils.isBlank( artifact.getPackaging() ) )
{
throw new ArchivaRestServiceException( "You must configure a type/packaging when using classifier",
400, null );
}
repository.deleteArtifact( artifactReference );
}
else
{
int index = path.lastIndexOf( '/' );
path = path.substring( 0, index );
File targetPath = new File( repoConfig.getLocation(), path );
if ( !targetPath.exists() )
{
//throw new ContentNotFoundException(
// artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion() );
log.warn( "targetPath {} not found skip file deletion", targetPath );
}
// TODO: this should be in the storage mechanism so that it is all tied together
// delete from file system
if ( !snapshotVersion )
{
repository.deleteVersion( ref );
}
else
{
Set<ArtifactReference> related = repository.getRelatedArtifacts( artifactReference );
log.debug( "related: {}", related );
for ( ArtifactReference artifactRef : related )
{
repository.deleteArtifact( artifactRef );
}
}
File metadataFile = getMetadata( targetPath.getAbsolutePath() );
ArchivaRepositoryMetadata metadata = getMetadata( metadataFile );
updateMetadata( metadata, metadataFile, lastUpdatedTimestamp, artifact );
}
Collection<ArtifactMetadata> artifacts = Collections.emptyList();
if ( snapshotVersion )
{
String baseVersion = VersionUtil.getBaseVersion( artifact.getVersion() );
artifacts =
metadataRepository.getArtifacts( repositoryId, artifact.getGroupId(), artifact.getArtifactId(),
baseVersion );
}
else
{
artifacts =
metadataRepository.getArtifacts( repositoryId, artifact.getGroupId(), artifact.getArtifactId(),
artifact.getVersion() );
}
log.debug( "artifacts: {}", artifacts );
if ( artifacts.isEmpty() )
{
if ( !snapshotVersion )
{
// verify metata repository doesn't contains anymore the version
Collection<String> projectVersions =
metadataRepository.getProjectVersions( repositoryId, artifact.getGroupId(),
artifact.getArtifactId() );
if ( projectVersions.contains( artifact.getVersion() ) )
{
log.warn( "artifact not found when deleted but version still here ! so force cleanup" );
metadataRepository.removeProjectVersion( repositoryId, artifact.getGroupId(),
artifact.getArtifactId(), artifact.getVersion() );
}
}
}
for ( ArtifactMetadata artifactMetadata : artifacts )
{
// TODO: mismatch between artifact (snapshot) version and project (base) version here
if ( artifactMetadata.getVersion().equals( artifact.getVersion() ) )
{
if ( StringUtils.isNotBlank( artifact.getClassifier() ) )
{
if ( StringUtils.isBlank( artifact.getPackaging() ) )
{
throw new ArchivaRestServiceException(
"You must configure a type/packaging when using classifier", 400, null );
}
// cleanup facet which contains classifier information
MavenArtifactFacet mavenArtifactFacet =
(MavenArtifactFacet) artifactMetadata.getFacet( MavenArtifactFacet.FACET_ID );
if ( StringUtils.equals( artifact.getClassifier(), mavenArtifactFacet.getClassifier() ) )
{
artifactMetadata.removeFacet( MavenArtifactFacet.FACET_ID );
String groupId = artifact.getGroupId(), artifactId = artifact.getArtifactId(), version =
artifact.getVersion();
MavenArtifactFacet mavenArtifactFacetToCompare = new MavenArtifactFacet();
mavenArtifactFacetToCompare.setClassifier( artifact.getClassifier() );
metadataRepository.removeArtifact( repositoryId, groupId, artifactId, version,
mavenArtifactFacetToCompare );
metadataRepository.save();
}
}
else
{
if ( snapshotVersion )
{
metadataRepository.removeArtifact( artifactMetadata,
VersionUtil.getBaseVersion( artifact.getVersion() ) );
}
else
{
metadataRepository.removeArtifact( artifactMetadata.getRepositoryId(),
artifactMetadata.getNamespace(),
artifactMetadata.getProject(), artifact.getVersion(),
artifactMetadata.getId() );
}
}
// TODO: move into the metadata repository proper - need to differentiate attachment of
// repository metadata to an artifact
for ( RepositoryListener listener : listeners )
{
listener.deleteArtifact( metadataRepository, repository.getId(),
artifactMetadata.getNamespace(), artifactMetadata.getProject(),
artifactMetadata.getVersion(), artifactMetadata.getId() );
}
triggerAuditEvent( repositoryId, path, AuditEvent.REMOVE_FILE );