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.AuditInformation;
23 import org.apache.archiva.admin.model.RepositoryAdminException;
24 import org.apache.archiva.admin.model.admin.ArchivaAdministration;
25 import org.apache.archiva.admin.model.beans.ManagedRepository;
26 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
27 import org.apache.archiva.audit.AuditEvent;
28 import org.apache.archiva.audit.AuditListener;
29 import org.apache.archiva.checksum.ChecksumAlgorithm;
30 import org.apache.archiva.checksum.ChecksummedFile;
31 import org.apache.archiva.common.plexusbridge.MavenIndexerUtils;
32 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
33 import org.apache.archiva.common.utils.VersionComparator;
34 import org.apache.archiva.common.utils.VersionUtil;
35 import org.apache.archiva.metadata.model.ArtifactMetadata;
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.repository.ContentNotFoundException;
45 import org.apache.archiva.repository.ManagedRepositoryContent;
46 import org.apache.archiva.repository.RepositoryContentFactory;
47 import org.apache.archiva.repository.RepositoryException;
48 import org.apache.archiva.repository.RepositoryNotFoundException;
49 import org.apache.archiva.repository.events.RepositoryListener;
50 import org.apache.archiva.repository.metadata.MetadataTools;
51 import org.apache.archiva.repository.metadata.RepositoryMetadataException;
52 import org.apache.archiva.repository.metadata.RepositoryMetadataReader;
53 import org.apache.archiva.repository.metadata.RepositoryMetadataWriter;
54 import org.apache.archiva.rest.api.model.Artifact;
55 import org.apache.archiva.rest.api.model.ArtifactTransferRequest;
56 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
57 import org.apache.archiva.rest.api.services.RepositoriesService;
58 import org.apache.archiva.scheduler.ArchivaTaskScheduler;
59 import org.apache.archiva.scheduler.indexing.ArchivaIndexingTaskExecutor;
60 import org.apache.archiva.scheduler.indexing.ArtifactIndexingTask;
61 import org.apache.archiva.scheduler.indexing.DownloadRemoteIndexException;
62 import org.apache.archiva.scheduler.indexing.DownloadRemoteIndexScheduler;
63 import org.apache.archiva.scheduler.repository.RepositoryArchivaTaskScheduler;
64 import org.apache.archiva.scheduler.repository.RepositoryTask;
65 import org.apache.archiva.security.common.ArchivaRoleConstants;
66 import org.apache.commons.io.FilenameUtils;
67 import org.apache.commons.io.IOUtils;
68 import org.apache.commons.lang.StringUtils;
69 import org.apache.maven.index.context.IndexingContext;
70 import org.codehaus.plexus.redback.authentication.AuthenticationResult;
71 import org.codehaus.plexus.redback.authorization.AuthorizationException;
72 import org.codehaus.plexus.redback.system.DefaultSecuritySession;
73 import org.codehaus.plexus.redback.system.SecuritySession;
74 import org.codehaus.plexus.redback.system.SecuritySystem;
75 import org.codehaus.plexus.redback.users.User;
76 import org.codehaus.plexus.redback.users.UserNotFoundException;
77 import org.codehaus.plexus.taskqueue.TaskQueueException;
78 import org.slf4j.Logger;
79 import org.slf4j.LoggerFactory;
80 import org.springframework.stereotype.Service;
82 import javax.inject.Inject;
83 import javax.inject.Named;
85 import java.io.FileInputStream;
86 import java.io.FileOutputStream;
87 import java.io.IOException;
88 import java.text.DateFormat;
89 import java.text.SimpleDateFormat;
90 import java.util.ArrayList;
91 import java.util.Calendar;
92 import java.util.Collection;
93 import java.util.Collections;
94 import java.util.Date;
95 import java.util.List;
96 import java.util.TimeZone;
99 * @author Olivier Lamy
102 @Service( "repositoriesService#rest" )
103 public class DefaultRepositoriesService
104 extends AbstractRestService
105 implements RepositoriesService
107 private Logger log = LoggerFactory.getLogger( getClass() );
110 @Named( value = "archivaTaskScheduler#repository" )
111 private RepositoryArchivaTaskScheduler repositoryTaskScheduler;
114 @Named( value = "taskExecutor#indexing" )
115 private ArchivaIndexingTaskExecutor archivaIndexingTaskExecutor;
118 private ManagedRepositoryAdmin managedRepositoryAdmin;
121 private PlexusSisuBridge plexusSisuBridge;
124 private MavenIndexerUtils mavenIndexerUtils;
127 private SecuritySystem securitySystem;
130 private RepositoryContentFactory repositoryFactory;
133 private ArchivaAdministration archivaAdministration;
136 @Named( value = "archivaTaskScheduler#repository" )
137 private ArchivaTaskScheduler scheduler;
140 private DownloadRemoteIndexScheduler downloadRemoteIndexScheduler;
143 @Named( value = "repositorySessionFactory" )
144 protected RepositorySessionFactory repositorySessionFactory;
147 protected List<RepositoryListener> listeners = new ArrayList<RepositoryListener>();
149 private ChecksumAlgorithm[] algorithms = new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 };
151 public Boolean scanRepository( String repositoryId, boolean fullScan )
153 if ( repositoryTaskScheduler.isProcessingRepositoryTask( repositoryId ) )
155 log.info( "scanning of repository with id {} already scheduled", repositoryId );
156 return Boolean.FALSE;
158 RepositoryTask task = new RepositoryTask();
159 task.setRepositoryId( repositoryId );
160 task.setScanAll( fullScan );
163 repositoryTaskScheduler.queueTask( task );
165 catch ( TaskQueueException e )
167 log.error( "failed to schedule scanning of repo with id {}", repositoryId, e );
173 public Boolean alreadyScanning( String repositoryId )
175 return repositoryTaskScheduler.isProcessingRepositoryTask( repositoryId );
178 public Boolean removeScanningTaskFromQueue( String repositoryId )
180 RepositoryTask task = new RepositoryTask();
181 task.setRepositoryId( repositoryId );
184 return repositoryTaskScheduler.unQueueTask( task );
186 catch ( TaskQueueException e )
188 log.error( "failed to unschedule scanning of repo with id {}", repositoryId, e );
193 public Boolean scanRepositoryNow( String repositoryId, boolean fullScan )
194 throws ArchivaRestServiceException
199 ManagedRepository repository = managedRepositoryAdmin.getManagedRepository( repositoryId );
201 IndexingContext context = managedRepositoryAdmin.createIndexContext( repository );
202 ArtifactIndexingTask task =
203 new ArtifactIndexingTask( repository, null, ArtifactIndexingTask.Action.FINISH, context );
205 task.setExecuteOnEntireRepo( fullScan );
206 task.setOnlyUpdate( false );
208 archivaIndexingTaskExecutor.executeTask( task );
211 catch ( Exception e )
213 log.error( e.getMessage(), e );
214 throw new ArchivaRestServiceException( e.getMessage() );
218 public Boolean scheduleDownloadRemoteIndex( String repositoryId, boolean now, boolean fullDownload )
219 throws ArchivaRestServiceException
223 downloadRemoteIndexScheduler.scheduleDownloadRemote( repositoryId, now, fullDownload );
225 catch ( DownloadRemoteIndexException e )
227 log.error( e.getMessage(), e );
228 throw new ArchivaRestServiceException( e.getMessage() );
233 public Boolean copyArtifact( ArtifactTransferRequest artifactTransferRequest )
234 throws ArchivaRestServiceException
237 String userName = getAuditInformation().getUser().getUsername();
238 if ( StringUtils.isBlank( userName ) )
240 throw new ArchivaRestServiceException( "copyArtifact call: userName not found" );
243 if ( StringUtils.isBlank( artifactTransferRequest.getRepositoryId() ) )
245 throw new ArchivaRestServiceException( "copyArtifact call: sourceRepositoryId cannot be null" );
248 if ( StringUtils.isBlank( artifactTransferRequest.getTargetRepositoryId() ) )
250 throw new ArchivaRestServiceException( "copyArtifact call: targetRepositoryId cannot be null" );
253 ManagedRepository source = null;
256 source = managedRepositoryAdmin.getManagedRepository( artifactTransferRequest.getRepositoryId() );
258 catch ( RepositoryAdminException e )
260 throw new ArchivaRestServiceException( e.getMessage() );
263 if ( source == null )
265 throw new ArchivaRestServiceException(
266 "cannot find repository with id " + artifactTransferRequest.getRepositoryId() );
269 ManagedRepository target = null;
272 target = managedRepositoryAdmin.getManagedRepository( artifactTransferRequest.getTargetRepositoryId() );
274 catch ( RepositoryAdminException e )
276 throw new ArchivaRestServiceException( e.getMessage() );
279 if ( target == null )
281 throw new ArchivaRestServiceException(
282 "cannot find repository with id " + artifactTransferRequest.getTargetRepositoryId() );
285 if ( StringUtils.isBlank( artifactTransferRequest.getGroupId() ) )
287 throw new ArchivaRestServiceException( "groupId is mandatory" );
290 if ( StringUtils.isBlank( artifactTransferRequest.getArtifactId() ) )
292 throw new ArchivaRestServiceException( "artifactId is mandatory" );
295 if ( StringUtils.isBlank( artifactTransferRequest.getVersion() ) )
297 throw new ArchivaRestServiceException( "version is mandatory" );
300 if ( VersionUtil.isSnapshot( artifactTransferRequest.getVersion() ) )
302 throw new ArchivaRestServiceException( "copy of SNAPSHOT not supported" );
305 // end check parameters
310 user = securitySystem.getUserManager().findUser( userName );
312 catch ( UserNotFoundException e )
314 throw new ArchivaRestServiceException( "user " + userName + " not found" );
317 // check karma on source : read
318 AuthenticationResult authn = new AuthenticationResult( true, userName, null );
319 SecuritySession securitySession = new DefaultSecuritySession( authn, user );
323 securitySystem.isAuthorized( securitySession, ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS,
324 artifactTransferRequest.getRepositoryId() );
327 throw new ArchivaRestServiceException(
328 "not authorized to access repo:" + artifactTransferRequest.getRepositoryId() );
331 catch ( AuthorizationException e )
333 log.error( "error reading permission: " + e.getMessage(), e );
334 throw new ArchivaRestServiceException( e.getMessage() );
337 // check karma on target: write
341 securitySystem.isAuthorized( securitySession, ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD,
342 artifactTransferRequest.getTargetRepositoryId() );
345 throw new ArchivaRestServiceException(
346 "not authorized to write to repo:" + artifactTransferRequest.getTargetRepositoryId() );
349 catch ( AuthorizationException e )
351 log.error( "error reading permission: " + e.getMessage(), e );
352 throw new ArchivaRestServiceException( e.getMessage() );
355 // sounds good we can continue !
357 ArtifactReference artifactReference = new ArtifactReference();
358 artifactReference.setArtifactId( artifactTransferRequest.getArtifactId() );
359 artifactReference.setGroupId( artifactTransferRequest.getGroupId() );
360 artifactReference.setVersion( artifactTransferRequest.getVersion() );
361 artifactReference.setClassifier( artifactTransferRequest.getClassifier() );
362 String packaging = StringUtils.trim( artifactTransferRequest.getPackaging() );
363 artifactReference.setType( StringUtils.isEmpty( packaging ) ? "jar" : packaging );
368 ManagedRepositoryContent sourceRepository =
369 repositoryFactory.getManagedRepositoryContent( artifactTransferRequest.getRepositoryId() );
371 String artifactSourcePath = sourceRepository.toPath( artifactReference );
373 if ( StringUtils.isEmpty( artifactSourcePath ) )
375 log.error( "cannot find artifact " + artifactTransferRequest.toString() );
376 throw new ArchivaRestServiceException( "cannot find artifact " + artifactTransferRequest.toString() );
379 File artifactFile = new File( source.getLocation(), artifactSourcePath );
381 if ( !artifactFile.exists() )
383 log.error( "cannot find artifact " + artifactTransferRequest.toString() );
384 throw new ArchivaRestServiceException( "cannot find artifact " + artifactTransferRequest.toString() );
387 ManagedRepositoryContent targetRepository =
388 repositoryFactory.getManagedRepositoryContent( artifactTransferRequest.getTargetRepositoryId() );
390 String artifactPath = targetRepository.toPath( artifactReference );
392 int lastIndex = artifactPath.lastIndexOf( '/' );
394 String path = artifactPath.substring( 0, lastIndex );
395 File targetPath = new File( target.getLocation(), path );
397 Date lastUpdatedTimestamp = Calendar.getInstance().getTime();
398 int newBuildNumber = 1;
399 String timestamp = null;
401 File versionMetadataFile = new File( targetPath, MetadataTools.MAVEN_METADATA );
402 ArchivaRepositoryMetadata versionMetadata = getMetadata( versionMetadataFile );
404 if ( !targetPath.exists() )
409 String filename = artifactPath.substring( lastIndex + 1 );
411 // FIXME some dupe with uploadaction
413 boolean fixChecksums =
414 !( archivaAdministration.getKnownContentConsumers().contains( "create-missing-checksums" ) );
416 File targetFile = new File( targetPath, filename );
417 if ( targetFile.exists() && target.isBlockRedeployments() )
419 throw new ArchivaRestServiceException(
420 "artifact already exists in target repo: " + artifactTransferRequest.getTargetRepositoryId()
421 + " and redeployment blocked" );
425 copyFile( artifactFile, targetPath, filename, fixChecksums );
426 queueRepositoryTask( target.getId(), targetFile );
429 // copy source pom to target repo
430 String pomFilename = filename;
431 if ( StringUtils.isNotBlank( artifactTransferRequest.getClassifier() ) )
433 pomFilename = StringUtils.remove( pomFilename, "-" + artifactTransferRequest.getClassifier() );
435 pomFilename = FilenameUtils.removeExtension( pomFilename ) + ".pom";
437 File pomFile = new File(
438 new File( source.getLocation(), artifactSourcePath.substring( 0, artifactPath.lastIndexOf( '/' ) ) ),
441 if ( pomFile != null && pomFile.length() > 0 )
443 copyFile( pomFile, targetPath, pomFilename, fixChecksums );
444 queueRepositoryTask( target.getId(), new File( targetPath, pomFilename ) );
449 // explicitly update only if metadata-updater consumer is not enabled!
450 if ( !archivaAdministration.getKnownContentConsumers().contains( "metadata-updater" ) )
452 updateProjectMetadata( targetPath.getAbsolutePath(), lastUpdatedTimestamp, timestamp, newBuildNumber,
453 fixChecksums, artifactTransferRequest );
459 "Artifact \'" + artifactTransferRequest.getGroupId() + ":" + artifactTransferRequest.getArtifactId()
460 + ":" + artifactTransferRequest.getVersion() + "\' was successfully deployed to repository \'"
461 + artifactTransferRequest.getTargetRepositoryId() + "\'";
464 catch ( RepositoryException e )
466 log.error( "RepositoryException: " + e.getMessage(), e );
467 throw new ArchivaRestServiceException( e.getMessage() );
469 catch ( RepositoryAdminException e )
471 log.error( "RepositoryAdminException: " + e.getMessage(), e );
472 throw new ArchivaRestServiceException( e.getMessage() );
474 catch ( IOException e )
476 log.error( "IOException: " + e.getMessage(), e );
477 throw new ArchivaRestServiceException( e.getMessage() );
482 //FIXME some duplicate with UploadAction
484 private void queueRepositoryTask( String repositoryId, File localFile )
486 RepositoryTask task = new RepositoryTask();
487 task.setRepositoryId( repositoryId );
488 task.setResourceFile( localFile );
489 task.setUpdateRelatedArtifacts( true );
490 //task.setScanAll( true );
494 scheduler.queueTask( task );
496 catch ( TaskQueueException e )
498 log.error( "Unable to queue repository task to execute consumers on resource file ['" + localFile.getName()
503 private ArchivaRepositoryMetadata getMetadata( File metadataFile )
504 throws RepositoryMetadataException
506 ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
507 if ( metadataFile.exists() )
509 metadata = RepositoryMetadataReader.read( metadataFile );
514 private File getMetadata( String targetPath )
516 String artifactPath = targetPath.substring( 0, targetPath.lastIndexOf( File.separatorChar ) );
518 return new File( artifactPath, MetadataTools.MAVEN_METADATA );
521 private void copyFile( File sourceFile, File targetPath, String targetFilename, boolean fixChecksums )
524 FileOutputStream out = new FileOutputStream( new File( targetPath, targetFilename ) );
525 FileInputStream input = new FileInputStream( sourceFile );
529 IOUtils.copy( input, out );
539 fixChecksums( new File( targetPath, targetFilename ) );
543 private void fixChecksums( File file )
545 ChecksummedFile checksum = new ChecksummedFile( file );
546 checksum.fixChecksums( algorithms );
549 private void updateProjectMetadata( String targetPath, Date lastUpdatedTimestamp, String timestamp, int buildNumber,
550 boolean fixChecksums, ArtifactTransferRequest artifactTransferRequest )
551 throws RepositoryMetadataException
553 List<String> availableVersions = new ArrayList<String>();
554 String latestVersion = artifactTransferRequest.getVersion();
556 File projectDir = new File( targetPath ).getParentFile();
557 File projectMetadataFile = new File( projectDir, MetadataTools.MAVEN_METADATA );
559 ArchivaRepositoryMetadata projectMetadata = getMetadata( projectMetadataFile );
561 if ( projectMetadataFile.exists() )
563 availableVersions = projectMetadata.getAvailableVersions();
565 Collections.sort( availableVersions, VersionComparator.getInstance() );
567 if ( !availableVersions.contains( artifactTransferRequest.getVersion() ) )
569 availableVersions.add( artifactTransferRequest.getVersion() );
572 latestVersion = availableVersions.get( availableVersions.size() - 1 );
576 availableVersions.add( artifactTransferRequest.getVersion() );
578 projectMetadata.setGroupId( artifactTransferRequest.getGroupId() );
579 projectMetadata.setArtifactId( artifactTransferRequest.getArtifactId() );
582 if ( projectMetadata.getGroupId() == null )
584 projectMetadata.setGroupId( artifactTransferRequest.getGroupId() );
587 if ( projectMetadata.getArtifactId() == null )
589 projectMetadata.setArtifactId( artifactTransferRequest.getArtifactId() );
592 projectMetadata.setLatestVersion( latestVersion );
593 projectMetadata.setLastUpdatedTimestamp( lastUpdatedTimestamp );
594 projectMetadata.setAvailableVersions( availableVersions );
596 if ( !VersionUtil.isSnapshot( artifactTransferRequest.getVersion() ) )
598 projectMetadata.setReleasedVersion( latestVersion );
601 RepositoryMetadataWriter.write( projectMetadata, projectMetadataFile );
605 fixChecksums( projectMetadataFile );
609 public Boolean deleteArtifact( Artifact artifact, String repositoryId )
610 throws ArchivaRestServiceException
612 String userName = (String) getAuditInformation().getUser().getUsername();
613 if ( StringUtils.isBlank( userName ) )
615 // TODO use constants from a class instead of magic number
616 throw new ArchivaRestServiceException( "deleteArtifact call: userName not found", 403 );
619 if ( artifact == null )
621 throw new ArchivaRestServiceException( "artifact cannot be null", 400 );
624 if ( StringUtils.isEmpty( artifact.getGroupId() ) )
626 throw new ArchivaRestServiceException( "artifact.groupId cannot be null", 400 );
629 if ( StringUtils.isEmpty( artifact.getArtifactId() ) )
631 throw new ArchivaRestServiceException( "artifact.artifactId cannot be null", 400 );
634 if ( StringUtils.isEmpty( repositoryId ) )
636 throw new ArchivaRestServiceException( "repositoryId cannot be null", 400 );
639 // TODO more control on artifact fields
641 RepositorySession repositorySession = repositorySessionFactory.createSession();
644 Date lastUpdatedTimestamp = Calendar.getInstance().getTime();
646 TimeZone timezone = TimeZone.getTimeZone( "UTC" );
647 DateFormat fmt = new SimpleDateFormat( "yyyyMMdd.HHmmss" );
648 fmt.setTimeZone( timezone );
649 ManagedRepository repoConfig = managedRepositoryAdmin.getManagedRepository( repositoryId );
651 VersionedReference ref = new VersionedReference();
652 ref.setArtifactId( artifact.getArtifactId() );
653 ref.setGroupId( artifact.getGroupId() );
654 ref.setVersion( artifact.getVersion() );
656 ManagedRepositoryContent repository = repositoryFactory.getManagedRepositoryContent( repositoryId );
658 if ( StringUtils.isNotBlank( artifact.getClassifier() ) )
660 if ( StringUtils.isBlank( artifact.getPackaging() ) )
662 throw new ArchivaRestServiceException( "You must configure a type/packaging when using classifier",
665 ArtifactReference artifactReference = new ArtifactReference();
666 artifactReference.setArtifactId( artifact.getArtifactId() );
667 artifactReference.setGroupId( artifact.getGroupId() );
668 artifactReference.setVersion( artifact.getVersion() );
669 artifactReference.setClassifier( artifact.getClassifier() );
670 artifactReference.setType( artifact.getPackaging() );
671 repository.deleteArtifact( artifactReference );
673 // TODO cleanup facet which contains classifier information
677 String path = repository.toMetadataPath( ref );
678 int index = path.lastIndexOf( '/' );
679 path = path.substring( 0, index );
680 File targetPath = new File( repoConfig.getLocation(), path );
682 if ( !targetPath.exists() )
684 throw new ContentNotFoundException(
685 artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion() );
688 // TODO: this should be in the storage mechanism so that it is all tied together
689 // delete from file system
690 repository.deleteVersion( ref );
692 File metadataFile = getMetadata( targetPath.getAbsolutePath() );
693 ArchivaRepositoryMetadata metadata = getMetadata( metadataFile );
695 updateMetadata( metadata, metadataFile, lastUpdatedTimestamp, artifact );
697 MetadataRepository metadataRepository = repositorySession.getRepository();
699 Collection<ArtifactMetadata> artifacts =
700 metadataRepository.getArtifacts( repositoryId, artifact.getGroupId(), artifact.getArtifactId(),
701 artifact.getVersion() );
703 for ( ArtifactMetadata artifactMetadata : artifacts )
705 // TODO: mismatch between artifact (snapshot) version and project (base) version here
706 if ( artifact.getVersion().equals( artifact.getVersion() ) )
708 metadataRepository.removeArtifact( artifactMetadata.getRepositoryId(),
709 artifactMetadata.getNamespace(), artifactMetadata.getProject(),
710 artifact.getVersion(), artifactMetadata.getId() );
712 // TODO: move into the metadata repository proper - need to differentiate attachment of
713 // repository metadata to an artifact
714 for ( RepositoryListener listener : listeners )
716 listener.deleteArtifact( metadataRepository, repository.getId(),
717 artifactMetadata.getNamespace(), artifactMetadata.getProject(),
718 artifactMetadata.getVersion(), artifactMetadata.getId() );
721 triggerAuditEvent( repositoryId, path, AuditEvent.REMOVE_FILE );
724 repositorySession.save();
727 catch ( ContentNotFoundException e )
729 throw new ArchivaRestServiceException( "Artifact does not exist: " + e.getMessage(), 400 );
731 catch ( RepositoryNotFoundException e )
733 throw new ArchivaRestServiceException( "Target repository cannot be found: " + e.getMessage(), 400 );
735 catch ( RepositoryException e )
737 throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 400 );
739 catch ( MetadataResolutionException e )
741 throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 400 );
743 catch ( MetadataRepositoryException e )
745 throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 400 );
747 catch ( RepositoryAdminException e )
749 throw new ArchivaRestServiceException( "RepositoryAdmin exception: " + e.getMessage(), 400 );
754 repositorySession.close();
761 * Update artifact level metadata. Creates one if metadata does not exist after artifact deletion.
765 private void updateMetadata( ArchivaRepositoryMetadata metadata, File metadataFile, Date lastUpdatedTimestamp,
767 throws RepositoryMetadataException
769 List<String> availableVersions = new ArrayList<String>();
770 String latestVersion = "";
772 if ( metadataFile.exists() )
774 if ( metadata.getAvailableVersions() != null )
776 availableVersions = metadata.getAvailableVersions();
778 if ( availableVersions.size() > 0 )
780 Collections.sort( availableVersions, VersionComparator.getInstance() );
782 if ( availableVersions.contains( artifact.getVersion() ) )
784 availableVersions.remove( availableVersions.indexOf( artifact.getVersion() ) );
786 if ( availableVersions.size() > 0 )
788 latestVersion = availableVersions.get( availableVersions.size() - 1 );
794 if ( metadata.getGroupId() == null )
796 metadata.setGroupId( artifact.getGroupId() );
798 if ( metadata.getArtifactId() == null )
800 metadata.setArtifactId( artifact.getArtifactId() );
803 if ( !VersionUtil.isSnapshot( artifact.getVersion() ) )
805 if ( metadata.getReleasedVersion() != null && metadata.getReleasedVersion().equals(
806 artifact.getVersion() ) )
808 metadata.setReleasedVersion( latestVersion );
812 metadata.setLatestVersion( latestVersion );
813 metadata.setLastUpdatedTimestamp( lastUpdatedTimestamp );
814 metadata.setAvailableVersions( availableVersions );
816 RepositoryMetadataWriter.write( metadata, metadataFile );
817 ChecksummedFile checksum = new ChecksummedFile( metadataFile );
818 checksum.fixChecksums( algorithms );
821 protected void triggerAuditEvent( String repositoryId, String filePath, String action )
823 AuditEvent auditEvent = new AuditEvent();
824 auditEvent.setAction( action );
825 auditEvent.setRepositoryId( repositoryId );
826 auditEvent.setResource( filePath );
827 AuditInformation auditInformation = getAuditInformation();
828 auditEvent.setUserId( auditInformation.getUser() == null ? "" : auditInformation.getUser().getUsername() );
829 auditEvent.setRemoteIP( auditInformation.getRemoteAddr() );
830 for ( AuditListener auditListener : getAuditListeners() )
832 auditListener.auditEvent( auditEvent );
836 public ManagedRepositoryAdmin getManagedRepositoryAdmin()
838 return managedRepositoryAdmin;
841 public void setManagedRepositoryAdmin( ManagedRepositoryAdmin managedRepositoryAdmin )
843 this.managedRepositoryAdmin = managedRepositoryAdmin;
846 public RepositoryContentFactory getRepositoryFactory()
848 return repositoryFactory;
851 public void setRepositoryFactory( RepositoryContentFactory repositoryFactory )
853 this.repositoryFactory = repositoryFactory;
856 public RepositorySessionFactory getRepositorySessionFactory()
858 return repositorySessionFactory;
861 public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
863 this.repositorySessionFactory = repositorySessionFactory;
866 public List<RepositoryListener> getListeners()
871 public void setListeners( List<RepositoryListener> listeners )
873 this.listeners = listeners;
876 public ArchivaAdministration getArchivaAdministration()
878 return archivaAdministration;
881 public void setArchivaAdministration( ArchivaAdministration archivaAdministration )
883 this.archivaAdministration = archivaAdministration;