1 package org.apache.archiva.web.action.admin.scanning;
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.admin.ArchivaAdministration;
26 import org.apache.archiva.admin.model.beans.FileType;
27 import org.apache.archiva.admin.repository.admin.FiletypeToMapClosure;
28 import org.apache.archiva.audit.Auditable;
29 import org.apache.archiva.repository.scanner.RepositoryContentConsumers;
30 import org.apache.archiva.security.common.ArchivaRoleConstants;
31 import org.apache.commons.collections.CollectionUtils;
32 import org.apache.commons.lang.StringUtils;
33 import org.apache.archiva.web.action.AbstractActionSupport;
34 import org.codehaus.plexus.redback.rbac.Resource;
35 import org.codehaus.redback.integration.interceptor.SecureAction;
36 import org.codehaus.redback.integration.interceptor.SecureActionBundle;
37 import org.codehaus.redback.integration.interceptor.SecureActionException;
38 import org.springframework.context.annotation.Scope;
39 import org.springframework.stereotype.Controller;
41 import javax.inject.Inject;
42 import java.util.ArrayList;
43 import java.util.Collections;
44 import java.util.List;
48 * RepositoryScanningAction
52 @Controller( "repositoryScanningAction" )
54 public class RepositoryScanningAction
55 extends AbstractActionSupport
56 implements Preparable, Validateable, SecureAction, Auditable
60 private RepositoryContentConsumers repoconsumerUtil;
63 private ArchivaAdministration archivaAdministration;
65 private Map<String, FileType> fileTypeMap;
67 private List<String> fileTypeIds;
70 * List of {@link AdminRepositoryConsumer} objects for consumers of known content.
72 private List<AdminRepositoryConsumer> knownContentConsumers;
75 * List of enabled {@link AdminRepositoryConsumer} objects for consumers of known content.
77 private List<String> enabledKnownContentConsumers;
80 * List of {@link AdminRepositoryConsumer} objects for consumers of invalid/unknown content.
82 private List<AdminRepositoryConsumer> invalidContentConsumers;
85 * List of enabled {@link AdminRepositoryConsumer} objects for consumers of invalid/unknown content.
87 private List<String> enabledInvalidContentConsumers;
89 private String pattern;
91 private String fileTypeId;
93 public void addActionError( String anErrorMessage )
95 super.addActionError( anErrorMessage );
96 log.warn( "[ActionError] {}", anErrorMessage );
99 public void addActionMessage( String aMessage )
101 super.addActionMessage( aMessage );
102 log.info( "[ActionMessage] {}", aMessage );
105 public String addFiletypePattern()
107 log.info( "Add New File Type Pattern [{}:{}]", getFileTypeId(), getPattern() );
109 if ( !isValidFiletypeCommand() )
116 getArchivaAdministration().addFileTypePattern( getFileTypeId(), getPattern(), getAuditInformation() );
118 catch ( RepositoryAdminException e )
120 addActionError( e.getMessage() );
126 public String removeFiletypePattern()
127 throws RepositoryAdminException
129 log.info( "Remove File Type Pattern [{}:{}]", getFileTypeId(), getPattern() );
131 if ( !isValidFiletypeCommand() )
138 getArchivaAdministration().removeFileTypePattern( getFileTypeId(), getPattern(), getAuditInformation() );
140 catch ( RepositoryAdminException e )
142 addActionError( "error adding file type pattern " + e.getMessage() );
149 public String getFileTypeId()
154 public List<String> getFileTypeIds()
159 public Map<String, FileType> getFileTypeMap()
164 public List<AdminRepositoryConsumer> getInvalidContentConsumers()
166 return invalidContentConsumers;
169 public List<AdminRepositoryConsumer> getKnownContentConsumers()
171 return knownContentConsumers;
174 public String getPattern()
179 public SecureActionBundle getSecureActionBundle()
180 throws SecureActionException
182 SecureActionBundle bundle = new SecureActionBundle();
184 bundle.setRequiresAuthentication( true );
185 bundle.addRequiredAuthorization( ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION, Resource.GLOBAL );
190 public void prepare()
195 FiletypeToMapClosure filetypeToMapClosure = new FiletypeToMapClosure();
197 CollectionUtils.forAllDo( archivaAdministration.getFileTypes(), filetypeToMapClosure );
198 fileTypeMap = filetypeToMapClosure.getMap();
200 AddAdminRepoConsumerClosure addAdminRepoConsumer =
201 new AddAdminRepoConsumerClosure( archivaAdministration.getKnownContentConsumers() );
202 CollectionUtils.forAllDo( repoconsumerUtil.getAvailableKnownConsumers(), addAdminRepoConsumer );
203 this.knownContentConsumers = addAdminRepoConsumer.getList();
204 Collections.sort( knownContentConsumers, AdminRepositoryConsumerComparator.getInstance() );
206 addAdminRepoConsumer = new AddAdminRepoConsumerClosure( archivaAdministration.getInvalidContentConsumers() );
207 CollectionUtils.forAllDo( repoconsumerUtil.getAvailableInvalidConsumers(), addAdminRepoConsumer );
208 this.invalidContentConsumers = addAdminRepoConsumer.getList();
209 Collections.sort( invalidContentConsumers, AdminRepositoryConsumerComparator.getInstance() );
211 fileTypeIds = new ArrayList<String>();
212 fileTypeIds.addAll( fileTypeMap.keySet() );
213 Collections.sort( fileTypeIds );
216 public void setFileTypeId( String fileTypeId )
218 this.fileTypeId = fileTypeId;
221 public void setPattern( String pattern )
223 this.pattern = pattern;
226 public String updateInvalidConsumers()
231 List<String> oldConsumers = getArchivaAdministration().getInvalidContentConsumers();
233 if ( enabledInvalidContentConsumers != null )
235 for ( String oldConsumer : oldConsumers )
237 if ( !enabledInvalidContentConsumers.contains( oldConsumer ) )
239 getArchivaAdministration().removeInvalidContentConsumer( oldConsumer, getAuditInformation() );
242 for ( String enabledKnowContentConsumer : enabledInvalidContentConsumers )
244 getArchivaAdministration().addInvalidContentConsumer( enabledKnowContentConsumer,
245 getAuditInformation() );
250 for ( String oldConsumer : oldConsumers )
252 getArchivaAdministration().removeInvalidContentConsumer( oldConsumer, getAuditInformation() );
256 catch ( RepositoryAdminException e )
258 log.error( e.getMessage(), e );
259 addActionError( "Error update invalidContentConsumers " + e.getMessage() );
262 addActionMessage( "Update Invalid Consumers" );
267 public String updateKnownConsumers()
272 List<String> oldConsumers = getArchivaAdministration().getKnownContentConsumers();
274 if ( enabledKnownContentConsumers != null )
276 for ( String oldConsumer : oldConsumers )
278 if ( !enabledKnownContentConsumers.contains( oldConsumer ) )
280 getArchivaAdministration().removeKnownContentConsumer( oldConsumer, getAuditInformation() );
283 for ( String enabledKnowContentConsumer : enabledKnownContentConsumers )
285 getArchivaAdministration().addKnownContentConsumer( enabledKnowContentConsumer,
286 getAuditInformation() );
291 for ( String oldConsumer : oldConsumers )
293 getArchivaAdministration().removeKnownContentConsumer( oldConsumer, getAuditInformation() );
297 catch ( RepositoryAdminException e )
299 log.error( e.getMessage(), e );
300 addActionError( "Error update knowContentConsumers " + e.getMessage() );
303 addActionMessage( "Update Known Consumers" );
308 private boolean isValidFiletypeCommand()
310 if ( StringUtils.isBlank( getFileTypeId() ) )
312 addActionError( "Unable to process blank filetype id." );
315 if ( StringUtils.isBlank( getPattern() ) )
317 addActionError( "Unable to process blank pattern." );
320 return !hasActionErrors();
324 public List<String> getEnabledInvalidContentConsumers()
326 return enabledInvalidContentConsumers;
329 public void setEnabledInvalidContentConsumers( List<String> enabledInvalidContentConsumers )
331 this.enabledInvalidContentConsumers = enabledInvalidContentConsumers;
334 public List<String> getEnabledKnownContentConsumers()
336 return enabledKnownContentConsumers;
339 public void setEnabledKnownContentConsumers( List<String> enabledKnownContentConsumers )
341 this.enabledKnownContentConsumers = enabledKnownContentConsumers;
344 public ArchivaAdministration getArchivaAdministration()
346 return archivaAdministration;
349 public void setArchivaAdministration( ArchivaAdministration archivaAdministration )
351 this.archivaAdministration = archivaAdministration;