]> source.dussan.org Git - archiva.git/commitdiff
add a service to be able to check ldap configuration
authorOlivier Lamy <olamy@apache.org>
Thu, 13 Dec 2012 20:03:16 +0000 (20:03 +0000)
committerOlivier Lamy <olamy@apache.org>
Thu, 13 Dec 2012 20:03:16 +0000 (20:03 +0000)
git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1421465 13f79535-47bb-0310-9956-ffa450edef68

archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/ArchivaRuntimeConfigurationService.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultArchivaRuntimeConfigurationService.java

index f67f4aed88b8dc5029957846fd080566a5ac8ef3..db037fa016cdf2f98474e6476c2ec55c08b1c643 100644 (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;
 }
index 30413abdedeb6e5f8d05d37935b190498a141d9b..867e451c02fe55cdabbee9f9a7cf51f56f1b5b55 100644 (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;
+    }
 }