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 java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
27 import com.opensymphony.xwork2.Preparable;
28 import com.opensymphony.xwork2.Validateable;
29 import org.apache.archiva.repository.scanner.RepositoryContentConsumers;
30 import org.apache.commons.collections.CollectionUtils;
31 import org.apache.commons.lang.StringUtils;
32 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
33 import org.apache.maven.archiva.configuration.Configuration;
34 import org.apache.maven.archiva.configuration.FileType;
35 import org.apache.maven.archiva.configuration.IndeterminateConfigurationException;
36 import org.apache.maven.archiva.configuration.RepositoryScanningConfiguration;
37 import org.apache.maven.archiva.configuration.functors.FiletypeSelectionPredicate;
38 import org.apache.maven.archiva.configuration.functors.FiletypeToMapClosure;
39 import org.apache.maven.archiva.repository.audit.AuditEvent;
40 import org.apache.maven.archiva.repository.audit.Auditable;
41 import org.apache.maven.archiva.security.ArchivaRoleConstants;
42 import org.apache.maven.archiva.web.action.PlexusActionSupport;
43 import org.codehaus.plexus.redback.rbac.Resource;
44 import org.codehaus.plexus.registry.RegistryException;
45 import org.codehaus.redback.integration.interceptor.SecureAction;
46 import org.codehaus.redback.integration.interceptor.SecureActionBundle;
47 import org.codehaus.redback.integration.interceptor.SecureActionException;
50 * RepositoryScanningAction
53 * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="repositoryScanningAction" instantiation-strategy="per-lookup"
55 public class RepositoryScanningAction
56 extends PlexusActionSupport
57 implements Preparable, Validateable, SecureAction, Auditable
62 private ArchivaConfiguration archivaConfiguration;
67 private RepositoryContentConsumers repoconsumerUtil;
69 private Map<String, FileType> fileTypeMap;
71 private List<String> fileTypeIds;
74 * List of {@link AdminRepositoryConsumer} objects for consumers of known content.
76 private List<AdminRepositoryConsumer> knownContentConsumers;
79 * List of enabled {@link AdminRepositoryConsumer} objects for consumers of known content.
81 private List<String> enabledKnownContentConsumers;
84 * List of {@link AdminRepositoryConsumer} objects for consumers of invalid/unknown content.
86 private List<AdminRepositoryConsumer> invalidContentConsumers;
89 * List of enabled {@link AdminRepositoryConsumer} objects for consumers of invalid/unknown content.
91 private List<String> enabledInvalidContentConsumers;
93 private String pattern;
95 private String fileTypeId;
97 public void addActionError( String anErrorMessage )
99 super.addActionError( anErrorMessage );
100 log.warn( "[ActionError] " + anErrorMessage );
103 public void addActionMessage( String aMessage )
105 super.addActionMessage( aMessage );
106 log.info( "[ActionMessage] " + aMessage );
109 public String addFiletypePattern()
111 log.info( "Add New File Type Pattern [" + getFileTypeId() + ":" + getPattern() + "]" );
113 if ( !isValidFiletypeCommand() )
118 String id = getFileTypeId();
119 String pattern = getPattern();
121 FileType filetype = findFileType( id );
122 if ( filetype == null )
124 addActionError( "Pattern not added, unable to find filetype " + id );
128 if ( filetype.getPatterns().contains( pattern ) )
130 addActionError( "Not adding pattern \"" + pattern + "\" to filetype " + id + " as it already exists." );
134 filetype.addPattern( pattern );
135 addActionMessage( "Added pattern \"" + pattern + "\" to filetype " + id );
137 triggerAuditEvent( AuditEvent.ADD_PATTERN + " " + pattern );
139 return saveConfiguration();
142 public String getFileTypeId()
147 public List<String> getFileTypeIds()
152 public Map<String, FileType> getFileTypeMap()
157 public List<AdminRepositoryConsumer> getInvalidContentConsumers()
159 return invalidContentConsumers;
162 public List<AdminRepositoryConsumer> getKnownContentConsumers()
164 return knownContentConsumers;
167 public String getPattern()
172 public SecureActionBundle getSecureActionBundle()
173 throws SecureActionException
175 SecureActionBundle bundle = new SecureActionBundle();
177 bundle.setRequiresAuthentication( true );
178 bundle.addRequiredAuthorization( ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION, Resource.GLOBAL );
183 public void prepare()
186 Configuration config = archivaConfiguration.getConfiguration();
187 RepositoryScanningConfiguration reposcanning = config.getRepositoryScanning();
189 FiletypeToMapClosure filetypeToMapClosure = new FiletypeToMapClosure();
191 CollectionUtils.forAllDo( reposcanning.getFileTypes(), filetypeToMapClosure );
192 fileTypeMap = filetypeToMapClosure.getMap();
194 AddAdminRepoConsumerClosure addAdminRepoConsumer;
196 addAdminRepoConsumer = new AddAdminRepoConsumerClosure( reposcanning.getKnownContentConsumers() );
197 CollectionUtils.forAllDo( repoconsumerUtil.getAvailableKnownConsumers(), addAdminRepoConsumer );
198 this.knownContentConsumers = addAdminRepoConsumer.getList();
199 Collections.sort( knownContentConsumers, AdminRepositoryConsumerComparator.getInstance() );
201 addAdminRepoConsumer = new AddAdminRepoConsumerClosure( reposcanning.getInvalidContentConsumers() );
202 CollectionUtils.forAllDo( repoconsumerUtil.getAvailableInvalidConsumers(), addAdminRepoConsumer );
203 this.invalidContentConsumers = addAdminRepoConsumer.getList();
204 Collections.sort( invalidContentConsumers, AdminRepositoryConsumerComparator.getInstance() );
206 fileTypeIds = new ArrayList<String>();
207 fileTypeIds.addAll( fileTypeMap.keySet() );
208 Collections.sort( fileTypeIds );
211 public String removeFiletypePattern()
213 log.info( "Remove File Type Pattern [" + getFileTypeId() + ":" + getPattern() + "]" );
215 if ( !isValidFiletypeCommand() )
220 FileType filetype = findFileType( getFileTypeId() );
221 if ( filetype == null )
223 addActionError( "Pattern not removed, unable to find filetype " + getFileTypeId() );
227 filetype.removePattern( getPattern() );
229 triggerAuditEvent( AuditEvent.REMOVE_PATTERN + " " + pattern );
231 return saveConfiguration();
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 = archivaConfiguration.getConfiguration().getRepositoryScanning().getInvalidContentConsumers();
250 archivaConfiguration.getConfiguration().getRepositoryScanning().setInvalidContentConsumers(
251 enabledInvalidContentConsumers );
253 if ( enabledInvalidContentConsumers != null )
255 filterAddedConsumers( oldConsumers, enabledInvalidContentConsumers );
256 filterRemovedConsumers( oldConsumers, enabledInvalidContentConsumers );
260 disableAllEnabledConsumers( oldConsumers );
263 return saveConfiguration();
266 public String updateKnownConsumers()
268 addActionMessage( "Update Known Consumers" );
270 List<String> oldConsumers = archivaConfiguration.getConfiguration().getRepositoryScanning().getKnownContentConsumers();
272 archivaConfiguration.getConfiguration().getRepositoryScanning().setKnownContentConsumers(
273 enabledKnownContentConsumers );
275 if ( enabledKnownContentConsumers != null )
277 filterAddedConsumers( oldConsumers, enabledKnownContentConsumers );
278 filterRemovedConsumers( oldConsumers, enabledKnownContentConsumers );
282 disableAllEnabledConsumers( oldConsumers );
285 return saveConfiguration();
288 private FileType findFileType( String id )
290 RepositoryScanningConfiguration scanning = archivaConfiguration.getConfiguration().getRepositoryScanning();
291 return (FileType) CollectionUtils.find( scanning.getFileTypes(), new FiletypeSelectionPredicate( id ) );
294 private boolean isValidFiletypeCommand()
296 if ( StringUtils.isBlank( getFileTypeId() ) )
298 addActionError( "Unable to process blank filetype id." );
301 if ( StringUtils.isBlank( getPattern() ) )
303 addActionError( "Unable to process blank pattern." );
306 return !hasActionErrors();
309 private String saveConfiguration()
313 archivaConfiguration.save( archivaConfiguration.getConfiguration() );
314 addActionMessage( "Successfully saved configuration" );
316 catch ( RegistryException e )
318 addActionError( "Unable to save configuration: " + e.getMessage() );
321 catch ( IndeterminateConfigurationException e )
323 addActionError( e.getMessage() );
330 private void filterAddedConsumers( List<String> oldList, List<String> newList )
332 for ( String consumer : newList )
334 if ( !oldList.contains( consumer ) )
336 triggerAuditEvent( AuditEvent.ENABLE_REPO_CONSUMER + " " + consumer );
341 private void filterRemovedConsumers( List<String> oldList, List<String> newList )
343 for ( String consumer : oldList )
345 if ( !newList.contains( consumer ) )
347 triggerAuditEvent( AuditEvent.DISABLE_REPO_CONSUMER + " " + consumer );
352 private void disableAllEnabledConsumers( List<String> consumers )
354 for ( String consumer : consumers )
356 triggerAuditEvent( AuditEvent.DISABLE_REPO_CONSUMER + " " + consumer );
360 public List<String> getEnabledInvalidContentConsumers()
362 return enabledInvalidContentConsumers;
365 public void setEnabledInvalidContentConsumers( List<String> enabledInvalidContentConsumers )
367 this.enabledInvalidContentConsumers = enabledInvalidContentConsumers;
370 public List<String> getEnabledKnownContentConsumers()
372 return enabledKnownContentConsumers;
375 public void setEnabledKnownContentConsumers( List<String> enabledKnownContentConsumers )
377 this.enabledKnownContentConsumers = enabledKnownContentConsumers;
380 public ArchivaConfiguration getArchivaConfiguration()
382 return archivaConfiguration;
385 public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
387 this.archivaConfiguration = archivaConfiguration;