add a service to be able to check ldap configuration

git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1421465 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Olivier Lamy 2012-12-13 20:03:16 +00:00
parent fedd25bc10
commit 6ccf27b7d1
2 changed files with 114 additions and 14 deletions

View File

@ -19,12 +19,14 @@ package org.apache.archiva.rest.api.services;
*/
import org.apache.archiva.admin.model.beans.ArchivaRuntimeConfiguration;
import org.apache.archiva.admin.model.beans.LdapConfiguration;
import org.apache.archiva.redback.authorization.RedbackAuthorization;
import org.apache.archiva.rest.api.model.UserManagerImplementationInformation;
import org.apache.archiva.security.common.ArchivaRoleConstants;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
@ -35,28 +37,44 @@ import java.util.List;
* @author Olivier Lamy
* @since 1.4-M4
*/
@Path ( "/archivaRuntimeConfigurationService/" )
@Path("/archivaRuntimeConfigurationService/")
public interface ArchivaRuntimeConfigurationService
{
@Path ( "archivaRuntimeConfiguration" )
@Path("archivaRuntimeConfiguration")
@GET
@Produces ( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } )
@RedbackAuthorization ( permissions = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION )
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@RedbackAuthorization(permissions = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION)
ArchivaRuntimeConfiguration getArchivaRuntimeConfigurationAdmin()
throws ArchivaRestServiceException;
@Path ( "archivaRuntimeConfiguration" )
@Path("archivaRuntimeConfiguration")
@PUT
@Produces ( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } )
@Consumes ( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } )
@RedbackAuthorization ( permissions = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION )
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@RedbackAuthorization(permissions = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION)
Boolean updateArchivaRuntimeConfiguration( ArchivaRuntimeConfiguration archivaRuntimeConfiguration )
throws ArchivaRestServiceException;
@Path ( "userManagerImplementationInformation" )
@Path("userManagerImplementationInformation")
@GET
@Produces ( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } )
@RedbackAuthorization ( permissions = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION )
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@RedbackAuthorization(permissions = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION)
List<UserManagerImplementationInformation> getUserManagerImplementationInformations()
throws ArchivaRestServiceException;
@Path( "checkLdapConnection" )
@GET
@Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } )
@RedbackAuthorization( permissions = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION )
Boolean checkLdapConnection()
throws ArchivaRestServiceException;
@Path( "checkLdapConnection" )
@POST
@Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } )
@Consumes( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } )
@RedbackAuthorization( permissions = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION )
Boolean checkLdapConnection( LdapConfiguration ldapConfiguration )
throws ArchivaRestServiceException;
}

View File

@ -20,9 +20,12 @@ package org.apache.archiva.rest.services;
import org.apache.archiva.admin.model.RepositoryAdminException;
import org.apache.archiva.admin.model.beans.ArchivaRuntimeConfiguration;
import org.apache.archiva.admin.model.beans.LdapConfiguration;
import org.apache.archiva.admin.model.runtime.ArchivaRuntimeConfigurationAdmin;
import org.apache.archiva.redback.common.ldap.connection.LdapConnection;
import org.apache.archiva.redback.common.ldap.connection.LdapConnectionConfiguration;
import org.apache.archiva.redback.common.ldap.connection.LdapConnectionFactory;
import org.apache.archiva.redback.common.ldap.connection.LdapException;
import org.apache.archiva.redback.policy.CookieSettings;
import org.apache.archiva.redback.policy.PasswordRule;
import org.apache.archiva.redback.users.UserManager;
@ -35,17 +38,19 @@ import org.springframework.stereotype.Service;
import javax.inject.Inject;
import javax.inject.Named;
import javax.naming.InvalidNameException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Properties;
/**
* @author Olivier Lamy
* @since 1.4-M4
*/
@Service( "archivaRuntimeConfigurationService#rest" )
@Service("archivaRuntimeConfigurationService#rest")
public class DefaultArchivaRuntimeConfigurationService
extends AbstractRestService
implements ArchivaRuntimeConfigurationService
@ -54,14 +59,14 @@ public class DefaultArchivaRuntimeConfigurationService
private ArchivaRuntimeConfigurationAdmin archivaRuntimeConfigurationAdmin;
@Inject
@Named( value = "userManager#configurable" )
@Named(value = "userManager#configurable")
private UserManager userManager;
@Inject
private ApplicationContext applicationContext;
@Inject
@Named( value = "ldapConnectionFactory#configurable" )
@Named(value = "ldapConnectionFactory#configurable")
private LdapConnectionFactory ldapConnectionFactory;
public ArchivaRuntimeConfiguration getArchivaRuntimeConfigurationAdmin()
@ -154,6 +159,83 @@ public class DefaultArchivaRuntimeConfigurationService
return informations;
}
public Boolean checkLdapConnection()
throws ArchivaRestServiceException
{
LdapConnection ldapConnection = null;
try
{
ldapConnection = ldapConnectionFactory.getConnection();
}
catch ( LdapException e )
{
log.warn( "fail to get LdapConnection: {}", e.getMessage() );
throw new ArchivaRestServiceException( e.getMessage(), e );
}
finally
{
if ( ldapConnection != null )
{
ldapConnection.close();
}
}
return Boolean.TRUE;
}
public Boolean checkLdapConnection( LdapConfiguration ldapConfiguration )
throws ArchivaRestServiceException
{
LdapConnection ldapConnection = null;
try
{
LdapConnectionConfiguration ldapConnectionConfiguration =
new LdapConnectionConfiguration( ldapConfiguration.getHostName(), ldapConfiguration.getPort(),
ldapConfiguration.getBaseDn(), ldapConfiguration.getContextFactory(),
ldapConfiguration.getBindDn(), ldapConfiguration.getPassword(),
ldapConfiguration.getAuthenticationMethod(),
toProperties( ldapConfiguration.getExtraProperties() ) );
ldapConnection = ldapConnectionFactory.getConnection( ldapConnectionConfiguration );
}
catch ( InvalidNameException e )
{
log.warn( "fail to get LdapConnection: {}", e.getMessage() );
throw new ArchivaRestServiceException( e.getMessage(), e );
}
catch ( LdapException e )
{
log.warn( "fail to get LdapConnection: {}", e.getMessage() );
throw new ArchivaRestServiceException( e.getMessage(), e );
}
finally
{
if ( ldapConnection != null )
{
ldapConnection.close();
}
}
return Boolean.TRUE;
}
private Properties toProperties( Map<String, String> map )
{
Properties properties = new Properties();
if ( map == null || map.isEmpty() )
{
return properties;
}
for ( Map.Entry<String, String> entry : map.entrySet() )
{
properties.put( entry.getKey(), entry.getValue() );
}
return properties;
}
}