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
23 import java.text.DateFormat;
24 import java.text.SimpleDateFormat;
25 import java.util.ArrayList;
26 import java.util.Calendar;
27 import java.util.Collections;
28 import java.util.Date;
29 import java.util.List;
30 import java.util.TimeZone;
32 import org.apache.archiva.checksum.ChecksumAlgorithm;
33 import org.apache.archiva.checksum.ChecksummedFile;
34 import org.apache.maven.archiva.common.utils.VersionComparator;
35 import org.apache.maven.archiva.common.utils.VersionUtil;
36 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
37 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
38 import org.apache.maven.archiva.database.ArchivaDatabaseException;
39 import org.apache.maven.archiva.database.ArtifactDAO;
40 import org.apache.maven.archiva.database.constraints.ArtifactVersionsConstraint;
41 import org.apache.maven.archiva.database.updater.DatabaseConsumers;
42 import org.apache.maven.archiva.model.ArchivaArtifact;
43 import org.apache.maven.archiva.model.ArchivaRepositoryMetadata;
44 import org.apache.maven.archiva.model.VersionedReference;
45 import org.apache.maven.archiva.repository.ContentNotFoundException;
46 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
47 import org.apache.maven.archiva.repository.RepositoryContentFactory;
48 import org.apache.maven.archiva.repository.RepositoryException;
49 import org.apache.maven.archiva.repository.RepositoryNotFoundException;
50 import org.apache.maven.archiva.repository.audit.AuditEvent;
51 import org.apache.maven.archiva.repository.audit.Auditable;
52 import org.apache.maven.archiva.repository.metadata.MetadataTools;
53 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataException;
54 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataReader;
55 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataWriter;
56 import org.apache.maven.archiva.security.AccessDeniedException;
57 import org.apache.maven.archiva.security.ArchivaSecurityException;
58 import org.apache.maven.archiva.security.PrincipalNotFoundException;
59 import org.apache.maven.archiva.security.UserRepositories;
61 import com.opensymphony.xwork2.Preparable;
62 import com.opensymphony.xwork2.Validateable;
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-hint="jdo"
116 private ArtifactDAO artifactDAO;
119 * @plexus.requirement
121 private DatabaseConsumers databaseConsumers;
123 private ChecksumAlgorithm[] algorithms = new ChecksumAlgorithm[] { ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 };
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 =
205 configuration.getConfiguration().findManagedRepositoryById( repositoryId );
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 File targetPath = new File( repoConfig.getLocation(), path.substring( 0, index ) );
218 if ( !targetPath.exists() )
220 throw new ContentNotFoundException( groupId + ":" + artifactId + ":" + version );
223 // delete from file system
224 repository.deleteVersion( ref );
226 File metadataFile = getMetadata( targetPath.getAbsolutePath() );
227 ArchivaRepositoryMetadata metadata = getMetadata( metadataFile );
229 updateMetadata( metadata, metadataFile, lastUpdatedTimestamp );
231 ArtifactVersionsConstraint constraint =
232 new ArtifactVersionsConstraint( repositoryId, groupId, artifactId, false );
233 List<ArchivaArtifact> artifacts = null;
237 artifacts = artifactDAO.queryArtifacts( constraint );
239 if ( artifacts != null )
241 for ( ArchivaArtifact artifact : artifacts )
243 if ( artifact.getVersion().equals( version ) )
245 databaseConsumers.executeCleanupConsumer( artifact );
250 catch ( ArchivaDatabaseException e )
252 addActionError( "Error occurred while cleaning up database: " + e.getMessage() );
257 "Artifact \'" + groupId + ":" + artifactId + ":" + version +
258 "\' was successfully deleted from repository \'" + repositoryId + "\'";
260 triggerAuditEvent( repositoryId, groupId + ":" + artifactId + ":" + version,
261 AuditEvent.REMOVE_FILE );
263 addActionMessage( msg );
268 catch ( ContentNotFoundException e )
270 addActionError( "Artifact does not exist: " + e.getMessage() );
273 catch ( RepositoryNotFoundException e )
275 addActionError( "Target repository cannot be found: " + e.getMessage() );
278 catch ( RepositoryException e )
280 addActionError( "Repository exception: " + e.getMessage() );
285 private File getMetadata( String targetPath )
287 String artifactPath = targetPath.substring( 0, targetPath.lastIndexOf( File.separatorChar ) );
289 return new File( artifactPath, MetadataTools.MAVEN_METADATA );
292 private ArchivaRepositoryMetadata getMetadata( File metadataFile )
293 throws RepositoryMetadataException
295 ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
296 if ( metadataFile.exists() )
298 metadata = RepositoryMetadataReader.read( metadataFile );
304 * Update artifact level metadata. Creates one if metadata does not exist after artifact deletion.
308 private void updateMetadata( ArchivaRepositoryMetadata metadata, File metadataFile, Date lastUpdatedTimestamp )
309 throws RepositoryMetadataException
311 List<String> availableVersions = new ArrayList<String>();
312 String latestVersion = "";
314 if ( metadataFile.exists() )
316 if ( metadata.getAvailableVersions() != null )
318 availableVersions = metadata.getAvailableVersions();
320 if ( availableVersions.size() > 0 )
322 Collections.sort( availableVersions, VersionComparator.getInstance() );
324 if ( availableVersions.contains( version ) )
326 availableVersions.remove( availableVersions.indexOf( version ) );
328 if ( availableVersions.size() > 0 )
330 latestVersion = availableVersions.get( availableVersions.size() - 1 );
336 if ( metadata.getGroupId() == null )
338 metadata.setGroupId( groupId );
340 if ( metadata.getArtifactId() == null )
342 metadata.setArtifactId( artifactId );
345 if ( !VersionUtil.isSnapshot( version ) )
347 if ( metadata.getReleasedVersion().equals( version ) )
349 metadata.setReleasedVersion( latestVersion );
353 metadata.setLatestVersion( latestVersion );
354 metadata.setLastUpdatedTimestamp( lastUpdatedTimestamp );
355 metadata.setAvailableVersions( availableVersions );
357 RepositoryMetadataWriter.write( metadata, metadataFile );
358 ChecksummedFile checksum = new ChecksummedFile( metadataFile );
359 checksum.fixChecksums( algorithms );
362 public void validate()
366 if ( !userRepositories.isAuthorizedToDeleteArtifacts( getPrincipal(), repositoryId ) )
368 addActionError( "User is not authorized to delete artifacts in repository '" + repositoryId + "'." );
371 if ( ( version.length() > 0 ) && ( !VersionUtil.isVersion( version ) ) )
373 addActionError( "Invalid version." );
376 catch ( AccessDeniedException e )
378 addActionError( e.getMessage() );
380 catch ( ArchivaSecurityException e )
382 addActionError( e.getMessage() );
386 private List<String> getManagableRepos()
390 return userRepositories.getManagableRepositoryIds( getPrincipal() );
392 catch ( PrincipalNotFoundException e )
394 log.warn( e.getMessage(), e );
396 catch ( AccessDeniedException e )
398 log.warn( e.getMessage(), e );
399 // TODO: pass this onto the screen.
401 catch ( ArchivaSecurityException e )
403 log.warn( e.getMessage(), e );
405 return Collections.emptyList();