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.audit.AuditEvent;
27 import org.apache.archiva.checksum.ChecksumAlgorithm;
28 import org.apache.archiva.checksum.ChecksummedFile;
29 import org.apache.archiva.common.plexusbridge.MavenIndexerUtils;
30 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
31 import org.apache.archiva.common.utils.VersionComparator;
32 import org.apache.archiva.common.utils.VersionUtil;
33 import org.apache.archiva.maven2.metadata.MavenMetadataReader;
34 import org.apache.archiva.metadata.model.ArtifactMetadata;
35 import org.apache.archiva.metadata.repository.MetadataRepository;
36 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
37 import org.apache.archiva.metadata.repository.MetadataResolutionException;
38 import org.apache.archiva.metadata.repository.RepositorySession;
39 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
40 import org.apache.archiva.model.ArchivaRepositoryMetadata;
41 import org.apache.archiva.model.ArtifactReference;
42 import org.apache.archiva.model.VersionedReference;
43 import org.apache.archiva.redback.users.User;
44 import org.apache.archiva.redback.users.UserNotFoundException;
45 import org.apache.archiva.repository.ContentNotFoundException;
46 import org.apache.archiva.repository.ManagedRepositoryContent;
47 import org.apache.archiva.repository.RepositoryContentFactory;
48 import org.apache.archiva.repository.RepositoryException;
49 import org.apache.archiva.repository.RepositoryNotFoundException;
50 import org.apache.archiva.repository.events.RepositoryListener;
51 import org.apache.archiva.repository.metadata.MetadataTools;
52 import org.apache.archiva.repository.metadata.RepositoryMetadataException;
53 import org.apache.archiva.repository.metadata.RepositoryMetadataWriter;
54 import org.apache.archiva.repository.scanner.RepositoryScanStatistics;
55 import org.apache.archiva.repository.scanner.RepositoryScanner;
56 import org.apache.archiva.repository.scanner.RepositoryScannerException;
57 import org.apache.archiva.rest.api.model.Artifact;
58 import org.apache.archiva.rest.api.model.ArtifactTransferRequest;
59 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
60 import org.apache.archiva.rest.api.services.RepositoriesService;
61 import org.apache.archiva.scheduler.ArchivaTaskScheduler;
62 import org.apache.archiva.scheduler.indexing.ArchivaIndexingTaskExecutor;
63 import org.apache.archiva.scheduler.indexing.ArtifactIndexingTask;
64 import org.apache.archiva.scheduler.indexing.DownloadRemoteIndexException;
65 import org.apache.archiva.scheduler.indexing.DownloadRemoteIndexScheduler;
66 import org.apache.archiva.scheduler.repository.RepositoryArchivaTaskScheduler;
67 import org.apache.archiva.scheduler.repository.RepositoryTask;
68 import org.apache.archiva.security.ArchivaSecurityException;
69 import org.apache.archiva.security.common.ArchivaRoleConstants;
70 import org.apache.archiva.xml.XMLException;
71 import org.apache.commons.io.FilenameUtils;
72 import org.apache.commons.io.IOUtils;
73 import org.apache.commons.lang.StringUtils;
74 import org.apache.maven.index.context.IndexingContext;
75 import org.apache.archiva.redback.authentication.AuthenticationResult;
76 import org.apache.archiva.redback.authorization.AuthorizationException;
77 import org.codehaus.plexus.redback.system.DefaultSecuritySession;
78 import org.codehaus.plexus.redback.system.SecuritySession;
79 import org.codehaus.plexus.redback.system.SecuritySystem;
80 import org.codehaus.plexus.taskqueue.TaskQueueException;
81 import org.slf4j.Logger;
82 import org.slf4j.LoggerFactory;
83 import org.springframework.stereotype.Service;
85 import javax.inject.Inject;
86 import javax.inject.Named;
87 import javax.ws.rs.core.Response;
89 import java.io.FileInputStream;
90 import java.io.FileOutputStream;
91 import java.io.IOException;
92 import java.text.DateFormat;
93 import java.text.SimpleDateFormat;
94 import java.util.ArrayList;
95 import java.util.Calendar;
96 import java.util.Collection;
97 import java.util.Collections;
98 import java.util.Date;
99 import java.util.List;
100 import java.util.TimeZone;
103 * @author Olivier Lamy
106 @Service( "repositoriesService#rest" )
107 public class DefaultRepositoriesService
108 extends AbstractRestService
109 implements RepositoriesService
111 private Logger log = LoggerFactory.getLogger( getClass() );
114 @Named( value = "archivaTaskScheduler#repository" )
115 private RepositoryArchivaTaskScheduler repositoryTaskScheduler;
118 @Named( value = "taskExecutor#indexing" )
119 private ArchivaIndexingTaskExecutor archivaIndexingTaskExecutor;
122 private ManagedRepositoryAdmin managedRepositoryAdmin;
125 private PlexusSisuBridge plexusSisuBridge;
128 private MavenIndexerUtils mavenIndexerUtils;
131 private SecuritySystem securitySystem;
134 private RepositoryContentFactory repositoryFactory;
137 private ArchivaAdministration archivaAdministration;
140 @Named( value = "archivaTaskScheduler#repository" )
141 private ArchivaTaskScheduler scheduler;
144 private DownloadRemoteIndexScheduler downloadRemoteIndexScheduler;
147 @Named( value = "repositorySessionFactory" )
148 protected RepositorySessionFactory repositorySessionFactory;
151 protected List<RepositoryListener> listeners = new ArrayList<RepositoryListener>();
154 private RepositoryScanner repoScanner;
156 private ChecksumAlgorithm[] algorithms = new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 };
158 public Boolean scanRepository( String repositoryId, boolean fullScan )
160 if ( repositoryTaskScheduler.isProcessingRepositoryTask( repositoryId ) )
162 log.info( "scanning of repository with id {} already scheduled", repositoryId );
163 return Boolean.FALSE;
165 RepositoryTask task = new RepositoryTask();
166 task.setRepositoryId( repositoryId );
167 task.setScanAll( fullScan );
170 repositoryTaskScheduler.queueTask( task );
172 catch ( TaskQueueException e )
174 log.error( "failed to schedule scanning of repo with id {}", repositoryId, e );
180 public Boolean alreadyScanning( String repositoryId )
182 return repositoryTaskScheduler.isProcessingRepositoryTask( 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 );
200 public Boolean scanRepositoryNow( String repositoryId, boolean fullScan )
201 throws ArchivaRestServiceException
206 ManagedRepository repository = managedRepositoryAdmin.getManagedRepository( repositoryId );
208 IndexingContext context = managedRepositoryAdmin.createIndexContext( repository );
210 ArtifactIndexingTask task =
211 new ArtifactIndexingTask( repository, null, ArtifactIndexingTask.Action.FINISH, context );
213 task.setExecuteOnEntireRepo( true );
214 task.setOnlyUpdate( !fullScan );
216 archivaIndexingTaskExecutor.executeTask( task );
219 catch ( Exception e )
221 log.error( e.getMessage(), e );
222 throw new ArchivaRestServiceException( e.getMessage() );
226 public Boolean scheduleDownloadRemoteIndex( String repositoryId, boolean now, boolean fullDownload )
227 throws ArchivaRestServiceException
231 downloadRemoteIndexScheduler.scheduleDownloadRemote( repositoryId, now, fullDownload );
233 catch ( DownloadRemoteIndexException e )
235 log.error( e.getMessage(), e );
236 throw new ArchivaRestServiceException( e.getMessage() );
241 public Boolean copyArtifact( ArtifactTransferRequest artifactTransferRequest )
242 throws ArchivaRestServiceException
245 String userName = getAuditInformation().getUser().getUsername();
246 if ( StringUtils.isBlank( userName ) )
248 throw new ArchivaRestServiceException( "copyArtifact call: userName not found" );
251 if ( StringUtils.isBlank( artifactTransferRequest.getRepositoryId() ) )
253 throw new ArchivaRestServiceException( "copyArtifact call: sourceRepositoryId cannot be null" );
256 if ( StringUtils.isBlank( artifactTransferRequest.getTargetRepositoryId() ) )
258 throw new ArchivaRestServiceException( "copyArtifact call: targetRepositoryId cannot be null" );
261 ManagedRepository source = null;
264 source = managedRepositoryAdmin.getManagedRepository( artifactTransferRequest.getRepositoryId() );
266 catch ( RepositoryAdminException e )
268 throw new ArchivaRestServiceException( e.getMessage() );
271 if ( source == null )
273 throw new ArchivaRestServiceException(
274 "cannot find repository with id " + artifactTransferRequest.getRepositoryId() );
277 ManagedRepository target = null;
280 target = managedRepositoryAdmin.getManagedRepository( artifactTransferRequest.getTargetRepositoryId() );
282 catch ( RepositoryAdminException e )
284 throw new ArchivaRestServiceException( e.getMessage() );
287 if ( target == null )
289 throw new ArchivaRestServiceException(
290 "cannot find repository with id " + artifactTransferRequest.getTargetRepositoryId() );
293 if ( StringUtils.isBlank( artifactTransferRequest.getGroupId() ) )
295 throw new ArchivaRestServiceException( "groupId is mandatory" );
298 if ( StringUtils.isBlank( artifactTransferRequest.getArtifactId() ) )
300 throw new ArchivaRestServiceException( "artifactId is mandatory" );
303 if ( StringUtils.isBlank( artifactTransferRequest.getVersion() ) )
305 throw new ArchivaRestServiceException( "version is mandatory" );
308 if ( VersionUtil.isSnapshot( artifactTransferRequest.getVersion() ) )
310 throw new ArchivaRestServiceException( "copy of SNAPSHOT not supported" );
313 // end check parameters
318 user = securitySystem.getUserManager().findUser( userName );
320 catch ( UserNotFoundException e )
322 throw new ArchivaRestServiceException( "user " + userName + " not found" );
325 // check karma on source : read
326 AuthenticationResult authn = new AuthenticationResult( true, userName, null );
327 SecuritySession securitySession = new DefaultSecuritySession( authn, user );
331 securitySystem.isAuthorized( securitySession, ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS,
332 artifactTransferRequest.getRepositoryId() );
335 throw new ArchivaRestServiceException(
336 "not authorized to access repo:" + artifactTransferRequest.getRepositoryId() );
339 catch ( AuthorizationException e )
341 log.error( "error reading permission: " + e.getMessage(), e );
342 throw new ArchivaRestServiceException( e.getMessage() );
345 // check karma on target: write
349 securitySystem.isAuthorized( securitySession, ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD,
350 artifactTransferRequest.getTargetRepositoryId() );
353 throw new ArchivaRestServiceException(
354 "not authorized to write to repo:" + artifactTransferRequest.getTargetRepositoryId() );
357 catch ( AuthorizationException e )
359 log.error( "error reading permission: " + e.getMessage(), e );
360 throw new ArchivaRestServiceException( e.getMessage() );
363 // sounds good we can continue !
365 ArtifactReference artifactReference = new ArtifactReference();
366 artifactReference.setArtifactId( artifactTransferRequest.getArtifactId() );
367 artifactReference.setGroupId( artifactTransferRequest.getGroupId() );
368 artifactReference.setVersion( artifactTransferRequest.getVersion() );
369 artifactReference.setClassifier( artifactTransferRequest.getClassifier() );
370 String packaging = StringUtils.trim( artifactTransferRequest.getPackaging() );
371 artifactReference.setType( StringUtils.isEmpty( packaging ) ? "jar" : packaging );
376 ManagedRepositoryContent sourceRepository =
377 repositoryFactory.getManagedRepositoryContent( artifactTransferRequest.getRepositoryId() );
379 String artifactSourcePath = sourceRepository.toPath( artifactReference );
381 if ( StringUtils.isEmpty( artifactSourcePath ) )
383 log.error( "cannot find artifact " + artifactTransferRequest.toString() );
384 throw new ArchivaRestServiceException( "cannot find artifact " + artifactTransferRequest.toString() );
387 File artifactFile = new File( source.getLocation(), artifactSourcePath );
389 if ( !artifactFile.exists() )
391 log.error( "cannot find artifact " + artifactTransferRequest.toString() );
392 throw new ArchivaRestServiceException( "cannot find artifact " + artifactTransferRequest.toString() );
395 ManagedRepositoryContent targetRepository =
396 repositoryFactory.getManagedRepositoryContent( artifactTransferRequest.getTargetRepositoryId() );
398 String artifactPath = targetRepository.toPath( artifactReference );
400 int lastIndex = artifactPath.lastIndexOf( '/' );
402 String path = artifactPath.substring( 0, lastIndex );
403 File targetPath = new File( target.getLocation(), path );
405 Date lastUpdatedTimestamp = Calendar.getInstance().getTime();
406 int newBuildNumber = 1;
407 String timestamp = null;
409 File versionMetadataFile = new File( targetPath, MetadataTools.MAVEN_METADATA );
410 ArchivaRepositoryMetadata versionMetadata = getMetadata( versionMetadataFile );
412 if ( !targetPath.exists() )
417 String filename = artifactPath.substring( lastIndex + 1 );
419 // FIXME some dupe with uploadaction
421 boolean fixChecksums =
422 !( archivaAdministration.getKnownContentConsumers().contains( "create-missing-checksums" ) );
424 File targetFile = new File( targetPath, filename );
425 if ( targetFile.exists() && target.isBlockRedeployments() )
427 throw new ArchivaRestServiceException(
428 "artifact already exists in target repo: " + artifactTransferRequest.getTargetRepositoryId()
429 + " and redeployment blocked" );
433 copyFile( artifactFile, targetPath, filename, fixChecksums );
434 queueRepositoryTask( target.getId(), targetFile );
437 // copy source pom to target repo
438 String pomFilename = filename;
439 if ( StringUtils.isNotBlank( artifactTransferRequest.getClassifier() ) )
441 pomFilename = StringUtils.remove( pomFilename, "-" + artifactTransferRequest.getClassifier() );
443 pomFilename = FilenameUtils.removeExtension( pomFilename ) + ".pom";
445 File pomFile = new File(
446 new File( source.getLocation(), artifactSourcePath.substring( 0, artifactPath.lastIndexOf( '/' ) ) ),
449 if ( pomFile != null && pomFile.length() > 0 )
451 copyFile( pomFile, targetPath, pomFilename, fixChecksums );
452 queueRepositoryTask( target.getId(), new File( targetPath, pomFilename ) );
457 // explicitly update only if metadata-updater consumer is not enabled!
458 if ( !archivaAdministration.getKnownContentConsumers().contains( "metadata-updater" ) )
460 updateProjectMetadata( targetPath.getAbsolutePath(), lastUpdatedTimestamp, timestamp, newBuildNumber,
461 fixChecksums, artifactTransferRequest );
467 "Artifact \'" + artifactTransferRequest.getGroupId() + ":" + artifactTransferRequest.getArtifactId()
468 + ":" + artifactTransferRequest.getVersion() + "\' was successfully deployed to repository \'"
469 + artifactTransferRequest.getTargetRepositoryId() + "\'";
472 catch ( RepositoryException e )
474 log.error( "RepositoryException: " + e.getMessage(), e );
475 throw new ArchivaRestServiceException( e.getMessage() );
477 catch ( RepositoryAdminException e )
479 log.error( "RepositoryAdminException: " + e.getMessage(), e );
480 throw new ArchivaRestServiceException( e.getMessage() );
482 catch ( IOException e )
484 log.error( "IOException: " + e.getMessage(), e );
485 throw new ArchivaRestServiceException( e.getMessage() );
490 //FIXME some duplicate with UploadAction
492 private void queueRepositoryTask( String repositoryId, File localFile )
494 RepositoryTask task = new RepositoryTask();
495 task.setRepositoryId( repositoryId );
496 task.setResourceFile( localFile );
497 task.setUpdateRelatedArtifacts( true );
498 //task.setScanAll( true );
502 scheduler.queueTask( task );
504 catch ( TaskQueueException e )
506 log.error( "Unable to queue repository task to execute consumers on resource file ['" + localFile.getName()
511 private ArchivaRepositoryMetadata getMetadata( File metadataFile )
512 throws RepositoryMetadataException
514 ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
515 if ( metadataFile.exists() )
519 metadata = MavenMetadataReader.read( metadataFile );
521 catch ( XMLException e )
523 throw new RepositoryMetadataException( e.getMessage(), e );
529 private File getMetadata( String targetPath )
531 String artifactPath = targetPath.substring( 0, targetPath.lastIndexOf( File.separatorChar ) );
533 return new File( artifactPath, MetadataTools.MAVEN_METADATA );
536 private void copyFile( File sourceFile, File targetPath, String targetFilename, boolean fixChecksums )
539 FileOutputStream out = new FileOutputStream( new File( targetPath, targetFilename ) );
540 FileInputStream input = new FileInputStream( sourceFile );
544 IOUtils.copy( input, out );
554 fixChecksums( new File( targetPath, targetFilename ) );
558 private void fixChecksums( File file )
560 ChecksummedFile checksum = new ChecksummedFile( file );
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<String>();
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 );
620 fixChecksums( projectMetadataFile );
624 public Boolean deleteArtifact( Artifact artifact, String repositoryId )
625 throws ArchivaRestServiceException
628 if ( StringUtils.isEmpty( repositoryId ) )
630 throw new ArchivaRestServiceException( "repositoryId cannot be null", 400 );
633 if ( !isAuthorizedToDeleteArtifacts( repositoryId ) )
635 throw new ArchivaRestServiceException( "not authorized to delete artifacts", 403 );
638 if ( artifact == null )
640 throw new ArchivaRestServiceException( "artifact cannot be null", 400 );
643 if ( StringUtils.isEmpty( artifact.getGroupId() ) )
645 throw new ArchivaRestServiceException( "artifact.groupId cannot be null", 400 );
648 if ( StringUtils.isEmpty( artifact.getArtifactId() ) )
650 throw new ArchivaRestServiceException( "artifact.artifactId cannot be null", 400 );
653 // TODO more control on artifact fields
655 RepositorySession repositorySession = repositorySessionFactory.createSession();
658 Date lastUpdatedTimestamp = Calendar.getInstance().getTime();
660 TimeZone timezone = TimeZone.getTimeZone( "UTC" );
661 DateFormat fmt = new SimpleDateFormat( "yyyyMMdd.HHmmss" );
662 fmt.setTimeZone( timezone );
663 ManagedRepository repoConfig = managedRepositoryAdmin.getManagedRepository( repositoryId );
665 VersionedReference ref = new VersionedReference();
666 ref.setArtifactId( artifact.getArtifactId() );
667 ref.setGroupId( artifact.getGroupId() );
668 ref.setVersion( artifact.getVersion() );
670 ManagedRepositoryContent repository = repositoryFactory.getManagedRepositoryContent( repositoryId );
672 if ( StringUtils.isNotBlank( artifact.getClassifier() ) )
674 if ( StringUtils.isBlank( artifact.getPackaging() ) )
676 throw new ArchivaRestServiceException( "You must configure a type/packaging when using classifier",
679 ArtifactReference artifactReference = new ArtifactReference();
680 artifactReference.setArtifactId( artifact.getArtifactId() );
681 artifactReference.setGroupId( artifact.getGroupId() );
682 artifactReference.setVersion( artifact.getVersion() );
683 artifactReference.setClassifier( artifact.getClassifier() );
684 artifactReference.setType( artifact.getPackaging() );
685 repository.deleteArtifact( artifactReference );
687 // TODO cleanup facet which contains classifier information
691 String path = repository.toMetadataPath( ref );
692 int index = path.lastIndexOf( '/' );
693 path = path.substring( 0, index );
694 File targetPath = new File( repoConfig.getLocation(), path );
696 if ( !targetPath.exists() )
698 throw new ContentNotFoundException(
699 artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion() );
702 // TODO: this should be in the storage mechanism so that it is all tied together
703 // delete from file system
704 repository.deleteVersion( ref );
706 File metadataFile = getMetadata( targetPath.getAbsolutePath() );
707 ArchivaRepositoryMetadata metadata = getMetadata( metadataFile );
709 updateMetadata( metadata, metadataFile, lastUpdatedTimestamp, artifact );
711 MetadataRepository metadataRepository = repositorySession.getRepository();
713 Collection<ArtifactMetadata> artifacts =
714 metadataRepository.getArtifacts( repositoryId, artifact.getGroupId(), artifact.getArtifactId(),
715 artifact.getVersion() );
717 for ( ArtifactMetadata artifactMetadata : artifacts )
719 // TODO: mismatch between artifact (snapshot) version and project (base) version here
720 if ( artifact.getVersion().equals( artifact.getVersion() ) )
722 metadataRepository.removeArtifact( artifactMetadata.getRepositoryId(),
723 artifactMetadata.getNamespace(), artifactMetadata.getProject(),
724 artifact.getVersion(), artifactMetadata.getId() );
726 // TODO: move into the metadata repository proper - need to differentiate attachment of
727 // repository metadata to an artifact
728 for ( RepositoryListener listener : listeners )
730 listener.deleteArtifact( metadataRepository, repository.getId(),
731 artifactMetadata.getNamespace(), artifactMetadata.getProject(),
732 artifactMetadata.getVersion(), artifactMetadata.getId() );
735 triggerAuditEvent( repositoryId, path, AuditEvent.REMOVE_FILE );
738 repositorySession.save();
741 catch ( ContentNotFoundException e )
743 throw new ArchivaRestServiceException( "Artifact does not exist: " + e.getMessage(), 400 );
745 catch ( RepositoryNotFoundException e )
747 throw new ArchivaRestServiceException( "Target repository cannot be found: " + e.getMessage(), 400 );
749 catch ( RepositoryException e )
751 throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500 );
753 catch ( MetadataResolutionException e )
755 throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500 );
757 catch ( MetadataRepositoryException e )
759 throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500 );
761 catch ( RepositoryAdminException e )
763 throw new ArchivaRestServiceException( "RepositoryAdmin exception: " + e.getMessage(), 500 );
768 repositorySession.close();
773 public Boolean isAuthorizedToDeleteArtifacts( String repoId )
774 throws ArchivaRestServiceException
777 getAuditInformation().getUser() == null ? "guest" : getAuditInformation().getUser().getUsername();
781 boolean res = userRepositories.isAuthorizedToDeleteArtifacts( userName, repoId );
784 catch ( ArchivaSecurityException e )
786 throw new ArchivaRestServiceException( e.getMessage(),
787 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode() );
791 public RepositoryScanStatistics scanRepositoryDirectoriesNow( String repositoryId )
792 throws ArchivaRestServiceException
794 long sinceWhen = RepositoryScanner.FRESH_SCAN;
797 return repoScanner.scan( getManagedRepositoryAdmin().getManagedRepository( repositoryId ), sinceWhen );
799 catch ( RepositoryScannerException e )
801 log.error( e.getMessage(), e );
802 throw new ArchivaRestServiceException( "RepositoryScannerException exception: " + e.getMessage(), 500 );
804 catch ( RepositoryAdminException e )
806 log.error( e.getMessage(), e );
807 throw new ArchivaRestServiceException( "RepositoryScannerException exception: " + e.getMessage(), 500 );
812 * Update artifact level metadata. Creates one if metadata does not exist after artifact deletion.
816 private void updateMetadata( ArchivaRepositoryMetadata metadata, File metadataFile, Date lastUpdatedTimestamp,
818 throws RepositoryMetadataException
820 List<String> availableVersions = new ArrayList<String>();
821 String latestVersion = "";
823 if ( metadataFile.exists() )
825 if ( metadata.getAvailableVersions() != null )
827 availableVersions = metadata.getAvailableVersions();
829 if ( availableVersions.size() > 0 )
831 Collections.sort( availableVersions, VersionComparator.getInstance() );
833 if ( availableVersions.contains( artifact.getVersion() ) )
835 availableVersions.remove( availableVersions.indexOf( artifact.getVersion() ) );
837 if ( availableVersions.size() > 0 )
839 latestVersion = availableVersions.get( availableVersions.size() - 1 );
845 if ( metadata.getGroupId() == null )
847 metadata.setGroupId( artifact.getGroupId() );
849 if ( metadata.getArtifactId() == null )
851 metadata.setArtifactId( artifact.getArtifactId() );
854 if ( !VersionUtil.isSnapshot( artifact.getVersion() ) )
856 if ( metadata.getReleasedVersion() != null && metadata.getReleasedVersion().equals(
857 artifact.getVersion() ) )
859 metadata.setReleasedVersion( latestVersion );
863 metadata.setLatestVersion( latestVersion );
864 metadata.setLastUpdatedTimestamp( lastUpdatedTimestamp );
865 metadata.setAvailableVersions( availableVersions );
867 RepositoryMetadataWriter.write( metadata, metadataFile );
868 ChecksummedFile checksum = new ChecksummedFile( metadataFile );
869 checksum.fixChecksums( algorithms );
872 public ManagedRepositoryAdmin getManagedRepositoryAdmin()
874 return managedRepositoryAdmin;
877 public void setManagedRepositoryAdmin( ManagedRepositoryAdmin managedRepositoryAdmin )
879 this.managedRepositoryAdmin = managedRepositoryAdmin;
882 public RepositoryContentFactory getRepositoryFactory()
884 return repositoryFactory;
887 public void setRepositoryFactory( RepositoryContentFactory repositoryFactory )
889 this.repositoryFactory = repositoryFactory;
892 public RepositorySessionFactory getRepositorySessionFactory()
894 return repositorySessionFactory;
897 public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
899 this.repositorySessionFactory = repositorySessionFactory;
902 public List<RepositoryListener> getListeners()
907 public void setListeners( List<RepositoryListener> listeners )
909 this.listeners = listeners;
912 public ArchivaAdministration getArchivaAdministration()
914 return archivaAdministration;
917 public void setArchivaAdministration( ArchivaAdministration archivaAdministration )
919 this.archivaAdministration = archivaAdministration;