1 package org.apache.archiva.rest.services;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.archiva.admin.model.RepositoryAdminException;
23 import org.apache.archiva.admin.model.admin.ArchivaAdministration;
24 import org.apache.archiva.checksum.ChecksumAlgorithm;
25 import org.apache.archiva.checksum.ChecksummedFile;
26 import org.apache.archiva.common.utils.VersionComparator;
27 import org.apache.archiva.common.utils.VersionUtil;
28 import org.apache.archiva.maven2.metadata.MavenMetadataReader;
29 import org.apache.archiva.maven2.model.Artifact;
30 import org.apache.archiva.metadata.model.ArtifactMetadata;
31 import org.apache.archiva.metadata.model.facets.AuditEvent;
32 import org.apache.archiva.metadata.model.maven2.MavenArtifactFacet;
33 import org.apache.archiva.metadata.repository.MetadataRepository;
34 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
35 import org.apache.archiva.metadata.repository.MetadataResolutionException;
36 import org.apache.archiva.metadata.repository.RepositorySession;
37 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
38 import org.apache.archiva.model.ArchivaRepositoryMetadata;
39 import org.apache.archiva.model.ArtifactReference;
40 import org.apache.archiva.model.VersionedReference;
41 import org.apache.archiva.redback.authentication.AuthenticationResult;
42 import org.apache.archiva.redback.authorization.AuthorizationException;
43 import org.apache.archiva.redback.components.cache.Cache;
44 import org.apache.archiva.redback.components.taskqueue.TaskQueueException;
45 import org.apache.archiva.redback.system.DefaultSecuritySession;
46 import org.apache.archiva.redback.system.SecuritySession;
47 import org.apache.archiva.redback.system.SecuritySystem;
48 import org.apache.archiva.redback.users.User;
49 import org.apache.archiva.redback.users.UserManagerException;
50 import org.apache.archiva.redback.users.UserNotFoundException;
51 import org.apache.archiva.repository.ContentNotFoundException;
52 import org.apache.archiva.repository.ManagedRepository;
53 import org.apache.archiva.repository.ManagedRepositoryContent;
54 import org.apache.archiva.repository.RepositoryException;
55 import org.apache.archiva.repository.RepositoryNotFoundException;
56 import org.apache.archiva.repository.RepositoryRegistry;
57 import org.apache.archiva.repository.content.RepositoryStorage;
58 import org.apache.archiva.repository.content.StorageAsset;
59 import org.apache.archiva.repository.content.StorageUtil;
60 import org.apache.archiva.repository.events.RepositoryListener;
61 import org.apache.archiva.repository.metadata.MetadataTools;
62 import org.apache.archiva.repository.metadata.RepositoryMetadataException;
63 import org.apache.archiva.repository.metadata.RepositoryMetadataWriter;
64 import org.apache.archiva.repository.scanner.RepositoryScanStatistics;
65 import org.apache.archiva.repository.scanner.RepositoryScanner;
66 import org.apache.archiva.repository.scanner.RepositoryScannerException;
67 import org.apache.archiva.repository.scanner.RepositoryScannerInstance;
68 import org.apache.archiva.rest.api.model.ArtifactTransferRequest;
69 import org.apache.archiva.rest.api.model.StringList;
70 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
71 import org.apache.archiva.rest.api.services.RepositoriesService;
72 import org.apache.archiva.scheduler.ArchivaTaskScheduler;
73 import org.apache.archiva.scheduler.indexing.maven.ArchivaIndexingTaskExecutor;
74 import org.apache.archiva.scheduler.indexing.ArtifactIndexingTask;
75 import org.apache.archiva.scheduler.indexing.DownloadRemoteIndexException;
76 import org.apache.archiva.scheduler.indexing.DownloadRemoteIndexScheduler;
77 import org.apache.archiva.scheduler.repository.model.RepositoryTask;
78 import org.apache.archiva.security.ArchivaSecurityException;
79 import org.apache.archiva.security.common.ArchivaRoleConstants;
80 import org.apache.archiva.xml.XMLException;
81 import org.apache.commons.io.FilenameUtils;
82 import org.apache.commons.lang.StringUtils;
83 import org.slf4j.Logger;
84 import org.slf4j.LoggerFactory;
85 import org.springframework.beans.factory.annotation.Autowired;
86 import org.springframework.stereotype.Service;
88 import javax.inject.Inject;
89 import javax.inject.Named;
90 import javax.ws.rs.core.Response;
91 import java.io.IOException;
92 import java.nio.file.FileSystems;
93 import java.nio.file.Files;
94 import java.nio.file.Path;
95 import java.nio.file.Paths;
96 import java.nio.file.StandardCopyOption;
97 import java.text.DateFormat;
98 import java.text.SimpleDateFormat;
99 import java.util.ArrayList;
100 import java.util.Arrays;
101 import java.util.Calendar;
102 import java.util.Collection;
103 import java.util.Collections;
104 import java.util.Date;
105 import java.util.List;
106 import java.util.Set;
107 import java.util.TimeZone;
110 * @author Olivier Lamy
113 @Service("repositoriesService#rest")
114 public class DefaultRepositoriesService
115 extends AbstractRestService
116 implements RepositoriesService
118 private Logger log = LoggerFactory.getLogger( getClass() );
121 @Named(value = "taskExecutor#indexing")
122 private ArchivaIndexingTaskExecutor archivaIndexingTaskExecutor;
125 private RepositoryRegistry repositoryRegistry;
128 private SecuritySystem securitySystem;
131 @Named(value = "archivaTaskScheduler#repository")
132 private ArchivaTaskScheduler<RepositoryTask> scheduler;
135 private DownloadRemoteIndexScheduler downloadRemoteIndexScheduler;
138 @Named(value = "repositorySessionFactory")
139 protected RepositorySessionFactory repositorySessionFactory;
142 @Autowired(required = false)
143 protected List<RepositoryListener> listeners = new ArrayList<RepositoryListener>();
146 private RepositoryScanner repoScanner;
149 * Cache used for namespaces
152 @Named(value = "cache#namespaces")
153 private Cache<String, Collection<String>> namespacesCache;
155 private List<ChecksumAlgorithm> algorithms = Arrays.asList(ChecksumAlgorithm.SHA256, ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 );
158 public Boolean scanRepository( String repositoryId, boolean fullScan )
160 return doScanRepository( repositoryId, fullScan );
164 public Boolean alreadyScanning( String repositoryId )
166 // check queue first to make sure it doesn't get dequeued between calls
167 if ( repositoryTaskScheduler.isProcessingRepositoryTask( repositoryId ) )
171 for ( RepositoryScannerInstance scan : repoScanner.getInProgressScans() )
173 if ( scan.getRepository().getId().equals( repositoryId ) )
182 public Boolean removeScanningTaskFromQueue( String repositoryId )
184 RepositoryTask task = new RepositoryTask();
185 task.setRepositoryId( repositoryId );
188 return repositoryTaskScheduler.unQueueTask( task );
190 catch ( TaskQueueException e )
192 log.error( "failed to unschedule scanning of repo with id {}", repositoryId, e );
197 private ManagedRepositoryContent getManagedRepositoryContent(String id) throws RepositoryException
199 org.apache.archiva.repository.ManagedRepository repo = repositoryRegistry.getManagedRepository( id );
201 throw new RepositoryException( "Repository not found "+id );
203 return repo.getContent();
207 public Boolean scanRepositoryNow( String repositoryId, boolean fullScan )
208 throws ArchivaRestServiceException
214 org.apache.archiva.repository.ManagedRepository repository = repositoryRegistry.getManagedRepository( repositoryId );
217 ArtifactIndexingTask task =
218 new ArtifactIndexingTask( repository, null, ArtifactIndexingTask.Action.FINISH, repository.getIndexingContext() );
220 task.setExecuteOnEntireRepo( true );
221 task.setOnlyUpdate( !fullScan );
223 archivaIndexingTaskExecutor.executeTask( task );
225 scheduler.queueTask( new RepositoryTask( repositoryId, fullScan ) );
229 catch ( Exception e )
231 log.error( e.getMessage(), e );
232 throw new ArchivaRestServiceException( e.getMessage(), e );
237 public Boolean scheduleDownloadRemoteIndex( String repositoryId, boolean now, boolean fullDownload )
238 throws ArchivaRestServiceException
242 downloadRemoteIndexScheduler.scheduleDownloadRemote( repositoryId, now, fullDownload );
244 catch ( DownloadRemoteIndexException e )
246 log.error( e.getMessage(), e );
247 throw new ArchivaRestServiceException( e.getMessage(), e );
253 public Boolean copyArtifact( ArtifactTransferRequest artifactTransferRequest )
254 throws ArchivaRestServiceException
257 String userName = getAuditInformation().getUser().getUsername();
258 if ( StringUtils.isBlank( userName ) )
260 throw new ArchivaRestServiceException( "copyArtifact call: userName not found", null );
263 if ( StringUtils.isBlank( artifactTransferRequest.getRepositoryId() ) )
265 throw new ArchivaRestServiceException( "copyArtifact call: sourceRepositoryId cannot be null", null );
268 if ( StringUtils.isBlank( artifactTransferRequest.getTargetRepositoryId() ) )
270 throw new ArchivaRestServiceException( "copyArtifact call: targetRepositoryId cannot be null", null );
273 ManagedRepository source = null;
274 source = repositoryRegistry.getManagedRepository( artifactTransferRequest.getRepositoryId() );
276 if ( source == null )
278 throw new ArchivaRestServiceException(
279 "cannot find repository with id " + artifactTransferRequest.getRepositoryId(), null );
282 ManagedRepository target = null;
283 target = repositoryRegistry.getManagedRepository( artifactTransferRequest.getTargetRepositoryId() );
285 if ( target == null )
287 throw new ArchivaRestServiceException(
288 "cannot find repository with id " + artifactTransferRequest.getTargetRepositoryId(), null );
291 if ( StringUtils.isBlank( artifactTransferRequest.getGroupId() ) )
293 throw new ArchivaRestServiceException( "groupId is mandatory", null );
296 if ( StringUtils.isBlank( artifactTransferRequest.getArtifactId() ) )
298 throw new ArchivaRestServiceException( "artifactId is mandatory", null );
301 if ( StringUtils.isBlank( artifactTransferRequest.getVersion() ) )
303 throw new ArchivaRestServiceException( "version is mandatory", null );
306 if ( VersionUtil.isSnapshot( artifactTransferRequest.getVersion() ) )
308 throw new ArchivaRestServiceException( "copy of SNAPSHOT not supported", null );
311 // end check parameters
316 user = securitySystem.getUserManager().findUser( userName );
318 catch ( UserNotFoundException e )
320 throw new ArchivaRestServiceException( "user " + userName + " not found", e );
322 catch ( UserManagerException e )
324 throw new ArchivaRestServiceException( "ArchivaRestServiceException:" + e.getMessage(), e );
327 // check karma on source : read
328 AuthenticationResult authn = new AuthenticationResult( true, userName, null );
329 SecuritySession securitySession = new DefaultSecuritySession( authn, user );
333 securitySystem.isAuthorized( securitySession, ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS,
334 artifactTransferRequest.getRepositoryId() );
337 throw new ArchivaRestServiceException(
338 "not authorized to access repo:" + artifactTransferRequest.getRepositoryId(), null );
341 catch ( AuthorizationException e )
343 log.error( "error reading permission: {}", e.getMessage(), e );
344 throw new ArchivaRestServiceException( e.getMessage(), e );
347 // check karma on target: write
351 securitySystem.isAuthorized( securitySession, ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD,
352 artifactTransferRequest.getTargetRepositoryId() );
355 throw new ArchivaRestServiceException(
356 "not authorized to write to repo:" + artifactTransferRequest.getTargetRepositoryId(), null );
359 catch ( AuthorizationException e )
361 log.error( "error reading permission: {}", e.getMessage(), e );
362 throw new ArchivaRestServiceException( e.getMessage(), e );
365 // sounds good we can continue !
367 ArtifactReference artifactReference = new ArtifactReference();
368 artifactReference.setArtifactId( artifactTransferRequest.getArtifactId() );
369 artifactReference.setGroupId( artifactTransferRequest.getGroupId() );
370 artifactReference.setVersion( artifactTransferRequest.getVersion() );
371 artifactReference.setClassifier( artifactTransferRequest.getClassifier() );
372 String packaging = StringUtils.trim( artifactTransferRequest.getPackaging() );
373 artifactReference.setType( StringUtils.isEmpty( packaging ) ? "jar" : packaging );
378 ManagedRepositoryContent sourceRepository =
379 getManagedRepositoryContent( artifactTransferRequest.getRepositoryId() );
381 String artifactSourcePath = sourceRepository.toPath( artifactReference );
383 if ( StringUtils.isEmpty( artifactSourcePath ) )
385 log.error( "cannot find artifact {}", artifactTransferRequest );
386 throw new ArchivaRestServiceException( "cannot find artifact " + artifactTransferRequest.toString(),
390 StorageAsset artifactFile = source.getAsset( artifactSourcePath );
392 if ( !artifactFile.exists() )
394 log.error( "cannot find artifact {}", artifactTransferRequest );
395 throw new ArchivaRestServiceException( "cannot find artifact " + artifactTransferRequest.toString(),
399 ManagedRepositoryContent targetRepository =
400 getManagedRepositoryContent( artifactTransferRequest.getTargetRepositoryId() );
402 String artifactPath = targetRepository.toPath( artifactReference );
404 int lastIndex = artifactPath.lastIndexOf( '/' );
406 String path = artifactPath.substring( 0, lastIndex );
407 StorageAsset targetPath = target.getAsset( path );
409 Date lastUpdatedTimestamp = Calendar.getInstance().getTime();
410 int newBuildNumber = 1;
411 String timestamp = null;
413 StorageAsset versionMetadataFile = target.getAsset(path + "/" + MetadataTools.MAVEN_METADATA );
414 /* unused */ getMetadata( versionMetadataFile );
416 if ( !targetPath.exists() )
418 targetPath = target.addAsset(targetPath.getPath(), true);
422 String filename = artifactPath.substring( lastIndex + 1 );
424 boolean fixChecksums =
425 !( archivaAdministration.getKnownContentConsumers().contains( "create-missing-checksums" ) );
427 StorageAsset targetFile = target.getAsset(targetPath + "/" + filename );
428 if ( targetFile.exists() && target.blocksRedeployments())
430 throw new ArchivaRestServiceException(
431 "artifact already exists in target repo: " + artifactTransferRequest.getTargetRepositoryId()
432 + " and redeployment blocked", null
437 copyFile( source, artifactFile, target, targetFile, fixChecksums );
438 queueRepositoryTask( target.getId(), targetFile );
441 // copy source pom to target repo
442 String pomFilename = filename;
443 if ( StringUtils.isNotBlank( artifactTransferRequest.getClassifier() ) )
445 pomFilename = StringUtils.remove( pomFilename, "-" + artifactTransferRequest.getClassifier() );
447 pomFilename = FilenameUtils.removeExtension( pomFilename ) + ".pom";
449 StorageAsset pomFile = source.getAsset(
450 artifactSourcePath.substring( 0, artifactPath.lastIndexOf( '/' ) )+"/"+ pomFilename );
452 if ( pomFile != null && pomFile.exists() )
454 StorageAsset targetPomFile = target.getAsset( targetPath.getPath() + "/" + pomFilename );
455 copyFile( source, pomFile, target, targetPomFile, fixChecksums );
456 queueRepositoryTask( target.getId(), targetPomFile );
461 // explicitly update only if metadata-updater consumer is not enabled!
462 if ( !archivaAdministration.getKnownContentConsumers().contains( "metadata-updater" ) )
464 updateProjectMetadata( target, targetPath, lastUpdatedTimestamp, timestamp, newBuildNumber,
465 fixChecksums, artifactTransferRequest );
471 "Artifact \'" + artifactTransferRequest.getGroupId() + ":" + artifactTransferRequest.getArtifactId()
472 + ":" + artifactTransferRequest.getVersion() + "\' was successfully deployed to repository \'"
473 + artifactTransferRequest.getTargetRepositoryId() + "\'";
474 log.debug("copyArtifact {}", msg);
477 catch ( RepositoryException e )
479 log.error( "RepositoryException: {}", e.getMessage(), e );
480 throw new ArchivaRestServiceException( e.getMessage(), e );
482 catch ( RepositoryAdminException e )
484 log.error( "RepositoryAdminException: {}", e.getMessage(), e );
485 throw new ArchivaRestServiceException( e.getMessage(), e );
487 catch ( IOException e )
489 log.error( "IOException: {}", e.getMessage(), e );
490 throw new ArchivaRestServiceException( e.getMessage(), e );
495 private void queueRepositoryTask( String repositoryId, StorageAsset localFile )
498 RepositoryTask task = new RepositoryTask();
499 task.setRepositoryId( repositoryId );
500 task.setResourceFile( localFile );
501 task.setUpdateRelatedArtifacts( true );
502 //task.setScanAll( true );
506 scheduler.queueTask( task );
508 catch ( TaskQueueException e )
510 log.error( "Unable to queue repository task to execute consumers on resource file ['{}"
511 + "'].", localFile.getName());
515 private ArchivaRepositoryMetadata getMetadata( StorageAsset metadataFile )
516 throws RepositoryMetadataException
518 ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
519 if ( metadataFile.exists() )
523 metadata = MavenMetadataReader.read( metadataFile.getFilePath() );
525 catch ( XMLException e )
527 throw new RepositoryMetadataException( e.getMessage(), e );
533 private StorageAsset getMetadata( RepositoryStorage storage, String targetPath )
535 return storage.getAsset( targetPath + "/" + MetadataTools.MAVEN_METADATA );
540 * Copies the asset to the new target.
542 private void copyFile( RepositoryStorage sourceStorage, StorageAsset sourceFile, RepositoryStorage targetStorage, StorageAsset targetPath, boolean fixChecksums )
546 StorageUtil.copyAsset( sourceStorage, sourceFile, targetStorage, targetPath, true );
549 fixChecksums( targetPath );
553 private void fixChecksums( StorageAsset file )
555 Path destinationFile = file.getFilePath();
556 if (destinationFile!=null)
558 ChecksummedFile checksum = new ChecksummedFile( destinationFile );
559 checksum.fixChecksums( algorithms );
563 private void updateProjectMetadata( RepositoryStorage storage, StorageAsset targetPath, Date lastUpdatedTimestamp, String timestamp, int buildNumber,
564 boolean fixChecksums, ArtifactTransferRequest artifactTransferRequest )
565 throws RepositoryMetadataException
567 List<String> availableVersions = new ArrayList<>();
568 String latestVersion = artifactTransferRequest.getVersion();
570 StorageAsset projectDir = targetPath.getParent();
571 StorageAsset projectMetadataFile = storage.getAsset( projectDir.getPath()+"/"+MetadataTools.MAVEN_METADATA );
573 ArchivaRepositoryMetadata projectMetadata = getMetadata( projectMetadataFile );
575 if ( projectMetadataFile.exists() )
577 availableVersions = projectMetadata.getAvailableVersions();
579 Collections.sort( availableVersions, VersionComparator.getInstance() );
581 if ( !availableVersions.contains( artifactTransferRequest.getVersion() ) )
583 availableVersions.add( artifactTransferRequest.getVersion() );
586 latestVersion = availableVersions.get( availableVersions.size() - 1 );
590 availableVersions.add( artifactTransferRequest.getVersion() );
592 projectMetadata.setGroupId( artifactTransferRequest.getGroupId() );
593 projectMetadata.setArtifactId( artifactTransferRequest.getArtifactId() );
596 if ( projectMetadata.getGroupId() == null )
598 projectMetadata.setGroupId( artifactTransferRequest.getGroupId() );
601 if ( projectMetadata.getArtifactId() == null )
603 projectMetadata.setArtifactId( artifactTransferRequest.getArtifactId() );
606 projectMetadata.setLatestVersion( latestVersion );
607 projectMetadata.setLastUpdatedTimestamp( lastUpdatedTimestamp );
608 projectMetadata.setAvailableVersions( availableVersions );
610 if ( !VersionUtil.isSnapshot( artifactTransferRequest.getVersion() ) )
612 projectMetadata.setReleasedVersion( latestVersion );
615 RepositoryMetadataWriter.write( projectMetadata, projectMetadataFile.getFilePath());
619 fixChecksums( projectMetadataFile );
624 public Boolean removeProjectVersion( String repositoryId, String namespace, String projectId, String version )
625 throws ArchivaRestServiceException
627 // if not a generic we can use the standard way to delete artifact
628 if ( !VersionUtil.isGenericSnapshot( version ) )
630 Artifact artifact = new Artifact( namespace, projectId, version );
631 artifact.setRepositoryId( repositoryId );
632 artifact.setContext( repositoryId );
633 return deleteArtifact( artifact );
636 if ( StringUtils.isEmpty( repositoryId ) )
638 throw new ArchivaRestServiceException( "repositoryId cannot be null", 400, null );
641 if ( !isAuthorizedToDeleteArtifacts( repositoryId ) )
643 throw new ArchivaRestServiceException( "not authorized to delete artifacts", 403, null );
646 if ( StringUtils.isEmpty( namespace ) )
648 throw new ArchivaRestServiceException( "groupId cannot be null", 400, null );
651 if ( StringUtils.isEmpty( projectId ) )
653 throw new ArchivaRestServiceException( "artifactId cannot be null", 400, null );
656 if ( StringUtils.isEmpty( version ) )
658 throw new ArchivaRestServiceException( "version cannot be null", 400, null );
661 RepositorySession repositorySession = repositorySessionFactory.createSession();
665 ManagedRepositoryContent repository = getManagedRepositoryContent( repositoryId );
667 VersionedReference ref = new VersionedReference();
668 ref.setArtifactId( projectId );
669 ref.setGroupId( namespace );
670 ref.setVersion( version );
672 repository.deleteVersion( ref );
675 ProjectReference projectReference = new ProjectReference();
676 projectReference.setGroupId( namespace );
677 projectReference.setArtifactId( projectId );
679 repository.getVersions( )
682 ArtifactReference artifactReference = new ArtifactReference();
683 artifactReference.setGroupId( namespace );
684 artifactReference.setArtifactId( projectId );
685 artifactReference.setVersion( version );
687 MetadataRepository metadataRepository = repositorySession.getRepository();
689 Set<ArtifactReference> related = repository.getRelatedArtifacts( artifactReference );
690 log.debug( "related: {}", related );
691 for ( ArtifactReference artifactRef : related )
693 repository.deleteArtifact( artifactRef );
696 Collection<ArtifactMetadata> artifacts =
697 metadataRepository.getArtifacts( repositoryId, namespace, projectId, version );
699 for ( ArtifactMetadata artifactMetadata : artifacts )
701 metadataRepository.removeArtifact( artifactMetadata, version );
704 metadataRepository.removeProjectVersion( repositoryId, namespace, projectId, version );
706 catch ( MetadataRepositoryException e )
708 throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
710 catch ( MetadataResolutionException e )
712 throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
714 catch ( RepositoryException e )
716 throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
721 repositorySession.save();
723 repositorySession.close();
730 public Boolean deleteArtifact( Artifact artifact )
731 throws ArchivaRestServiceException
734 String repositoryId = artifact.getContext();
735 // some rest call can use context or repositoryId
737 if ( StringUtils.isEmpty( repositoryId ) )
739 repositoryId = artifact.getRepositoryId();
741 if ( StringUtils.isEmpty( repositoryId ) )
743 throw new ArchivaRestServiceException( "repositoryId cannot be null", 400, null );
746 if ( !isAuthorizedToDeleteArtifacts( repositoryId ) )
748 throw new ArchivaRestServiceException( "not authorized to delete artifacts", 403, null );
751 if ( artifact == null )
753 throw new ArchivaRestServiceException( "artifact cannot be null", 400, null );
756 if ( StringUtils.isEmpty( artifact.getGroupId() ) )
758 throw new ArchivaRestServiceException( "artifact.groupId cannot be null", 400, null );
761 if ( StringUtils.isEmpty( artifact.getArtifactId() ) )
763 throw new ArchivaRestServiceException( "artifact.artifactId cannot be null", 400, null );
766 // TODO more control on artifact fields
768 boolean snapshotVersion =
769 VersionUtil.isSnapshot( artifact.getVersion() ) | VersionUtil.isGenericSnapshot( artifact.getVersion() );
771 RepositorySession repositorySession = repositorySessionFactory.createSession();
774 Date lastUpdatedTimestamp = Calendar.getInstance().getTime();
776 TimeZone timezone = TimeZone.getTimeZone( "UTC" );
777 DateFormat fmt = new SimpleDateFormat( "yyyyMMdd.HHmmss" );
778 fmt.setTimeZone( timezone );
779 ManagedRepository repo = repositoryRegistry.getManagedRepository( repositoryId );
781 VersionedReference ref = new VersionedReference();
782 ref.setArtifactId( artifact.getArtifactId() );
783 ref.setGroupId( artifact.getGroupId() );
784 ref.setVersion( artifact.getVersion() );
786 ManagedRepositoryContent repository = getManagedRepositoryContent( repositoryId );
788 ArtifactReference artifactReference = new ArtifactReference();
789 artifactReference.setArtifactId( artifact.getArtifactId() );
790 artifactReference.setGroupId( artifact.getGroupId() );
791 artifactReference.setVersion( artifact.getVersion() );
792 artifactReference.setClassifier( artifact.getClassifier() );
793 artifactReference.setType( artifact.getPackaging() );
795 MetadataRepository metadataRepository = repositorySession.getRepository();
797 String path = repository.toMetadataPath( ref );
799 if ( StringUtils.isNotBlank( artifact.getClassifier() ) )
801 if ( StringUtils.isBlank( artifact.getPackaging() ) )
803 throw new ArchivaRestServiceException( "You must configure a type/packaging when using classifier",
807 repository.deleteArtifact( artifactReference );
813 int index = path.lastIndexOf( '/' );
814 path = path.substring( 0, index );
815 StorageAsset targetPath = repo.getAsset( path );
817 if ( targetPath.exists() )
819 //throw new ContentNotFoundException(
820 // artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion() );
821 log.warn( "targetPath {} not found skip file deletion", targetPath );
824 // TODO: this should be in the storage mechanism so that it is all tied together
825 // delete from file system
826 if ( !snapshotVersion )
828 repository.deleteVersion( ref );
832 Set<ArtifactReference> related = repository.getRelatedArtifacts( artifactReference );
833 log.debug( "related: {}", related );
834 for ( ArtifactReference artifactRef : related )
836 repository.deleteArtifact( artifactRef );
839 StorageAsset metadataFile = getMetadata( repo, targetPath.getPath() );
840 ArchivaRepositoryMetadata metadata = getMetadata( metadataFile );
842 updateMetadata( metadata, metadataFile, lastUpdatedTimestamp, artifact );
844 Collection<ArtifactMetadata> artifacts = Collections.emptyList();
846 if ( snapshotVersion )
848 String baseVersion = VersionUtil.getBaseVersion( artifact.getVersion() );
850 metadataRepository.getArtifacts( repositoryId, artifact.getGroupId(), artifact.getArtifactId(),
856 metadataRepository.getArtifacts( repositoryId, artifact.getGroupId(), artifact.getArtifactId(),
857 artifact.getVersion() );
860 log.debug( "artifacts: {}", artifacts );
862 if ( artifacts.isEmpty() )
864 if ( !snapshotVersion )
866 // verify metata repository doesn't contains anymore the version
867 Collection<String> projectVersions =
868 metadataRepository.getProjectVersions( repositoryId, artifact.getGroupId(),
869 artifact.getArtifactId() );
871 if ( projectVersions.contains( artifact.getVersion() ) )
873 log.warn( "artifact not found when deleted but version still here ! so force cleanup" );
874 metadataRepository.removeProjectVersion( repositoryId, artifact.getGroupId(),
875 artifact.getArtifactId(), artifact.getVersion() );
881 for ( ArtifactMetadata artifactMetadata : artifacts )
884 // TODO: mismatch between artifact (snapshot) version and project (base) version here
885 if ( artifactMetadata.getVersion().equals( artifact.getVersion() ) )
887 if ( StringUtils.isNotBlank( artifact.getClassifier() ) )
889 if ( StringUtils.isBlank( artifact.getPackaging() ) )
891 throw new ArchivaRestServiceException(
892 "You must configure a type/packaging when using classifier", 400, null );
894 // cleanup facet which contains classifier information
895 MavenArtifactFacet mavenArtifactFacet =
896 (MavenArtifactFacet) artifactMetadata.getFacet( MavenArtifactFacet.FACET_ID );
898 if ( StringUtils.equals( artifact.getClassifier(), mavenArtifactFacet.getClassifier() ) )
900 artifactMetadata.removeFacet( MavenArtifactFacet.FACET_ID );
901 String groupId = artifact.getGroupId(), artifactId = artifact.getArtifactId(), version =
902 artifact.getVersion();
903 MavenArtifactFacet mavenArtifactFacetToCompare = new MavenArtifactFacet();
904 mavenArtifactFacetToCompare.setClassifier( artifact.getClassifier() );
905 metadataRepository.removeArtifact( repositoryId, groupId, artifactId, version,
906 mavenArtifactFacetToCompare );
907 metadataRepository.save();
913 if ( snapshotVersion )
915 metadataRepository.removeArtifact( artifactMetadata,
916 VersionUtil.getBaseVersion( artifact.getVersion() ) );
920 metadataRepository.removeArtifact( artifactMetadata.getRepositoryId(),
921 artifactMetadata.getNamespace(),
922 artifactMetadata.getProject(), artifact.getVersion(),
923 artifactMetadata.getId() );
926 // TODO: move into the metadata repository proper - need to differentiate attachment of
927 // repository metadata to an artifact
928 for ( RepositoryListener listener : listeners )
930 listener.deleteArtifact( metadataRepository, repository.getId(),
931 artifactMetadata.getNamespace(), artifactMetadata.getProject(),
932 artifactMetadata.getVersion(), artifactMetadata.getId() );
935 triggerAuditEvent( repositoryId, path, AuditEvent.REMOVE_FILE );
939 catch ( ContentNotFoundException e )
941 throw new ArchivaRestServiceException( "Artifact does not exist: " + e.getMessage(), 400, e );
943 catch ( RepositoryNotFoundException e )
945 throw new ArchivaRestServiceException( "Target repository cannot be found: " + e.getMessage(), 400, e );
947 catch ( RepositoryException e )
949 throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
951 catch ( MetadataResolutionException e )
953 throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
955 catch ( MetadataRepositoryException e )
957 throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
962 repositorySession.save();
964 repositorySession.close();
970 public Boolean deleteGroupId( String groupId, String repositoryId )
971 throws ArchivaRestServiceException
973 if ( StringUtils.isEmpty( repositoryId ) )
975 throw new ArchivaRestServiceException( "repositoryId cannot be null", 400, null );
978 if ( !isAuthorizedToDeleteArtifacts( repositoryId ) )
980 throw new ArchivaRestServiceException( "not authorized to delete artifacts", 403, null );
983 if ( StringUtils.isEmpty( groupId ) )
985 throw new ArchivaRestServiceException( "groupId cannot be null", 400, null );
988 RepositorySession repositorySession = repositorySessionFactory.createSession();
992 ManagedRepositoryContent repository = getManagedRepositoryContent( repositoryId );
994 repository.deleteGroupId( groupId );
996 MetadataRepository metadataRepository = repositorySession.getRepository();
998 metadataRepository.removeNamespace( repositoryId, groupId );
1000 // just invalidate cache entry
1001 String cacheKey = repositoryId + "-" + groupId;
1002 namespacesCache.remove( cacheKey );
1003 namespacesCache.remove( repositoryId );
1005 metadataRepository.save();
1007 catch ( MetadataRepositoryException e )
1009 log.error( e.getMessage(), e );
1010 throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
1012 catch ( RepositoryException e )
1014 log.error( e.getMessage(), e );
1015 throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
1020 repositorySession.close();
1026 public Boolean deleteProject( String groupId, String projectId, String repositoryId )
1027 throws ArchivaRestServiceException
1029 if ( StringUtils.isEmpty( repositoryId ) )
1031 throw new ArchivaRestServiceException( "repositoryId cannot be null", 400, null );
1034 if ( !isAuthorizedToDeleteArtifacts( repositoryId ) )
1036 throw new ArchivaRestServiceException( "not authorized to delete artifacts", 403, null );
1039 if ( StringUtils.isEmpty( groupId ) )
1041 throw new ArchivaRestServiceException( "groupId cannot be null", 400, null );
1044 if ( StringUtils.isEmpty( projectId ) )
1046 throw new ArchivaRestServiceException( "artifactId cannot be null", 400, null );
1049 RepositorySession repositorySession = repositorySessionFactory.createSession();
1053 ManagedRepositoryContent repository = getManagedRepositoryContent( repositoryId );
1055 repository.deleteProject( groupId, projectId );
1057 catch ( ContentNotFoundException e )
1059 log.warn( "skip ContentNotFoundException: {}", e.getMessage() );
1061 catch ( RepositoryException e )
1063 log.error( e.getMessage(), e );
1064 throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
1070 MetadataRepository metadataRepository = repositorySession.getRepository();
1072 metadataRepository.removeProject( repositoryId, groupId, projectId );
1074 metadataRepository.save();
1076 catch ( MetadataRepositoryException e )
1078 log.error( e.getMessage(), e );
1079 throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
1084 repositorySession.close();
1091 public Boolean isAuthorizedToDeleteArtifacts( String repoId )
1092 throws ArchivaRestServiceException
1095 getAuditInformation().getUser() == null ? "guest" : getAuditInformation().getUser().getUsername();
1099 return userRepositories.isAuthorizedToDeleteArtifacts( userName, repoId );
1101 catch ( ArchivaSecurityException e )
1103 throw new ArchivaRestServiceException( e.getMessage(),
1104 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
1109 public RepositoryScanStatistics scanRepositoryDirectoriesNow( String repositoryId )
1110 throws ArchivaRestServiceException
1112 long sinceWhen = RepositoryScanner.FRESH_SCAN;
1115 return repoScanner.scan( repositoryRegistry.getManagedRepository( repositoryId ), sinceWhen );
1117 catch ( RepositoryScannerException e )
1119 log.error( e.getMessage(), e );
1120 throw new ArchivaRestServiceException( "RepositoryScannerException exception: " + e.getMessage(), 500, e );
1125 * Update artifact level metadata. Creates one if metadata does not exist after artifact deletion.
1129 private void updateMetadata( ArchivaRepositoryMetadata metadata, StorageAsset metadataFile, Date lastUpdatedTimestamp,
1131 throws RepositoryMetadataException
1133 List<String> availableVersions = new ArrayList<>();
1134 String latestVersion = "";
1136 if ( metadataFile.exists() )
1138 if ( metadata.getAvailableVersions() != null )
1140 availableVersions = metadata.getAvailableVersions();
1142 if ( availableVersions.size() > 0 )
1144 Collections.sort( availableVersions, VersionComparator.getInstance() );
1146 if ( availableVersions.contains( artifact.getVersion() ) )
1148 availableVersions.remove( availableVersions.indexOf( artifact.getVersion() ) );
1150 if ( availableVersions.size() > 0 )
1152 latestVersion = availableVersions.get( availableVersions.size() - 1 );
1158 if ( metadata.getGroupId() == null )
1160 metadata.setGroupId( artifact.getGroupId() );
1162 if ( metadata.getArtifactId() == null )
1164 metadata.setArtifactId( artifact.getArtifactId() );
1167 if ( !VersionUtil.isSnapshot( artifact.getVersion() ) )
1169 if ( metadata.getReleasedVersion() != null && metadata.getReleasedVersion().equals(
1170 artifact.getVersion() ) )
1172 metadata.setReleasedVersion( latestVersion );
1176 metadata.setLatestVersion( latestVersion );
1177 metadata.setLastUpdatedTimestamp( lastUpdatedTimestamp );
1178 metadata.setAvailableVersions( availableVersions );
1180 RepositoryMetadataWriter.write( metadata, metadataFile.getFilePath());
1181 ChecksummedFile checksum = new ChecksummedFile( metadataFile.getFilePath() );
1182 checksum.fixChecksums( algorithms );
1186 public StringList getRunningRemoteDownloadIds()
1188 return new StringList( downloadRemoteIndexScheduler.getRunningRemoteDownloadIds() );
1191 public RepositorySessionFactory getRepositorySessionFactory()
1193 return repositorySessionFactory;
1196 public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
1198 this.repositorySessionFactory = repositorySessionFactory;
1201 public List<RepositoryListener> getListeners()
1206 public void setListeners( List<RepositoryListener> listeners )
1208 this.listeners = listeners;
1211 public ArchivaAdministration getArchivaAdministration()
1213 return archivaAdministration;
1216 public void setArchivaAdministration( ArchivaAdministration archivaAdministration )
1218 this.archivaAdministration = archivaAdministration;