[MRM-1506] api to configure RemoteRepository : extract code from ManagedRepo admin for validation

git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1164931 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Olivier Lamy 2011-09-03 21:22:56 +00:00
parent 025c72c95c
commit 77287dbb1c
9 changed files with 93 additions and 67 deletions

View File

@ -20,6 +20,7 @@ package org.apache.archiva.admin.repository.managed;
import org.apache.archiva.admin.AuditInformation;
import org.apache.archiva.admin.repository.RepositoryAdminException;
import org.apache.archiva.admin.repository.RepositoryCommonValidator;
import org.apache.archiva.audit.AuditEvent;
import org.apache.archiva.audit.AuditListener;
import org.apache.archiva.metadata.repository.MetadataRepository;
@ -29,6 +30,7 @@ import org.apache.archiva.metadata.repository.RepositorySessionFactory;
import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
import org.apache.archiva.scheduler.repository.RepositoryArchivaTaskScheduler;
import org.apache.archiva.scheduler.repository.RepositoryTask;
import org.apache.archiva.security.ArchivaRoleConstants;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.validator.GenericValidator;
@ -37,7 +39,6 @@ import org.apache.maven.archiva.configuration.Configuration;
import org.apache.maven.archiva.configuration.IndeterminateConfigurationException;
import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
import org.apache.archiva.security.ArchivaRoleConstants;
import org.codehaus.plexus.redback.role.RoleManager;
import org.codehaus.plexus.redback.role.RoleManagerException;
import org.codehaus.plexus.redback.users.User;
@ -68,9 +69,6 @@ import java.util.Map;
public class DefaultManagedRepositoryAdmin
implements ManagedRepositoryAdmin
{
public static final String REPOSITORY_ID_VALID_EXPRESSION = "^[a-zA-Z0-9._-]+$";
public static final String REPOSITORY_NAME_VALID_EXPRESSION = "^([a-zA-Z0-9.)/_(-]|\\s)+$";
public static final String REPOSITORY_LOCATION_VALID_EXPRESSION = "^[-a-zA-Z0-9._/~:?!&=\\\\]+$";
@ -98,6 +96,9 @@ public class DefaultManagedRepositoryAdmin
@Inject
private List<AuditListener> auditListeners = new ArrayList<AuditListener>();
@Inject
private RepositoryCommonValidator repositoryCommonValidator;
@Inject
protected RoleManager roleManager;
@ -143,6 +144,8 @@ public class DefaultManagedRepositoryAdmin
AuditInformation auditInformation )
throws RepositoryAdminException
{
repositoryCommonValidator.basicValidation( managedRepository, false );
triggerAuditEvent( managedRepository.getId(), null, AuditEvent.ADD_MANAGED_REPO, auditInformation );
return
addManagedRepository( managedRepository.getId(), managedRepository.getLayout(), managedRepository.getName(),
@ -166,17 +169,6 @@ public class DefaultManagedRepositoryAdmin
Configuration config = getArchivaConfiguration().getConfiguration();
if ( config.getManagedRepositoriesAsMap().containsKey( repoId ) )
{
throw new RepositoryAdminException( "Unable to add new repository with id [" + repoId
+ "], that id already exists as a managed repository." );
}
else if ( config.getRepositoryGroupsAsMap().containsKey( repoId ) )
{
throw new RepositoryAdminException( "Unable to add new repository with id [" + repoId
+ "], that id already exists as a repository group." );
}
// FIXME : olamy can be empty to avoid scheduled scan ?
if ( StringUtils.isNotBlank( cronExpression ) )
{
@ -192,30 +184,7 @@ public class DefaultManagedRepositoryAdmin
throw new RepositoryAdminException( "Cron expression cannot be empty." );
}
if ( StringUtils.isBlank( repoId ) )
{
throw new RepositoryAdminException( "Repository ID cannot be empty." );
}
if ( !GenericValidator.matchRegexp( repoId, REPOSITORY_ID_VALID_EXPRESSION ) )
{
throw new RepositoryAdminException(
"Invalid repository ID. Identifier must only contain alphanumeric characters, underscores(_), dots(.), and dashes(-)." );
}
if ( StringUtils.isBlank( name ) )
{
throw new RepositoryAdminException( "repository name cannot be empty" );
}
if ( !GenericValidator.matchRegexp( name, REPOSITORY_NAME_VALID_EXPRESSION ) )
{
throw new RepositoryAdminException(
"Invalid repository name. Repository Name must only contain alphanumeric characters, white-spaces(' '), "
+ "forward-slashes(/), open-parenthesis('('), close-parenthesis(')'), underscores(_), dots(.), and dashes(-)." );
}
String repoLocation = removeExpressions( location );
String repoLocation = repositoryCommonValidator.removeExpressions( location );
if ( !GenericValidator.matchRegexp( repoLocation, REPOSITORY_LOCATION_VALID_EXPRESSION ) )
{
@ -404,12 +373,16 @@ public class DefaultManagedRepositoryAdmin
AuditInformation auditInformation, boolean resetStats )
throws RepositoryAdminException
{
// Ensure that the fields are valid.
Configuration configuration = getArchivaConfiguration().getConfiguration();
log.debug( "updateManagedConfiguration repo {} needStage {} resetStats {} ",
Arrays.asList( managedRepository, needStageRepo, resetStats ).toArray() );
// Ensure that the fields are valid.
repositoryCommonValidator.basicValidation( managedRepository, true );
Configuration configuration = getArchivaConfiguration().getConfiguration();
ManagedRepositoryConfiguration toremove = configuration.findManagedRepositoryById( managedRepository.getId() );
if ( toremove != null )
@ -488,14 +461,6 @@ public class DefaultManagedRepositoryAdmin
}
public String removeExpressions( String directory )
{
String value = StringUtils.replace( directory, "${appserver.base}",
getRegistry().getString( "appserver.base", "${appserver.base}" ) );
value = StringUtils.replace( value, "${appserver.home}",
getRegistry().getString( "appserver.home", "${appserver.home}" ) );
return value;
}
private void saveConfiguration( Configuration config )
throws RepositoryAdminException
@ -685,4 +650,14 @@ public class DefaultManagedRepositoryAdmin
{
this.registry = registry;
}
public RepositoryCommonValidator getRepositoryCommonValidator()
{
return repositoryCommonValidator;
}
public void setRepositoryCommonValidator( RepositoryCommonValidator repositoryCommonValidator )
{
this.repositoryCommonValidator = repositoryCommonValidator;
}
}

View File

@ -48,11 +48,5 @@ public interface ManagedRepositoryAdmin
AuditInformation auditInformation, boolean resetStats )
throws RepositoryAdminException;
/**
* replace some interpolations ${appserver.base} with correct values
* @param directory
* @return
*/
String removeExpressions( String directory );
}

View File

@ -21,9 +21,9 @@ package org.apache.archiva.admin.repository.managed;
import org.apache.archiva.admin.AuditInformation;
import org.apache.archiva.admin.mock.MockAuditListener;
import org.apache.archiva.audit.AuditEvent;
import org.apache.archiva.security.ArchivaRoleConstants;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.archiva.security.ArchivaRoleConstants;
import org.codehaus.plexus.redback.role.RoleManager;
import org.codehaus.plexus.redback.users.User;
import org.codehaus.plexus.redback.users.memory.SimpleUser;
@ -184,6 +184,7 @@ public class ManagedRepositoryAdminTest
assertAuditListenerCallAndUpdateAddAndDelete( false );
mockAuditListener.clearEvents();
}
@ -242,6 +243,7 @@ public class ManagedRepositoryAdminTest
assertTemplateRoleNotExists( repoId + STAGE_REPO_ID_END );
mockAuditListener.clearEvents();
}
@Test
@ -264,6 +266,8 @@ public class ManagedRepositoryAdminTest
ManagedRepository repo = getTestManagedRepository( repoId, repoLocation );
managedRepositoryAdmin.addManagedRepository( repo, false, getFakeAuditInformation() );
assertTemplateRoleExists( repoId );
@ -323,6 +327,7 @@ public class ManagedRepositoryAdminTest
assertAuditListenerCallAndUpdateAddAndDelete( true );
mockAuditListener.clearEvents();
}
//----------------------------------

View File

@ -19,14 +19,15 @@ package org.apache.maven.archiva.web.action.admin.repositories;
* under the License.
*/
import org.apache.archiva.admin.repository.RepositoryCommonValidator;
import org.apache.archiva.admin.repository.managed.ManagedRepositoryAdmin;
import org.apache.archiva.audit.Auditable;
import org.apache.archiva.security.ArchivaRoleConstants;
import org.apache.maven.archiva.configuration.ArchivaConfiguration;
import org.apache.maven.archiva.configuration.Configuration;
import org.apache.maven.archiva.configuration.IndeterminateConfigurationException;
import org.apache.maven.archiva.configuration.InvalidConfigurationException;
import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
import org.apache.archiva.security.ArchivaRoleConstants;
import org.apache.maven.archiva.web.action.AbstractActionSupport;
import org.codehaus.plexus.redback.rbac.Resource;
import org.codehaus.plexus.registry.RegistryException;
@ -60,6 +61,9 @@ public abstract class AbstractRepositoriesAdminAction
@Inject
private ManagedRepositoryAdmin managedRepositoryAdmin;
@Inject
private RepositoryCommonValidator repositoryCommonValidator;
public ArchivaConfiguration getArchivaConfiguration()
{
return archivaConfiguration;
@ -133,4 +137,13 @@ public abstract class AbstractRepositoriesAdminAction
this.managedRepositoryAdmin = managedRepositoryAdmin;
}
public RepositoryCommonValidator getRepositoryCommonValidator()
{
return repositoryCommonValidator;
}
public void setRepositoryCommonValidator( RepositoryCommonValidator repositoryCommonValidator )
{
this.repositoryCommonValidator = repositoryCommonValidator;
}
}

View File

@ -25,7 +25,6 @@ import org.apache.archiva.admin.repository.RepositoryAdminException;
import org.apache.archiva.admin.repository.managed.ManagedRepository;
import org.apache.commons.lang.StringUtils;
import org.apache.maven.archiva.configuration.Configuration;
import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
import org.codehaus.redback.components.scheduler.CronExpressionValidator;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
@ -74,7 +73,7 @@ public class AddManagedRepositoryAction
public String commit()
{
repository.setLocation( getManagedRepositoryAdmin().removeExpressions( repository.getLocation() ) );
repository.setLocation( getRepositoryCommonValidator().removeExpressions( repository.getLocation() ) );
File location = new File( repository.getLocation() );
if ( location.exists() )

View File

@ -95,7 +95,7 @@ public class EditManagedRepositoryAction
boolean resetStats = false;
// check if the location was changed
repository.setLocation( getManagedRepositoryAdmin().removeExpressions( repository.getLocation() ) );
repository.setLocation( getRepositoryCommonValidator().removeExpressions( repository.getLocation() ) );
if ( !StringUtils.equalsIgnoreCase( existingConfig.getLocation().trim(), repository.getLocation().trim() ) )
{

View File

@ -20,15 +20,16 @@ package org.apache.maven.archiva.web.action.admin.repositories;
*/
import com.opensymphony.xwork2.Action;
import org.apache.archiva.admin.repository.RepositoryCommonValidator;
import org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin;
import org.apache.archiva.admin.repository.managed.ManagedRepository;
import org.apache.archiva.scheduler.repository.RepositoryArchivaTaskScheduler;
import org.apache.archiva.scheduler.repository.RepositoryTask;
import org.apache.archiva.security.ArchivaRoleConstants;
import org.apache.archiva.web.validator.utils.ValidatorUtil;
import org.apache.commons.io.FileUtils;
import org.apache.maven.archiva.configuration.ArchivaConfiguration;
import org.apache.maven.archiva.configuration.Configuration;
import org.apache.archiva.security.ArchivaRoleConstants;
import org.apache.archiva.web.validator.utils.ValidatorUtil;
import org.codehaus.plexus.redback.role.RoleManager;
import org.codehaus.plexus.registry.Registry;
import org.codehaus.redback.integration.interceptor.SecureActionBundle;
@ -98,6 +99,16 @@ public class AddManagedRepositoryActionTest
( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRegistry( registry );
( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRepositoryTaskScheduler(
repositoryTaskScheduler );
RepositoryCommonValidator repositoryCommonValidator = new RepositoryCommonValidator();
repositoryCommonValidator.setArchivaConfiguration( archivaConfiguration );
repositoryCommonValidator.setRegistry( registry );
( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRepositoryCommonValidator(
repositoryCommonValidator );
action.setRepositoryCommonValidator( repositoryCommonValidator );
action.setManagedRepositoryAdmin( getManagedRepositoryAdmin() );
}
@ -118,6 +129,8 @@ public class AddManagedRepositoryActionTest
public void testAddRepositoryInitialPage()
throws Exception
{
archivaConfiguration.getConfiguration();
archivaConfigurationControl.setReturnValue( new Configuration() );
archivaConfiguration.getConfiguration();
archivaConfigurationControl.setReturnValue( new Configuration() );
archivaConfigurationControl.replay();
@ -185,6 +198,9 @@ public class AddManagedRepositoryActionTest
archivaConfiguration.getConfiguration();
archivaConfigurationControl.setReturnValue( configuration );
archivaConfiguration.getConfiguration();
archivaConfigurationControl.setReturnValue( configuration );
archivaConfiguration.save( configuration );
archivaConfigurationControl.replay();

View File

@ -21,6 +21,7 @@ package org.apache.maven.archiva.web.action.admin.repositories;
import com.opensymphony.xwork2.Action;
import org.apache.archiva.admin.repository.RepositoryAdminException;
import org.apache.archiva.admin.repository.RepositoryCommonValidator;
import org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin;
import org.apache.archiva.admin.repository.managed.ManagedRepository;
import org.apache.archiva.audit.AuditListener;
@ -121,6 +122,15 @@ public class EditManagedRepositoryActionTest
( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setAuditListeners(
new ArrayList<AuditListener>( 0 ) );
RepositoryCommonValidator repositoryCommonValidator = new RepositoryCommonValidator();
repositoryCommonValidator.setArchivaConfiguration( archivaConfiguration );
repositoryCommonValidator.setRegistry( registry );
( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRepositoryCommonValidator(
repositoryCommonValidator );
action.setRepositoryCommonValidator( repositoryCommonValidator );
action.setManagedRepositoryAdmin( getManagedRepositoryAdmin() );
}
@ -229,6 +239,7 @@ public class EditManagedRepositoryActionTest
archivaConfigurationControl.setReturnValue( configuration );
archivaConfigurationControl.setReturnValue( configuration );
archivaConfigurationControl.setReturnValue( configuration );
archivaConfigurationControl.setReturnValue( configuration );
Configuration stageRepoConfiguration = new Configuration();
@ -331,6 +342,8 @@ public class EditManagedRepositoryActionTest
archivaConfigurationControl.setReturnValue( configuration );
archivaConfigurationControl.setReturnValue( configuration );
archivaConfigurationControl.setReturnValue( configuration );
archivaConfigurationControl.setReturnValue( buildEasyConfiguration() );
Configuration stageRepoConfiguration = buildEasyConfiguration();

View File

@ -20,6 +20,7 @@ package org.apache.archiva.web.xmlrpc.services;
*/
import junit.framework.TestCase;
import org.apache.archiva.admin.repository.RepositoryCommonValidator;
import org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin;
import org.apache.archiva.audit.AuditEvent;
import org.apache.archiva.audit.AuditListener;
@ -34,6 +35,7 @@ import org.apache.archiva.repository.events.RepositoryListener;
import org.apache.archiva.repository.scanner.RepositoryContentConsumers;
import org.apache.archiva.scheduler.repository.RepositoryArchivaTaskScheduler;
import org.apache.archiva.scheduler.repository.RepositoryTask;
import org.apache.archiva.security.ArchivaRoleConstants;
import org.apache.archiva.stagerepository.merge.RepositoryMerger;
import org.apache.archiva.web.xmlrpc.api.beans.ManagedRepository;
import org.apache.archiva.web.xmlrpc.api.beans.RemoteRepository;
@ -54,7 +56,6 @@ import org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryConte
import org.apache.maven.archiva.repository.content.ManagedLegacyRepositoryContent;
import org.apache.maven.archiva.repository.content.PathParser;
import org.apache.maven.archiva.repository.layout.LayoutException;
import org.apache.archiva.security.ArchivaRoleConstants;
import org.codehaus.plexus.redback.role.RoleManager;
import org.codehaus.plexus.registry.Registry;
import org.easymock.MockControl;
@ -62,6 +63,7 @@ import org.easymock.classextension.MockClassControl;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ -156,6 +158,8 @@ public class AdministrationServiceImplTest
private DefaultManagedRepositoryAdmin managedRepositoryAdmin;
private ApplicationContext applicationContext;
@Before
public void setUp()
throws Exception
@ -223,6 +227,12 @@ public class AdministrationServiceImplTest
managedRepositoryAdmin.setAuditListeners( Arrays.asList( auditListener ) );
managedRepositoryAdmin.setRoleManager( roleManager );
RepositoryCommonValidator repositoryCommonValidator = new RepositoryCommonValidator();
repositoryCommonValidator.setArchivaConfiguration( archivaConfig );
repositoryCommonValidator.setRegistry( registry );
managedRepositoryAdmin.setRepositoryCommonValidator( repositoryCommonValidator );
service = new AdministrationServiceImpl( archivaConfig, repoConsumersUtil, repositoryFactory,
repositorySessionFactory, repositoryTaskScheduler,
Collections.singletonList( listener ), repositoryStatisticsManager,
@ -1147,6 +1157,7 @@ public class AdministrationServiceImplTest
remoteRepoMap.put( "repo1", remoteRepo );
repoGroupMap.put( "repo1", repoGroup );
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
configControl.expectAndReturn( config.getManagedRepositoriesAsMap(), managedRepoMap );
@ -1168,10 +1179,10 @@ public class AdministrationServiceImplTest
}
catch ( Exception e )
{
assertEquals(
"Invalid repository location. Directory must only contain alphanumeric characters, equals(=), question-marks(?), "
+ "exclamation-points(!), ampersands(&amp;), forward-slashes(/), back-slashes(\\), underscores(_), dots(.), colons(:), tildes(~), and dashes(-).",
e.getMessage() );
assertEquals( "message found " + e.getMessage(),
"Invalid repository location. Directory must only contain alphanumeric characters, equals(=), question-marks(?), "
+ "exclamation-points(!), ampersands(&amp;), forward-slashes(/), back-slashes(\\), underscores(_), dots(.), colons(:), tildes(~), and dashes(-).",
e.getMessage() );
}
registryControl.verify();