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.metadata.repository.storage.maven2.MavenArtifactFacet;
41 import org.apache.archiva.model.ArchivaRepositoryMetadata;
42 import org.apache.archiva.model.ArtifactReference;
43 import org.apache.archiva.model.VersionedReference;
44 import org.apache.archiva.redback.authentication.AuthenticationResult;
45 import org.apache.archiva.redback.authorization.AuthorizationException;
46 import org.apache.archiva.redback.components.taskqueue.TaskQueueException;
47 import org.apache.archiva.redback.system.DefaultSecuritySession;
48 import org.apache.archiva.redback.system.SecuritySession;
49 import org.apache.archiva.redback.system.SecuritySystem;
50 import org.apache.archiva.redback.users.User;
51 import org.apache.archiva.redback.users.UserNotFoundException;
52 import org.apache.archiva.repository.ContentNotFoundException;
53 import org.apache.archiva.repository.ManagedRepositoryContent;
54 import org.apache.archiva.repository.RepositoryContentFactory;
55 import org.apache.archiva.repository.RepositoryException;
56 import org.apache.archiva.repository.RepositoryNotFoundException;
57 import org.apache.archiva.repository.events.RepositoryListener;
58 import org.apache.archiva.repository.metadata.MetadataTools;
59 import org.apache.archiva.repository.metadata.RepositoryMetadataException;
60 import org.apache.archiva.repository.metadata.RepositoryMetadataWriter;
61 import org.apache.archiva.repository.scanner.RepositoryScanStatistics;
62 import org.apache.archiva.repository.scanner.RepositoryScanner;
63 import org.apache.archiva.repository.scanner.RepositoryScannerException;
64 import org.apache.archiva.rest.api.model.Artifact;
65 import org.apache.archiva.rest.api.model.ArtifactTransferRequest;
66 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
67 import org.apache.archiva.rest.api.services.RepositoriesService;
68 import org.apache.archiva.scheduler.ArchivaTaskScheduler;
69 import org.apache.archiva.scheduler.indexing.ArchivaIndexingTaskExecutor;
70 import org.apache.archiva.scheduler.indexing.ArtifactIndexingTask;
71 import org.apache.archiva.scheduler.indexing.DownloadRemoteIndexException;
72 import org.apache.archiva.scheduler.indexing.DownloadRemoteIndexScheduler;
73 import org.apache.archiva.scheduler.repository.RepositoryArchivaTaskScheduler;
74 import org.apache.archiva.scheduler.repository.RepositoryTask;
75 import org.apache.archiva.security.ArchivaSecurityException;
76 import org.apache.archiva.security.common.ArchivaRoleConstants;
77 import org.apache.archiva.xml.XMLException;
78 import org.apache.commons.io.FilenameUtils;
79 import org.apache.commons.io.IOUtils;
80 import org.apache.commons.lang.StringUtils;
81 import org.apache.maven.index.context.IndexingContext;
82 import org.slf4j.Logger;
83 import org.slf4j.LoggerFactory;
84 import org.springframework.stereotype.Service;
86 import javax.inject.Inject;
87 import javax.inject.Named;
88 import javax.ws.rs.core.Response;
90 import java.io.FileInputStream;
91 import java.io.FileOutputStream;
92 import java.io.IOException;
93 import java.text.DateFormat;
94 import java.text.SimpleDateFormat;
95 import java.util.ArrayList;
96 import java.util.Calendar;
97 import java.util.Collection;
98 import java.util.Collections;
99 import java.util.Date;
100 import java.util.List;
101 import java.util.TimeZone;
104 * @author Olivier Lamy
107 @Service( "repositoriesService#rest" )
108 public class DefaultRepositoriesService
109 extends AbstractRestService
110 implements RepositoriesService
112 private Logger log = LoggerFactory.getLogger( getClass() );
115 @Named( value = "archivaTaskScheduler#repository" )
116 private RepositoryArchivaTaskScheduler repositoryTaskScheduler;
119 @Named( value = "taskExecutor#indexing" )
120 private ArchivaIndexingTaskExecutor archivaIndexingTaskExecutor;
123 private ManagedRepositoryAdmin managedRepositoryAdmin;
126 private PlexusSisuBridge plexusSisuBridge;
129 private MavenIndexerUtils mavenIndexerUtils;
132 private SecuritySystem securitySystem;
135 private RepositoryContentFactory repositoryFactory;
138 @Named( value = "archivaTaskScheduler#repository" )
139 private ArchivaTaskScheduler scheduler;
142 private DownloadRemoteIndexScheduler downloadRemoteIndexScheduler;
145 @Named( value = "repositorySessionFactory" )
146 protected RepositorySessionFactory repositorySessionFactory;
149 protected List<RepositoryListener> listeners = new ArrayList<RepositoryListener>();
152 private RepositoryScanner repoScanner;
154 private ChecksumAlgorithm[] algorithms = new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 };
156 public Boolean scanRepository( String repositoryId, boolean fullScan )
158 if ( repositoryTaskScheduler.isProcessingRepositoryTask( repositoryId ) )
160 log.info( "scanning of repository with id {} already scheduled", repositoryId );
161 return Boolean.FALSE;
163 RepositoryTask task = new RepositoryTask();
164 task.setRepositoryId( repositoryId );
165 task.setScanAll( fullScan );
168 repositoryTaskScheduler.queueTask( task );
170 catch ( TaskQueueException e )
172 log.error( "failed to schedule scanning of repo with id {}", repositoryId, e );
178 public Boolean alreadyScanning( String repositoryId )
180 return repositoryTaskScheduler.isProcessingRepositoryTask( repositoryId );
183 public Boolean removeScanningTaskFromQueue( String repositoryId )
185 RepositoryTask task = new RepositoryTask();
186 task.setRepositoryId( repositoryId );
189 return repositoryTaskScheduler.unQueueTask( task );
191 catch ( TaskQueueException e )
193 log.error( "failed to unschedule scanning of repo with id {}", repositoryId, e );
198 public Boolean scanRepositoryNow( String repositoryId, boolean fullScan )
199 throws ArchivaRestServiceException
204 ManagedRepository repository = managedRepositoryAdmin.getManagedRepository( repositoryId );
206 IndexingContext context = managedRepositoryAdmin.createIndexContext( repository );
208 ArtifactIndexingTask task =
209 new ArtifactIndexingTask( repository, null, ArtifactIndexingTask.Action.FINISH, context );
211 task.setExecuteOnEntireRepo( true );
212 task.setOnlyUpdate( !fullScan );
214 archivaIndexingTaskExecutor.executeTask( task );
217 catch ( Exception e )
219 log.error( e.getMessage(), e );
220 throw new ArchivaRestServiceException( e.getMessage(), e );
224 public Boolean scheduleDownloadRemoteIndex( String repositoryId, boolean now, boolean fullDownload )
225 throws ArchivaRestServiceException
229 downloadRemoteIndexScheduler.scheduleDownloadRemote( repositoryId, now, fullDownload );
231 catch ( DownloadRemoteIndexException e )
233 log.error( e.getMessage(), e );
234 throw new ArchivaRestServiceException( e.getMessage(), e );
239 public Boolean copyArtifact( ArtifactTransferRequest artifactTransferRequest )
240 throws ArchivaRestServiceException
243 String userName = getAuditInformation().getUser().getUsername();
244 if ( StringUtils.isBlank( userName ) )
246 throw new ArchivaRestServiceException( "copyArtifact call: userName not found", null );
249 if ( StringUtils.isBlank( artifactTransferRequest.getRepositoryId() ) )
251 throw new ArchivaRestServiceException( "copyArtifact call: sourceRepositoryId cannot be null", null );
254 if ( StringUtils.isBlank( artifactTransferRequest.getTargetRepositoryId() ) )
256 throw new ArchivaRestServiceException( "copyArtifact call: targetRepositoryId cannot be null", null );
259 ManagedRepository source = null;
262 source = managedRepositoryAdmin.getManagedRepository( artifactTransferRequest.getRepositoryId() );
264 catch ( RepositoryAdminException e )
266 throw new ArchivaRestServiceException( e.getMessage(), e );
269 if ( source == null )
271 throw new ArchivaRestServiceException(
272 "cannot find repository with id " + artifactTransferRequest.getRepositoryId(), null );
275 ManagedRepository target = null;
278 target = managedRepositoryAdmin.getManagedRepository( artifactTransferRequest.getTargetRepositoryId() );
280 catch ( RepositoryAdminException e )
282 throw new ArchivaRestServiceException( e.getMessage(), e );
285 if ( target == null )
287 throw new ArchivaRestServiceException(
288 "cannot find repository with id " + artifactTransferRequest.getTargetRepositoryId(), null );
291 if ( StringUtils.isBlank( artifactTransferRequest.getGroupId() ) )
293 throw new ArchivaRestServiceException( "groupId is mandatory", null );
296 if ( StringUtils.isBlank( artifactTransferRequest.getArtifactId() ) )
298 throw new ArchivaRestServiceException( "artifactId is mandatory", null );
301 if ( StringUtils.isBlank( artifactTransferRequest.getVersion() ) )
303 throw new ArchivaRestServiceException( "version is mandatory", null );
306 if ( VersionUtil.isSnapshot( artifactTransferRequest.getVersion() ) )
308 throw new ArchivaRestServiceException( "copy of SNAPSHOT not supported", null );
311 // end check parameters
316 user = securitySystem.getUserManager().findUser( userName );
318 catch ( UserNotFoundException e )
320 throw new ArchivaRestServiceException( "user " + userName + " not found", null );
323 // check karma on source : read
324 AuthenticationResult authn = new AuthenticationResult( true, userName, null );
325 SecuritySession securitySession = new DefaultSecuritySession( authn, user );
329 securitySystem.isAuthorized( securitySession, ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS,
330 artifactTransferRequest.getRepositoryId() );
333 throw new ArchivaRestServiceException(
334 "not authorized to access repo:" + artifactTransferRequest.getRepositoryId(), null );
337 catch ( AuthorizationException e )
339 log.error( "error reading permission: " + e.getMessage(), e );
340 throw new ArchivaRestServiceException( e.getMessage(), e );
343 // check karma on target: write
347 securitySystem.isAuthorized( securitySession, ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD,
348 artifactTransferRequest.getTargetRepositoryId() );
351 throw new ArchivaRestServiceException(
352 "not authorized to write to repo:" + artifactTransferRequest.getTargetRepositoryId(), null );
355 catch ( AuthorizationException e )
357 log.error( "error reading permission: " + e.getMessage(), e );
358 throw new ArchivaRestServiceException( e.getMessage(), e );
361 // sounds good we can continue !
363 ArtifactReference artifactReference = new ArtifactReference();
364 artifactReference.setArtifactId( artifactTransferRequest.getArtifactId() );
365 artifactReference.setGroupId( artifactTransferRequest.getGroupId() );
366 artifactReference.setVersion( artifactTransferRequest.getVersion() );
367 artifactReference.setClassifier( artifactTransferRequest.getClassifier() );
368 String packaging = StringUtils.trim( artifactTransferRequest.getPackaging() );
369 artifactReference.setType( StringUtils.isEmpty( packaging ) ? "jar" : packaging );
374 ManagedRepositoryContent sourceRepository =
375 repositoryFactory.getManagedRepositoryContent( artifactTransferRequest.getRepositoryId() );
377 String artifactSourcePath = sourceRepository.toPath( artifactReference );
379 if ( StringUtils.isEmpty( artifactSourcePath ) )
381 log.error( "cannot find artifact " + artifactTransferRequest.toString() );
382 throw new ArchivaRestServiceException( "cannot find artifact " + artifactTransferRequest.toString(),
386 File artifactFile = new File( source.getLocation(), artifactSourcePath );
388 if ( !artifactFile.exists() )
390 log.error( "cannot find artifact " + artifactTransferRequest.toString() );
391 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", null );
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(), e );
477 catch ( RepositoryAdminException e )
479 log.error( "RepositoryAdminException: " + e.getMessage(), e );
480 throw new ArchivaRestServiceException( e.getMessage(), e );
482 catch ( IOException e )
484 log.error( "IOException: " + e.getMessage(), e );
485 throw new ArchivaRestServiceException( e.getMessage(), e );
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 );
548 IOUtils.closeQuietly( out );
549 IOUtils.closeQuietly( input );
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 )
625 throws ArchivaRestServiceException
628 String repositoryId = artifact.getContext();
629 if ( StringUtils.isEmpty( repositoryId ) )
631 throw new ArchivaRestServiceException( "repositoryId cannot be null", 400, null );
634 if ( !isAuthorizedToDeleteArtifacts( repositoryId ) )
636 throw new ArchivaRestServiceException( "not authorized to delete artifacts", 403, null );
639 if ( artifact == null )
641 throw new ArchivaRestServiceException( "artifact cannot be null", 400, null );
644 if ( StringUtils.isEmpty( artifact.getGroupId() ) )
646 throw new ArchivaRestServiceException( "artifact.groupId cannot be null", 400, null );
649 if ( StringUtils.isEmpty( artifact.getArtifactId() ) )
651 throw new ArchivaRestServiceException( "artifact.artifactId cannot be null", 400, null );
654 // TODO more control on artifact fields
656 RepositorySession repositorySession = repositorySessionFactory.createSession();
659 Date lastUpdatedTimestamp = Calendar.getInstance().getTime();
661 TimeZone timezone = TimeZone.getTimeZone( "UTC" );
662 DateFormat fmt = new SimpleDateFormat( "yyyyMMdd.HHmmss" );
663 fmt.setTimeZone( timezone );
664 ManagedRepository repoConfig = managedRepositoryAdmin.getManagedRepository( repositoryId );
666 VersionedReference ref = new VersionedReference();
667 ref.setArtifactId( artifact.getArtifactId() );
668 ref.setGroupId( artifact.getGroupId() );
669 ref.setVersion( artifact.getVersion() );
671 ManagedRepositoryContent repository = repositoryFactory.getManagedRepositoryContent( repositoryId );
673 MetadataRepository metadataRepository = repositorySession.getRepository();
675 String path = repository.toMetadataPath( ref );
677 if ( StringUtils.isNotBlank( artifact.getClassifier() ) )
679 if ( StringUtils.isBlank( artifact.getPackaging() ) )
681 throw new ArchivaRestServiceException( "You must configure a type/packaging when using classifier",
684 ArtifactReference artifactReference = new ArtifactReference();
685 artifactReference.setArtifactId( artifact.getArtifactId() );
686 artifactReference.setGroupId( artifact.getGroupId() );
687 artifactReference.setVersion( artifact.getVersion() );
688 artifactReference.setClassifier( artifact.getClassifier() );
689 artifactReference.setType( artifact.getPackaging() );
690 repository.deleteArtifact( artifactReference );
696 int index = path.lastIndexOf( '/' );
697 path = path.substring( 0, index );
698 File targetPath = new File( repoConfig.getLocation(), path );
700 if ( !targetPath.exists() )
702 throw new ContentNotFoundException(
703 artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion() );
706 // TODO: this should be in the storage mechanism so that it is all tied together
707 // delete from file system
708 repository.deleteVersion( ref );
710 File metadataFile = getMetadata( targetPath.getAbsolutePath() );
711 ArchivaRepositoryMetadata metadata = getMetadata( metadataFile );
713 updateMetadata( metadata, metadataFile, lastUpdatedTimestamp, artifact );
715 Collection<ArtifactMetadata> artifacts =
716 metadataRepository.getArtifacts( repositoryId, artifact.getGroupId(), artifact.getArtifactId(),
717 artifact.getVersion() );
719 for ( ArtifactMetadata artifactMetadata : artifacts )
722 // TODO: mismatch between artifact (snapshot) version and project (base) version here
723 if ( artifactMetadata.getVersion().equals( artifact.getVersion() ) )
725 if ( StringUtils.isNotBlank( artifact.getClassifier() ) )
727 if ( StringUtils.isBlank( artifact.getPackaging() ) )
729 throw new ArchivaRestServiceException(
730 "You must configure a type/packaging when using classifier", 400, null );
732 // cleanup facet which contains classifier information
733 MavenArtifactFacet mavenArtifactFacet =
734 (MavenArtifactFacet) artifactMetadata.getFacet( MavenArtifactFacet.FACET_ID );
736 if ( StringUtils.equals( artifact.getClassifier(), mavenArtifactFacet.getClassifier() ) )
738 artifactMetadata.removeFacet( MavenArtifactFacet.FACET_ID );
739 String groupId = artifact.getGroupId(), artifactId = artifact.getArtifactId(), version =
740 artifact.getVersion();
741 //metadataRepository.updateArtifact( repositoryId, groupId, artifactId, version,
742 // artifactMetadata );
743 // String repositoryId, String namespace, String project, String version, String projectId, MetadataFacet metadataFacet
744 MavenArtifactFacet mavenArtifactFacetToCompare = new MavenArtifactFacet();
745 mavenArtifactFacetToCompare.setClassifier( artifact.getClassifier() );
746 metadataRepository.removeArtifact( repositoryId, groupId, artifactId, version,
747 mavenArtifactFacetToCompare );
748 metadataRepository.save();
754 metadataRepository.removeArtifact( artifactMetadata.getRepositoryId(),
755 artifactMetadata.getNamespace(),
756 artifactMetadata.getProject(), artifact.getVersion(),
757 artifactMetadata.getId() );
759 // TODO: move into the metadata repository proper - need to differentiate attachment of
760 // repository metadata to an artifact
761 for ( RepositoryListener listener : listeners )
763 listener.deleteArtifact( metadataRepository, repository.getId(),
764 artifactMetadata.getNamespace(), artifactMetadata.getProject(),
765 artifactMetadata.getVersion(), artifactMetadata.getId() );
768 triggerAuditEvent( repositoryId, path, AuditEvent.REMOVE_FILE );
774 catch ( ContentNotFoundException e )
776 throw new ArchivaRestServiceException( "Artifact does not exist: " + e.getMessage(), 400, e );
778 catch ( RepositoryNotFoundException e )
780 throw new ArchivaRestServiceException( "Target repository cannot be found: " + e.getMessage(), 400, e );
782 catch ( RepositoryException e )
784 throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
786 catch ( MetadataResolutionException e )
788 throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
790 catch ( MetadataRepositoryException e )
792 throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
794 catch ( RepositoryAdminException e )
796 throw new ArchivaRestServiceException( "RepositoryAdmin exception: " + e.getMessage(), 500, e );
800 repositorySession.save();
802 repositorySession.close();
807 public Boolean deleteGroupId( String groupId, String repositoryId )
808 throws ArchivaRestServiceException
810 if ( StringUtils.isEmpty( repositoryId ) )
812 throw new ArchivaRestServiceException( "repositoryId cannot be null", 400, null );
815 if ( !isAuthorizedToDeleteArtifacts( repositoryId ) )
817 throw new ArchivaRestServiceException( "not authorized to delete artifacts", 403, null );
820 if ( StringUtils.isEmpty( groupId ) )
822 throw new ArchivaRestServiceException( "artifact.groupId cannot be null", 400, null );
827 ManagedRepositoryContent repository = repositoryFactory.getManagedRepositoryContent( repositoryId );
829 repository.deleteGroupId( groupId );
832 catch ( RepositoryException e )
834 log.error( e.getMessage(), e );
835 throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
840 public Boolean isAuthorizedToDeleteArtifacts( String repoId )
841 throws ArchivaRestServiceException
844 getAuditInformation().getUser() == null ? "guest" : getAuditInformation().getUser().getUsername();
848 boolean res = userRepositories.isAuthorizedToDeleteArtifacts( userName, repoId );
851 catch ( ArchivaSecurityException e )
853 throw new ArchivaRestServiceException( e.getMessage(),
854 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
858 public RepositoryScanStatistics scanRepositoryDirectoriesNow( String repositoryId )
859 throws ArchivaRestServiceException
861 long sinceWhen = RepositoryScanner.FRESH_SCAN;
864 return repoScanner.scan( getManagedRepositoryAdmin().getManagedRepository( repositoryId ), sinceWhen );
866 catch ( RepositoryScannerException e )
868 log.error( e.getMessage(), e );
869 throw new ArchivaRestServiceException( "RepositoryScannerException exception: " + e.getMessage(), 500, e );
871 catch ( RepositoryAdminException e )
873 log.error( e.getMessage(), e );
874 throw new ArchivaRestServiceException( "RepositoryScannerException exception: " + e.getMessage(), 500, e );
879 * Update artifact level metadata. Creates one if metadata does not exist after artifact deletion.
883 private void updateMetadata( ArchivaRepositoryMetadata metadata, File metadataFile, Date lastUpdatedTimestamp,
885 throws RepositoryMetadataException
887 List<String> availableVersions = new ArrayList<String>();
888 String latestVersion = "";
890 if ( metadataFile.exists() )
892 if ( metadata.getAvailableVersions() != null )
894 availableVersions = metadata.getAvailableVersions();
896 if ( availableVersions.size() > 0 )
898 Collections.sort( availableVersions, VersionComparator.getInstance() );
900 if ( availableVersions.contains( artifact.getVersion() ) )
902 availableVersions.remove( availableVersions.indexOf( artifact.getVersion() ) );
904 if ( availableVersions.size() > 0 )
906 latestVersion = availableVersions.get( availableVersions.size() - 1 );
912 if ( metadata.getGroupId() == null )
914 metadata.setGroupId( artifact.getGroupId() );
916 if ( metadata.getArtifactId() == null )
918 metadata.setArtifactId( artifact.getArtifactId() );
921 if ( !VersionUtil.isSnapshot( artifact.getVersion() ) )
923 if ( metadata.getReleasedVersion() != null && metadata.getReleasedVersion().equals(
924 artifact.getVersion() ) )
926 metadata.setReleasedVersion( latestVersion );
930 metadata.setLatestVersion( latestVersion );
931 metadata.setLastUpdatedTimestamp( lastUpdatedTimestamp );
932 metadata.setAvailableVersions( availableVersions );
934 RepositoryMetadataWriter.write( metadata, metadataFile );
935 ChecksummedFile checksum = new ChecksummedFile( metadataFile );
936 checksum.fixChecksums( algorithms );
939 public ManagedRepositoryAdmin getManagedRepositoryAdmin()
941 return managedRepositoryAdmin;
944 public void setManagedRepositoryAdmin( ManagedRepositoryAdmin managedRepositoryAdmin )
946 this.managedRepositoryAdmin = managedRepositoryAdmin;
949 public RepositoryContentFactory getRepositoryFactory()
951 return repositoryFactory;
954 public void setRepositoryFactory( RepositoryContentFactory repositoryFactory )
956 this.repositoryFactory = repositoryFactory;
959 public RepositorySessionFactory getRepositorySessionFactory()
961 return repositorySessionFactory;
964 public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
966 this.repositorySessionFactory = repositorySessionFactory;
969 public List<RepositoryListener> getListeners()
974 public void setListeners( List<RepositoryListener> listeners )
976 this.listeners = listeners;
979 public ArchivaAdministration getArchivaAdministration()
981 return archivaAdministration;
984 public void setArchivaAdministration( ArchivaAdministration archivaAdministration )
986 this.archivaAdministration = archivaAdministration;