1 package org.apache.maven.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.audit.AuditEvent;
25 import org.apache.archiva.audit.Auditable;
26 import org.apache.archiva.repository.scanner.RepositoryContentConsumers;
27 import org.apache.archiva.security.common.ArchivaRoleConstants;
28 import org.apache.commons.collections.CollectionUtils;
29 import org.apache.commons.lang.StringUtils;
30 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
31 import org.apache.maven.archiva.configuration.Configuration;
32 import org.apache.maven.archiva.configuration.FileType;
33 import org.apache.maven.archiva.configuration.IndeterminateConfigurationException;
34 import org.apache.maven.archiva.configuration.RepositoryScanningConfiguration;
35 import org.apache.maven.archiva.configuration.functors.FiletypeSelectionPredicate;
36 import org.apache.maven.archiva.configuration.functors.FiletypeToMapClosure;
37 import org.apache.maven.archiva.web.action.AbstractActionSupport;
38 import org.codehaus.plexus.redback.rbac.Resource;
39 import org.codehaus.plexus.registry.RegistryException;
40 import org.codehaus.redback.integration.interceptor.SecureAction;
41 import org.codehaus.redback.integration.interceptor.SecureActionBundle;
42 import org.codehaus.redback.integration.interceptor.SecureActionException;
43 import org.springframework.context.annotation.Scope;
44 import org.springframework.stereotype.Controller;
46 import javax.inject.Inject;
47 import java.util.ArrayList;
48 import java.util.Collections;
49 import java.util.List;
53 * RepositoryScanningAction
57 @Controller( "repositoryScanningAction" )
59 public class RepositoryScanningAction
60 extends AbstractActionSupport
61 implements Preparable, Validateable, SecureAction, Auditable
65 private ArchivaConfiguration archivaConfiguration;
68 private RepositoryContentConsumers repoconsumerUtil;
70 private Map<String, FileType> fileTypeMap;
72 private List<String> fileTypeIds;
75 * List of {@link AdminRepositoryConsumer} objects for consumers of known content.
77 private List<AdminRepositoryConsumer> knownContentConsumers;
80 * List of enabled {@link AdminRepositoryConsumer} objects for consumers of known content.
82 private List<String> enabledKnownContentConsumers;
85 * List of {@link AdminRepositoryConsumer} objects for consumers of invalid/unknown content.
87 private List<AdminRepositoryConsumer> invalidContentConsumers;
90 * List of enabled {@link AdminRepositoryConsumer} objects for consumers of invalid/unknown content.
92 private List<String> enabledInvalidContentConsumers;
94 private String pattern;
96 private String fileTypeId;
98 public void addActionError( String anErrorMessage )
100 super.addActionError( anErrorMessage );
101 log.warn( "[ActionError] {}", anErrorMessage );
104 public void addActionMessage( String aMessage )
106 super.addActionMessage( aMessage );
107 log.info( "[ActionMessage] {}", aMessage );
110 public String addFiletypePattern()
112 log.info( "Add New File Type Pattern [" + getFileTypeId() + ":" + getPattern() + "]" );
114 if ( !isValidFiletypeCommand() )
119 String id = getFileTypeId();
120 String pattern = getPattern();
122 FileType filetype = findFileType( id );
123 if ( filetype == null )
125 addActionError( "Pattern not added, unable to find filetype " + id );
129 if ( filetype.getPatterns().contains( pattern ) )
131 addActionError( "Not adding pattern \"" + pattern + "\" to filetype " + id + " as it already exists." );
135 filetype.addPattern( pattern );
136 addActionMessage( "Added pattern \"" + pattern + "\" to filetype " + id );
138 triggerAuditEvent( AuditEvent.ADD_PATTERN + " " + pattern );
140 return saveConfiguration();
143 public String removeFiletypePattern()
145 log.info( "Remove File Type Pattern [" + getFileTypeId() + ":" + getPattern() + "]" );
147 if ( !isValidFiletypeCommand() )
152 FileType filetype = findFileType( getFileTypeId() );
153 if ( filetype == null )
155 addActionError( "Pattern not removed, unable to find filetype " + getFileTypeId() );
159 filetype.removePattern( getPattern() );
161 triggerAuditEvent( AuditEvent.REMOVE_PATTERN + " " + pattern );
163 return saveConfiguration();
166 public String getFileTypeId()
171 public List<String> getFileTypeIds()
176 public Map<String, FileType> getFileTypeMap()
181 public List<AdminRepositoryConsumer> getInvalidContentConsumers()
183 return invalidContentConsumers;
186 public List<AdminRepositoryConsumer> getKnownContentConsumers()
188 return knownContentConsumers;
191 public String getPattern()
196 public SecureActionBundle getSecureActionBundle()
197 throws SecureActionException
199 SecureActionBundle bundle = new SecureActionBundle();
201 bundle.setRequiresAuthentication( true );
202 bundle.addRequiredAuthorization( ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION, Resource.GLOBAL );
207 public void prepare()
210 Configuration config = archivaConfiguration.getConfiguration();
211 RepositoryScanningConfiguration reposcanning = config.getRepositoryScanning();
213 FiletypeToMapClosure filetypeToMapClosure = new FiletypeToMapClosure();
215 CollectionUtils.forAllDo( reposcanning.getFileTypes(), filetypeToMapClosure );
216 fileTypeMap = filetypeToMapClosure.getMap();
218 AddAdminRepoConsumerClosure addAdminRepoConsumer =
219 new AddAdminRepoConsumerClosure( reposcanning.getKnownContentConsumers() );
220 CollectionUtils.forAllDo( repoconsumerUtil.getAvailableKnownConsumers(), addAdminRepoConsumer );
221 this.knownContentConsumers = addAdminRepoConsumer.getList();
222 Collections.sort( knownContentConsumers, AdminRepositoryConsumerComparator.getInstance() );
224 addAdminRepoConsumer = new AddAdminRepoConsumerClosure( reposcanning.getInvalidContentConsumers() );
225 CollectionUtils.forAllDo( repoconsumerUtil.getAvailableInvalidConsumers(), addAdminRepoConsumer );
226 this.invalidContentConsumers = addAdminRepoConsumer.getList();
227 Collections.sort( invalidContentConsumers, AdminRepositoryConsumerComparator.getInstance() );
229 fileTypeIds = new ArrayList<String>();
230 fileTypeIds.addAll( fileTypeMap.keySet() );
231 Collections.sort( fileTypeIds );
234 public void setFileTypeId( String fileTypeId )
236 this.fileTypeId = fileTypeId;
239 public void setPattern( String pattern )
241 this.pattern = pattern;
244 public String updateInvalidConsumers()
246 addActionMessage( "Update Invalid Consumers" );
248 List<String> oldConsumers =
249 archivaConfiguration.getConfiguration().getRepositoryScanning().getInvalidContentConsumers();
251 archivaConfiguration.getConfiguration().getRepositoryScanning().setInvalidContentConsumers(
252 enabledInvalidContentConsumers );
254 if ( enabledInvalidContentConsumers != null )
256 filterAddedConsumers( oldConsumers, enabledInvalidContentConsumers );
257 filterRemovedConsumers( oldConsumers, enabledInvalidContentConsumers );
261 disableAllEnabledConsumers( oldConsumers );
264 return saveConfiguration();
267 public String updateKnownConsumers()
269 addActionMessage( "Update Known Consumers" );
271 List<String> oldConsumers =
272 archivaConfiguration.getConfiguration().getRepositoryScanning().getKnownContentConsumers();
274 archivaConfiguration.getConfiguration().getRepositoryScanning().setKnownContentConsumers(
275 enabledKnownContentConsumers );
277 if ( enabledKnownContentConsumers != null )
279 filterAddedConsumers( oldConsumers, enabledKnownContentConsumers );
280 filterRemovedConsumers( oldConsumers, enabledKnownContentConsumers );
284 disableAllEnabledConsumers( oldConsumers );
287 return saveConfiguration();
290 private FileType findFileType( String id )
292 RepositoryScanningConfiguration scanning = archivaConfiguration.getConfiguration().getRepositoryScanning();
293 return (FileType) CollectionUtils.find( scanning.getFileTypes(), new FiletypeSelectionPredicate( id ) );
296 private boolean isValidFiletypeCommand()
298 if ( StringUtils.isBlank( getFileTypeId() ) )
300 addActionError( "Unable to process blank filetype id." );
303 if ( StringUtils.isBlank( getPattern() ) )
305 addActionError( "Unable to process blank pattern." );
308 return !hasActionErrors();
311 private String saveConfiguration()
315 archivaConfiguration.save( archivaConfiguration.getConfiguration() );
316 addActionMessage( "Successfully saved configuration" );
318 catch ( RegistryException e )
320 addActionError( "Unable to save configuration: " + e.getMessage() );
323 catch ( IndeterminateConfigurationException e )
325 addActionError( e.getMessage() );
332 private void filterAddedConsumers( List<String> oldList, List<String> newList )
334 for ( String consumer : newList )
336 if ( !oldList.contains( consumer ) )
338 triggerAuditEvent( AuditEvent.ENABLE_REPO_CONSUMER + " " + consumer );
343 private void filterRemovedConsumers( List<String> oldList, List<String> newList )
345 for ( String consumer : oldList )
347 if ( !newList.contains( consumer ) )
349 triggerAuditEvent( AuditEvent.DISABLE_REPO_CONSUMER + " " + consumer );
354 private void disableAllEnabledConsumers( List<String> consumers )
356 for ( String consumer : consumers )
358 triggerAuditEvent( AuditEvent.DISABLE_REPO_CONSUMER + " " + consumer );
362 public List<String> getEnabledInvalidContentConsumers()
364 return enabledInvalidContentConsumers;
367 public void setEnabledInvalidContentConsumers( List<String> enabledInvalidContentConsumers )
369 this.enabledInvalidContentConsumers = enabledInvalidContentConsumers;
372 public List<String> getEnabledKnownContentConsumers()
374 return enabledKnownContentConsumers;
377 public void setEnabledKnownContentConsumers( List<String> enabledKnownContentConsumers )
379 this.enabledKnownContentConsumers = enabledKnownContentConsumers;
382 public ArchivaConfiguration getArchivaConfiguration()
384 return archivaConfiguration;
387 public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
389 this.archivaConfiguration = archivaConfiguration;