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.admin.model.beans.ManagedRepository;
25 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
26 import org.apache.archiva.checksum.ChecksumAlgorithm;
27 import org.apache.archiva.checksum.ChecksummedFile;
28 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
29 import org.apache.archiva.common.utils.VersionComparator;
30 import org.apache.archiva.common.utils.VersionUtil;
31 import org.apache.archiva.maven2.metadata.MavenMetadataReader;
32 import org.apache.archiva.maven2.model.Artifact;
33 import org.apache.archiva.metadata.model.ArtifactMetadata;
34 import org.apache.archiva.metadata.model.facets.AuditEvent;
35 import org.apache.archiva.metadata.model.maven2.MavenArtifactFacet;
36 import org.apache.archiva.metadata.repository.MetadataRepository;
37 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
38 import org.apache.archiva.metadata.repository.MetadataResolutionException;
39 import org.apache.archiva.metadata.repository.RepositorySession;
40 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
41 import org.apache.archiva.model.ArchivaRepositoryMetadata;
42 import org.apache.archiva.model.ArtifactReference;
43 import org.apache.archiva.model.VersionedReference;
44 import org.apache.archiva.redback.authentication.AuthenticationResult;
45 import org.apache.archiva.redback.authorization.AuthorizationException;
46 import org.apache.archiva.redback.components.cache.Cache;
47 import org.apache.archiva.redback.components.taskqueue.TaskQueueException;
48 import org.apache.archiva.redback.system.DefaultSecuritySession;
49 import org.apache.archiva.redback.system.SecuritySession;
50 import org.apache.archiva.redback.system.SecuritySystem;
51 import org.apache.archiva.redback.users.User;
52 import org.apache.archiva.redback.users.UserManagerException;
53 import org.apache.archiva.redback.users.UserNotFoundException;
54 import org.apache.archiva.repository.ContentNotFoundException;
55 import org.apache.archiva.repository.ManagedRepositoryContent;
56 import org.apache.archiva.repository.RepositoryContentFactory;
57 import org.apache.archiva.repository.RepositoryException;
58 import org.apache.archiva.repository.RepositoryNotFoundException;
59 import org.apache.archiva.repository.events.RepositoryListener;
60 import org.apache.archiva.repository.metadata.MetadataTools;
61 import org.apache.archiva.repository.metadata.RepositoryMetadataException;
62 import org.apache.archiva.repository.metadata.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.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.archiva.xml.XMLException;
80 import org.apache.commons.io.FilenameUtils;
81 import org.apache.commons.lang.StringUtils;
82 import org.apache.maven.index.context.IndexingContext;
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;
92 import java.io.IOException;
93 import java.nio.file.Files;
94 import java.nio.file.StandardCopyOption;
95 import java.text.DateFormat;
96 import java.text.SimpleDateFormat;
97 import java.util.ArrayList;
98 import java.util.Calendar;
99 import java.util.Collection;
100 import java.util.Collections;
101 import java.util.Date;
102 import java.util.List;
103 import java.util.Set;
104 import java.util.TimeZone;
107 * @author Olivier Lamy
110 @Service("repositoriesService#rest")
111 public class DefaultRepositoriesService
112 extends AbstractRestService
113 implements RepositoriesService
115 private Logger log = LoggerFactory.getLogger( getClass() );
118 @Named(value = "taskExecutor#indexing")
119 private ArchivaIndexingTaskExecutor archivaIndexingTaskExecutor;
122 private ManagedRepositoryAdmin managedRepositoryAdmin;
125 private PlexusSisuBridge plexusSisuBridge;
128 private SecuritySystem securitySystem;
131 private RepositoryContentFactory repositoryFactory;
134 @Named(value = "archivaTaskScheduler#repository")
135 private ArchivaTaskScheduler scheduler;
138 private DownloadRemoteIndexScheduler downloadRemoteIndexScheduler;
141 @Named(value = "repositorySessionFactory")
142 protected RepositorySessionFactory repositorySessionFactory;
145 @Autowired(required = false)
146 protected List<RepositoryListener> listeners = new ArrayList<RepositoryListener>();
149 private RepositoryScanner repoScanner;
152 * Cache used for namespaces
155 @Named(value = "cache#namespaces")
156 private Cache<String, Collection<String>> namespacesCache;
158 private ChecksumAlgorithm[] algorithms = new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 };
161 public Boolean scanRepository( String repositoryId, boolean fullScan )
163 return doScanRepository( repositoryId, fullScan );
167 public Boolean alreadyScanning( String repositoryId )
169 // check queue first to make sure it doesn't get dequeued between calls
170 if ( repositoryTaskScheduler.isProcessingRepositoryTask( repositoryId ) )
174 for ( RepositoryScannerInstance scan : repoScanner.getInProgressScans() )
176 if ( scan.getRepository().getId().equals( repositoryId ) )
185 public Boolean removeScanningTaskFromQueue( String repositoryId )
187 RepositoryTask task = new RepositoryTask();
188 task.setRepositoryId( repositoryId );
191 return repositoryTaskScheduler.unQueueTask( task );
193 catch ( TaskQueueException e )
195 log.error( "failed to unschedule scanning of repo with id {}", repositoryId, e );
201 public Boolean scanRepositoryNow( String repositoryId, boolean fullScan )
202 throws ArchivaRestServiceException
207 ManagedRepository repository = managedRepositoryAdmin.getManagedRepository( repositoryId );
209 IndexingContext context = managedRepositoryAdmin.createIndexContext( repository );
211 ArtifactIndexingTask task =
212 new ArtifactIndexingTask( repository, null, ArtifactIndexingTask.Action.FINISH, context );
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;
270 source = managedRepositoryAdmin.getManagedRepository( artifactTransferRequest.getRepositoryId() );
272 catch ( RepositoryAdminException e )
274 throw new ArchivaRestServiceException( e.getMessage(), e );
277 if ( source == null )
279 throw new ArchivaRestServiceException(
280 "cannot find repository with id " + artifactTransferRequest.getRepositoryId(), null );
283 ManagedRepository target = null;
286 target = managedRepositoryAdmin.getManagedRepository( artifactTransferRequest.getTargetRepositoryId() );
288 catch ( RepositoryAdminException e )
290 throw new ArchivaRestServiceException( e.getMessage(), e );
293 if ( target == null )
295 throw new ArchivaRestServiceException(
296 "cannot find repository with id " + artifactTransferRequest.getTargetRepositoryId(), null );
299 if ( StringUtils.isBlank( artifactTransferRequest.getGroupId() ) )
301 throw new ArchivaRestServiceException( "groupId is mandatory", null );
304 if ( StringUtils.isBlank( artifactTransferRequest.getArtifactId() ) )
306 throw new ArchivaRestServiceException( "artifactId is mandatory", null );
309 if ( StringUtils.isBlank( artifactTransferRequest.getVersion() ) )
311 throw new ArchivaRestServiceException( "version is mandatory", null );
314 if ( VersionUtil.isSnapshot( artifactTransferRequest.getVersion() ) )
316 throw new ArchivaRestServiceException( "copy of SNAPSHOT not supported", null );
319 // end check parameters
324 user = securitySystem.getUserManager().findUser( userName );
326 catch ( UserNotFoundException e )
328 throw new ArchivaRestServiceException( "user " + userName + " not found", e );
330 catch ( UserManagerException e )
332 throw new ArchivaRestServiceException( "ArchivaRestServiceException:" + e.getMessage(), e );
335 // check karma on source : read
336 AuthenticationResult authn = new AuthenticationResult( true, userName, null );
337 SecuritySession securitySession = new DefaultSecuritySession( authn, user );
341 securitySystem.isAuthorized( securitySession, ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS,
342 artifactTransferRequest.getRepositoryId() );
345 throw new ArchivaRestServiceException(
346 "not authorized to access repo:" + artifactTransferRequest.getRepositoryId(), null );
349 catch ( AuthorizationException e )
351 log.error( "error reading permission: {}", e.getMessage(), e );
352 throw new ArchivaRestServiceException( e.getMessage(), e );
355 // check karma on target: write
359 securitySystem.isAuthorized( securitySession, ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD,
360 artifactTransferRequest.getTargetRepositoryId() );
363 throw new ArchivaRestServiceException(
364 "not authorized to write to repo:" + artifactTransferRequest.getTargetRepositoryId(), null );
367 catch ( AuthorizationException e )
369 log.error( "error reading permission: {}", e.getMessage(), e );
370 throw new ArchivaRestServiceException( e.getMessage(), e );
373 // sounds good we can continue !
375 ArtifactReference artifactReference = new ArtifactReference();
376 artifactReference.setArtifactId( artifactTransferRequest.getArtifactId() );
377 artifactReference.setGroupId( artifactTransferRequest.getGroupId() );
378 artifactReference.setVersion( artifactTransferRequest.getVersion() );
379 artifactReference.setClassifier( artifactTransferRequest.getClassifier() );
380 String packaging = StringUtils.trim( artifactTransferRequest.getPackaging() );
381 artifactReference.setType( StringUtils.isEmpty( packaging ) ? "jar" : packaging );
386 ManagedRepositoryContent sourceRepository =
387 repositoryFactory.getManagedRepositoryContent( artifactTransferRequest.getRepositoryId() );
389 String artifactSourcePath = sourceRepository.toPath( artifactReference );
391 if ( StringUtils.isEmpty( artifactSourcePath ) )
393 log.error( "cannot find artifact {}", artifactTransferRequest );
394 throw new ArchivaRestServiceException( "cannot find artifact " + artifactTransferRequest.toString(),
398 File artifactFile = new File( source.getLocation(), artifactSourcePath );
400 if ( !artifactFile.exists() )
402 log.error( "cannot find artifact {}", artifactTransferRequest );
403 throw new ArchivaRestServiceException( "cannot find artifact " + artifactTransferRequest.toString(),
407 ManagedRepositoryContent targetRepository =
408 repositoryFactory.getManagedRepositoryContent( artifactTransferRequest.getTargetRepositoryId() );
410 String artifactPath = targetRepository.toPath( artifactReference );
412 int lastIndex = artifactPath.lastIndexOf( '/' );
414 String path = artifactPath.substring( 0, lastIndex );
415 File targetPath = new File( target.getLocation(), path );
417 Date lastUpdatedTimestamp = Calendar.getInstance().getTime();
418 int newBuildNumber = 1;
419 String timestamp = null;
421 File versionMetadataFile = new File( targetPath, MetadataTools.MAVEN_METADATA );
422 /* unused */ getMetadata( versionMetadataFile );
424 if ( !targetPath.exists() )
429 String filename = artifactPath.substring( lastIndex + 1 );
431 boolean fixChecksums =
432 !( archivaAdministration.getKnownContentConsumers().contains( "create-missing-checksums" ) );
434 File targetFile = new File( targetPath, filename );
435 if ( targetFile.exists() && target.isBlockRedeployments() )
437 throw new ArchivaRestServiceException(
438 "artifact already exists in target repo: " + artifactTransferRequest.getTargetRepositoryId()
439 + " and redeployment blocked", null
444 copyFile( artifactFile, targetPath, filename, fixChecksums );
445 queueRepositoryTask( target.getId(), targetFile );
448 // copy source pom to target repo
449 String pomFilename = filename;
450 if ( StringUtils.isNotBlank( artifactTransferRequest.getClassifier() ) )
452 pomFilename = StringUtils.remove( pomFilename, "-" + artifactTransferRequest.getClassifier() );
454 pomFilename = FilenameUtils.removeExtension( pomFilename ) + ".pom";
456 File pomFile = new File(
457 new File( source.getLocation(), artifactSourcePath.substring( 0, artifactPath.lastIndexOf( '/' ) ) ),
460 if ( pomFile != null && pomFile.length() > 0 )
462 copyFile( pomFile, targetPath, pomFilename, fixChecksums );
463 queueRepositoryTask( target.getId(), new File( targetPath, pomFilename ) );
468 // explicitly update only if metadata-updater consumer is not enabled!
469 if ( !archivaAdministration.getKnownContentConsumers().contains( "metadata-updater" ) )
471 updateProjectMetadata( targetPath.getAbsolutePath(), lastUpdatedTimestamp, timestamp, newBuildNumber,
472 fixChecksums, artifactTransferRequest );
478 "Artifact \'" + artifactTransferRequest.getGroupId() + ":" + artifactTransferRequest.getArtifactId()
479 + ":" + artifactTransferRequest.getVersion() + "\' was successfully deployed to repository \'"
480 + artifactTransferRequest.getTargetRepositoryId() + "\'";
481 log.debug("copyArtifact {}", msg);
484 catch ( RepositoryException e )
486 log.error( "RepositoryException: {}", e.getMessage(), e );
487 throw new ArchivaRestServiceException( e.getMessage(), e );
489 catch ( RepositoryAdminException e )
491 log.error( "RepositoryAdminException: {}", e.getMessage(), e );
492 throw new ArchivaRestServiceException( e.getMessage(), e );
494 catch ( IOException e )
496 log.error( "IOException: {}", e.getMessage(), e );
497 throw new ArchivaRestServiceException( e.getMessage(), e );
502 private void queueRepositoryTask( String repositoryId, File localFile )
504 RepositoryTask task = new RepositoryTask();
505 task.setRepositoryId( repositoryId );
506 task.setResourceFile( localFile.toPath() );
507 task.setUpdateRelatedArtifacts( true );
508 //task.setScanAll( true );
512 scheduler.queueTask( task );
514 catch ( TaskQueueException e )
516 log.error( "Unable to queue repository task to execute consumers on resource file ['{}"
517 + "'].", localFile.getName() );
521 private ArchivaRepositoryMetadata getMetadata( File metadataFile )
522 throws RepositoryMetadataException
524 ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
525 if ( metadataFile.exists() )
529 metadata = MavenMetadataReader.read( metadataFile.toPath() );
531 catch ( XMLException e )
533 throw new RepositoryMetadataException( e.getMessage(), e );
539 private File getMetadata( String targetPath )
541 String artifactPath = targetPath.substring( 0, targetPath.lastIndexOf( File.separatorChar ) );
543 return new File( artifactPath, MetadataTools.MAVEN_METADATA );
546 private void copyFile( File sourceFile, File targetPath, String targetFilename, boolean fixChecksums )
549 Files.copy( sourceFile.toPath(), new File( targetPath, targetFilename ).toPath(), StandardCopyOption.REPLACE_EXISTING,
550 StandardCopyOption.COPY_ATTRIBUTES );
554 fixChecksums( new File( targetPath, targetFilename ) );
558 private void fixChecksums( File file )
560 ChecksummedFile checksum = new ChecksummedFile( file.toPath() );
561 checksum.fixChecksums( algorithms );
564 private void updateProjectMetadata( String targetPath, Date lastUpdatedTimestamp, String timestamp, int buildNumber,
565 boolean fixChecksums, ArtifactTransferRequest artifactTransferRequest )
566 throws RepositoryMetadataException
568 List<String> availableVersions = new ArrayList<>();
569 String latestVersion = artifactTransferRequest.getVersion();
571 File projectDir = new File( targetPath ).getParentFile();
572 File projectMetadataFile = new File( projectDir, MetadataTools.MAVEN_METADATA );
574 ArchivaRepositoryMetadata projectMetadata = getMetadata( projectMetadataFile );
576 if ( projectMetadataFile.exists() )
578 availableVersions = projectMetadata.getAvailableVersions();
580 Collections.sort( availableVersions, VersionComparator.getInstance() );
582 if ( !availableVersions.contains( artifactTransferRequest.getVersion() ) )
584 availableVersions.add( artifactTransferRequest.getVersion() );
587 latestVersion = availableVersions.get( availableVersions.size() - 1 );
591 availableVersions.add( artifactTransferRequest.getVersion() );
593 projectMetadata.setGroupId( artifactTransferRequest.getGroupId() );
594 projectMetadata.setArtifactId( artifactTransferRequest.getArtifactId() );
597 if ( projectMetadata.getGroupId() == null )
599 projectMetadata.setGroupId( artifactTransferRequest.getGroupId() );
602 if ( projectMetadata.getArtifactId() == null )
604 projectMetadata.setArtifactId( artifactTransferRequest.getArtifactId() );
607 projectMetadata.setLatestVersion( latestVersion );
608 projectMetadata.setLastUpdatedTimestamp( lastUpdatedTimestamp );
609 projectMetadata.setAvailableVersions( availableVersions );
611 if ( !VersionUtil.isSnapshot( artifactTransferRequest.getVersion() ) )
613 projectMetadata.setReleasedVersion( latestVersion );
616 RepositoryMetadataWriter.write( projectMetadata, projectMetadataFile.toPath() );
620 fixChecksums( projectMetadataFile );
625 public Boolean removeProjectVersion( String repositoryId, String namespace, String projectId, String version )
626 throws ArchivaRestServiceException
628 // if not a generic we can use the standard way to delete artifact
629 if ( !VersionUtil.isGenericSnapshot( version ) )
631 Artifact artifact = new Artifact( namespace, projectId, version );
632 artifact.setRepositoryId( repositoryId );
633 artifact.setContext( repositoryId );
634 return deleteArtifact( artifact );
637 if ( StringUtils.isEmpty( repositoryId ) )
639 throw new ArchivaRestServiceException( "repositoryId cannot be null", 400, null );
642 if ( !isAuthorizedToDeleteArtifacts( repositoryId ) )
644 throw new ArchivaRestServiceException( "not authorized to delete artifacts", 403, null );
647 if ( StringUtils.isEmpty( namespace ) )
649 throw new ArchivaRestServiceException( "groupId cannot be null", 400, null );
652 if ( StringUtils.isEmpty( projectId ) )
654 throw new ArchivaRestServiceException( "artifactId cannot be null", 400, null );
657 if ( StringUtils.isEmpty( version ) )
659 throw new ArchivaRestServiceException( "version cannot be null", 400, null );
662 RepositorySession repositorySession = repositorySessionFactory.createSession();
666 ManagedRepositoryContent repository = repositoryFactory.getManagedRepositoryContent( repositoryId );
668 VersionedReference ref = new VersionedReference();
669 ref.setArtifactId( projectId );
670 ref.setGroupId( namespace );
671 ref.setVersion( version );
673 repository.deleteVersion( ref );
676 ProjectReference projectReference = new ProjectReference();
677 projectReference.setGroupId( namespace );
678 projectReference.setArtifactId( projectId );
680 repository.getVersions( )
683 ArtifactReference artifactReference = new ArtifactReference();
684 artifactReference.setGroupId( namespace );
685 artifactReference.setArtifactId( projectId );
686 artifactReference.setVersion( version );
688 MetadataRepository metadataRepository = repositorySession.getRepository();
690 Set<ArtifactReference> related = repository.getRelatedArtifacts( artifactReference );
691 log.debug( "related: {}", related );
692 for ( ArtifactReference artifactRef : related )
694 repository.deleteArtifact( artifactRef );
697 Collection<ArtifactMetadata> artifacts =
698 metadataRepository.getArtifacts( repositoryId, namespace, projectId, version );
700 for ( ArtifactMetadata artifactMetadata : artifacts )
702 metadataRepository.removeArtifact( artifactMetadata, version );
705 metadataRepository.removeProjectVersion( repositoryId, namespace, projectId, version );
707 catch ( MetadataRepositoryException e )
709 throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
711 catch ( MetadataResolutionException e )
713 throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
715 catch ( RepositoryException e )
717 throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
722 repositorySession.save();
724 repositorySession.close();
731 public Boolean deleteArtifact( Artifact artifact )
732 throws ArchivaRestServiceException
735 String repositoryId = artifact.getContext();
736 // some rest call can use context or repositoryId
738 if ( StringUtils.isEmpty( repositoryId ) )
740 repositoryId = artifact.getRepositoryId();
742 if ( StringUtils.isEmpty( repositoryId ) )
744 throw new ArchivaRestServiceException( "repositoryId cannot be null", 400, null );
747 if ( !isAuthorizedToDeleteArtifacts( repositoryId ) )
749 throw new ArchivaRestServiceException( "not authorized to delete artifacts", 403, null );
752 if ( artifact == null )
754 throw new ArchivaRestServiceException( "artifact cannot be null", 400, null );
757 if ( StringUtils.isEmpty( artifact.getGroupId() ) )
759 throw new ArchivaRestServiceException( "artifact.groupId cannot be null", 400, null );
762 if ( StringUtils.isEmpty( artifact.getArtifactId() ) )
764 throw new ArchivaRestServiceException( "artifact.artifactId cannot be null", 400, null );
767 // TODO more control on artifact fields
769 boolean snapshotVersion =
770 VersionUtil.isSnapshot( artifact.getVersion() ) | VersionUtil.isGenericSnapshot( artifact.getVersion() );
772 RepositorySession repositorySession = repositorySessionFactory.createSession();
775 Date lastUpdatedTimestamp = Calendar.getInstance().getTime();
777 TimeZone timezone = TimeZone.getTimeZone( "UTC" );
778 DateFormat fmt = new SimpleDateFormat( "yyyyMMdd.HHmmss" );
779 fmt.setTimeZone( timezone );
780 ManagedRepository repoConfig = managedRepositoryAdmin.getManagedRepository( repositoryId );
782 VersionedReference ref = new VersionedReference();
783 ref.setArtifactId( artifact.getArtifactId() );
784 ref.setGroupId( artifact.getGroupId() );
785 ref.setVersion( artifact.getVersion() );
787 ManagedRepositoryContent repository = repositoryFactory.getManagedRepositoryContent( repositoryId );
789 ArtifactReference artifactReference = new ArtifactReference();
790 artifactReference.setArtifactId( artifact.getArtifactId() );
791 artifactReference.setGroupId( artifact.getGroupId() );
792 artifactReference.setVersion( artifact.getVersion() );
793 artifactReference.setClassifier( artifact.getClassifier() );
794 artifactReference.setType( artifact.getPackaging() );
796 MetadataRepository metadataRepository = repositorySession.getRepository();
798 String path = repository.toMetadataPath( ref );
800 if ( StringUtils.isNotBlank( artifact.getClassifier() ) )
802 if ( StringUtils.isBlank( artifact.getPackaging() ) )
804 throw new ArchivaRestServiceException( "You must configure a type/packaging when using classifier",
808 repository.deleteArtifact( artifactReference );
814 int index = path.lastIndexOf( '/' );
815 path = path.substring( 0, index );
816 File targetPath = new File( repoConfig.getLocation(), path );
818 if ( !targetPath.exists() )
820 //throw new ContentNotFoundException(
821 // artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion() );
822 log.warn( "targetPath {} not found skip file deletion", targetPath );
825 // TODO: this should be in the storage mechanism so that it is all tied together
826 // delete from file system
827 if ( !snapshotVersion )
829 repository.deleteVersion( ref );
833 Set<ArtifactReference> related = repository.getRelatedArtifacts( artifactReference );
834 log.debug( "related: {}", related );
835 for ( ArtifactReference artifactRef : related )
837 repository.deleteArtifact( artifactRef );
840 File metadataFile = getMetadata( targetPath.getAbsolutePath() );
841 ArchivaRepositoryMetadata metadata = getMetadata( metadataFile );
843 updateMetadata( metadata, metadataFile, lastUpdatedTimestamp, artifact );
845 Collection<ArtifactMetadata> artifacts = Collections.emptyList();
847 if ( snapshotVersion )
849 String baseVersion = VersionUtil.getBaseVersion( artifact.getVersion() );
851 metadataRepository.getArtifacts( repositoryId, artifact.getGroupId(), artifact.getArtifactId(),
857 metadataRepository.getArtifacts( repositoryId, artifact.getGroupId(), artifact.getArtifactId(),
858 artifact.getVersion() );
861 log.debug( "artifacts: {}", artifacts );
863 if ( artifacts.isEmpty() )
865 if ( !snapshotVersion )
867 // verify metata repository doesn't contains anymore the version
868 Collection<String> projectVersions =
869 metadataRepository.getProjectVersions( repositoryId, artifact.getGroupId(),
870 artifact.getArtifactId() );
872 if ( projectVersions.contains( artifact.getVersion() ) )
874 log.warn( "artifact not found when deleted but version still here ! so force cleanup" );
875 metadataRepository.removeProjectVersion( repositoryId, artifact.getGroupId(),
876 artifact.getArtifactId(), artifact.getVersion() );
882 for ( ArtifactMetadata artifactMetadata : artifacts )
885 // TODO: mismatch between artifact (snapshot) version and project (base) version here
886 if ( artifactMetadata.getVersion().equals( artifact.getVersion() ) )
888 if ( StringUtils.isNotBlank( artifact.getClassifier() ) )
890 if ( StringUtils.isBlank( artifact.getPackaging() ) )
892 throw new ArchivaRestServiceException(
893 "You must configure a type/packaging when using classifier", 400, null );
895 // cleanup facet which contains classifier information
896 MavenArtifactFacet mavenArtifactFacet =
897 (MavenArtifactFacet) artifactMetadata.getFacet( MavenArtifactFacet.FACET_ID );
899 if ( StringUtils.equals( artifact.getClassifier(), mavenArtifactFacet.getClassifier() ) )
901 artifactMetadata.removeFacet( MavenArtifactFacet.FACET_ID );
902 String groupId = artifact.getGroupId(), artifactId = artifact.getArtifactId(), version =
903 artifact.getVersion();
904 MavenArtifactFacet mavenArtifactFacetToCompare = new MavenArtifactFacet();
905 mavenArtifactFacetToCompare.setClassifier( artifact.getClassifier() );
906 metadataRepository.removeArtifact( repositoryId, groupId, artifactId, version,
907 mavenArtifactFacetToCompare );
908 metadataRepository.save();
914 if ( snapshotVersion )
916 metadataRepository.removeArtifact( artifactMetadata,
917 VersionUtil.getBaseVersion( artifact.getVersion() ) );
921 metadataRepository.removeArtifact( artifactMetadata.getRepositoryId(),
922 artifactMetadata.getNamespace(),
923 artifactMetadata.getProject(), artifact.getVersion(),
924 artifactMetadata.getId() );
927 // TODO: move into the metadata repository proper - need to differentiate attachment of
928 // repository metadata to an artifact
929 for ( RepositoryListener listener : listeners )
931 listener.deleteArtifact( metadataRepository, repository.getId(),
932 artifactMetadata.getNamespace(), artifactMetadata.getProject(),
933 artifactMetadata.getVersion(), artifactMetadata.getId() );
936 triggerAuditEvent( repositoryId, path, AuditEvent.REMOVE_FILE );
940 catch ( ContentNotFoundException e )
942 throw new ArchivaRestServiceException( "Artifact does not exist: " + e.getMessage(), 400, e );
944 catch ( RepositoryNotFoundException e )
946 throw new ArchivaRestServiceException( "Target repository cannot be found: " + e.getMessage(), 400, e );
948 catch ( RepositoryException e )
950 throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
952 catch ( MetadataResolutionException e )
954 throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
956 catch ( MetadataRepositoryException e )
958 throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
960 catch ( RepositoryAdminException e )
962 throw new ArchivaRestServiceException( "RepositoryAdmin exception: " + e.getMessage(), 500, e );
967 repositorySession.save();
969 repositorySession.close();
975 public Boolean deleteGroupId( String groupId, String repositoryId )
976 throws ArchivaRestServiceException
978 if ( StringUtils.isEmpty( repositoryId ) )
980 throw new ArchivaRestServiceException( "repositoryId cannot be null", 400, null );
983 if ( !isAuthorizedToDeleteArtifacts( repositoryId ) )
985 throw new ArchivaRestServiceException( "not authorized to delete artifacts", 403, null );
988 if ( StringUtils.isEmpty( groupId ) )
990 throw new ArchivaRestServiceException( "groupId cannot be null", 400, null );
993 RepositorySession repositorySession = repositorySessionFactory.createSession();
997 ManagedRepositoryContent repository = repositoryFactory.getManagedRepositoryContent( repositoryId );
999 repository.deleteGroupId( groupId );
1001 MetadataRepository metadataRepository = repositorySession.getRepository();
1003 metadataRepository.removeNamespace( repositoryId, groupId );
1005 // just invalidate cache entry
1006 String cacheKey = repositoryId + "-" + groupId;
1007 namespacesCache.remove( cacheKey );
1008 namespacesCache.remove( repositoryId );
1010 metadataRepository.save();
1012 catch ( MetadataRepositoryException e )
1014 log.error( e.getMessage(), e );
1015 throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
1017 catch ( RepositoryException e )
1019 log.error( e.getMessage(), e );
1020 throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
1025 repositorySession.close();
1031 public Boolean deleteProject( String groupId, String projectId, String repositoryId )
1032 throws ArchivaRestServiceException
1034 if ( StringUtils.isEmpty( repositoryId ) )
1036 throw new ArchivaRestServiceException( "repositoryId cannot be null", 400, null );
1039 if ( !isAuthorizedToDeleteArtifacts( repositoryId ) )
1041 throw new ArchivaRestServiceException( "not authorized to delete artifacts", 403, null );
1044 if ( StringUtils.isEmpty( groupId ) )
1046 throw new ArchivaRestServiceException( "groupId cannot be null", 400, null );
1049 if ( StringUtils.isEmpty( projectId ) )
1051 throw new ArchivaRestServiceException( "artifactId cannot be null", 400, null );
1054 RepositorySession repositorySession = repositorySessionFactory.createSession();
1058 ManagedRepositoryContent repository = repositoryFactory.getManagedRepositoryContent( repositoryId );
1060 repository.deleteProject( groupId, projectId );
1062 catch ( ContentNotFoundException e )
1064 log.warn( "skip ContentNotFoundException: {}", e.getMessage() );
1066 catch ( RepositoryException e )
1068 log.error( e.getMessage(), e );
1069 throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
1075 MetadataRepository metadataRepository = repositorySession.getRepository();
1077 metadataRepository.removeProject( repositoryId, groupId, projectId );
1079 metadataRepository.save();
1081 catch ( MetadataRepositoryException e )
1083 log.error( e.getMessage(), e );
1084 throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
1089 repositorySession.close();
1096 public Boolean isAuthorizedToDeleteArtifacts( String repoId )
1097 throws ArchivaRestServiceException
1100 getAuditInformation().getUser() == null ? "guest" : getAuditInformation().getUser().getUsername();
1104 return userRepositories.isAuthorizedToDeleteArtifacts( userName, repoId );
1106 catch ( ArchivaSecurityException e )
1108 throw new ArchivaRestServiceException( e.getMessage(),
1109 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
1114 public RepositoryScanStatistics scanRepositoryDirectoriesNow( String repositoryId )
1115 throws ArchivaRestServiceException
1117 long sinceWhen = RepositoryScanner.FRESH_SCAN;
1120 return repoScanner.scan( getManagedRepositoryAdmin().getManagedRepository( repositoryId ), sinceWhen );
1122 catch ( RepositoryScannerException e )
1124 log.error( e.getMessage(), e );
1125 throw new ArchivaRestServiceException( "RepositoryScannerException exception: " + e.getMessage(), 500, e );
1127 catch ( RepositoryAdminException e )
1129 log.error( e.getMessage(), e );
1130 throw new ArchivaRestServiceException( "RepositoryScannerException exception: " + e.getMessage(), 500, e );
1135 * Update artifact level metadata. Creates one if metadata does not exist after artifact deletion.
1139 private void updateMetadata( ArchivaRepositoryMetadata metadata, File metadataFile, Date lastUpdatedTimestamp,
1141 throws RepositoryMetadataException
1143 List<String> availableVersions = new ArrayList<>();
1144 String latestVersion = "";
1146 if ( metadataFile.exists() )
1148 if ( metadata.getAvailableVersions() != null )
1150 availableVersions = metadata.getAvailableVersions();
1152 if ( availableVersions.size() > 0 )
1154 Collections.sort( availableVersions, VersionComparator.getInstance() );
1156 if ( availableVersions.contains( artifact.getVersion() ) )
1158 availableVersions.remove( availableVersions.indexOf( artifact.getVersion() ) );
1160 if ( availableVersions.size() > 0 )
1162 latestVersion = availableVersions.get( availableVersions.size() - 1 );
1168 if ( metadata.getGroupId() == null )
1170 metadata.setGroupId( artifact.getGroupId() );
1172 if ( metadata.getArtifactId() == null )
1174 metadata.setArtifactId( artifact.getArtifactId() );
1177 if ( !VersionUtil.isSnapshot( artifact.getVersion() ) )
1179 if ( metadata.getReleasedVersion() != null && metadata.getReleasedVersion().equals(
1180 artifact.getVersion() ) )
1182 metadata.setReleasedVersion( latestVersion );
1186 metadata.setLatestVersion( latestVersion );
1187 metadata.setLastUpdatedTimestamp( lastUpdatedTimestamp );
1188 metadata.setAvailableVersions( availableVersions );
1190 RepositoryMetadataWriter.write( metadata, metadataFile.toPath() );
1191 ChecksummedFile checksum = new ChecksummedFile( metadataFile.toPath() );
1192 checksum.fixChecksums( algorithms );
1196 public StringList getRunningRemoteDownloadIds()
1198 return new StringList( downloadRemoteIndexScheduler.getRunningRemoteDownloadIds() );
1201 public ManagedRepositoryAdmin getManagedRepositoryAdmin()
1203 return managedRepositoryAdmin;
1206 public void setManagedRepositoryAdmin( ManagedRepositoryAdmin managedRepositoryAdmin )
1208 this.managedRepositoryAdmin = managedRepositoryAdmin;
1211 public RepositoryContentFactory getRepositoryFactory()
1213 return repositoryFactory;
1216 public void setRepositoryFactory( RepositoryContentFactory repositoryFactory )
1218 this.repositoryFactory = repositoryFactory;
1221 public RepositorySessionFactory getRepositorySessionFactory()
1223 return repositorySessionFactory;
1226 public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
1228 this.repositorySessionFactory = repositorySessionFactory;
1231 public List<RepositoryListener> getListeners()
1236 public void setListeners( List<RepositoryListener> listeners )
1238 this.listeners = listeners;
1241 public ArchivaAdministration getArchivaAdministration()
1243 return archivaAdministration;
1246 public void setArchivaAdministration( ArchivaAdministration archivaAdministration )
1248 this.archivaAdministration = archivaAdministration;