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.model.Artifact;
29 import org.apache.archiva.metadata.model.ArtifactMetadata;
30 import org.apache.archiva.metadata.model.facets.AuditEvent;
31 import org.apache.archiva.metadata.maven.model.MavenArtifactFacet;
32 import org.apache.archiva.metadata.repository.*;
33 import org.apache.archiva.model.ArchivaRepositoryMetadata;
34 import org.apache.archiva.model.ArtifactReference;
35 import org.apache.archiva.model.VersionedReference;
36 import org.apache.archiva.redback.authentication.AuthenticationResult;
37 import org.apache.archiva.redback.authorization.AuthorizationException;
38 import org.apache.archiva.components.cache.Cache;
39 import org.apache.archiva.components.taskqueue.TaskQueueException;
40 import org.apache.archiva.redback.system.DefaultSecuritySession;
41 import org.apache.archiva.redback.system.SecuritySession;
42 import org.apache.archiva.redback.system.SecuritySystem;
43 import org.apache.archiva.redback.users.User;
44 import org.apache.archiva.redback.users.UserManagerException;
45 import org.apache.archiva.redback.users.UserNotFoundException;
46 import org.apache.archiva.repository.ContentNotFoundException;
47 import org.apache.archiva.repository.LayoutException;
48 import org.apache.archiva.repository.ManagedRepository;
49 import org.apache.archiva.repository.ManagedRepositoryContent;
50 import org.apache.archiva.repository.RepositoryException;
51 import org.apache.archiva.repository.RepositoryNotFoundException;
52 import org.apache.archiva.repository.RepositoryRegistry;
53 import org.apache.archiva.repository.RepositoryType;
54 import org.apache.archiva.repository.content.ItemNotFoundException;
55 import org.apache.archiva.repository.content.base.ArchivaItemSelector;
56 import org.apache.archiva.repository.storage.fs.FsStorageUtil;
57 import org.apache.archiva.repository.storage.RepositoryStorage;
58 import org.apache.archiva.repository.storage.StorageAsset;
59 import org.apache.archiva.metadata.audit.RepositoryListener;
60 import org.apache.archiva.repository.metadata.base.MetadataTools;
61 import org.apache.archiva.repository.metadata.RepositoryMetadataException;
62 import org.apache.archiva.repository.metadata.base.RepositoryMetadataWriter;
63 import org.apache.archiva.repository.scanner.RepositoryScanStatistics;
64 import org.apache.archiva.repository.scanner.RepositoryScanner;
65 import org.apache.archiva.repository.scanner.RepositoryScannerException;
66 import org.apache.archiva.repository.scanner.RepositoryScannerInstance;
67 import org.apache.archiva.rest.api.model.ArtifactTransferRequest;
68 import org.apache.archiva.rest.api.model.StringList;
69 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
70 import org.apache.archiva.rest.api.services.RepositoriesService;
71 import org.apache.archiva.scheduler.ArchivaTaskScheduler;
72 import org.apache.archiva.scheduler.indexing.maven.ArchivaIndexingTaskExecutor;
73 import org.apache.archiva.scheduler.indexing.ArtifactIndexingTask;
74 import org.apache.archiva.scheduler.indexing.DownloadRemoteIndexException;
75 import org.apache.archiva.scheduler.indexing.DownloadRemoteIndexScheduler;
76 import org.apache.archiva.scheduler.repository.model.RepositoryTask;
77 import org.apache.archiva.security.ArchivaSecurityException;
78 import org.apache.archiva.security.common.ArchivaRoleConstants;
79 import org.apache.commons.io.FilenameUtils;
80 import org.apache.commons.lang3.StringUtils;
81 import org.slf4j.Logger;
82 import org.slf4j.LoggerFactory;
83 import org.springframework.beans.factory.annotation.Autowired;
84 import org.springframework.stereotype.Service;
86 import javax.inject.Inject;
87 import javax.inject.Named;
88 import javax.ws.rs.core.Response;
89 import java.io.IOException;
90 import java.io.OutputStreamWriter;
91 import java.nio.file.Path;
92 import java.text.DateFormat;
93 import java.text.SimpleDateFormat;
94 import java.util.ArrayList;
95 import java.util.Arrays;
96 import java.util.Calendar;
97 import java.util.Collection;
98 import java.util.Collections;
99 import java.util.Date;
100 import java.util.List;
101 import java.util.TimeZone;
104 * @author Olivier Lamy
107 @Service("repositoriesService#rest")
108 public class DefaultRepositoriesService
109 extends AbstractRestService
110 implements RepositoriesService
112 private Logger log = LoggerFactory.getLogger( getClass() );
115 @Named(value = "taskExecutor#indexing")
116 private ArchivaIndexingTaskExecutor archivaIndexingTaskExecutor;
119 private RepositoryRegistry repositoryRegistry;
122 private SecuritySystem securitySystem;
125 @Named(value = "archivaTaskScheduler#repository")
126 private ArchivaTaskScheduler<RepositoryTask> scheduler;
129 private DownloadRemoteIndexScheduler downloadRemoteIndexScheduler;
132 @Named(value = "repositorySessionFactory")
133 protected RepositorySessionFactory repositorySessionFactory;
136 @Autowired(required = false)
137 protected List<RepositoryListener> listeners = new ArrayList<RepositoryListener>();
140 private RepositoryScanner repoScanner;
143 * Cache used for namespaces
146 @Named(value = "cache#namespaces")
147 private Cache<String, Collection<String>> namespacesCache;
149 private List<ChecksumAlgorithm> algorithms = Arrays.asList(ChecksumAlgorithm.SHA256, ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 );
152 public Boolean scanRepository( String repositoryId, boolean fullScan )
154 return doScanRepository( repositoryId, fullScan );
158 public Boolean alreadyScanning( String repositoryId )
160 // check queue first to make sure it doesn't get dequeued between calls
161 if ( repositoryTaskScheduler.isProcessingRepositoryTask( repositoryId ) )
165 for ( RepositoryScannerInstance scan : repoScanner.getInProgressScans() )
167 if ( scan.getRepository().getId().equals( repositoryId ) )
176 public Boolean removeScanningTaskFromQueue( String repositoryId )
178 RepositoryTask task = new RepositoryTask();
179 task.setRepositoryId( repositoryId );
182 return repositoryTaskScheduler.unQueueTask( task );
184 catch ( TaskQueueException e )
186 log.error( "failed to unschedule scanning of repo with id {}", repositoryId, e );
191 private ManagedRepositoryContent getManagedRepositoryContent(String id) throws RepositoryException
193 org.apache.archiva.repository.ManagedRepository repo = repositoryRegistry.getManagedRepository( id );
195 throw new RepositoryException( "Repository not found "+id );
197 return repo.getContent();
201 public Boolean scanRepositoryNow( String repositoryId, boolean fullScan )
202 throws ArchivaRestServiceException
208 org.apache.archiva.repository.ManagedRepository repository = repositoryRegistry.getManagedRepository( repositoryId );
211 ArtifactIndexingTask task =
212 new ArtifactIndexingTask( repository, null, ArtifactIndexingTask.Action.FINISH, repository.getIndexingContext() );
214 task.setExecuteOnEntireRepo( true );
215 task.setOnlyUpdate( !fullScan );
217 archivaIndexingTaskExecutor.executeTask( task );
219 scheduler.queueTask( new RepositoryTask( repositoryId, fullScan ) );
223 catch ( Exception e )
225 log.error( e.getMessage(), e );
226 throw new ArchivaRestServiceException( e.getMessage(), e );
231 public Boolean scheduleDownloadRemoteIndex( String repositoryId, boolean now, boolean fullDownload )
232 throws ArchivaRestServiceException
236 downloadRemoteIndexScheduler.scheduleDownloadRemote( repositoryId, now, fullDownload );
238 catch ( DownloadRemoteIndexException e )
240 log.error( e.getMessage(), e );
241 throw new ArchivaRestServiceException( e.getMessage(), e );
247 public Boolean copyArtifact( ArtifactTransferRequest artifactTransferRequest )
248 throws ArchivaRestServiceException
251 String userName = getAuditInformation().getUser().getUsername();
252 if ( StringUtils.isBlank( userName ) )
254 throw new ArchivaRestServiceException( "copyArtifact call: userName not found", null );
257 if ( StringUtils.isBlank( artifactTransferRequest.getRepositoryId() ) )
259 throw new ArchivaRestServiceException( "copyArtifact call: sourceRepositoryId cannot be null", null );
262 if ( StringUtils.isBlank( artifactTransferRequest.getTargetRepositoryId() ) )
264 throw new ArchivaRestServiceException( "copyArtifact call: targetRepositoryId cannot be null", null );
267 ManagedRepository source = null;
268 source = repositoryRegistry.getManagedRepository( artifactTransferRequest.getRepositoryId() );
270 if ( source == null )
272 throw new ArchivaRestServiceException(
273 "cannot find repository with id " + artifactTransferRequest.getRepositoryId(), null );
276 ManagedRepository target = null;
277 target = repositoryRegistry.getManagedRepository( artifactTransferRequest.getTargetRepositoryId() );
279 if ( target == null )
281 throw new ArchivaRestServiceException(
282 "cannot find repository with id " + artifactTransferRequest.getTargetRepositoryId(), null );
285 if ( StringUtils.isBlank( artifactTransferRequest.getGroupId() ) )
287 throw new ArchivaRestServiceException( "groupId is mandatory", null );
290 if ( StringUtils.isBlank( artifactTransferRequest.getArtifactId() ) )
292 throw new ArchivaRestServiceException( "artifactId is mandatory", null );
295 if ( StringUtils.isBlank( artifactTransferRequest.getVersion() ) )
297 throw new ArchivaRestServiceException( "version is mandatory", null );
300 if ( VersionUtil.isSnapshot( artifactTransferRequest.getVersion() ) )
302 throw new ArchivaRestServiceException( "copy of SNAPSHOT not supported", null );
305 // end check parameters
310 user = securitySystem.getUserManager().findUser( userName );
312 catch ( UserNotFoundException e )
314 throw new ArchivaRestServiceException( "user " + userName + " not found", e );
316 catch ( UserManagerException e )
318 throw new ArchivaRestServiceException( "ArchivaRestServiceException:" + e.getMessage(), e );
321 // check karma on source : read
322 AuthenticationResult authn = new AuthenticationResult( true, userName, null );
323 SecuritySession securitySession = new DefaultSecuritySession( authn, user );
327 securitySystem.isAuthorized( securitySession, ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS,
328 artifactTransferRequest.getRepositoryId() );
331 throw new ArchivaRestServiceException(
332 "not authorized to access repo:" + artifactTransferRequest.getRepositoryId(), null );
335 catch ( AuthorizationException e )
337 log.error( "error reading permission: {}", e.getMessage(), e );
338 throw new ArchivaRestServiceException( e.getMessage(), e );
341 // check karma on target: write
345 securitySystem.isAuthorized( securitySession, ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD,
346 artifactTransferRequest.getTargetRepositoryId() );
349 throw new ArchivaRestServiceException(
350 "not authorized to write to repo:" + artifactTransferRequest.getTargetRepositoryId(), null );
353 catch ( AuthorizationException e )
355 log.error( "error reading permission: {}", e.getMessage(), e );
356 throw new ArchivaRestServiceException( e.getMessage(), e );
359 // sounds good we can continue !
361 ArtifactReference artifactReference = new ArtifactReference();
362 artifactReference.setArtifactId( artifactTransferRequest.getArtifactId() );
363 artifactReference.setGroupId( artifactTransferRequest.getGroupId() );
364 artifactReference.setVersion( artifactTransferRequest.getVersion() );
365 artifactReference.setClassifier( artifactTransferRequest.getClassifier() );
366 String packaging = StringUtils.trim( artifactTransferRequest.getPackaging() );
367 artifactReference.setType( StringUtils.isEmpty( packaging ) ? "jar" : packaging );
372 ManagedRepositoryContent sourceRepository =
373 getManagedRepositoryContent( artifactTransferRequest.getRepositoryId() );
375 String artifactSourcePath = sourceRepository.toPath( artifactReference );
377 if ( StringUtils.isEmpty( artifactSourcePath ) )
379 log.error( "cannot find artifact {}", artifactTransferRequest );
380 throw new ArchivaRestServiceException( "cannot find artifact " + artifactTransferRequest.toString(),
384 StorageAsset artifactFile = source.getAsset( artifactSourcePath );
386 if ( !artifactFile.exists() )
388 log.error( "cannot find artifact {}", artifactTransferRequest );
389 throw new ArchivaRestServiceException( "cannot find artifact " + artifactTransferRequest.toString(),
393 ManagedRepositoryContent targetRepository =
394 getManagedRepositoryContent( artifactTransferRequest.getTargetRepositoryId() );
396 String artifactPath = targetRepository.toPath( artifactReference );
398 int lastIndex = artifactPath.lastIndexOf( '/' );
400 String path = artifactPath.substring( 0, lastIndex );
401 StorageAsset targetDir = target.getAsset( path );
403 Date lastUpdatedTimestamp = Calendar.getInstance().getTime();
404 int newBuildNumber = 1;
405 String timestamp = null;
407 StorageAsset versionMetadataFile = target.getAsset(path + "/" + MetadataTools.MAVEN_METADATA );
408 /* unused */ getMetadata( targetRepository.getRepository().getType(), versionMetadataFile );
410 if ( !targetDir.exists() )
412 targetDir = target.addAsset(targetDir.getPath(), true);
416 String filename = artifactPath.substring( lastIndex + 1 );
418 boolean fixChecksums =
419 !( archivaAdministration.getKnownContentConsumers().contains( "create-missing-checksums" ) );
421 StorageAsset targetFile = target.getAsset(targetDir.getPath() + "/" + filename );
422 if ( targetFile.exists() && target.blocksRedeployments())
424 throw new ArchivaRestServiceException(
425 "artifact already exists in target repo: " + artifactTransferRequest.getTargetRepositoryId()
426 + " and redeployment blocked", null
431 copyFile(artifactFile, targetFile, fixChecksums );
432 queueRepositoryTask( target.getId(), targetFile );
435 // copy source pom to target repo
436 String pomFilename = filename;
437 if ( StringUtils.isNotBlank( artifactTransferRequest.getClassifier() ) )
439 pomFilename = StringUtils.remove( pomFilename, "-" + artifactTransferRequest.getClassifier() );
441 pomFilename = FilenameUtils.removeExtension( pomFilename ) + ".pom";
443 StorageAsset pomFile = source.getAsset(
444 artifactSourcePath.substring( 0, artifactPath.lastIndexOf( '/' ) )+"/"+ pomFilename );
446 if ( pomFile != null && pomFile.exists() )
448 StorageAsset targetPomFile = target.getAsset( targetDir.getPath() + "/" + pomFilename );
449 copyFile(pomFile, targetPomFile, fixChecksums );
450 queueRepositoryTask( target.getId(), targetPomFile );
455 // explicitly update only if metadata-updater consumer is not enabled!
456 if ( !archivaAdministration.getKnownContentConsumers().contains( "metadata-updater" ) )
458 updateProjectMetadata( target.getType(), target, targetDir, lastUpdatedTimestamp, timestamp, newBuildNumber,
459 fixChecksums, artifactTransferRequest );
465 "Artifact \'" + artifactTransferRequest.getGroupId() + ":" + artifactTransferRequest.getArtifactId()
466 + ":" + artifactTransferRequest.getVersion() + "\' was successfully deployed to repository \'"
467 + artifactTransferRequest.getTargetRepositoryId() + "\'";
468 log.debug("copyArtifact {}", msg);
471 catch ( RepositoryException e )
473 log.error( "RepositoryException: {}", e.getMessage(), e );
474 throw new ArchivaRestServiceException( e.getMessage(), e );
476 catch ( RepositoryAdminException e )
478 log.error( "RepositoryAdminException: {}", e.getMessage(), e );
479 throw new ArchivaRestServiceException( e.getMessage(), e );
481 catch ( IOException e )
483 log.error( "IOException: {}", e.getMessage(), e );
484 throw new ArchivaRestServiceException( e.getMessage(), e );
489 private void queueRepositoryTask( String repositoryId, StorageAsset localFile )
492 RepositoryTask task = new RepositoryTask();
493 task.setRepositoryId( repositoryId );
494 task.setResourceFile( localFile );
495 task.setUpdateRelatedArtifacts( true );
496 //task.setScanAll( true );
500 scheduler.queueTask( task );
502 catch ( TaskQueueException e )
504 log.error( "Unable to queue repository task to execute consumers on resource file ['{}"
505 + "'].", localFile.getName());
509 private ArchivaRepositoryMetadata getMetadata( RepositoryType repositoryType, StorageAsset metadataFile )
510 throws RepositoryMetadataException
512 ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
513 if ( metadataFile.exists() )
515 metadata = repositoryRegistry.getMetadataReader( repositoryType ).read( metadataFile );
520 private StorageAsset getMetadata( RepositoryStorage storage, String targetPath )
522 return storage.getAsset( targetPath + "/" + MetadataTools.MAVEN_METADATA );
527 * Copies the asset to the new target.
529 private void copyFile(StorageAsset sourceFile, StorageAsset targetPath, boolean fixChecksums)
533 FsStorageUtil.copyAsset( sourceFile, targetPath, true );
536 fixChecksums( targetPath );
540 private void fixChecksums( StorageAsset file )
542 Path destinationFile = file.getFilePath();
543 if (destinationFile!=null)
545 ChecksummedFile checksum = new ChecksummedFile( destinationFile );
546 checksum.fixChecksums( algorithms );
550 private void updateProjectMetadata( RepositoryType repositoryType, RepositoryStorage storage, StorageAsset targetPath, Date lastUpdatedTimestamp, String timestamp, int buildNumber,
551 boolean fixChecksums, ArtifactTransferRequest artifactTransferRequest )
552 throws RepositoryMetadataException
554 List<String> availableVersions = new ArrayList<>();
555 String latestVersion = artifactTransferRequest.getVersion();
557 StorageAsset projectDir = targetPath.getParent();
558 StorageAsset projectMetadataFile = storage.getAsset( projectDir.getPath()+"/"+MetadataTools.MAVEN_METADATA );
560 ArchivaRepositoryMetadata projectMetadata = getMetadata( repositoryType, projectMetadataFile );
562 if ( projectMetadataFile.exists() )
564 availableVersions = projectMetadata.getAvailableVersions();
566 Collections.sort( availableVersions, VersionComparator.getInstance() );
568 if ( !availableVersions.contains( artifactTransferRequest.getVersion() ) )
570 availableVersions.add( artifactTransferRequest.getVersion() );
573 latestVersion = availableVersions.get( availableVersions.size() - 1 );
577 availableVersions.add( artifactTransferRequest.getVersion() );
579 projectMetadata.setGroupId( artifactTransferRequest.getGroupId() );
580 projectMetadata.setArtifactId( artifactTransferRequest.getArtifactId() );
583 if ( projectMetadata.getGroupId() == null )
585 projectMetadata.setGroupId( artifactTransferRequest.getGroupId() );
588 if ( projectMetadata.getArtifactId() == null )
590 projectMetadata.setArtifactId( artifactTransferRequest.getArtifactId() );
593 projectMetadata.setLatestVersion( latestVersion );
594 projectMetadata.setLastUpdatedTimestamp( lastUpdatedTimestamp );
595 projectMetadata.setAvailableVersions( availableVersions );
597 if ( !VersionUtil.isSnapshot( artifactTransferRequest.getVersion() ) )
599 projectMetadata.setReleasedVersion( latestVersion );
602 try(OutputStreamWriter writer = new OutputStreamWriter(projectMetadataFile.getWriteStream(true))) {
603 RepositoryMetadataWriter.write(projectMetadata, writer);
604 } catch (IOException e) {
605 throw new RepositoryMetadataException(e);
610 fixChecksums( projectMetadataFile );
615 public Boolean removeProjectVersion( String repositoryId, String namespace, String projectId, String version )
616 throws ArchivaRestServiceException
618 // if not a generic we can use the standard way to delete artifact
619 if ( !VersionUtil.isGenericSnapshot( version ) )
621 Artifact artifact = new Artifact( namespace, projectId, version );
622 artifact.setRepositoryId( repositoryId );
623 artifact.setContext( repositoryId );
624 return deleteArtifact( artifact );
627 if ( StringUtils.isEmpty( repositoryId ) )
629 throw new ArchivaRestServiceException( "repositoryId cannot be null", 400, null );
632 if ( !isAuthorizedToDeleteArtifacts( repositoryId ) )
634 throw new ArchivaRestServiceException( "not authorized to delete artifacts", 403, null );
637 if ( StringUtils.isEmpty( namespace ) )
639 throw new ArchivaRestServiceException( "groupId cannot be null", 400, null );
642 if ( StringUtils.isEmpty( projectId ) )
644 throw new ArchivaRestServiceException( "artifactId cannot be null", 400, null );
647 if ( StringUtils.isEmpty( version ) )
649 throw new ArchivaRestServiceException( "version cannot be null", 400, null );
652 RepositorySession repositorySession = null;
655 repositorySession = repositorySessionFactory.createSession();
657 catch ( MetadataRepositoryException e )
659 e.printStackTrace( );
664 ManagedRepositoryContent repository = getManagedRepositoryContent( repositoryId );
666 VersionedReference ref = new VersionedReference();
667 ref.setArtifactId( projectId );
668 ref.setGroupId( namespace );
669 ref.setVersion( version );
671 repository.deleteVersion( ref );
674 ArtifactReference artifactReference = new ArtifactReference();
675 artifactReference.setGroupId( namespace );
676 artifactReference.setArtifactId( projectId );
677 artifactReference.setVersion( version );
679 MetadataRepository metadataRepository = repositorySession.getRepository();
681 List<ArtifactReference> related = repository.getRelatedArtifacts( repository.toVersion(artifactReference) );
682 log.debug( "related: {}", related );
683 for ( ArtifactReference artifactRef : related )
685 repository.deleteArtifact( artifactRef );
688 Collection<ArtifactMetadata> artifacts =
689 metadataRepository.getArtifacts(repositorySession , repositoryId, namespace, projectId, version );
691 for ( ArtifactMetadata artifactMetadata : artifacts )
693 metadataRepository.removeTimestampedArtifact(repositorySession , artifactMetadata, version );
696 metadataRepository.removeProjectVersion(repositorySession , repositoryId, namespace, projectId, version );
698 catch ( MetadataRepositoryException | MetadataResolutionException | RepositoryException | LayoutException e )
700 throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
706 repositorySession.save();
707 } catch (MetadataSessionException e) {
708 log.error("Session save failed {}", e.getMessage());
711 repositorySession.close();
719 public Boolean deleteArtifact( Artifact artifact )
720 throws ArchivaRestServiceException
723 String repositoryId = artifact.getContext();
724 // some rest call can use context or repositoryId
726 if ( StringUtils.isEmpty( repositoryId ) )
728 repositoryId = artifact.getRepositoryId();
730 if ( StringUtils.isEmpty( repositoryId ) )
732 throw new ArchivaRestServiceException( "repositoryId cannot be null", 400, null );
735 if ( !isAuthorizedToDeleteArtifacts( repositoryId ) )
737 throw new ArchivaRestServiceException( "not authorized to delete artifacts", 403, null );
740 if ( artifact == null )
742 throw new ArchivaRestServiceException( "artifact cannot be null", 400, null );
745 if ( StringUtils.isEmpty( artifact.getGroupId() ) )
747 throw new ArchivaRestServiceException( "artifact.groupId cannot be null", 400, null );
750 if ( StringUtils.isEmpty( artifact.getArtifactId() ) )
752 throw new ArchivaRestServiceException( "artifact.artifactId cannot be null", 400, null );
755 // TODO more control on artifact fields
757 boolean snapshotVersion =
758 VersionUtil.isSnapshot( artifact.getVersion() ) | VersionUtil.isGenericSnapshot( artifact.getVersion() );
760 RepositorySession repositorySession = null;
763 repositorySession = repositorySessionFactory.createSession();
765 catch ( MetadataRepositoryException e )
767 e.printStackTrace( );
771 Date lastUpdatedTimestamp = Calendar.getInstance().getTime();
773 TimeZone timezone = TimeZone.getTimeZone( "UTC" );
774 DateFormat fmt = new SimpleDateFormat( "yyyyMMdd.HHmmss" );
775 fmt.setTimeZone( timezone );
776 ManagedRepository repo = repositoryRegistry.getManagedRepository( repositoryId );
778 VersionedReference ref = new VersionedReference();
779 ref.setArtifactId( artifact.getArtifactId() );
780 ref.setGroupId( artifact.getGroupId() );
781 ref.setVersion( artifact.getVersion() );
783 ManagedRepositoryContent repository = getManagedRepositoryContent( repositoryId );
785 ArtifactReference artifactReference = new ArtifactReference();
786 artifactReference.setArtifactId( artifact.getArtifactId() );
787 artifactReference.setGroupId( artifact.getGroupId() );
788 artifactReference.setVersion( artifact.getVersion() );
789 artifactReference.setClassifier( artifact.getClassifier() );
790 artifactReference.setType( artifact.getType() );
792 ArchivaItemSelector selector = ArchivaItemSelector.builder( )
793 .withNamespace( artifact.getGroupId( ) )
794 .withProjectId( artifact.getArtifactId( ) )
795 .withVersion( artifact.getVersion( ) )
796 .withClassifier( artifact.getClassifier( ) )
797 .withArtifactId( artifact.getArtifactId( ) )
798 .withType( artifact.getType( ) )
799 .includeRelatedArtifacts()
802 MetadataRepository metadataRepository = repositorySession.getRepository();
804 String path = repository.toMetadataPath( ref );
806 if ( StringUtils.isNotBlank( artifact.getClassifier() ) )
808 if ( StringUtils.isBlank( artifact.getPackaging() ) )
810 throw new ArchivaRestServiceException( "You must configure a type/packaging when using classifier",
813 List<? extends org.apache.archiva.repository.content.Artifact> artifactItems = repository.getArtifacts( selector );
814 for ( org.apache.archiva.repository.content.Artifact aRef : artifactItems ) {
817 repository.deleteItem( aRef );
819 catch ( ItemNotFoundException e )
821 log.error( "Could not delete item, seems to be deleted by other thread. {}, {} ", aRef, e.getMessage( ) );
829 int index = path.lastIndexOf( '/' );
830 path = path.substring( 0, index );
831 StorageAsset targetPath = repo.getAsset( path );
833 if ( !targetPath.exists() )
835 //throw new ContentNotFoundException(
836 // artifact.getNamespace() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion() );
837 log.warn( "targetPath {} not found skip file deletion", targetPath );
841 // TODO: this should be in the storage mechanism so that it is all tied together
842 // delete from file system
843 if ( !snapshotVersion )
845 repository.deleteVersion( ref );
849 // We are deleting all version related artifacts for a snapshot version
850 VersionedReference versionRef = repository.toVersion( artifactReference );
851 List<ArtifactReference> related = repository.getRelatedArtifacts( versionRef );
852 log.debug( "related: {}", related );
853 for ( ArtifactReference artifactRef : related )
857 repository.deleteArtifact( artifactRef );
858 } catch (ContentNotFoundException e) {
859 log.warn( "Artifact that should be deleted, was not found: {}", artifactRef );
862 StorageAsset metadataFile = getMetadata( repo, targetPath.getPath() );
863 ArchivaRepositoryMetadata metadata = getMetadata( repository.getRepository().getType(), metadataFile );
865 updateMetadata( metadata, metadataFile, lastUpdatedTimestamp, artifact );
868 Collection<ArtifactMetadata> artifacts = Collections.emptyList();
870 if ( snapshotVersion )
872 String baseVersion = VersionUtil.getBaseVersion( artifact.getVersion() );
874 metadataRepository.getArtifacts(repositorySession , repositoryId, artifact.getGroupId(),
875 artifact.getArtifactId(), baseVersion );
880 metadataRepository.getArtifacts(repositorySession , repositoryId, artifact.getGroupId(),
881 artifact.getArtifactId(), artifact.getVersion() );
884 log.debug( "artifacts: {}", artifacts );
886 if ( artifacts.isEmpty() )
888 if ( !snapshotVersion )
890 // verify metata repository doesn't contains anymore the version
891 Collection<String> projectVersions =
892 metadataRepository.getProjectVersions(repositorySession , repositoryId,
893 artifact.getGroupId(), artifact.getArtifactId() );
895 if ( projectVersions.contains( artifact.getVersion() ) )
897 log.warn( "artifact not found when deleted but version still here ! so force cleanup" );
898 metadataRepository.removeProjectVersion(repositorySession , repositoryId,
899 artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion() );
905 for ( ArtifactMetadata artifactMetadata : artifacts )
908 // TODO: mismatch between artifact (snapshot) version and project (base) version here
909 if ( artifactMetadata.getVersion().equals( artifact.getVersion() ) )
911 if ( StringUtils.isNotBlank( artifact.getClassifier() ) )
913 if ( StringUtils.isBlank( artifact.getPackaging() ) )
915 throw new ArchivaRestServiceException(
916 "You must configure a type/packaging when using classifier", 400, null );
918 // cleanup facet which contains classifier information
919 MavenArtifactFacet mavenArtifactFacet =
920 (MavenArtifactFacet) artifactMetadata.getFacet( MavenArtifactFacet.FACET_ID );
922 if ( StringUtils.equals( artifact.getClassifier(), mavenArtifactFacet.getClassifier() ) )
924 artifactMetadata.removeFacet( MavenArtifactFacet.FACET_ID );
925 String groupId = artifact.getGroupId(), artifactId = artifact.getArtifactId(), version =
926 artifact.getVersion();
927 MavenArtifactFacet mavenArtifactFacetToCompare = new MavenArtifactFacet();
928 mavenArtifactFacetToCompare.setClassifier( artifact.getClassifier() );
929 metadataRepository.removeFacetFromArtifact(repositorySession , repositoryId, groupId, artifactId,
930 version, mavenArtifactFacetToCompare );
931 repositorySession.save();
937 if ( snapshotVersion )
939 metadataRepository.removeTimestampedArtifact(repositorySession ,
940 artifactMetadata, VersionUtil.getBaseVersion( artifact.getVersion() ) );
944 metadataRepository.removeArtifact(repositorySession ,
945 artifactMetadata.getRepositoryId(),
946 artifactMetadata.getNamespace(), artifactMetadata.getProject(),
947 artifact.getVersion(), artifactMetadata.getId() );
950 // TODO: move into the metadata repository proper - need to differentiate attachment of
951 // repository metadata to an artifact
952 for ( RepositoryListener listener : listeners )
954 listener.deleteArtifact( metadataRepository, repository.getId(),
955 artifactMetadata.getNamespace(), artifactMetadata.getProject(),
956 artifactMetadata.getVersion(), artifactMetadata.getId() );
959 triggerAuditEvent( repositoryId, path, AuditEvent.REMOVE_FILE );
963 catch ( ContentNotFoundException e )
965 throw new ArchivaRestServiceException( "Artifact does not exist: " + e.getMessage(), 400, e );
967 catch ( RepositoryNotFoundException e )
969 throw new ArchivaRestServiceException( "Target repository cannot be found: " + e.getMessage(), 400, e );
971 catch ( RepositoryException e )
973 throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
975 catch (MetadataResolutionException | MetadataSessionException | MetadataRepositoryException | LayoutException e )
977 throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
983 repositorySession.save();
984 } catch (MetadataSessionException e) {
985 log.error("Could not save sesion {}", e.getMessage());
988 repositorySession.close();
994 public Boolean deleteGroupId( String groupId, String repositoryId )
995 throws ArchivaRestServiceException
997 if ( StringUtils.isEmpty( repositoryId ) )
999 throw new ArchivaRestServiceException( "repositoryId cannot be null", 400, null );
1002 if ( !isAuthorizedToDeleteArtifacts( repositoryId ) )
1004 throw new ArchivaRestServiceException( "not authorized to delete artifacts", 403, null );
1007 if ( StringUtils.isEmpty( groupId ) )
1009 throw new ArchivaRestServiceException( "groupId cannot be null", 400, null );
1012 RepositorySession repositorySession = null;
1015 repositorySession = repositorySessionFactory.createSession();
1017 catch ( MetadataRepositoryException e )
1019 e.printStackTrace( );
1024 ManagedRepositoryContent repository = getManagedRepositoryContent( repositoryId );
1026 repository.deleteGroupId( groupId );
1028 MetadataRepository metadataRepository = repositorySession.getRepository();
1030 metadataRepository.removeNamespace(repositorySession , repositoryId, groupId );
1032 // just invalidate cache entry
1033 String cacheKey = repositoryId + "-" + groupId;
1034 namespacesCache.remove( cacheKey );
1035 namespacesCache.remove( repositoryId );
1037 repositorySession.save();
1039 catch (MetadataRepositoryException | MetadataSessionException e )
1041 log.error( e.getMessage(), e );
1042 throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
1044 catch ( RepositoryException e )
1046 log.error( e.getMessage(), e );
1047 throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
1052 repositorySession.close();
1058 public Boolean deleteProject( String groupId, String projectId, String repositoryId )
1059 throws ArchivaRestServiceException
1061 if ( StringUtils.isEmpty( repositoryId ) )
1063 throw new ArchivaRestServiceException( "repositoryId cannot be null", 400, null );
1066 if ( !isAuthorizedToDeleteArtifacts( repositoryId ) )
1068 throw new ArchivaRestServiceException( "not authorized to delete artifacts", 403, null );
1071 if ( StringUtils.isEmpty( groupId ) )
1073 throw new ArchivaRestServiceException( "groupId cannot be null", 400, null );
1076 if ( StringUtils.isEmpty( projectId ) )
1078 throw new ArchivaRestServiceException( "artifactId cannot be null", 400, null );
1081 RepositorySession repositorySession = null;
1084 repositorySession = repositorySessionFactory.createSession();
1086 catch ( MetadataRepositoryException e )
1088 e.printStackTrace( );
1093 ManagedRepositoryContent repository = getManagedRepositoryContent( repositoryId );
1095 repository.deleteProject( groupId, projectId );
1097 catch ( ContentNotFoundException e )
1099 log.warn( "skip ContentNotFoundException: {}", e.getMessage() );
1101 catch ( RepositoryException e )
1103 log.error( e.getMessage(), e );
1104 throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
1110 MetadataRepository metadataRepository = repositorySession.getRepository();
1112 metadataRepository.removeProject(repositorySession , repositoryId, groupId, projectId );
1114 repositorySession.save();
1116 catch (MetadataRepositoryException | MetadataSessionException e )
1118 log.error( e.getMessage(), e );
1119 throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
1124 repositorySession.close();
1131 public Boolean isAuthorizedToDeleteArtifacts( String repoId )
1132 throws ArchivaRestServiceException
1135 getAuditInformation().getUser() == null ? "guest" : getAuditInformation().getUser().getUsername();
1139 return userRepositories.isAuthorizedToDeleteArtifacts( userName, repoId );
1141 catch ( ArchivaSecurityException e )
1143 throw new ArchivaRestServiceException( e.getMessage(),
1144 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
1149 public RepositoryScanStatistics scanRepositoryDirectoriesNow( String repositoryId )
1150 throws ArchivaRestServiceException
1152 long sinceWhen = RepositoryScanner.FRESH_SCAN;
1155 return repoScanner.scan( repositoryRegistry.getManagedRepository( repositoryId ), sinceWhen );
1157 catch ( RepositoryScannerException e )
1159 log.error( e.getMessage(), e );
1160 throw new ArchivaRestServiceException( "RepositoryScannerException exception: " + e.getMessage(), 500, e );
1165 * Update artifact level metadata. Creates one if metadata does not exist after artifact deletion.
1169 private void updateMetadata( ArchivaRepositoryMetadata metadata, StorageAsset metadataFile, Date lastUpdatedTimestamp,
1171 throws RepositoryMetadataException
1173 List<String> availableVersions = new ArrayList<>();
1174 String latestVersion = "";
1176 if ( metadataFile.exists() )
1178 if ( metadata.getAvailableVersions() != null )
1180 availableVersions = metadata.getAvailableVersions();
1182 if ( availableVersions.size() > 0 )
1184 Collections.sort( availableVersions, VersionComparator.getInstance() );
1186 if ( availableVersions.contains( artifact.getVersion() ) )
1188 availableVersions.remove( availableVersions.indexOf( artifact.getVersion() ) );
1190 if ( availableVersions.size() > 0 )
1192 latestVersion = availableVersions.get( availableVersions.size() - 1 );
1198 if ( metadata.getGroupId() == null )
1200 metadata.setGroupId( artifact.getGroupId() );
1202 if ( metadata.getArtifactId() == null )
1204 metadata.setArtifactId( artifact.getArtifactId() );
1207 if ( !VersionUtil.isSnapshot( artifact.getVersion() ) )
1209 if ( metadata.getReleasedVersion() != null && metadata.getReleasedVersion().equals(
1210 artifact.getVersion() ) )
1212 metadata.setReleasedVersion( latestVersion );
1216 metadata.setLatestVersion( latestVersion );
1217 metadata.setLastUpdatedTimestamp( lastUpdatedTimestamp );
1218 metadata.setAvailableVersions( availableVersions );
1220 try (OutputStreamWriter writer = new OutputStreamWriter(metadataFile.getWriteStream(true))) {
1221 RepositoryMetadataWriter.write(metadata, writer);
1222 } catch (IOException e) {
1223 throw new RepositoryMetadataException(e);
1225 ChecksummedFile checksum = new ChecksummedFile( metadataFile.getFilePath() );
1226 checksum.fixChecksums( algorithms );
1230 public StringList getRunningRemoteDownloadIds()
1232 return new StringList( downloadRemoteIndexScheduler.getRunningRemoteDownloadIds() );
1235 public RepositorySessionFactory getRepositorySessionFactory()
1237 return repositorySessionFactory;
1240 public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
1242 this.repositorySessionFactory = repositorySessionFactory;
1245 public List<RepositoryListener> getListeners()
1250 public void setListeners( List<RepositoryListener> listeners )
1252 this.listeners = listeners;
1255 public ArchivaAdministration getArchivaAdministration()
1257 return archivaAdministration;
1260 public void setArchivaAdministration( ArchivaAdministration archivaAdministration )
1262 this.archivaAdministration = archivaAdministration;