import org.apache.archiva.admin.repository.AbstractRepositoryAdminTest;
import org.apache.archiva.admin.repository.RepositoryAdminException;
+import org.apache.archiva.audit.AuditEvent;
import org.junit.Test;
import javax.inject.Inject;
assertFalse(
archivaAdministration.getLegacyArtifactPaths().contains( new LegacyArtifactPath( "foo", "bar" ) ) );
assertEquals( initialSize, archivaAdministration.getLegacyArtifactPaths().size() );
+ mockAuditListener.clearEvents();
}
@Test
assertEquals( initialSize, archivaAdministration.getRepositoryScanning().getFileTypes().size() );
assertNull( archivaAdministration.getFileType( "foo" ) );
+ mockAuditListener.clearEvents();
+ }
+
+ @Test
+ public void knownContentConsumersTest()
+ throws Exception
+ {
+ int initialSize = archivaAdministration.getRepositoryScanning().getKnownContentConsumers().size();
+
+ archivaAdministration.addKnownContentConsumer( "foo", getFakeAuditInformation() );
+
+ assertEquals( initialSize + 1,
+ archivaAdministration.getRepositoryScanning().getKnownContentConsumers().size() );
+ assertTrue( archivaAdministration.getRepositoryScanning().getKnownContentConsumers().contains( "foo" ) );
+
+ // ensure we don't add it twice as it's an ArrayList as storage
+ archivaAdministration.addKnownContentConsumer( "foo", getFakeAuditInformation() );
+
+ assertEquals( initialSize + 1,
+ archivaAdministration.getRepositoryScanning().getKnownContentConsumers().size() );
+ assertTrue( archivaAdministration.getRepositoryScanning().getKnownContentConsumers().contains( "foo" ) );
+ mockAuditListener.clearEvents();
+
+ }
+
+ @Test
+ public void invalidContentConsumersTest()
+ throws Exception
+ {
+ int initialSize = archivaAdministration.getRepositoryScanning().getInvalidContentConsumers().size();
+
+ archivaAdministration.addInvalidContentConsumer( "foo", getFakeAuditInformation() );
+
+ assertEquals( initialSize + 1,
+ archivaAdministration.getRepositoryScanning().getInvalidContentConsumers().size() );
+ assertTrue( archivaAdministration.getRepositoryScanning().getInvalidContentConsumers().contains( "foo" ) );
+
+ // ensure we don't add it twice as it's an ArrayList as storage
+ archivaAdministration.addInvalidContentConsumer( "foo", getFakeAuditInformation() );
+
+ assertEquals( initialSize + 1,
+ archivaAdministration.getRepositoryScanning().getInvalidContentConsumers().size() );
+ assertTrue( archivaAdministration.getRepositoryScanning().getInvalidContentConsumers().contains( "foo" ) );
+
+ assertEquals( 1, mockAuditListener.getAuditEvents().size() );
+ assertEquals( AuditEvent.ENABLE_REPO_CONSUMER, mockAuditListener.getAuditEvents().get( 0 ).getAction() );
+
+ mockAuditListener.clearEvents();
+
}
}
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.Validateable;
-import org.apache.archiva.audit.AuditEvent;
+import org.apache.archiva.admin.repository.RepositoryAdminException;
+import org.apache.archiva.admin.repository.admin.ArchivaAdministration;
+import org.apache.archiva.admin.repository.admin.FileType;
+import org.apache.archiva.admin.repository.admin.FiletypeToMapClosure;
+import org.apache.archiva.admin.repository.admin.RepositoryScanning;
import org.apache.archiva.audit.Auditable;
import org.apache.archiva.repository.scanner.RepositoryContentConsumers;
import org.apache.archiva.security.common.ArchivaRoleConstants;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
-import org.apache.maven.archiva.configuration.ArchivaConfiguration;
-import org.apache.maven.archiva.configuration.Configuration;
-import org.apache.maven.archiva.configuration.FileType;
-import org.apache.maven.archiva.configuration.IndeterminateConfigurationException;
-import org.apache.maven.archiva.configuration.RepositoryScanningConfiguration;
-import org.apache.maven.archiva.configuration.functors.FiletypeSelectionPredicate;
-import org.apache.maven.archiva.configuration.functors.FiletypeToMapClosure;
import org.apache.maven.archiva.web.action.AbstractActionSupport;
import org.codehaus.plexus.redback.rbac.Resource;
-import org.codehaus.plexus.registry.RegistryException;
import org.codehaus.redback.integration.interceptor.SecureAction;
import org.codehaus.redback.integration.interceptor.SecureActionBundle;
import org.codehaus.redback.integration.interceptor.SecureActionException;
{
@Inject
- private ArchivaConfiguration archivaConfiguration;
+ private RepositoryContentConsumers repoconsumerUtil;
@Inject
- private RepositoryContentConsumers repoconsumerUtil;
+ private ArchivaAdministration archivaAdministration;
private Map<String, FileType> fileTypeMap;
public String addFiletypePattern()
{
- log.info( "Add New File Type Pattern [" + getFileTypeId() + ":" + getPattern() + "]" );
+ log.info( "Add New File Type Pattern [{}:{}]", getFileTypeId(), getPattern() );
if ( !isValidFiletypeCommand() )
{
return INPUT;
}
- String id = getFileTypeId();
- String pattern = getPattern();
-
- FileType filetype = findFileType( id );
- if ( filetype == null )
+ try
{
- addActionError( "Pattern not added, unable to find filetype " + id );
- return INPUT;
+ getArchivaAdministration().addFileTypePattern( getFileTypeId(), getPattern(), getAuditInformation() );
}
-
- if ( filetype.getPatterns().contains( pattern ) )
+ catch ( RepositoryAdminException e )
{
- addActionError( "Not adding pattern \"" + pattern + "\" to filetype " + id + " as it already exists." );
+ addActionError( "error adding file type pattern " + e.getMessage() );
return INPUT;
}
-
- filetype.addPattern( pattern );
- addActionMessage( "Added pattern \"" + pattern + "\" to filetype " + id );
-
- triggerAuditEvent( AuditEvent.ADD_PATTERN + " " + pattern );
-
- return saveConfiguration();
+ return SUCCESS;
}
public String removeFiletypePattern()
+ throws RepositoryAdminException
{
- log.info( "Remove File Type Pattern [" + getFileTypeId() + ":" + getPattern() + "]" );
+ log.info( "Remove File Type Pattern [{}:{}]", getFileTypeId(), getPattern() );
if ( !isValidFiletypeCommand() )
{
return INPUT;
}
- FileType filetype = findFileType( getFileTypeId() );
- if ( filetype == null )
+ try
{
- addActionError( "Pattern not removed, unable to find filetype " + getFileTypeId() );
+ getArchivaAdministration().removeFileTypePattern( getFileTypeId(), getPattern(), getAuditInformation() );
+ }
+ catch ( RepositoryAdminException e )
+ {
+ addActionError( "error adding file type pattern " + e.getMessage() );
return INPUT;
}
- filetype.removePattern( getPattern() );
-
- triggerAuditEvent( AuditEvent.REMOVE_PATTERN + " " + pattern );
-
- return saveConfiguration();
+ return SUCCESS;
}
public String getFileTypeId()
public void prepare()
throws Exception
{
- Configuration config = archivaConfiguration.getConfiguration();
- RepositoryScanningConfiguration reposcanning = config.getRepositoryScanning();
+ RepositoryScanning reposcanning = archivaAdministration.getRepositoryScanning();
FiletypeToMapClosure filetypeToMapClosure = new FiletypeToMapClosure();
public String updateInvalidConsumers()
{
- addActionMessage( "Update Invalid Consumers" );
-
- List<String> oldConsumers =
- archivaConfiguration.getConfiguration().getRepositoryScanning().getInvalidContentConsumers();
- archivaConfiguration.getConfiguration().getRepositoryScanning().setInvalidContentConsumers(
- enabledInvalidContentConsumers );
-
- if ( enabledInvalidContentConsumers != null )
+ try
{
- filterAddedConsumers( oldConsumers, enabledInvalidContentConsumers );
- filterRemovedConsumers( oldConsumers, enabledInvalidContentConsumers );
+ List<String> oldConsumers = getArchivaAdministration().getRepositoryScanning().getInvalidContentConsumers();
+
+ if ( enabledInvalidContentConsumers != null )
+ {
+ for ( String oldConsumer : oldConsumers )
+ {
+ if ( !enabledInvalidContentConsumers.contains( oldConsumer ) )
+ {
+ getArchivaAdministration().removeInvalidContentConsumer( oldConsumer, getAuditInformation() );
+ }
+ }
+ for ( String enabledKnowContentConsumer : enabledInvalidContentConsumers )
+ {
+ getArchivaAdministration().addInvalidContentConsumer( enabledKnowContentConsumer,
+ getAuditInformation() );
+ }
+ }
+ else
+ {
+ for ( String oldConsumer : oldConsumers )
+ {
+ getArchivaAdministration().removeInvalidContentConsumer( oldConsumer, getAuditInformation() );
+ }
+ }
}
- else
+ catch ( RepositoryAdminException e )
{
- disableAllEnabledConsumers( oldConsumers );
+ log.error( e.getMessage(), e );
+ addActionError( "Error update invalidContentConsumers " + e.getMessage() );
+ return INPUT;
}
+ addActionMessage( "Update Invalid Consumers" );
- return saveConfiguration();
+ return SUCCESS;
}
public String updateKnownConsumers()
{
- addActionMessage( "Update Known Consumers" );
-
- List<String> oldConsumers =
- archivaConfiguration.getConfiguration().getRepositoryScanning().getKnownContentConsumers();
- archivaConfiguration.getConfiguration().getRepositoryScanning().setKnownContentConsumers(
- enabledKnownContentConsumers );
-
- if ( enabledKnownContentConsumers != null )
+ try
{
- filterAddedConsumers( oldConsumers, enabledKnownContentConsumers );
- filterRemovedConsumers( oldConsumers, enabledKnownContentConsumers );
+ List<String> oldConsumers = getArchivaAdministration().getRepositoryScanning().getKnownContentConsumers();
+
+ if ( enabledKnownContentConsumers != null )
+ {
+ for ( String oldConsumer : oldConsumers )
+ {
+ if ( !enabledKnownContentConsumers.contains( oldConsumer ) )
+ {
+ getArchivaAdministration().removeKnownContentConsumer( oldConsumer, getAuditInformation() );
+ }
+ }
+ for ( String enabledKnowContentConsumer : enabledKnownContentConsumers )
+ {
+ getArchivaAdministration().addKnownContentConsumer( enabledKnowContentConsumer,
+ getAuditInformation() );
+ }
+ }
+ else
+ {
+ for ( String oldConsumer : oldConsumers )
+ {
+ getArchivaAdministration().removeKnownContentConsumer( oldConsumer, getAuditInformation() );
+ }
+ }
}
- else
+ catch ( RepositoryAdminException e )
{
- disableAllEnabledConsumers( oldConsumers );
+ log.error( e.getMessage(), e );
+ addActionError( "Error update knowContentConsumers " + e.getMessage() );
+ return INPUT;
}
+ addActionMessage( "Update Known Consumers" );
- return saveConfiguration();
+ return SUCCESS;
}
private FileType findFileType( String id )
+ throws RepositoryAdminException
{
- RepositoryScanningConfiguration scanning = archivaConfiguration.getConfiguration().getRepositoryScanning();
- return (FileType) CollectionUtils.find( scanning.getFileTypes(), new FiletypeSelectionPredicate( id ) );
+ return getArchivaAdministration().getFileType( id );
}
private boolean isValidFiletypeCommand()
return !hasActionErrors();
}
- private String saveConfiguration()
- {
- try
- {
- archivaConfiguration.save( archivaConfiguration.getConfiguration() );
- addActionMessage( "Successfully saved configuration" );
- }
- catch ( RegistryException e )
- {
- addActionError( "Unable to save configuration: " + e.getMessage() );
- return INPUT;
- }
- catch ( IndeterminateConfigurationException e )
- {
- addActionError( e.getMessage() );
- return INPUT;
- }
-
- return SUCCESS;
- }
-
- private void filterAddedConsumers( List<String> oldList, List<String> newList )
- {
- for ( String consumer : newList )
- {
- if ( !oldList.contains( consumer ) )
- {
- triggerAuditEvent( AuditEvent.ENABLE_REPO_CONSUMER + " " + consumer );
- }
- }
- }
-
- private void filterRemovedConsumers( List<String> oldList, List<String> newList )
- {
- for ( String consumer : oldList )
- {
- if ( !newList.contains( consumer ) )
- {
- triggerAuditEvent( AuditEvent.DISABLE_REPO_CONSUMER + " " + consumer );
- }
- }
- }
-
- private void disableAllEnabledConsumers( List<String> consumers )
- {
- for ( String consumer : consumers )
- {
- triggerAuditEvent( AuditEvent.DISABLE_REPO_CONSUMER + " " + consumer );
- }
- }
public List<String> getEnabledInvalidContentConsumers()
{
this.enabledKnownContentConsumers = enabledKnownContentConsumers;
}
- public ArchivaConfiguration getArchivaConfiguration()
+ public ArchivaAdministration getArchivaAdministration()
{
- return archivaConfiguration;
+ return archivaAdministration;
}
- public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
+ public void setArchivaAdministration( ArchivaAdministration archivaAdministration )
{
- this.archivaConfiguration = archivaConfiguration;
+ this.archivaAdministration = archivaAdministration;
}
}
package org.apache.maven.archiva.web.action.admin.scanning;
+import org.apache.archiva.admin.repository.admin.DefaultArchivaAdministration;
import org.apache.maven.archiva.configuration.ArchivaConfiguration;
import org.apache.maven.archiva.configuration.Configuration;
import org.apache.maven.archiva.configuration.RepositoryScanningConfiguration;
extends AbstractActionTestCase
{
private RepositoryScanningAction action;
-
+
private MockControl archivaConfigControl;
-
+
private ArchivaConfiguration archivaConfig;
-
+
private Configuration config;
-
- protected void setUp()
- throws Exception
+
+ protected void setUp()
+ throws Exception
{
-
+
super.setUp();
-
+
archivaConfigControl = MockControl.createControl( ArchivaConfiguration.class );
archivaConfig = (ArchivaConfiguration) archivaConfigControl.getMock();
-
+
action = new RepositoryScanningAction();
-
+
config = new Configuration();
-
- RepositoryScanningConfiguration repositoryScanningConfig = new RepositoryScanningConfiguration( );
-
+
+ RepositoryScanningConfiguration repositoryScanningConfig = new RepositoryScanningConfiguration();
+
repositoryScanningConfig.setKnownContentConsumers( createKnownContentConsumersList() );
-
+
config.setRepositoryScanning( repositoryScanningConfig );
- action.setArchivaConfiguration( archivaConfig );
+ DefaultArchivaAdministration archivaAdministration = new DefaultArchivaAdministration();
+ archivaAdministration.setArchivaConfiguration( archivaConfig );
+ action.setArchivaAdministration( archivaAdministration );
+
}
-
+
public void testUpdateKnownConsumers()
throws Exception
{
- archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
- archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
- archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
-
+ archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 10 );
+
+ archivaConfig.save( config );
+ archivaConfig.save( config );
+ archivaConfig.save( config );
+ archivaConfig.save( config );
+ archivaConfig.save( config );
+ archivaConfig.save( config );
+ archivaConfig.save( config );
archivaConfig.save( config );
archivaConfigControl.replay();
-
+
setEnabledKnownContentConsumers();
-
+
String returnString = action.updateKnownConsumers();
-
+
List<String> results = config.getRepositoryScanning().getKnownContentConsumers();
-
+
assertEquals( action.SUCCESS, returnString );
- assertEquals( 8, results.size() );
+ assertEquals( "results " + results, 8, results.size() );
}
-
+
public void testDisableAllKnownConsumers()
throws Exception
{
- archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
- archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
- archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
-
+ archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 10 );
+
+ archivaConfig.save( config );
+ archivaConfig.save( config );
+ archivaConfig.save( config );
+ archivaConfig.save( config );
+ archivaConfig.save( config );
+ archivaConfig.save( config );
+ archivaConfig.save( config );
archivaConfig.save( config );
archivaConfigControl.replay();
-
+
action.setEnabledKnownContentConsumers( null );
-
+
String returnString = action.updateKnownConsumers();
-
+
List<String> results = config.getRepositoryScanning().getKnownContentConsumers();
-
+
assertEquals( action.SUCCESS, returnString );
assertEquals( 0, results.size() );
}
-
+
private void setEnabledKnownContentConsumers()
{
action.setEnabledKnownContentConsumers( createKnownContentConsumersList() );
}
-
- private List<String> createKnownContentConsumersList( )
+
+ private List<String> createKnownContentConsumersList()
{
List<String> knownContentConsumers = new ArrayList<String>();
knownContentConsumers.add( "auto-remove" );
knownContentConsumers.add( "repository-purge" );
knownContentConsumers.add( "update-db-artifact" );
knownContentConsumers.add( "validate-checksums" );
-
+
return knownContentConsumers;
}
}