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;
107 private UserRepositories userRepositories;
110 private ArchivaConfiguration configuration;
113 private RepositoryContentFactory repositoryFactory;
116 private List<RepositoryListener> listeners;
118 private ChecksumAlgorithm[] algorithms = new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 };
121 public void initialize()
126 public String getGroupId()
131 public void setGroupId( String groupId )
133 this.groupId = groupId;
136 public String getArtifactId()
141 public void setArtifactId( String artifactId )
143 this.artifactId = artifactId;
146 public String getVersion()
151 public void setVersion( String version )
153 this.version = version;
156 public String getRepositoryId()
161 public void setRepositoryId( String repositoryId )
163 this.repositoryId = repositoryId;
166 public List<String> getManagedRepos()
171 public void setManagedRepos( List<String> managedRepos )
173 this.managedRepos = managedRepos;
176 public void prepare()
178 managedRepos = getManagableRepos();
181 public String input()
188 // reset the fields so the form is clear when
189 // the action returns to the jsp page
196 public String doDelete()
198 Date lastUpdatedTimestamp = Calendar.getInstance().getTime();
200 TimeZone timezone = TimeZone.getTimeZone( "UTC" );
201 DateFormat fmt = new SimpleDateFormat( "yyyyMMdd.HHmmss" );
202 fmt.setTimeZone( timezone );
203 ManagedRepositoryConfiguration repoConfig =
204 configuration.getConfiguration().findManagedRepositoryById( repositoryId );
206 VersionedReference ref = new VersionedReference();
207 ref.setArtifactId( artifactId );
208 ref.setGroupId( groupId );
209 ref.setVersion( version );
211 RepositorySession repositorySession = repositorySessionFactory.createSession();
214 ManagedRepositoryContent repository = repositoryFactory.getManagedRepositoryContent( repositoryId );
216 String path = repository.toMetadataPath( ref );
217 int index = path.lastIndexOf( '/' );
218 path = path.substring( 0, index );
219 File targetPath = new File( repoConfig.getLocation(), path );
221 if ( !targetPath.exists() )
223 throw new ContentNotFoundException( groupId + ":" + artifactId + ":" + version );
226 // TODO: this should be in the storage mechanism so that it is all tied together
227 // delete from file system
228 repository.deleteVersion( ref );
230 File metadataFile = getMetadata( targetPath.getAbsolutePath() );
231 ArchivaRepositoryMetadata metadata = getMetadata( metadataFile );
233 updateMetadata( metadata, metadataFile, lastUpdatedTimestamp );
235 MetadataRepository metadataRepository = repositorySession.getRepository();
236 Collection<ArtifactMetadata> artifacts =
237 metadataRepository.getArtifacts( repositoryId, groupId, artifactId, version );
239 for ( ArtifactMetadata artifact : artifacts )
241 // TODO: mismatch between artifact (snapshot) version and project (base) version here
242 if ( artifact.getVersion().equals( version ) )
244 metadataRepository.removeArtifact( artifact.getRepositoryId(), artifact.getNamespace(),
245 artifact.getProject(), artifact.getVersion(), artifact.getId() );
247 // TODO: move into the metadata repository proper - need to differentiate attachment of
248 // repository metadata to an artifact
249 for ( RepositoryListener listener : listeners )
251 listener.deleteArtifact( metadataRepository, repository.getId(), artifact.getNamespace(),
252 artifact.getProject(), artifact.getVersion(), artifact.getId() );
255 triggerAuditEvent( repositoryId, path, AuditEvent.REMOVE_FILE );
258 repositorySession.save();
260 catch ( ContentNotFoundException e )
262 addActionError( "Artifact does not exist: " + e.getMessage() );
265 catch ( RepositoryNotFoundException e )
267 addActionError( "Target repository cannot be found: " + e.getMessage() );
270 catch ( RepositoryException e )
272 addActionError( "Repository exception: " + e.getMessage() );
275 catch ( MetadataResolutionException e )
277 addActionError( "Repository exception: " + e.getMessage() );
280 catch ( MetadataRepositoryException e )
282 addActionError( "Repository exception: " + e.getMessage() );
287 repositorySession.close();
290 String msg = "Artifact \'" + groupId + ":" + artifactId + ":" + version
291 + "\' was successfully deleted from repository \'" + repositoryId + "\'";
293 addActionMessage( msg );
299 private File getMetadata( String targetPath )
301 String artifactPath = targetPath.substring( 0, targetPath.lastIndexOf( File.separatorChar ) );
303 return new File( artifactPath, MetadataTools.MAVEN_METADATA );
306 private ArchivaRepositoryMetadata getMetadata( File metadataFile )
307 throws RepositoryMetadataException
309 ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
310 if ( metadataFile.exists() )
312 metadata = RepositoryMetadataReader.read( metadataFile );
318 * Update artifact level metadata. Creates one if metadata does not exist after artifact deletion.
322 private void updateMetadata( ArchivaRepositoryMetadata metadata, File metadataFile, Date lastUpdatedTimestamp )
323 throws RepositoryMetadataException
325 List<String> availableVersions = new ArrayList<String>();
326 String latestVersion = "";
328 if ( metadataFile.exists() )
330 if ( metadata.getAvailableVersions() != null )
332 availableVersions = metadata.getAvailableVersions();
334 if ( availableVersions.size() > 0 )
336 Collections.sort( availableVersions, VersionComparator.getInstance() );
338 if ( availableVersions.contains( version ) )
340 availableVersions.remove( availableVersions.indexOf( version ) );
342 if ( availableVersions.size() > 0 )
344 latestVersion = availableVersions.get( availableVersions.size() - 1 );
350 if ( metadata.getGroupId() == null )
352 metadata.setGroupId( groupId );
354 if ( metadata.getArtifactId() == null )
356 metadata.setArtifactId( artifactId );
359 if ( !VersionUtil.isSnapshot( version ) )
361 if ( metadata.getReleasedVersion() != null && metadata.getReleasedVersion().equals( version ) )
363 metadata.setReleasedVersion( latestVersion );
367 metadata.setLatestVersion( latestVersion );
368 metadata.setLastUpdatedTimestamp( lastUpdatedTimestamp );
369 metadata.setAvailableVersions( availableVersions );
371 RepositoryMetadataWriter.write( metadata, metadataFile );
372 ChecksummedFile checksum = new ChecksummedFile( metadataFile );
373 checksum.fixChecksums( algorithms );
376 public void validate()
380 if ( !userRepositories.isAuthorizedToDeleteArtifacts( getPrincipal(), repositoryId ) )
382 addActionError( "User is not authorized to delete artifacts in repository '" + repositoryId + "'." );
385 if ( ( version.length() > 0 ) && ( !VersionUtil.isVersion( version ) ) )
387 addActionError( "Invalid version." );
390 catch ( AccessDeniedException e )
392 addActionError( e.getMessage() );
394 catch ( ArchivaSecurityException e )
396 addActionError( e.getMessage() );
399 // trims all request parameter values, since the trailing/leading white-spaces are ignored during validation.
400 trimAllRequestParameterValues();
403 private List<String> getManagableRepos()
407 return userRepositories.getManagableRepositoryIds( getPrincipal() );
409 catch ( PrincipalNotFoundException e )
411 log.warn( e.getMessage(), e );
413 catch ( AccessDeniedException e )
415 log.warn( e.getMessage(), e );
416 // TODO: pass this onto the screen.
418 catch ( ArchivaSecurityException e )
420 log.warn( e.getMessage(), e );
422 return Collections.emptyList();
425 private void trimAllRequestParameterValues()
427 if ( StringUtils.isNotEmpty( groupId ) )
429 groupId = groupId.trim();
432 if ( StringUtils.isNotEmpty( artifactId ) )
434 artifactId = artifactId.trim();
437 if ( StringUtils.isNotEmpty( version ) )
439 version = version.trim();
442 if ( StringUtils.isNotEmpty( repositoryId ) )
444 repositoryId = repositoryId.trim();
448 public List<RepositoryListener> getListeners()
453 public void setRepositoryFactory( RepositoryContentFactory repositoryFactory )
455 this.repositoryFactory = repositoryFactory;
458 public void setConfiguration( ArchivaConfiguration configuration )
460 this.configuration = configuration;