mirror of https://github.com/apache/archiva.git
[MRM-1506] api to configure RemoteRepository : implementation of add and delete
git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1165041 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
8269936883
commit
77eaae1d01
|
@ -19,10 +19,9 @@ package org.apache.archiva.admin.repository.managed;
|
|||
*/
|
||||
|
||||
import org.apache.archiva.admin.AuditInformation;
|
||||
import org.apache.archiva.admin.repository.AbstractRepositoryAdmin;
|
||||
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;
|
||||
import org.apache.archiva.metadata.repository.MetadataRepositoryException;
|
||||
import org.apache.archiva.metadata.repository.RepositorySession;
|
||||
|
@ -34,16 +33,11 @@ import org.apache.archiva.security.ArchivaRoleConstants;
|
|||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.commons.validator.GenericValidator;
|
||||
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.ManagedRepositoryConfiguration;
|
||||
import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
|
||||
import org.codehaus.plexus.redback.role.RoleManager;
|
||||
import org.codehaus.plexus.redback.role.RoleManagerException;
|
||||
import org.codehaus.plexus.redback.users.User;
|
||||
import org.codehaus.plexus.registry.Registry;
|
||||
import org.codehaus.plexus.registry.RegistryException;
|
||||
import org.codehaus.plexus.taskqueue.TaskQueueException;
|
||||
import org.codehaus.redback.components.scheduler.CronExpressionValidator;
|
||||
import org.slf4j.Logger;
|
||||
|
@ -67,6 +61,7 @@ import java.util.Map;
|
|||
*/
|
||||
@Service( "managedRepositoryAdmin#default" )
|
||||
public class DefaultManagedRepositoryAdmin
|
||||
extends AbstractRepositoryAdmin
|
||||
implements ManagedRepositoryAdmin
|
||||
{
|
||||
|
||||
|
@ -76,12 +71,6 @@ public class DefaultManagedRepositoryAdmin
|
|||
|
||||
public static final String STAGE_REPO_ID_END = "-stage";
|
||||
|
||||
@Inject
|
||||
private ArchivaConfiguration archivaConfiguration;
|
||||
|
||||
@Inject
|
||||
@Named( value = "commons-configuration" )
|
||||
private Registry registry;
|
||||
|
||||
@Inject
|
||||
@Named( value = "archivaTaskScheduler#repository" )
|
||||
|
@ -93,11 +82,6 @@ public class DefaultManagedRepositoryAdmin
|
|||
@Inject
|
||||
private RepositoryStatisticsManager repositoryStatisticsManager;
|
||||
|
||||
@Inject
|
||||
private List<AuditListener> auditListeners = new ArrayList<AuditListener>();
|
||||
|
||||
@Inject
|
||||
private RepositoryCommonValidator repositoryCommonValidator;
|
||||
|
||||
@Inject
|
||||
protected RoleManager roleManager;
|
||||
|
@ -145,7 +129,7 @@ public class DefaultManagedRepositoryAdmin
|
|||
throws RepositoryAdminException
|
||||
{
|
||||
|
||||
repositoryCommonValidator.basicValidation( managedRepository, false );
|
||||
getRepositoryCommonValidator().basicValidation( managedRepository, false );
|
||||
triggerAuditEvent( managedRepository.getId(), null, AuditEvent.ADD_MANAGED_REPO, auditInformation );
|
||||
return
|
||||
addManagedRepository( managedRepository.getId(), managedRepository.getLayout(), managedRepository.getName(),
|
||||
|
@ -184,7 +168,7 @@ public class DefaultManagedRepositoryAdmin
|
|||
throw new RepositoryAdminException( "Cron expression cannot be empty." );
|
||||
}
|
||||
|
||||
String repoLocation = repositoryCommonValidator.removeExpressions( location );
|
||||
String repoLocation = getRepositoryCommonValidator().removeExpressions( location );
|
||||
|
||||
if ( !GenericValidator.matchRegexp( repoLocation, REPOSITORY_LOCATION_VALID_EXPRESSION ) )
|
||||
{
|
||||
|
@ -379,7 +363,7 @@ public class DefaultManagedRepositoryAdmin
|
|||
|
||||
// Ensure that the fields are valid.
|
||||
|
||||
repositoryCommonValidator.basicValidation( managedRepository, true );
|
||||
getRepositoryCommonValidator().basicValidation( managedRepository, true );
|
||||
|
||||
Configuration configuration = getArchivaConfiguration().getConfiguration();
|
||||
|
||||
|
@ -446,38 +430,6 @@ public class DefaultManagedRepositoryAdmin
|
|||
// utils methods
|
||||
//--------------------------
|
||||
|
||||
protected void triggerAuditEvent( String repositoryId, String resource, String action,
|
||||
AuditInformation auditInformation )
|
||||
{
|
||||
User user = auditInformation == null ? null : auditInformation.getUser();
|
||||
AuditEvent event =
|
||||
new AuditEvent( repositoryId, user == null ? "null" : (String) user.getPrincipal(), resource, action );
|
||||
event.setRemoteIP( auditInformation == null ? "null" : auditInformation.getRemoteAddr() );
|
||||
|
||||
for ( AuditListener listener : getAuditListeners() )
|
||||
{
|
||||
listener.auditEvent( event );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void saveConfiguration( Configuration config )
|
||||
throws RepositoryAdminException
|
||||
{
|
||||
try
|
||||
{
|
||||
getArchivaConfiguration().save( config );
|
||||
}
|
||||
catch ( RegistryException e )
|
||||
{
|
||||
throw new RepositoryAdminException( "Error occurred in the registry.", e );
|
||||
}
|
||||
catch ( IndeterminateConfigurationException e )
|
||||
{
|
||||
throw new RepositoryAdminException( "Error occurred while saving the configuration.", e );
|
||||
}
|
||||
}
|
||||
|
||||
protected void addRepository( ManagedRepositoryConfiguration repository, Configuration configuration )
|
||||
throws RepositoryAdminException, IOException
|
||||
|
@ -581,15 +533,6 @@ public class DefaultManagedRepositoryAdmin
|
|||
// setters/getters
|
||||
//--------------------------
|
||||
|
||||
public ArchivaConfiguration getArchivaConfiguration()
|
||||
{
|
||||
return archivaConfiguration;
|
||||
}
|
||||
|
||||
public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
|
||||
{
|
||||
this.archivaConfiguration = archivaConfiguration;
|
||||
}
|
||||
|
||||
public RoleManager getRoleManager()
|
||||
{
|
||||
|
@ -621,15 +564,6 @@ public class DefaultManagedRepositoryAdmin
|
|||
this.repositorySessionFactory = repositorySessionFactory;
|
||||
}
|
||||
|
||||
public List<AuditListener> getAuditListeners()
|
||||
{
|
||||
return auditListeners;
|
||||
}
|
||||
|
||||
public void setAuditListeners( List<AuditListener> auditListeners )
|
||||
{
|
||||
this.auditListeners = auditListeners;
|
||||
}
|
||||
|
||||
public RepositoryArchivaTaskScheduler getRepositoryTaskScheduler()
|
||||
{
|
||||
|
@ -640,24 +574,4 @@ public class DefaultManagedRepositoryAdmin
|
|||
{
|
||||
this.repositoryTaskScheduler = repositoryTaskScheduler;
|
||||
}
|
||||
|
||||
public Registry getRegistry()
|
||||
{
|
||||
return registry;
|
||||
}
|
||||
|
||||
public void setRegistry( Registry registry )
|
||||
{
|
||||
this.registry = registry;
|
||||
}
|
||||
|
||||
public RepositoryCommonValidator getRepositoryCommonValidator()
|
||||
{
|
||||
return repositoryCommonValidator;
|
||||
}
|
||||
|
||||
public void setRepositoryCommonValidator( RepositoryCommonValidator repositoryCommonValidator )
|
||||
{
|
||||
this.repositoryCommonValidator = repositoryCommonValidator;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,13 +18,14 @@ package org.apache.archiva.admin.repository.remote;
|
|||
* under the License.
|
||||
*/
|
||||
|
||||
import org.apache.archiva.admin.repository.AbstractRepositoryAdmin;
|
||||
import org.apache.archiva.admin.repository.RepositoryAdminException;
|
||||
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.ProxyConnectorConfiguration;
|
||||
import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
@ -34,16 +35,16 @@ import java.util.List;
|
|||
*/
|
||||
@Service( "remoteRepositoryAdmin#default" )
|
||||
public class DefaultRemoteRepositoryAdmin
|
||||
extends AbstractRepositoryAdmin
|
||||
implements RemoteRepositoryAdmin
|
||||
{
|
||||
@Inject
|
||||
private ArchivaConfiguration archivaConfiguration;
|
||||
|
||||
|
||||
public List<RemoteRepository> getRemoteRepositories()
|
||||
throws RepositoryAdminException
|
||||
{
|
||||
List<RemoteRepository> remoteRepositories = new ArrayList<RemoteRepository>();
|
||||
for ( RemoteRepositoryConfiguration repositoryConfiguration : archivaConfiguration.getConfiguration().getRemoteRepositories() )
|
||||
for ( RemoteRepositoryConfiguration repositoryConfiguration : getArchivaConfiguration().getConfiguration().getRemoteRepositories() )
|
||||
{
|
||||
remoteRepositories.add(
|
||||
new RemoteRepository( repositoryConfiguration.getId(), repositoryConfiguration.getName(),
|
||||
|
@ -67,16 +68,63 @@ public class DefaultRemoteRepositoryAdmin
|
|||
return null;
|
||||
}
|
||||
|
||||
public Boolean deleteRemoteRepository( String repositoryId )
|
||||
throws RepositoryAdminException
|
||||
{
|
||||
return null; //To change body of implemented methods use File | Settings | File Templates.
|
||||
}
|
||||
|
||||
public Boolean addRemoteRepository( RemoteRepository remoteRepository )
|
||||
throws RepositoryAdminException
|
||||
{
|
||||
return null; //To change body of implemented methods use File | Settings | File Templates.
|
||||
getRepositoryCommonValidator().basicValidation( remoteRepository, false );
|
||||
|
||||
//TODO we can validate it's a good uri/url
|
||||
if ( StringUtils.isEmpty( remoteRepository.getUrl() ) )
|
||||
{
|
||||
throw new RepositoryAdminException( "url cannot be null" );
|
||||
}
|
||||
|
||||
//MRM-752 - url needs trimming
|
||||
remoteRepository.setUrl( StringUtils.trim( remoteRepository.getUrl() ) );
|
||||
|
||||
RemoteRepositoryConfiguration remoteRepositoryConfiguration = new RemoteRepositoryConfiguration();
|
||||
remoteRepositoryConfiguration.setId( remoteRepository.getId() );
|
||||
remoteRepositoryConfiguration.setPassword( remoteRepository.getPassword() );
|
||||
remoteRepositoryConfiguration.setTimeout( remoteRepository.getTimeout() );
|
||||
remoteRepositoryConfiguration.setUrl( remoteRepository.getUrl() );
|
||||
remoteRepositoryConfiguration.setUsername( remoteRepository.getUserName() );
|
||||
remoteRepositoryConfiguration.setLayout( remoteRepository.getLayout() );
|
||||
remoteRepositoryConfiguration.setName( remoteRepository.getName() );
|
||||
|
||||
Configuration configuration = getArchivaConfiguration().getConfiguration();
|
||||
configuration.addRemoteRepository( remoteRepositoryConfiguration );
|
||||
saveConfiguration( configuration );
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
|
||||
public Boolean deleteRemoteRepository( String repositoryId )
|
||||
throws RepositoryAdminException
|
||||
{
|
||||
Configuration configuration = getArchivaConfiguration().getConfiguration();
|
||||
|
||||
RemoteRepositoryConfiguration remoteRepositoryConfiguration =
|
||||
configuration.getRemoteRepositoriesAsMap().get( repositoryId );
|
||||
if ( remoteRepositoryConfiguration == null )
|
||||
{
|
||||
throw new RepositoryAdminException(
|
||||
"remoteRepository with id " + repositoryId + " not exist cannot remove it" );
|
||||
}
|
||||
|
||||
configuration.removeRemoteRepository( remoteRepositoryConfiguration );
|
||||
|
||||
// [MRM-520] Proxy Connectors are not deleted with the deletion of a Repository.
|
||||
List<ProxyConnectorConfiguration> proxyConnectors = getProxyConnectors();
|
||||
for ( ProxyConnectorConfiguration proxyConnector : proxyConnectors )
|
||||
{
|
||||
if ( StringUtils.equals( proxyConnector.getTargetRepoId(), repositoryId ) )
|
||||
{
|
||||
configuration.removeProxyConnector( proxyConnector );
|
||||
}
|
||||
}
|
||||
|
||||
saveConfiguration( configuration );
|
||||
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
|
||||
public Boolean updateRemoteRepository( RemoteRepository remoteRepository )
|
||||
|
|
|
@ -55,4 +55,45 @@ public class RemoteRepositoryAdminTest
|
|||
assertNull( central.getUserName() );
|
||||
assertNull( central.getPassword() );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addAndDelete()
|
||||
throws Exception
|
||||
{
|
||||
int initialSize = remoteRepositoryAdmin.getRemoteRepositories().size();
|
||||
|
||||
RemoteRepository remoteRepository = getRemoteRepository();
|
||||
|
||||
remoteRepositoryAdmin.addRemoteRepository( remoteRepository );
|
||||
|
||||
assertEquals( initialSize + 1, remoteRepositoryAdmin.getRemoteRepositories().size() );
|
||||
|
||||
RemoteRepository repo = remoteRepositoryAdmin.getRemoteRepository( "foo" );
|
||||
assertNotNull( repo );
|
||||
assertEquals( getRemoteRepository().getPassword(), repo.getPassword() );
|
||||
assertEquals( getRemoteRepository().getUrl(), repo.getUrl() );
|
||||
assertEquals( getRemoteRepository().getUserName(), repo.getUserName() );
|
||||
assertEquals( getRemoteRepository().getName(), repo.getName() );
|
||||
assertEquals( getRemoteRepository().getTimeout(), repo.getTimeout() );
|
||||
|
||||
remoteRepositoryAdmin.deleteRemoteRepository( "foo" );
|
||||
|
||||
assertEquals( initialSize, remoteRepositoryAdmin.getRemoteRepositories().size() );
|
||||
|
||||
repo = remoteRepositoryAdmin.getRemoteRepository( "foo" );
|
||||
assertNull( repo );
|
||||
}
|
||||
|
||||
|
||||
private RemoteRepository getRemoteRepository()
|
||||
{
|
||||
RemoteRepository remoteRepository = new RemoteRepository();
|
||||
remoteRepository.setUrl( "http://foo.com/maven-it-rocks" );
|
||||
remoteRepository.setTimeout( 10 );
|
||||
remoteRepository.setName( "maven foo" );
|
||||
remoteRepository.setUserName( "foo-name" );
|
||||
remoteRepository.setPassword( "toto" );
|
||||
remoteRepository.setId( "foo" );
|
||||
return remoteRepository;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue