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.commons.collections.CollectionUtils;
28 import org.apache.commons.lang.StringUtils;
29 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
30 import org.apache.maven.archiva.configuration.Configuration;
31 import org.apache.maven.archiva.configuration.FileType;
32 import org.apache.maven.archiva.configuration.IndeterminateConfigurationException;
33 import org.apache.maven.archiva.configuration.RepositoryScanningConfiguration;
34 import org.apache.maven.archiva.configuration.functors.FiletypeSelectionPredicate;
35 import org.apache.maven.archiva.configuration.functors.FiletypeToMapClosure;
36 import org.apache.archiva.security.ArchivaRoleConstants;
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
56 * plexus.component role="com.opensymphony.xwork2.Action" role-hint="repositoryScanningAction" instantiation-strategy="per-lookup"
58 @Controller( "repositoryScanningAction" )
60 public class RepositoryScanningAction
61 extends AbstractActionSupport
62 implements Preparable, Validateable, SecureAction, Auditable
68 private ArchivaConfiguration archivaConfiguration;
74 private RepositoryContentConsumers repoconsumerUtil;
76 private Map<String, FileType> fileTypeMap;
78 private List<String> fileTypeIds;
81 * List of {@link AdminRepositoryConsumer} objects for consumers of known content.
83 private List<AdminRepositoryConsumer> knownContentConsumers;
86 * List of enabled {@link AdminRepositoryConsumer} objects for consumers of known content.
88 private List<String> enabledKnownContentConsumers;
91 * List of {@link AdminRepositoryConsumer} objects for consumers of invalid/unknown content.
93 private List<AdminRepositoryConsumer> invalidContentConsumers;
96 * List of enabled {@link AdminRepositoryConsumer} objects for consumers of invalid/unknown content.
98 private List<String> enabledInvalidContentConsumers;
100 private String pattern;
102 private String fileTypeId;
104 public void addActionError( String anErrorMessage )
106 super.addActionError( anErrorMessage );
107 log.warn( "[ActionError] {}", anErrorMessage );
110 public void addActionMessage( String aMessage )
112 super.addActionMessage( aMessage );
113 log.info( "[ActionMessage] {}", aMessage );
116 public String addFiletypePattern()
118 log.info( "Add New File Type Pattern [" + getFileTypeId() + ":" + getPattern() + "]" );
120 if ( !isValidFiletypeCommand() )
125 String id = getFileTypeId();
126 String pattern = getPattern();
128 FileType filetype = findFileType( id );
129 if ( filetype == null )
131 addActionError( "Pattern not added, unable to find filetype " + id );
135 if ( filetype.getPatterns().contains( pattern ) )
137 addActionError( "Not adding pattern \"" + pattern + "\" to filetype " + id + " as it already exists." );
141 filetype.addPattern( pattern );
142 addActionMessage( "Added pattern \"" + pattern + "\" to filetype " + id );
144 triggerAuditEvent( AuditEvent.ADD_PATTERN + " " + pattern );
146 return saveConfiguration();
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()
193 Configuration config = archivaConfiguration.getConfiguration();
194 RepositoryScanningConfiguration reposcanning = config.getRepositoryScanning();
196 FiletypeToMapClosure filetypeToMapClosure = new FiletypeToMapClosure();
198 CollectionUtils.forAllDo( reposcanning.getFileTypes(), filetypeToMapClosure );
199 fileTypeMap = filetypeToMapClosure.getMap();
201 AddAdminRepoConsumerClosure addAdminRepoConsumer;
203 addAdminRepoConsumer = new AddAdminRepoConsumerClosure( reposcanning.getKnownContentConsumers() );
204 CollectionUtils.forAllDo( repoconsumerUtil.getAvailableKnownConsumers(), addAdminRepoConsumer );
205 this.knownContentConsumers = addAdminRepoConsumer.getList();
206 Collections.sort( knownContentConsumers, AdminRepositoryConsumerComparator.getInstance() );
208 addAdminRepoConsumer = new AddAdminRepoConsumerClosure( reposcanning.getInvalidContentConsumers() );
209 CollectionUtils.forAllDo( repoconsumerUtil.getAvailableInvalidConsumers(), addAdminRepoConsumer );
210 this.invalidContentConsumers = addAdminRepoConsumer.getList();
211 Collections.sort( invalidContentConsumers, AdminRepositoryConsumerComparator.getInstance() );
213 fileTypeIds = new ArrayList<String>();
214 fileTypeIds.addAll( fileTypeMap.keySet() );
215 Collections.sort( fileTypeIds );
218 public String removeFiletypePattern()
220 log.info( "Remove File Type Pattern [" + getFileTypeId() + ":" + getPattern() + "]" );
222 if ( !isValidFiletypeCommand() )
227 FileType filetype = findFileType( getFileTypeId() );
228 if ( filetype == null )
230 addActionError( "Pattern not removed, unable to find filetype " + getFileTypeId() );
234 filetype.removePattern( getPattern() );
236 triggerAuditEvent( AuditEvent.REMOVE_PATTERN + " " + pattern );
238 return saveConfiguration();
241 public void setFileTypeId( String fileTypeId )
243 this.fileTypeId = fileTypeId;
246 public void setPattern( String pattern )
248 this.pattern = pattern;
251 public String updateInvalidConsumers()
253 addActionMessage( "Update Invalid Consumers" );
255 List<String> oldConsumers =
256 archivaConfiguration.getConfiguration().getRepositoryScanning().getInvalidContentConsumers();
258 archivaConfiguration.getConfiguration().getRepositoryScanning().setInvalidContentConsumers(
259 enabledInvalidContentConsumers );
261 if ( enabledInvalidContentConsumers != null )
263 filterAddedConsumers( oldConsumers, enabledInvalidContentConsumers );
264 filterRemovedConsumers( oldConsumers, enabledInvalidContentConsumers );
268 disableAllEnabledConsumers( oldConsumers );
271 return saveConfiguration();
274 public String updateKnownConsumers()
276 addActionMessage( "Update Known Consumers" );
278 List<String> oldConsumers =
279 archivaConfiguration.getConfiguration().getRepositoryScanning().getKnownContentConsumers();
281 archivaConfiguration.getConfiguration().getRepositoryScanning().setKnownContentConsumers(
282 enabledKnownContentConsumers );
284 if ( enabledKnownContentConsumers != null )
286 filterAddedConsumers( oldConsumers, enabledKnownContentConsumers );
287 filterRemovedConsumers( oldConsumers, enabledKnownContentConsumers );
291 disableAllEnabledConsumers( oldConsumers );
294 return saveConfiguration();
297 private FileType findFileType( String id )
299 RepositoryScanningConfiguration scanning = archivaConfiguration.getConfiguration().getRepositoryScanning();
300 return (FileType) CollectionUtils.find( scanning.getFileTypes(), new FiletypeSelectionPredicate( id ) );
303 private boolean isValidFiletypeCommand()
305 if ( StringUtils.isBlank( getFileTypeId() ) )
307 addActionError( "Unable to process blank filetype id." );
310 if ( StringUtils.isBlank( getPattern() ) )
312 addActionError( "Unable to process blank pattern." );
315 return !hasActionErrors();
318 private String saveConfiguration()
322 archivaConfiguration.save( archivaConfiguration.getConfiguration() );
323 addActionMessage( "Successfully saved configuration" );
325 catch ( RegistryException e )
327 addActionError( "Unable to save configuration: " + e.getMessage() );
330 catch ( IndeterminateConfigurationException e )
332 addActionError( e.getMessage() );
339 private void filterAddedConsumers( List<String> oldList, List<String> newList )
341 for ( String consumer : newList )
343 if ( !oldList.contains( consumer ) )
345 triggerAuditEvent( AuditEvent.ENABLE_REPO_CONSUMER + " " + consumer );
350 private void filterRemovedConsumers( List<String> oldList, List<String> newList )
352 for ( String consumer : oldList )
354 if ( !newList.contains( consumer ) )
356 triggerAuditEvent( AuditEvent.DISABLE_REPO_CONSUMER + " " + consumer );
361 private void disableAllEnabledConsumers( List<String> consumers )
363 for ( String consumer : consumers )
365 triggerAuditEvent( AuditEvent.DISABLE_REPO_CONSUMER + " " + consumer );
369 public List<String> getEnabledInvalidContentConsumers()
371 return enabledInvalidContentConsumers;
374 public void setEnabledInvalidContentConsumers( List<String> enabledInvalidContentConsumers )
376 this.enabledInvalidContentConsumers = enabledInvalidContentConsumers;
379 public List<String> getEnabledKnownContentConsumers()
381 return enabledKnownContentConsumers;
384 public void setEnabledKnownContentConsumers( List<String> enabledKnownContentConsumers )
386 this.enabledKnownContentConsumers = enabledKnownContentConsumers;
389 public ArchivaConfiguration getArchivaConfiguration()
391 return archivaConfiguration;
394 public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
396 this.archivaConfiguration = archivaConfiguration;