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.authentication.AuthenticationResult;
44 import org.apache.archiva.redback.authorization.AuthorizationException;
45 import org.apache.archiva.redback.components.taskqueue.TaskQueueException;
46 import org.apache.archiva.redback.system.DefaultSecuritySession;
47 import org.apache.archiva.redback.system.SecuritySession;
48 import org.apache.archiva.redback.system.SecuritySystem;
49 import org.apache.archiva.redback.users.User;
50 import org.apache.archiva.redback.users.UserNotFoundException;
51 import org.apache.archiva.repository.ContentNotFoundException;
52 import org.apache.archiva.repository.ManagedRepositoryContent;
53 import org.apache.archiva.repository.RepositoryContentFactory;
54 import org.apache.archiva.repository.RepositoryException;
55 import org.apache.archiva.repository.RepositoryNotFoundException;
56 import org.apache.archiva.repository.events.RepositoryListener;
57 import org.apache.archiva.repository.metadata.MetadataTools;
58 import org.apache.archiva.repository.metadata.RepositoryMetadataException;
59 import org.apache.archiva.repository.metadata.RepositoryMetadataWriter;
60 import org.apache.archiva.repository.scanner.RepositoryScanStatistics;
61 import org.apache.archiva.repository.scanner.RepositoryScanner;
62 import org.apache.archiva.repository.scanner.RepositoryScannerException;
63 import org.apache.archiva.rest.api.model.Artifact;
64 import org.apache.archiva.rest.api.model.ArtifactTransferRequest;
65 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
66 import org.apache.archiva.rest.api.services.RepositoriesService;
67 import org.apache.archiva.scheduler.ArchivaTaskScheduler;
68 import org.apache.archiva.scheduler.indexing.ArchivaIndexingTaskExecutor;
69 import org.apache.archiva.scheduler.indexing.ArtifactIndexingTask;
70 import org.apache.archiva.scheduler.indexing.DownloadRemoteIndexException;
71 import org.apache.archiva.scheduler.indexing.DownloadRemoteIndexScheduler;
72 import org.apache.archiva.scheduler.repository.RepositoryArchivaTaskScheduler;
73 import org.apache.archiva.scheduler.repository.RepositoryTask;
74 import org.apache.archiva.security.ArchivaSecurityException;
75 import org.apache.archiva.security.common.ArchivaRoleConstants;
76 import org.apache.archiva.xml.XMLException;
77 import org.apache.commons.io.FilenameUtils;
78 import org.apache.commons.io.IOUtils;
79 import org.apache.commons.lang.StringUtils;
80 import org.apache.maven.index.context.IndexingContext;
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 @Named( value = "archivaTaskScheduler#repository" )
138 private ArchivaTaskScheduler scheduler;
141 private DownloadRemoteIndexScheduler downloadRemoteIndexScheduler;
144 @Named( value = "repositorySessionFactory" )
145 protected RepositorySessionFactory repositorySessionFactory;
148 protected List<RepositoryListener> listeners = new ArrayList<RepositoryListener>();
151 private RepositoryScanner repoScanner;
153 private ChecksumAlgorithm[] algorithms = new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 };
155 public Boolean scanRepository( String repositoryId, boolean fullScan )
157 if ( repositoryTaskScheduler.isProcessingRepositoryTask( repositoryId ) )
159 log.info( "scanning of repository with id {} already scheduled", repositoryId );
160 return Boolean.FALSE;
162 RepositoryTask task = new RepositoryTask();
163 task.setRepositoryId( repositoryId );
164 task.setScanAll( fullScan );
167 repositoryTaskScheduler.queueTask( task );
169 catch ( TaskQueueException e )
171 log.error( "failed to schedule scanning of repo with id {}", repositoryId, e );
177 public Boolean alreadyScanning( String repositoryId )
179 return repositoryTaskScheduler.isProcessingRepositoryTask( repositoryId );
182 public Boolean removeScanningTaskFromQueue( String repositoryId )
184 RepositoryTask task = new RepositoryTask();
185 task.setRepositoryId( repositoryId );
188 return repositoryTaskScheduler.unQueueTask( task );
190 catch ( TaskQueueException e )
192 log.error( "failed to unschedule scanning of repo with id {}", repositoryId, e );
197 public Boolean scanRepositoryNow( String repositoryId, boolean fullScan )
198 throws ArchivaRestServiceException
203 ManagedRepository repository = managedRepositoryAdmin.getManagedRepository( repositoryId );
205 IndexingContext context = managedRepositoryAdmin.createIndexContext( repository );
207 ArtifactIndexingTask task =
208 new ArtifactIndexingTask( repository, null, ArtifactIndexingTask.Action.FINISH, context );
210 task.setExecuteOnEntireRepo( true );
211 task.setOnlyUpdate( !fullScan );
213 archivaIndexingTaskExecutor.executeTask( task );
216 catch ( Exception e )
218 log.error( e.getMessage(), e );
219 throw new ArchivaRestServiceException( e.getMessage(), e );
223 public Boolean scheduleDownloadRemoteIndex( String repositoryId, boolean now, boolean fullDownload )
224 throws ArchivaRestServiceException
228 downloadRemoteIndexScheduler.scheduleDownloadRemote( repositoryId, now, fullDownload );
230 catch ( DownloadRemoteIndexException e )
232 log.error( e.getMessage(), e );
233 throw new ArchivaRestServiceException( e.getMessage(), e );
238 public Boolean copyArtifact( ArtifactTransferRequest artifactTransferRequest )
239 throws ArchivaRestServiceException
242 String userName = getAuditInformation().getUser().getUsername();
243 if ( StringUtils.isBlank( userName ) )
245 throw new ArchivaRestServiceException( "copyArtifact call: userName not found", null );
248 if ( StringUtils.isBlank( artifactTransferRequest.getRepositoryId() ) )
250 throw new ArchivaRestServiceException( "copyArtifact call: sourceRepositoryId cannot be null", null );
253 if ( StringUtils.isBlank( artifactTransferRequest.getTargetRepositoryId() ) )
255 throw new ArchivaRestServiceException( "copyArtifact call: targetRepositoryId cannot be null", null );
258 ManagedRepository source = null;
261 source = managedRepositoryAdmin.getManagedRepository( artifactTransferRequest.getRepositoryId() );
263 catch ( RepositoryAdminException e )
265 throw new ArchivaRestServiceException( e.getMessage(), e );
268 if ( source == null )
270 throw new ArchivaRestServiceException(
271 "cannot find repository with id " + artifactTransferRequest.getRepositoryId(), null );
274 ManagedRepository target = null;
277 target = managedRepositoryAdmin.getManagedRepository( artifactTransferRequest.getTargetRepositoryId() );
279 catch ( RepositoryAdminException e )
281 throw new ArchivaRestServiceException( e.getMessage(), e );
284 if ( target == null )
286 throw new ArchivaRestServiceException(
287 "cannot find repository with id " + artifactTransferRequest.getTargetRepositoryId(), null );
290 if ( StringUtils.isBlank( artifactTransferRequest.getGroupId() ) )
292 throw new ArchivaRestServiceException( "groupId is mandatory", null );
295 if ( StringUtils.isBlank( artifactTransferRequest.getArtifactId() ) )
297 throw new ArchivaRestServiceException( "artifactId is mandatory", null );
300 if ( StringUtils.isBlank( artifactTransferRequest.getVersion() ) )
302 throw new ArchivaRestServiceException( "version is mandatory", null );
305 if ( VersionUtil.isSnapshot( artifactTransferRequest.getVersion() ) )
307 throw new ArchivaRestServiceException( "copy of SNAPSHOT not supported", null );
310 // end check parameters
315 user = securitySystem.getUserManager().findUser( userName );
317 catch ( UserNotFoundException e )
319 throw new ArchivaRestServiceException( "user " + userName + " not found", null );
322 // check karma on source : read
323 AuthenticationResult authn = new AuthenticationResult( true, userName, null );
324 SecuritySession securitySession = new DefaultSecuritySession( authn, user );
328 securitySystem.isAuthorized( securitySession, ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS,
329 artifactTransferRequest.getRepositoryId() );
332 throw new ArchivaRestServiceException(
333 "not authorized to access repo:" + artifactTransferRequest.getRepositoryId(), null );
336 catch ( AuthorizationException e )
338 log.error( "error reading permission: " + e.getMessage(), e );
339 throw new ArchivaRestServiceException( e.getMessage(), e );
342 // check karma on target: write
346 securitySystem.isAuthorized( securitySession, ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD,
347 artifactTransferRequest.getTargetRepositoryId() );
350 throw new ArchivaRestServiceException(
351 "not authorized to write to repo:" + artifactTransferRequest.getTargetRepositoryId(), null );
354 catch ( AuthorizationException e )
356 log.error( "error reading permission: " + e.getMessage(), e );
357 throw new ArchivaRestServiceException( e.getMessage(), e );
360 // sounds good we can continue !
362 ArtifactReference artifactReference = new ArtifactReference();
363 artifactReference.setArtifactId( artifactTransferRequest.getArtifactId() );
364 artifactReference.setGroupId( artifactTransferRequest.getGroupId() );
365 artifactReference.setVersion( artifactTransferRequest.getVersion() );
366 artifactReference.setClassifier( artifactTransferRequest.getClassifier() );
367 String packaging = StringUtils.trim( artifactTransferRequest.getPackaging() );
368 artifactReference.setType( StringUtils.isEmpty( packaging ) ? "jar" : packaging );
373 ManagedRepositoryContent sourceRepository =
374 repositoryFactory.getManagedRepositoryContent( artifactTransferRequest.getRepositoryId() );
376 String artifactSourcePath = sourceRepository.toPath( artifactReference );
378 if ( StringUtils.isEmpty( artifactSourcePath ) )
380 log.error( "cannot find artifact " + artifactTransferRequest.toString() );
381 throw new ArchivaRestServiceException( "cannot find artifact " + artifactTransferRequest.toString(),
385 File artifactFile = new File( source.getLocation(), artifactSourcePath );
387 if ( !artifactFile.exists() )
389 log.error( "cannot find artifact " + artifactTransferRequest.toString() );
390 throw new ArchivaRestServiceException( "cannot find artifact " + artifactTransferRequest.toString(),
394 ManagedRepositoryContent targetRepository =
395 repositoryFactory.getManagedRepositoryContent( artifactTransferRequest.getTargetRepositoryId() );
397 String artifactPath = targetRepository.toPath( artifactReference );
399 int lastIndex = artifactPath.lastIndexOf( '/' );
401 String path = artifactPath.substring( 0, lastIndex );
402 File targetPath = new File( target.getLocation(), path );
404 Date lastUpdatedTimestamp = Calendar.getInstance().getTime();
405 int newBuildNumber = 1;
406 String timestamp = null;
408 File versionMetadataFile = new File( targetPath, MetadataTools.MAVEN_METADATA );
409 ArchivaRepositoryMetadata versionMetadata = getMetadata( versionMetadataFile );
411 if ( !targetPath.exists() )
416 String filename = artifactPath.substring( lastIndex + 1 );
418 // FIXME some dupe with uploadaction
420 boolean fixChecksums =
421 !( archivaAdministration.getKnownContentConsumers().contains( "create-missing-checksums" ) );
423 File targetFile = new File( targetPath, filename );
424 if ( targetFile.exists() && target.isBlockRedeployments() )
426 throw new ArchivaRestServiceException(
427 "artifact already exists in target repo: " + artifactTransferRequest.getTargetRepositoryId()
428 + " and redeployment blocked", null );
432 copyFile( artifactFile, targetPath, filename, fixChecksums );
433 queueRepositoryTask( target.getId(), targetFile );
436 // copy source pom to target repo
437 String pomFilename = filename;
438 if ( StringUtils.isNotBlank( artifactTransferRequest.getClassifier() ) )
440 pomFilename = StringUtils.remove( pomFilename, "-" + artifactTransferRequest.getClassifier() );
442 pomFilename = FilenameUtils.removeExtension( pomFilename ) + ".pom";
444 File pomFile = new File(
445 new File( source.getLocation(), artifactSourcePath.substring( 0, artifactPath.lastIndexOf( '/' ) ) ),
448 if ( pomFile != null && pomFile.length() > 0 )
450 copyFile( pomFile, targetPath, pomFilename, fixChecksums );
451 queueRepositoryTask( target.getId(), new File( targetPath, pomFilename ) );
456 // explicitly update only if metadata-updater consumer is not enabled!
457 if ( !archivaAdministration.getKnownContentConsumers().contains( "metadata-updater" ) )
459 updateProjectMetadata( targetPath.getAbsolutePath(), lastUpdatedTimestamp, timestamp, newBuildNumber,
460 fixChecksums, artifactTransferRequest );
466 "Artifact \'" + artifactTransferRequest.getGroupId() + ":" + artifactTransferRequest.getArtifactId()
467 + ":" + artifactTransferRequest.getVersion() + "\' was successfully deployed to repository \'"
468 + artifactTransferRequest.getTargetRepositoryId() + "\'";
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 //FIXME some duplicate with UploadAction
491 private void queueRepositoryTask( String repositoryId, File localFile )
493 RepositoryTask task = new RepositoryTask();
494 task.setRepositoryId( repositoryId );
495 task.setResourceFile( localFile );
496 task.setUpdateRelatedArtifacts( true );
497 //task.setScanAll( true );
501 scheduler.queueTask( task );
503 catch ( TaskQueueException e )
505 log.error( "Unable to queue repository task to execute consumers on resource file ['" + localFile.getName()
510 private ArchivaRepositoryMetadata getMetadata( File metadataFile )
511 throws RepositoryMetadataException
513 ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
514 if ( metadataFile.exists() )
518 metadata = MavenMetadataReader.read( metadataFile );
520 catch ( XMLException e )
522 throw new RepositoryMetadataException( e.getMessage(), e );
528 private File getMetadata( String targetPath )
530 String artifactPath = targetPath.substring( 0, targetPath.lastIndexOf( File.separatorChar ) );
532 return new File( artifactPath, MetadataTools.MAVEN_METADATA );
535 private void copyFile( File sourceFile, File targetPath, String targetFilename, boolean fixChecksums )
538 FileOutputStream out = new FileOutputStream( new File( targetPath, targetFilename ) );
539 FileInputStream input = new FileInputStream( sourceFile );
543 IOUtils.copy( input, out );
547 IOUtils.closeQuietly( out );
548 IOUtils.closeQuietly( input );
553 fixChecksums( new File( targetPath, targetFilename ) );
557 private void fixChecksums( File file )
559 ChecksummedFile checksum = new ChecksummedFile( file );
560 checksum.fixChecksums( algorithms );
563 private void updateProjectMetadata( String targetPath, Date lastUpdatedTimestamp, String timestamp, int buildNumber,
564 boolean fixChecksums, ArtifactTransferRequest artifactTransferRequest )
565 throws RepositoryMetadataException
567 List<String> availableVersions = new ArrayList<String>();
568 String latestVersion = artifactTransferRequest.getVersion();
570 File projectDir = new File( targetPath ).getParentFile();
571 File projectMetadataFile = new File( projectDir, MetadataTools.MAVEN_METADATA );
573 ArchivaRepositoryMetadata projectMetadata = getMetadata( projectMetadataFile );
575 if ( projectMetadataFile.exists() )
577 availableVersions = projectMetadata.getAvailableVersions();
579 Collections.sort( availableVersions, VersionComparator.getInstance() );
581 if ( !availableVersions.contains( artifactTransferRequest.getVersion() ) )
583 availableVersions.add( artifactTransferRequest.getVersion() );
586 latestVersion = availableVersions.get( availableVersions.size() - 1 );
590 availableVersions.add( artifactTransferRequest.getVersion() );
592 projectMetadata.setGroupId( artifactTransferRequest.getGroupId() );
593 projectMetadata.setArtifactId( artifactTransferRequest.getArtifactId() );
596 if ( projectMetadata.getGroupId() == null )
598 projectMetadata.setGroupId( artifactTransferRequest.getGroupId() );
601 if ( projectMetadata.getArtifactId() == null )
603 projectMetadata.setArtifactId( artifactTransferRequest.getArtifactId() );
606 projectMetadata.setLatestVersion( latestVersion );
607 projectMetadata.setLastUpdatedTimestamp( lastUpdatedTimestamp );
608 projectMetadata.setAvailableVersions( availableVersions );
610 if ( !VersionUtil.isSnapshot( artifactTransferRequest.getVersion() ) )
612 projectMetadata.setReleasedVersion( latestVersion );
615 RepositoryMetadataWriter.write( projectMetadata, projectMetadataFile );
619 fixChecksums( projectMetadataFile );
623 public Boolean deleteArtifact( Artifact artifact )
624 throws ArchivaRestServiceException
627 String repositoryId = artifact.getContext();
628 if ( StringUtils.isEmpty( repositoryId ) )
630 throw new ArchivaRestServiceException( "repositoryId cannot be null", 400, null );
633 if ( !isAuthorizedToDeleteArtifacts( repositoryId ) )
635 throw new ArchivaRestServiceException( "not authorized to delete artifacts", 403, null );
638 if ( artifact == null )
640 throw new ArchivaRestServiceException( "artifact cannot be null", 400, null );
643 if ( StringUtils.isEmpty( artifact.getGroupId() ) )
645 throw new ArchivaRestServiceException( "artifact.groupId cannot be null", 400, null );
648 if ( StringUtils.isEmpty( artifact.getArtifactId() ) )
650 throw new ArchivaRestServiceException( "artifact.artifactId cannot be null", 400, null );
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 ( artifactMetadata.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 );
739 repositorySession.save();
741 repositorySession.close();
744 catch ( ContentNotFoundException e )
746 throw new ArchivaRestServiceException( "Artifact does not exist: " + e.getMessage(), 400, e );
748 catch ( RepositoryNotFoundException e )
750 throw new ArchivaRestServiceException( "Target repository cannot be found: " + e.getMessage(), 400, e );
752 catch ( RepositoryException e )
754 throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
756 catch ( MetadataResolutionException e )
758 throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
760 catch ( MetadataRepositoryException e )
762 throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
764 catch ( RepositoryAdminException e )
766 throw new ArchivaRestServiceException( "RepositoryAdmin exception: " + e.getMessage(), 500, e );
771 repositorySession.close();
776 public Boolean isAuthorizedToDeleteArtifacts( String repoId )
777 throws ArchivaRestServiceException
780 getAuditInformation().getUser() == null ? "guest" : getAuditInformation().getUser().getUsername();
784 boolean res = userRepositories.isAuthorizedToDeleteArtifacts( userName, repoId );
787 catch ( ArchivaSecurityException e )
789 throw new ArchivaRestServiceException( e.getMessage(),
790 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
794 public RepositoryScanStatistics scanRepositoryDirectoriesNow( String repositoryId )
795 throws ArchivaRestServiceException
797 long sinceWhen = RepositoryScanner.FRESH_SCAN;
800 return repoScanner.scan( getManagedRepositoryAdmin().getManagedRepository( repositoryId ), sinceWhen );
802 catch ( RepositoryScannerException e )
804 log.error( e.getMessage(), e );
805 throw new ArchivaRestServiceException( "RepositoryScannerException exception: " + e.getMessage(), 500, e );
807 catch ( RepositoryAdminException e )
809 log.error( e.getMessage(), e );
810 throw new ArchivaRestServiceException( "RepositoryScannerException exception: " + e.getMessage(), 500, e );
815 * Update artifact level metadata. Creates one if metadata does not exist after artifact deletion.
819 private void updateMetadata( ArchivaRepositoryMetadata metadata, File metadataFile, Date lastUpdatedTimestamp,
821 throws RepositoryMetadataException
823 List<String> availableVersions = new ArrayList<String>();
824 String latestVersion = "";
826 if ( metadataFile.exists() )
828 if ( metadata.getAvailableVersions() != null )
830 availableVersions = metadata.getAvailableVersions();
832 if ( availableVersions.size() > 0 )
834 Collections.sort( availableVersions, VersionComparator.getInstance() );
836 if ( availableVersions.contains( artifact.getVersion() ) )
838 availableVersions.remove( availableVersions.indexOf( artifact.getVersion() ) );
840 if ( availableVersions.size() > 0 )
842 latestVersion = availableVersions.get( availableVersions.size() - 1 );
848 if ( metadata.getGroupId() == null )
850 metadata.setGroupId( artifact.getGroupId() );
852 if ( metadata.getArtifactId() == null )
854 metadata.setArtifactId( artifact.getArtifactId() );
857 if ( !VersionUtil.isSnapshot( artifact.getVersion() ) )
859 if ( metadata.getReleasedVersion() != null && metadata.getReleasedVersion().equals(
860 artifact.getVersion() ) )
862 metadata.setReleasedVersion( latestVersion );
866 metadata.setLatestVersion( latestVersion );
867 metadata.setLastUpdatedTimestamp( lastUpdatedTimestamp );
868 metadata.setAvailableVersions( availableVersions );
870 RepositoryMetadataWriter.write( metadata, metadataFile );
871 ChecksummedFile checksum = new ChecksummedFile( metadataFile );
872 checksum.fixChecksums( algorithms );
875 public ManagedRepositoryAdmin getManagedRepositoryAdmin()
877 return managedRepositoryAdmin;
880 public void setManagedRepositoryAdmin( ManagedRepositoryAdmin managedRepositoryAdmin )
882 this.managedRepositoryAdmin = managedRepositoryAdmin;
885 public RepositoryContentFactory getRepositoryFactory()
887 return repositoryFactory;
890 public void setRepositoryFactory( RepositoryContentFactory repositoryFactory )
892 this.repositoryFactory = repositoryFactory;
895 public RepositorySessionFactory getRepositorySessionFactory()
897 return repositorySessionFactory;
900 public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
902 this.repositorySessionFactory = repositorySessionFactory;
905 public List<RepositoryListener> getListeners()
910 public void setListeners( List<RepositoryListener> listeners )
912 this.listeners = listeners;
915 public ArchivaAdministration getArchivaAdministration()
917 return archivaAdministration;
920 public void setArchivaAdministration( ArchivaAdministration archivaAdministration )
922 this.archivaAdministration = archivaAdministration;