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 getFileTypeId()
148 public List<String> getFileTypeIds()
153 public Map<String, FileType> getFileTypeMap()
158 public List<AdminRepositoryConsumer> getInvalidContentConsumers()
160 return invalidContentConsumers;
163 public List<AdminRepositoryConsumer> getKnownContentConsumers()
165 return knownContentConsumers;
168 public String getPattern()
173 public SecureActionBundle getSecureActionBundle()
174 throws SecureActionException
176 SecureActionBundle bundle = new SecureActionBundle();
178 bundle.setRequiresAuthentication( true );
179 bundle.addRequiredAuthorization( ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION, Resource.GLOBAL );
184 public void prepare()
187 Configuration config = archivaConfiguration.getConfiguration();
188 RepositoryScanningConfiguration reposcanning = config.getRepositoryScanning();
190 FiletypeToMapClosure filetypeToMapClosure = new FiletypeToMapClosure();
192 CollectionUtils.forAllDo( reposcanning.getFileTypes(), filetypeToMapClosure );
193 fileTypeMap = filetypeToMapClosure.getMap();
195 AddAdminRepoConsumerClosure addAdminRepoConsumer;
197 addAdminRepoConsumer = new AddAdminRepoConsumerClosure( reposcanning.getKnownContentConsumers() );
198 CollectionUtils.forAllDo( repoconsumerUtil.getAvailableKnownConsumers(), addAdminRepoConsumer );
199 this.knownContentConsumers = addAdminRepoConsumer.getList();
200 Collections.sort( knownContentConsumers, AdminRepositoryConsumerComparator.getInstance() );
202 addAdminRepoConsumer = new AddAdminRepoConsumerClosure( reposcanning.getInvalidContentConsumers() );
203 CollectionUtils.forAllDo( repoconsumerUtil.getAvailableInvalidConsumers(), addAdminRepoConsumer );
204 this.invalidContentConsumers = addAdminRepoConsumer.getList();
205 Collections.sort( invalidContentConsumers, AdminRepositoryConsumerComparator.getInstance() );
207 fileTypeIds = new ArrayList<String>();
208 fileTypeIds.addAll( fileTypeMap.keySet() );
209 Collections.sort( fileTypeIds );
212 public String removeFiletypePattern()
214 log.info( "Remove File Type Pattern [" + getFileTypeId() + ":" + getPattern() + "]" );
216 if ( !isValidFiletypeCommand() )
221 FileType filetype = findFileType( getFileTypeId() );
222 if ( filetype == null )
224 addActionError( "Pattern not removed, unable to find filetype " + getFileTypeId() );
228 filetype.removePattern( getPattern() );
230 triggerAuditEvent( AuditEvent.REMOVE_PATTERN + " " + pattern );
232 return saveConfiguration();
235 public void setFileTypeId( String fileTypeId )
237 this.fileTypeId = fileTypeId;
240 public void setPattern( String pattern )
242 this.pattern = pattern;
245 public String updateInvalidConsumers()
247 addActionMessage( "Update Invalid Consumers" );
249 List<String> oldConsumers =
250 archivaConfiguration.getConfiguration().getRepositoryScanning().getInvalidContentConsumers();
252 archivaConfiguration.getConfiguration().getRepositoryScanning().setInvalidContentConsumers(
253 enabledInvalidContentConsumers );
255 if ( enabledInvalidContentConsumers != null )
257 filterAddedConsumers( oldConsumers, enabledInvalidContentConsumers );
258 filterRemovedConsumers( oldConsumers, enabledInvalidContentConsumers );
262 disableAllEnabledConsumers( oldConsumers );
265 return saveConfiguration();
268 public String updateKnownConsumers()
270 addActionMessage( "Update Known Consumers" );
272 List<String> oldConsumers =
273 archivaConfiguration.getConfiguration().getRepositoryScanning().getKnownContentConsumers();
275 archivaConfiguration.getConfiguration().getRepositoryScanning().setKnownContentConsumers(
276 enabledKnownContentConsumers );
278 if ( enabledKnownContentConsumers != null )
280 filterAddedConsumers( oldConsumers, enabledKnownContentConsumers );
281 filterRemovedConsumers( oldConsumers, enabledKnownContentConsumers );
285 disableAllEnabledConsumers( oldConsumers );
288 return saveConfiguration();
291 private FileType findFileType( String id )
293 RepositoryScanningConfiguration scanning = archivaConfiguration.getConfiguration().getRepositoryScanning();
294 return (FileType) CollectionUtils.find( scanning.getFileTypes(), new FiletypeSelectionPredicate( id ) );
297 private boolean isValidFiletypeCommand()
299 if ( StringUtils.isBlank( getFileTypeId() ) )
301 addActionError( "Unable to process blank filetype id." );
304 if ( StringUtils.isBlank( getPattern() ) )
306 addActionError( "Unable to process blank pattern." );
309 return !hasActionErrors();
312 private String saveConfiguration()
316 archivaConfiguration.save( archivaConfiguration.getConfiguration() );
317 addActionMessage( "Successfully saved configuration" );
319 catch ( RegistryException e )
321 addActionError( "Unable to save configuration: " + e.getMessage() );
324 catch ( IndeterminateConfigurationException e )
326 addActionError( e.getMessage() );
333 private void filterAddedConsumers( List<String> oldList, List<String> newList )
335 for ( String consumer : newList )
337 if ( !oldList.contains( consumer ) )
339 triggerAuditEvent( AuditEvent.ENABLE_REPO_CONSUMER + " " + consumer );
344 private void filterRemovedConsumers( List<String> oldList, List<String> newList )
346 for ( String consumer : oldList )
348 if ( !newList.contains( consumer ) )
350 triggerAuditEvent( AuditEvent.DISABLE_REPO_CONSUMER + " " + consumer );
355 private void disableAllEnabledConsumers( List<String> consumers )
357 for ( String consumer : consumers )
359 triggerAuditEvent( AuditEvent.DISABLE_REPO_CONSUMER + " " + consumer );
363 public List<String> getEnabledInvalidContentConsumers()
365 return enabledInvalidContentConsumers;
368 public void setEnabledInvalidContentConsumers( List<String> enabledInvalidContentConsumers )
370 this.enabledInvalidContentConsumers = enabledInvalidContentConsumers;
373 public List<String> getEnabledKnownContentConsumers()
375 return enabledKnownContentConsumers;
378 public void setEnabledKnownContentConsumers( List<String> enabledKnownContentConsumers )
380 this.enabledKnownContentConsumers = enabledKnownContentConsumers;
383 public ArchivaConfiguration getArchivaConfiguration()
385 return archivaConfiguration;
388 public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
390 this.archivaConfiguration = archivaConfiguration;