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.metadata.repository.RepositorySession;
33 import org.apache.archiva.repository.events.RepositoryListener;
34 import org.apache.commons.lang.StringUtils;
35 import org.apache.maven.archiva.common.utils.VersionComparator;
36 import org.apache.maven.archiva.common.utils.VersionUtil;
37 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
38 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
39 import org.apache.maven.archiva.model.ArchivaRepositoryMetadata;
40 import org.apache.maven.archiva.model.VersionedReference;
41 import org.apache.maven.archiva.repository.ContentNotFoundException;
42 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
43 import org.apache.maven.archiva.repository.RepositoryContentFactory;
44 import org.apache.maven.archiva.repository.RepositoryException;
45 import org.apache.maven.archiva.repository.RepositoryNotFoundException;
46 import org.apache.maven.archiva.repository.metadata.MetadataTools;
47 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataException;
48 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataReader;
49 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataWriter;
50 import org.apache.archiva.security.AccessDeniedException;
51 import org.apache.archiva.security.ArchivaSecurityException;
52 import org.apache.archiva.security.PrincipalNotFoundException;
53 import org.apache.archiva.security.UserRepositories;
54 import org.springframework.context.annotation.Scope;
55 import org.springframework.stereotype.Controller;
57 import javax.annotation.PostConstruct;
58 import javax.inject.Inject;
60 import java.text.DateFormat;
61 import java.text.SimpleDateFormat;
62 import java.util.ArrayList;
63 import java.util.Calendar;
64 import java.util.Collection;
65 import java.util.Collections;
66 import java.util.Date;
67 import java.util.List;
68 import java.util.TimeZone;
71 * Delete an artifact. Metadata will be updated if one exists, otherwise it would be created.
73 * plexus.component role="com.opensymphony.xwork2.Action" role-hint="deleteArtifactAction" instantiation-strategy="per-lookup"
75 @Controller( "deleteArtifactAction" )
77 public class DeleteArtifactAction
78 extends AbstractActionSupport
79 implements Validateable, Preparable, Auditable
82 * The groupId of the artifact to be deleted.
84 private String groupId;
87 * The artifactId of the artifact to be deleted.
89 private String artifactId;
92 * The version of the artifact to be deleted.
94 private String version;
97 * The repository where the artifact is to be deleted.
99 private String repositoryId;
102 * List of managed repositories to delete from.
104 private List<String> managedRepos;
110 private UserRepositories userRepositories;
113 * plexus.requirement role-hint="default"
116 private ArchivaConfiguration configuration;
122 private RepositoryContentFactory repositoryFactory;
125 * plexus.requirement role="org.apache.archiva.repository.events.RepositoryListener"
128 private List<RepositoryListener> listeners;
130 private ChecksumAlgorithm[] algorithms = new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 };
133 public void initialize()
138 public String getGroupId()
143 public void setGroupId( String groupId )
145 this.groupId = groupId;
148 public String getArtifactId()
153 public void setArtifactId( String artifactId )
155 this.artifactId = artifactId;
158 public String getVersion()
163 public void setVersion( String version )
165 this.version = version;
168 public String getRepositoryId()
173 public void setRepositoryId( String repositoryId )
175 this.repositoryId = repositoryId;
178 public List<String> getManagedRepos()
183 public void setManagedRepos( List<String> managedRepos )
185 this.managedRepos = managedRepos;
188 public void prepare()
190 managedRepos = getManagableRepos();
193 public String input()
200 // reset the fields so the form is clear when
201 // the action returns to the jsp page
208 public String doDelete()
210 Date lastUpdatedTimestamp = Calendar.getInstance().getTime();
212 TimeZone timezone = TimeZone.getTimeZone( "UTC" );
213 DateFormat fmt = new SimpleDateFormat( "yyyyMMdd.HHmmss" );
214 fmt.setTimeZone( timezone );
215 ManagedRepositoryConfiguration repoConfig =
216 configuration.getConfiguration().findManagedRepositoryById( repositoryId );
218 VersionedReference ref = new VersionedReference();
219 ref.setArtifactId( artifactId );
220 ref.setGroupId( groupId );
221 ref.setVersion( version );
223 RepositorySession repositorySession = repositorySessionFactory.createSession();
226 ManagedRepositoryContent repository = repositoryFactory.getManagedRepositoryContent( repositoryId );
228 String path = repository.toMetadataPath( ref );
229 int index = path.lastIndexOf( '/' );
230 path = path.substring( 0, index );
231 File targetPath = new File( repoConfig.getLocation(), path );
233 if ( !targetPath.exists() )
235 throw new ContentNotFoundException( groupId + ":" + artifactId + ":" + version );
238 // TODO: this should be in the storage mechanism so that it is all tied together
239 // delete from file system
240 repository.deleteVersion( ref );
242 File metadataFile = getMetadata( targetPath.getAbsolutePath() );
243 ArchivaRepositoryMetadata metadata = getMetadata( metadataFile );
245 updateMetadata( metadata, metadataFile, lastUpdatedTimestamp );
247 MetadataRepository metadataRepository = repositorySession.getRepository();
248 Collection<ArtifactMetadata> artifacts =
249 metadataRepository.getArtifacts( repositoryId, groupId, artifactId, version );
251 for ( ArtifactMetadata artifact : artifacts )
253 // TODO: mismatch between artifact (snapshot) version and project (base) version here
254 if ( artifact.getVersion().equals( version ) )
256 metadataRepository.removeArtifact( artifact.getRepositoryId(), artifact.getNamespace(),
257 artifact.getProject(), artifact.getVersion(), artifact.getId() );
259 // TODO: move into the metadata repository proper - need to differentiate attachment of
260 // repository metadata to an artifact
261 for ( RepositoryListener listener : listeners )
263 listener.deleteArtifact( metadataRepository, repository.getId(), artifact.getNamespace(),
264 artifact.getProject(), artifact.getVersion(), artifact.getId() );
267 triggerAuditEvent( repositoryId, path, AuditEvent.REMOVE_FILE );
270 repositorySession.save();
272 catch ( ContentNotFoundException e )
274 addActionError( "Artifact does not exist: " + e.getMessage() );
277 catch ( RepositoryNotFoundException e )
279 addActionError( "Target repository cannot be found: " + e.getMessage() );
282 catch ( RepositoryException e )
284 addActionError( "Repository exception: " + e.getMessage() );
287 catch ( MetadataResolutionException e )
289 addActionError( "Repository exception: " + e.getMessage() );
292 catch ( MetadataRepositoryException e )
294 addActionError( "Repository exception: " + e.getMessage() );
299 repositorySession.close();
302 String msg = "Artifact \'" + groupId + ":" + artifactId + ":" + version
303 + "\' was successfully deleted from repository \'" + repositoryId + "\'";
305 addActionMessage( msg );
311 private File getMetadata( String targetPath )
313 String artifactPath = targetPath.substring( 0, targetPath.lastIndexOf( File.separatorChar ) );
315 return new File( artifactPath, MetadataTools.MAVEN_METADATA );
318 private ArchivaRepositoryMetadata getMetadata( File metadataFile )
319 throws RepositoryMetadataException
321 ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
322 if ( metadataFile.exists() )
324 metadata = RepositoryMetadataReader.read( metadataFile );
330 * Update artifact level metadata. Creates one if metadata does not exist after artifact deletion.
334 private void updateMetadata( ArchivaRepositoryMetadata metadata, File metadataFile, Date lastUpdatedTimestamp )
335 throws RepositoryMetadataException
337 List<String> availableVersions = new ArrayList<String>();
338 String latestVersion = "";
340 if ( metadataFile.exists() )
342 if ( metadata.getAvailableVersions() != null )
344 availableVersions = metadata.getAvailableVersions();
346 if ( availableVersions.size() > 0 )
348 Collections.sort( availableVersions, VersionComparator.getInstance() );
350 if ( availableVersions.contains( version ) )
352 availableVersions.remove( availableVersions.indexOf( version ) );
354 if ( availableVersions.size() > 0 )
356 latestVersion = availableVersions.get( availableVersions.size() - 1 );
362 if ( metadata.getGroupId() == null )
364 metadata.setGroupId( groupId );
366 if ( metadata.getArtifactId() == null )
368 metadata.setArtifactId( artifactId );
371 if ( !VersionUtil.isSnapshot( version ) )
373 if ( metadata.getReleasedVersion() != null && metadata.getReleasedVersion().equals( version ) )
375 metadata.setReleasedVersion( latestVersion );
379 metadata.setLatestVersion( latestVersion );
380 metadata.setLastUpdatedTimestamp( lastUpdatedTimestamp );
381 metadata.setAvailableVersions( availableVersions );
383 RepositoryMetadataWriter.write( metadata, metadataFile );
384 ChecksummedFile checksum = new ChecksummedFile( metadataFile );
385 checksum.fixChecksums( algorithms );
388 public void validate()
392 if ( !userRepositories.isAuthorizedToDeleteArtifacts( getPrincipal(), repositoryId ) )
394 addActionError( "User is not authorized to delete artifacts in repository '" + repositoryId + "'." );
397 if ( ( version.length() > 0 ) && ( !VersionUtil.isVersion( version ) ) )
399 addActionError( "Invalid version." );
402 catch ( AccessDeniedException e )
404 addActionError( e.getMessage() );
406 catch ( ArchivaSecurityException e )
408 addActionError( e.getMessage() );
411 // trims all request parameter values, since the trailing/leading white-spaces are ignored during validation.
412 trimAllRequestParameterValues();
415 private List<String> getManagableRepos()
419 return userRepositories.getManagableRepositoryIds( getPrincipal() );
421 catch ( PrincipalNotFoundException e )
423 log.warn( e.getMessage(), e );
425 catch ( AccessDeniedException e )
427 log.warn( e.getMessage(), e );
428 // TODO: pass this onto the screen.
430 catch ( ArchivaSecurityException e )
432 log.warn( e.getMessage(), e );
434 return Collections.emptyList();
437 private void trimAllRequestParameterValues()
439 if ( StringUtils.isNotEmpty( groupId ) )
441 groupId = groupId.trim();
444 if ( StringUtils.isNotEmpty( artifactId ) )
446 artifactId = artifactId.trim();
449 if ( StringUtils.isNotEmpty( version ) )
451 version = version.trim();
454 if ( StringUtils.isNotEmpty( repositoryId ) )
456 repositoryId = repositoryId.trim();
460 public List<RepositoryListener> getListeners()
465 public void setRepositoryFactory( RepositoryContentFactory repositoryFactory )
467 this.repositoryFactory = repositoryFactory;
470 public void setConfiguration( ArchivaConfiguration configuration )
472 this.configuration = configuration;