1 package org.apache.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.admin.model.RepositoryAdminException;
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.Auditable;
29 import org.apache.archiva.checksum.ChecksumAlgorithm;
30 import org.apache.archiva.checksum.ChecksummedFile;
31 import org.apache.archiva.metadata.model.ArtifactMetadata;
32 import org.apache.archiva.metadata.repository.MetadataRepository;
33 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
34 import org.apache.archiva.metadata.repository.MetadataResolutionException;
35 import org.apache.archiva.metadata.repository.RepositorySession;
36 import org.apache.archiva.repository.events.RepositoryListener;
37 import org.apache.archiva.security.AccessDeniedException;
38 import org.apache.archiva.security.ArchivaSecurityException;
39 import org.apache.archiva.security.PrincipalNotFoundException;
40 import org.apache.archiva.security.UserRepositories;
41 import org.apache.commons.lang.StringUtils;
42 import org.apache.archiva.common.utils.VersionComparator;
43 import org.apache.archiva.common.utils.VersionUtil;
44 import org.apache.archiva.model.ArchivaRepositoryMetadata;
45 import org.apache.archiva.model.VersionedReference;
46 import org.apache.archiva.repository.ContentNotFoundException;
47 import org.apache.archiva.repository.ManagedRepositoryContent;
48 import org.apache.archiva.repository.RepositoryContentFactory;
49 import org.apache.archiva.repository.RepositoryException;
50 import org.apache.archiva.repository.RepositoryNotFoundException;
51 import org.apache.archiva.repository.metadata.MetadataTools;
52 import org.apache.archiva.repository.metadata.RepositoryMetadataException;
53 import org.apache.archiva.repository.metadata.RepositoryMetadataReader;
54 import org.apache.archiva.repository.metadata.RepositoryMetadataWriter;
55 import org.springframework.context.annotation.Scope;
56 import org.springframework.stereotype.Controller;
58 import javax.annotation.PostConstruct;
59 import javax.inject.Inject;
61 import java.text.DateFormat;
62 import java.text.SimpleDateFormat;
63 import java.util.ArrayList;
64 import java.util.Calendar;
65 import java.util.Collection;
66 import java.util.Collections;
67 import java.util.Date;
68 import java.util.List;
69 import java.util.TimeZone;
72 * Delete an artifact. Metadata will be updated if one exists, otherwise it would be created.
74 @Controller( "deleteArtifactAction" )
76 public class DeleteArtifactAction
77 extends AbstractActionSupport
78 implements Validateable, Preparable, Auditable
81 * The groupId of the artifact to be deleted.
83 private String groupId;
86 * The artifactId of the artifact to be deleted.
88 private String artifactId;
91 * The version of the artifact to be deleted.
93 private String version;
96 * The repository where the artifact is to be deleted.
98 private String repositoryId;
101 * List of managed repositories to delete from.
103 private List<String> managedRepos;
106 private UserRepositories userRepositories;
109 private RepositoryContentFactory repositoryFactory;
112 private List<RepositoryListener> listeners;
115 private ManagedRepositoryAdmin managedRepositoryAdmin;
117 private ChecksumAlgorithm[] algorithms = new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 };
120 public void initialize()
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()
198 RepositorySession repositorySession = repositorySessionFactory.createSession();
201 Date lastUpdatedTimestamp = Calendar.getInstance().getTime();
203 TimeZone timezone = TimeZone.getTimeZone( "UTC" );
204 DateFormat fmt = new SimpleDateFormat( "yyyyMMdd.HHmmss" );
205 fmt.setTimeZone( timezone );
206 ManagedRepository repoConfig = getManagedRepositoryAdmin().getManagedRepository( repositoryId );
208 VersionedReference ref = new VersionedReference();
209 ref.setArtifactId( artifactId );
210 ref.setGroupId( groupId );
211 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 MetadataRepository metadataRepository = repositorySession.getRepository();
234 Collection<ArtifactMetadata> artifacts =
235 metadataRepository.getArtifacts( repositoryId, groupId, artifactId, version );
237 for ( ArtifactMetadata artifact : artifacts )
239 // TODO: mismatch between artifact (snapshot) version and project (base) version here
240 if ( artifact.getVersion().equals( version ) )
242 metadataRepository.removeArtifact( artifact.getRepositoryId(), artifact.getNamespace(),
243 artifact.getProject(), artifact.getVersion(), artifact.getId() );
245 // TODO: move into the metadata repository proper - need to differentiate attachment of
246 // repository metadata to an artifact
247 for ( RepositoryListener listener : listeners )
249 listener.deleteArtifact( metadataRepository, repository.getId(), artifact.getNamespace(),
250 artifact.getProject(), artifact.getVersion(), artifact.getId() );
253 triggerAuditEvent( repositoryId, path, AuditEvent.REMOVE_FILE );
256 repositorySession.save();
258 catch ( ContentNotFoundException e )
260 addActionError( "Artifact does not exist: " + e.getMessage() );
263 catch ( RepositoryNotFoundException e )
265 addActionError( "Target repository cannot be found: " + e.getMessage() );
268 catch ( RepositoryException e )
270 addActionError( "Repository exception: " + e.getMessage() );
273 catch ( MetadataResolutionException e )
275 addActionError( "Repository exception: " + e.getMessage() );
278 catch ( MetadataRepositoryException e )
280 addActionError( "Repository exception: " + e.getMessage() );
283 catch ( RepositoryAdminException e )
285 addActionError( "RepositoryAdmin exception: " + e.getMessage() );
290 repositorySession.close();
293 String msg = "Artifact \'" + groupId + ":" + artifactId + ":" + version
294 + "\' was successfully deleted from repository \'" + repositoryId + "\'";
296 addActionMessage( msg );
302 private File getMetadata( String targetPath )
304 String artifactPath = targetPath.substring( 0, targetPath.lastIndexOf( File.separatorChar ) );
306 return new File( artifactPath, MetadataTools.MAVEN_METADATA );
309 private ArchivaRepositoryMetadata getMetadata( File metadataFile )
310 throws RepositoryMetadataException
312 ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
313 if ( metadataFile.exists() )
315 metadata = RepositoryMetadataReader.read( metadataFile );
321 * Update artifact level metadata. Creates one if metadata does not exist after artifact deletion.
325 private void updateMetadata( ArchivaRepositoryMetadata metadata, File metadataFile, Date lastUpdatedTimestamp )
326 throws RepositoryMetadataException
328 List<String> availableVersions = new ArrayList<String>();
329 String latestVersion = "";
331 if ( metadataFile.exists() )
333 if ( metadata.getAvailableVersions() != null )
335 availableVersions = metadata.getAvailableVersions();
337 if ( availableVersions.size() > 0 )
339 Collections.sort( availableVersions, VersionComparator.getInstance() );
341 if ( availableVersions.contains( version ) )
343 availableVersions.remove( availableVersions.indexOf( version ) );
345 if ( availableVersions.size() > 0 )
347 latestVersion = availableVersions.get( availableVersions.size() - 1 );
353 if ( metadata.getGroupId() == null )
355 metadata.setGroupId( groupId );
357 if ( metadata.getArtifactId() == null )
359 metadata.setArtifactId( artifactId );
362 if ( !VersionUtil.isSnapshot( version ) )
364 if ( metadata.getReleasedVersion() != null && metadata.getReleasedVersion().equals( version ) )
366 metadata.setReleasedVersion( latestVersion );
370 metadata.setLatestVersion( latestVersion );
371 metadata.setLastUpdatedTimestamp( lastUpdatedTimestamp );
372 metadata.setAvailableVersions( availableVersions );
374 RepositoryMetadataWriter.write( metadata, metadataFile );
375 ChecksummedFile checksum = new ChecksummedFile( metadataFile );
376 checksum.fixChecksums( algorithms );
379 public void validate()
383 if ( !userRepositories.isAuthorizedToDeleteArtifacts( getPrincipal(), repositoryId ) )
385 addActionError( "User is not authorized to delete artifacts in repository '" + repositoryId + "'." );
388 if ( ( version.length() > 0 ) && ( !VersionUtil.isVersion( version ) ) )
390 addActionError( "Invalid version." );
393 catch ( AccessDeniedException e )
395 addActionError( e.getMessage() );
397 catch ( ArchivaSecurityException e )
399 addActionError( e.getMessage() );
402 // trims all request parameter values, since the trailing/leading white-spaces are ignored during validation.
403 trimAllRequestParameterValues();
406 private List<String> getManagableRepos()
410 return userRepositories.getManagableRepositoryIds( getPrincipal() );
412 catch ( PrincipalNotFoundException e )
414 log.warn( e.getMessage(), e );
416 catch ( AccessDeniedException e )
418 log.warn( e.getMessage(), e );
419 // TODO: pass this onto the screen.
421 catch ( ArchivaSecurityException e )
423 log.warn( e.getMessage(), e );
425 return Collections.emptyList();
428 private void trimAllRequestParameterValues()
430 if ( StringUtils.isNotEmpty( groupId ) )
432 groupId = groupId.trim();
435 if ( StringUtils.isNotEmpty( artifactId ) )
437 artifactId = artifactId.trim();
440 if ( StringUtils.isNotEmpty( version ) )
442 version = version.trim();
445 if ( StringUtils.isNotEmpty( repositoryId ) )
447 repositoryId = repositoryId.trim();
451 public List<RepositoryListener> getListeners()
456 public void setRepositoryFactory( RepositoryContentFactory repositoryFactory )
458 this.repositoryFactory = repositoryFactory;
461 public ManagedRepositoryAdmin getManagedRepositoryAdmin()
463 return managedRepositoryAdmin;
466 public void setManagedRepositoryAdmin( ManagedRepositoryAdmin managedRepositoryAdmin )
468 this.managedRepositoryAdmin = managedRepositoryAdmin;