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.managed.ManagedRepositoryAdmin;
25 import org.apache.archiva.audit.Auditable;
26 import org.apache.archiva.checksum.ChecksumAlgorithm;
27 import org.apache.archiva.common.utils.VersionUtil;
28 import org.apache.archiva.rest.api.model.Artifact;
29 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
30 import org.apache.archiva.rest.api.services.RepositoriesService;
31 import org.apache.archiva.security.AccessDeniedException;
32 import org.apache.archiva.security.ArchivaSecurityException;
33 import org.apache.archiva.security.PrincipalNotFoundException;
34 import org.apache.archiva.security.UserRepositories;
35 import org.apache.commons.lang.StringUtils;
36 import org.apache.archiva.redback.rest.services.RedbackAuthenticationThreadLocal;
37 import org.springframework.context.annotation.Scope;
38 import org.springframework.stereotype.Controller;
40 import javax.annotation.PostConstruct;
41 import javax.inject.Inject;
42 import java.util.Collections;
43 import java.util.List;
46 * Delete an artifact. Metadata will be updated if one exists, otherwise it would be created.
48 @Controller( "deleteArtifactAction" )
50 public class DeleteArtifactAction
51 extends AbstractActionSupport
52 implements Validateable, Preparable, Auditable
55 * The groupId of the artifact to be deleted.
57 private String groupId;
60 * The artifactId of the artifact to be deleted.
62 private String artifactId;
65 * The version of the artifact to be deleted.
67 private String version;
71 * The classifier of the artifact to be deleted (optionnal)
73 private String classifier;
77 * The type of the artifact to be deleted (optionnal) (default jar)
82 * The repository where the artifact is to be deleted.
84 private String repositoryId;
87 * List of managed repositories to delete from.
89 private List<String> managedRepos;
92 private UserRepositories userRepositories;
95 private ManagedRepositoryAdmin managedRepositoryAdmin;
98 private RepositoriesService repositoriesService;
100 private ChecksumAlgorithm[] algorithms = new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 };
103 public void initialize()
108 public String getGroupId()
113 public void setGroupId( String groupId )
115 this.groupId = groupId;
118 public String getArtifactId()
123 public void setArtifactId( String artifactId )
125 this.artifactId = artifactId;
128 public String getVersion()
133 public void setVersion( String version )
135 this.version = version;
138 public String getRepositoryId()
143 public void setRepositoryId( String repositoryId )
145 this.repositoryId = repositoryId;
148 public List<String> getManagedRepos()
153 public void setManagedRepos( List<String> managedRepos )
155 this.managedRepos = managedRepos;
158 public void prepare()
160 managedRepos = getManagableRepos();
163 public String getClassifier()
168 public void setClassifier( String classifier )
170 this.classifier = classifier;
173 public String getType()
178 public void setType( String type )
183 public String input()
190 // reset the fields so the form is clear when
191 // the action returns to the jsp page
200 public String doDelete()
202 // services need a ThreadLocal variable to test karma
203 RedbackAuthenticationThreadLocal.set( getRedbackRequestInformation() );
206 Artifact artifact = new Artifact();
207 artifact.setGroupId( groupId );
208 artifact.setArtifactId( artifactId );
209 artifact.setVersion( version );
210 artifact.setClassifier( classifier );
211 artifact.setPackaging( type );
213 repositoriesService.deleteArtifact( artifact, repositoryId );
215 catch ( ArchivaRestServiceException e )
217 addActionError( "ArchivaRestServiceException exception: " + e.getMessage() );
222 RedbackAuthenticationThreadLocal.set( null );
225 StringBuilder msg = new StringBuilder( "Artifact \'" ).append( groupId ).append( ":" ).append( artifactId );
227 if ( StringUtils.isNotEmpty( classifier ) )
229 msg.append( ":" ).append( classifier );
231 msg.append( ":" ).append( version ).append( "' was successfully deleted from repository '" ).append(
232 repositoryId ).append( "'" );
233 addActionMessage( msg.toString() );
238 public void validate()
242 if ( !userRepositories.isAuthorizedToDeleteArtifacts( getPrincipal(), repositoryId ) )
244 addActionError( "User is not authorized to delete artifacts in repository '" + repositoryId + "'." );
247 if ( ( version.length() > 0 ) && ( !VersionUtil.isVersion( version ) ) )
249 addActionError( "Invalid version." );
252 catch ( AccessDeniedException e )
254 addActionError( e.getMessage() );
256 catch ( ArchivaSecurityException e )
258 addActionError( e.getMessage() );
261 // trims all request parameter values, since the trailing/leading white-spaces are ignored during validation.
262 trimAllRequestParameterValues();
265 private List<String> getManagableRepos()
269 return userRepositories.getManagableRepositoryIds( getPrincipal() );
271 catch ( PrincipalNotFoundException e )
273 log.warn( e.getMessage(), e );
275 catch ( AccessDeniedException e )
277 log.warn( e.getMessage(), e );
278 // TODO: pass this onto the screen.
280 catch ( ArchivaSecurityException e )
282 log.warn( e.getMessage(), e );
284 return Collections.emptyList();
287 private void trimAllRequestParameterValues()
289 if ( StringUtils.isNotEmpty( groupId ) )
291 groupId = groupId.trim();
294 if ( StringUtils.isNotEmpty( artifactId ) )
296 artifactId = artifactId.trim();
299 if ( StringUtils.isNotEmpty( version ) )
301 version = version.trim();
304 if ( StringUtils.isNotEmpty( repositoryId ) )
306 repositoryId = repositoryId.trim();
310 public ManagedRepositoryAdmin getManagedRepositoryAdmin()
312 return managedRepositoryAdmin;
315 public void setManagedRepositoryAdmin( ManagedRepositoryAdmin managedRepositoryAdmin )
317 this.managedRepositoryAdmin = managedRepositoryAdmin;
320 public RepositoriesService getRepositoriesService()
322 return repositoriesService;
325 public void setRepositoriesService( RepositoriesService repositoriesService )
327 this.repositoriesService = repositoriesService;