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 );
212 artifact.setContext( repositoryId );
214 repositoriesService.deleteArtifact( artifact );
216 catch ( ArchivaRestServiceException e )
218 addActionError( "ArchivaRestServiceException exception: " + e.getMessage() );
223 RedbackAuthenticationThreadLocal.set( null );
226 StringBuilder msg = new StringBuilder( "Artifact \'" ).append( groupId ).append( ":" ).append( artifactId );
228 if ( StringUtils.isNotEmpty( classifier ) )
230 msg.append( ":" ).append( classifier );
232 msg.append( ":" ).append( version ).append( "' was successfully deleted from repository '" ).append(
233 repositoryId ).append( "'" );
234 addActionMessage( msg.toString() );
239 public void validate()
243 if ( !userRepositories.isAuthorizedToDeleteArtifacts( getPrincipal(), repositoryId ) )
245 addActionError( "User is not authorized to delete artifacts in repository '" + repositoryId + "'." );
248 if ( ( version.length() > 0 ) && ( !VersionUtil.isVersion( version ) ) )
250 addActionError( "Invalid version." );
253 catch ( AccessDeniedException e )
255 addActionError( e.getMessage() );
257 catch ( ArchivaSecurityException e )
259 addActionError( e.getMessage() );
262 // trims all request parameter values, since the trailing/leading white-spaces are ignored during validation.
263 trimAllRequestParameterValues();
266 private List<String> getManagableRepos()
270 return userRepositories.getManagableRepositoryIds( getPrincipal() );
272 catch ( PrincipalNotFoundException e )
274 log.warn( e.getMessage(), e );
276 catch ( AccessDeniedException e )
278 log.warn( e.getMessage(), e );
279 // TODO: pass this onto the screen.
281 catch ( ArchivaSecurityException e )
283 log.warn( e.getMessage(), e );
285 return Collections.emptyList();
288 private void trimAllRequestParameterValues()
290 if ( StringUtils.isNotEmpty( groupId ) )
292 groupId = groupId.trim();
295 if ( StringUtils.isNotEmpty( artifactId ) )
297 artifactId = artifactId.trim();
300 if ( StringUtils.isNotEmpty( version ) )
302 version = version.trim();
305 if ( StringUtils.isNotEmpty( repositoryId ) )
307 repositoryId = repositoryId.trim();
311 public ManagedRepositoryAdmin getManagedRepositoryAdmin()
313 return managedRepositoryAdmin;
316 public void setManagedRepositoryAdmin( ManagedRepositoryAdmin managedRepositoryAdmin )
318 this.managedRepositoryAdmin = managedRepositoryAdmin;
321 public RepositoriesService getRepositoriesService()
323 return repositoriesService;
326 public void setRepositoriesService( RepositoriesService repositoriesService )
328 this.repositoriesService = repositoriesService;