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( true );
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 );
610 public Boolean deleteArtifact( Artifact artifact, String repositoryId )
611 throws ArchivaRestServiceException
613 String userName = getAuditInformation().getUser().getUsername();
614 if ( StringUtils.isBlank( userName ) )
616 // TODO use constants from a class instead of magic number
617 throw new ArchivaRestServiceException( "deleteArtifact call: userName not found", 403 );
619 if ( artifact == null )
621 throw new ArchivaRestServiceException( "artifact cannot be null", 400 );
623 if ( StringUtils.isEmpty( repositoryId ) )
625 throw new ArchivaRestServiceException( "repositoryId cannot be null", 400 );
628 // TODO more control on artifact fields
630 RepositorySession repositorySession = repositorySessionFactory.createSession();
633 Date lastUpdatedTimestamp = Calendar.getInstance().getTime();
635 TimeZone timezone = TimeZone.getTimeZone( "UTC" );
636 DateFormat fmt = new SimpleDateFormat( "yyyyMMdd.HHmmss" );
637 fmt.setTimeZone( timezone );
638 ManagedRepository repoConfig = managedRepositoryAdmin.getManagedRepository( repositoryId );
640 VersionedReference ref = new VersionedReference();
641 ref.setArtifactId( artifact.getArtifactId() );
642 ref.setGroupId( artifact.getGroupId() );
643 ref.setVersion( artifact.getVersion() );
645 ManagedRepositoryContent repository = repositoryFactory.getManagedRepositoryContent( repositoryId );
647 if ( StringUtils.isNotBlank( artifact.getClassifier() ) )
649 if ( StringUtils.isBlank( artifact.getPackaging() ) )
651 throw new ArchivaRestServiceException( "You must configure a type/packaging when using classifier",
654 ArtifactReference artifactReference = new ArtifactReference();
655 artifactReference.setArtifactId( artifact.getArtifactId() );
656 artifactReference.setGroupId( artifact.getGroupId() );
657 artifactReference.setVersion( artifact.getVersion() );
658 artifactReference.setClassifier( artifact.getClassifier() );
659 artifactReference.setType( artifact.getPackaging() );
660 repository.deleteArtifact( artifactReference );
662 // TODO cleanup facet which contains classifier information
666 String path = repository.toMetadataPath( ref );
667 int index = path.lastIndexOf( '/' );
668 path = path.substring( 0, index );
669 File targetPath = new File( repoConfig.getLocation(), path );
671 if ( !targetPath.exists() )
673 throw new ContentNotFoundException(
674 artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion() );
677 // TODO: this should be in the storage mechanism so that it is all tied together
678 // delete from file system
679 repository.deleteVersion( ref );
681 File metadataFile = getMetadata( targetPath.getAbsolutePath() );
682 ArchivaRepositoryMetadata metadata = getMetadata( metadataFile );
684 updateMetadata( metadata, metadataFile, lastUpdatedTimestamp, artifact );
686 MetadataRepository metadataRepository = repositorySession.getRepository();
688 Collection<ArtifactMetadata> artifacts =
689 metadataRepository.getArtifacts( repositoryId, artifact.getGroupId(), artifact.getArtifactId(),
690 artifact.getVersion() );
692 for ( ArtifactMetadata artifactMetadata : artifacts )
694 // TODO: mismatch between artifact (snapshot) version and project (base) version here
695 if ( artifact.getVersion().equals( artifact.getVersion() ) )
697 metadataRepository.removeArtifact( artifactMetadata.getRepositoryId(),
698 artifactMetadata.getNamespace(), artifactMetadata.getProject(),
699 artifact.getVersion(), artifactMetadata.getId() );
701 // TODO: move into the metadata repository proper - need to differentiate attachment of
702 // repository metadata to an artifact
703 for ( RepositoryListener listener : listeners )
705 listener.deleteArtifact( metadataRepository, repository.getId(),
706 artifactMetadata.getNamespace(), artifactMetadata.getProject(),
707 artifactMetadata.getVersion(), artifactMetadata.getId() );
710 triggerAuditEvent( repositoryId, path, AuditEvent.REMOVE_FILE );
713 repositorySession.save();
716 catch ( ContentNotFoundException e )
718 throw new ArchivaRestServiceException( "Artifact does not exist: " + e.getMessage(), 400 );
720 catch ( RepositoryNotFoundException e )
722 throw new ArchivaRestServiceException( "Target repository cannot be found: " + e.getMessage(), 400 );
724 catch ( RepositoryException e )
726 throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 400 );
728 catch ( MetadataResolutionException e )
730 throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 400 );
732 catch ( MetadataRepositoryException e )
734 throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 400 );
736 catch ( RepositoryAdminException e )
738 throw new ArchivaRestServiceException( "RepositoryAdmin exception: " + e.getMessage(), 400 );
743 repositorySession.close();
750 * Update artifact level metadata. Creates one if metadata does not exist after artifact deletion.
754 private void updateMetadata( ArchivaRepositoryMetadata metadata, File metadataFile, Date lastUpdatedTimestamp,
756 throws RepositoryMetadataException
758 List<String> availableVersions = new ArrayList<String>();
759 String latestVersion = "";
761 if ( metadataFile.exists() )
763 if ( metadata.getAvailableVersions() != null )
765 availableVersions = metadata.getAvailableVersions();
767 if ( availableVersions.size() > 0 )
769 Collections.sort( availableVersions, VersionComparator.getInstance() );
771 if ( availableVersions.contains( artifact.getVersion() ) )
773 availableVersions.remove( availableVersions.indexOf( artifact.getVersion() ) );
775 if ( availableVersions.size() > 0 )
777 latestVersion = availableVersions.get( availableVersions.size() - 1 );
783 if ( metadata.getGroupId() == null )
785 metadata.setGroupId( artifact.getGroupId() );
787 if ( metadata.getArtifactId() == null )
789 metadata.setArtifactId( artifact.getArtifactId() );
792 if ( !VersionUtil.isSnapshot( artifact.getVersion() ) )
794 if ( metadata.getReleasedVersion() != null && metadata.getReleasedVersion().equals(
795 artifact.getVersion() ) )
797 metadata.setReleasedVersion( latestVersion );
801 metadata.setLatestVersion( latestVersion );
802 metadata.setLastUpdatedTimestamp( lastUpdatedTimestamp );
803 metadata.setAvailableVersions( availableVersions );
805 RepositoryMetadataWriter.write( metadata, metadataFile );
806 ChecksummedFile checksum = new ChecksummedFile( metadataFile );
807 checksum.fixChecksums( algorithms );
810 protected void triggerAuditEvent( String repositoryId, String filePath, String action )
812 AuditEvent auditEvent = new AuditEvent();
813 auditEvent.setAction( action );
814 auditEvent.setRepositoryId( repositoryId );
815 auditEvent.setResource( filePath );
816 AuditInformation auditInformation = getAuditInformation();
817 auditEvent.setUserId( auditInformation.getUser() == null ? "" : auditInformation.getUser().getUsername() );
818 auditEvent.setRemoteIP( auditInformation.getRemoteAddr() );
819 for ( AuditListener auditListener : getAuditListeners() )
821 auditListener.auditEvent( auditEvent );