1 package org.apache.maven.archiva.web.action;
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 com.opensymphony.xwork2.Preparable;
23 import com.opensymphony.xwork2.Validateable;
24 import org.apache.archiva.audit.AuditEvent;
25 import org.apache.archiva.audit.Auditable;
26 import org.apache.archiva.checksum.ChecksumAlgorithm;
27 import org.apache.archiva.checksum.ChecksummedFile;
28 import org.apache.archiva.metadata.model.ArtifactMetadata;
29 import org.apache.archiva.metadata.repository.MetadataRepository;
30 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
31 import org.apache.archiva.metadata.repository.MetadataResolutionException;
32 import org.apache.archiva.repository.events.RepositoryListener;
33 import org.apache.maven.archiva.common.utils.VersionComparator;
34 import org.apache.maven.archiva.common.utils.VersionUtil;
35 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
36 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
37 import org.apache.maven.archiva.model.ArchivaRepositoryMetadata;
38 import org.apache.maven.archiva.model.VersionedReference;
39 import org.apache.maven.archiva.repository.ContentNotFoundException;
40 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
41 import org.apache.maven.archiva.repository.RepositoryContentFactory;
42 import org.apache.maven.archiva.repository.RepositoryException;
43 import org.apache.maven.archiva.repository.RepositoryNotFoundException;
44 import org.apache.maven.archiva.repository.metadata.MetadataTools;
45 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataException;
46 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataReader;
47 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataWriter;
48 import org.apache.maven.archiva.security.AccessDeniedException;
49 import org.apache.maven.archiva.security.ArchivaSecurityException;
50 import org.apache.maven.archiva.security.PrincipalNotFoundException;
51 import org.apache.maven.archiva.security.UserRepositories;
54 import java.text.DateFormat;
55 import java.text.SimpleDateFormat;
56 import java.util.ArrayList;
57 import java.util.Calendar;
58 import java.util.Collection;
59 import java.util.Collections;
60 import java.util.Date;
61 import java.util.List;
62 import java.util.TimeZone;
65 * Delete an artifact. Metadata will be updated if one exists, otherwise it would be created.
67 * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="deleteArtifactAction" instantiation-strategy="per-lookup"
69 public class DeleteArtifactAction
70 extends PlexusActionSupport
71 implements Validateable, Preparable, Auditable
74 * The groupId of the artifact to be deleted.
76 private String groupId;
79 * The artifactId of the artifact to be deleted.
81 private String artifactId;
84 * The version of the artifact to be deleted.
86 private String version;
89 * The repository where the artifact is to be deleted.
91 private String repositoryId;
94 * List of managed repositories to delete from.
96 private List<String> managedRepos;
101 private UserRepositories userRepositories;
104 * @plexus.requirement role-hint="default"
106 private ArchivaConfiguration configuration;
109 * @plexus.requirement
111 private RepositoryContentFactory repositoryFactory;
114 * @plexus.requirement role="org.apache.archiva.repository.events.RepositoryListener"
116 private List<RepositoryListener> listeners;
118 private ChecksumAlgorithm[] algorithms = new ChecksumAlgorithm[]{ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5};
121 * @plexus.requirement
123 private MetadataRepository metadataRepository;
125 public String getGroupId()
130 public void setGroupId( String groupId )
132 this.groupId = groupId;
135 public String getArtifactId()
140 public void setArtifactId( String artifactId )
142 this.artifactId = artifactId;
145 public String getVersion()
150 public void setVersion( String version )
152 this.version = version;
155 public String getRepositoryId()
160 public void setRepositoryId( String repositoryId )
162 this.repositoryId = repositoryId;
165 public List<String> getManagedRepos()
170 public void setManagedRepos( List<String> managedRepos )
172 this.managedRepos = managedRepos;
175 public void prepare()
177 managedRepos = getManagableRepos();
180 public String input()
187 // reset the fields so the form is clear when
188 // the action returns to the jsp page
195 public String doDelete()
199 Date lastUpdatedTimestamp = Calendar.getInstance().getTime();
201 TimeZone timezone = TimeZone.getTimeZone( "UTC" );
202 DateFormat fmt = new SimpleDateFormat( "yyyyMMdd.HHmmss" );
203 fmt.setTimeZone( timezone );
204 ManagedRepositoryConfiguration repoConfig = configuration.getConfiguration().findManagedRepositoryById(
207 VersionedReference ref = new VersionedReference();
208 ref.setArtifactId( artifactId );
209 ref.setGroupId( groupId );
210 ref.setVersion( version );
212 ManagedRepositoryContent repository = repositoryFactory.getManagedRepositoryContent( repositoryId );
214 String path = repository.toMetadataPath( ref );
215 int index = path.lastIndexOf( '/' );
216 path = path.substring( 0, index );
217 File targetPath = new File( repoConfig.getLocation(), path );
219 if ( !targetPath.exists() )
221 throw new ContentNotFoundException( groupId + ":" + artifactId + ":" + version );
224 // TODO: this should be in the storage mechanism so that it is all tied together
225 // delete from file system
226 repository.deleteVersion( ref );
228 File metadataFile = getMetadata( targetPath.getAbsolutePath() );
229 ArchivaRepositoryMetadata metadata = getMetadata( metadataFile );
231 updateMetadata( metadata, metadataFile, lastUpdatedTimestamp );
233 Collection<ArtifactMetadata> artifacts = metadataRepository.getArtifacts( repositoryId, groupId, artifactId,
236 for ( ArtifactMetadata artifact : artifacts )
238 // TODO: mismatch between artifact (snapshot) version and project (base) version here
239 if ( artifact.getVersion().equals( version ) )
241 metadataRepository.removeArtifact( artifact.getRepositoryId(), artifact.getNamespace(),
242 artifact.getProject(), artifact.getVersion(), artifact.getId() );
244 // TODO: move into the metadata repository proper - need to differentiate attachment of
245 // repository metadata to an artifact
246 for ( RepositoryListener listener : listeners )
248 listener.deleteArtifact( repository.getId(), artifact.getNamespace(), artifact.getProject(),
249 artifact.getVersion(), artifact.getId() );
252 triggerAuditEvent( repositoryId, path, AuditEvent.REMOVE_FILE );
256 String msg = "Artifact \'" + groupId + ":" + artifactId + ":" + version +
257 "\' was successfully deleted from repository \'" + repositoryId + "\'";
259 addActionMessage( msg );
264 catch ( ContentNotFoundException e )
266 addActionError( "Artifact does not exist: " + e.getMessage() );
269 catch ( RepositoryNotFoundException e )
271 addActionError( "Target repository cannot be found: " + e.getMessage() );
274 catch ( RepositoryException e )
276 addActionError( "Repository exception: " + e.getMessage() );
279 catch ( MetadataResolutionException e )
281 addActionError( "Repository exception: " + e.getMessage() );
284 catch ( MetadataRepositoryException e )
286 addActionError( "Repository exception: " + e.getMessage() );
291 private File getMetadata( String targetPath )
293 String artifactPath = targetPath.substring( 0, targetPath.lastIndexOf( File.separatorChar ) );
295 return new File( artifactPath, MetadataTools.MAVEN_METADATA );
298 private ArchivaRepositoryMetadata getMetadata( File metadataFile )
299 throws RepositoryMetadataException
301 ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
302 if ( metadataFile.exists() )
304 metadata = RepositoryMetadataReader.read( metadataFile );
310 * Update artifact level metadata. Creates one if metadata does not exist after artifact deletion.
314 private void updateMetadata( ArchivaRepositoryMetadata metadata, File metadataFile, Date lastUpdatedTimestamp )
315 throws RepositoryMetadataException
317 List<String> availableVersions = new ArrayList<String>();
318 String latestVersion = "";
320 if ( metadataFile.exists() )
322 if ( metadata.getAvailableVersions() != null )
324 availableVersions = metadata.getAvailableVersions();
326 if ( availableVersions.size() > 0 )
328 Collections.sort( availableVersions, VersionComparator.getInstance() );
330 if ( availableVersions.contains( version ) )
332 availableVersions.remove( availableVersions.indexOf( version ) );
334 if ( availableVersions.size() > 0 )
336 latestVersion = availableVersions.get( availableVersions.size() - 1 );
342 if ( metadata.getGroupId() == null )
344 metadata.setGroupId( groupId );
346 if ( metadata.getArtifactId() == null )
348 metadata.setArtifactId( artifactId );
351 if ( !VersionUtil.isSnapshot( version ) )
353 if ( metadata.getReleasedVersion() != null && metadata.getReleasedVersion().equals( version ) )
355 metadata.setReleasedVersion( latestVersion );
359 metadata.setLatestVersion( latestVersion );
360 metadata.setLastUpdatedTimestamp( lastUpdatedTimestamp );
361 metadata.setAvailableVersions( availableVersions );
363 RepositoryMetadataWriter.write( metadata, metadataFile );
364 ChecksummedFile checksum = new ChecksummedFile( metadataFile );
365 checksum.fixChecksums( algorithms );
368 public void validate()
372 if ( !userRepositories.isAuthorizedToDeleteArtifacts( getPrincipal(), repositoryId ) )
374 addActionError( "User is not authorized to delete artifacts in repository '" + repositoryId + "'." );
377 if ( ( version.length() > 0 ) && ( !VersionUtil.isVersion( version ) ) )
379 addActionError( "Invalid version." );
382 catch ( AccessDeniedException e )
384 addActionError( e.getMessage() );
386 catch ( ArchivaSecurityException e )
388 addActionError( e.getMessage() );
392 private List<String> getManagableRepos()
396 return userRepositories.getManagableRepositoryIds( getPrincipal() );
398 catch ( PrincipalNotFoundException e )
400 log.warn( e.getMessage(), e );
402 catch ( AccessDeniedException e )
404 log.warn( e.getMessage(), e );
405 // TODO: pass this onto the screen.
407 catch ( ArchivaSecurityException e )
409 log.warn( e.getMessage(), e );
411 return Collections.emptyList();
414 public List<RepositoryListener> getListeners()
419 public void setRepositoryFactory( RepositoryContentFactory repositoryFactory )
421 this.repositoryFactory = repositoryFactory;
424 public void setConfiguration( ArchivaConfiguration configuration )
426 this.configuration = configuration;
429 public void setMetadataRepository( MetadataRepository metadataRepository )
431 this.metadataRepository = metadataRepository;