]> source.dussan.org Git - archiva.git/commitdiff
cleanup interface and add default implementation
authorOlivier Lamy <olamy@apache.org>
Wed, 30 Jan 2013 17:46:05 +0000 (17:46 +0000)
committerOlivier Lamy <olamy@apache.org>
Wed, 30 Jan 2013 17:46:05 +0000 (17:46 +0000)
git-svn-id: https://svn.apache.org/repos/asf/archiva/redback/redback-core/trunk@1440551 13f79535-47bb-0310-9956-ffa450edef68

redback-common/redback-common-ldap/src/main/java/org/apache/archiva/redback/common/ldap/role/DefaultLdapRoleMapperConfiguration.java
redback-common/redback-common-ldap/src/main/java/org/apache/archiva/redback/common/ldap/role/LdapRoleMapperConfiguration.java
redback-integrations/redback-rest/redback-rest-api/src/main/java/org/apache/archiva/redback/rest/api/model/LdapGroupMapping.java
redback-integrations/redback-rest/redback-rest-api/src/main/java/org/apache/archiva/redback/rest/api/model/StringList.java [new file with mode: 0644]
redback-integrations/redback-rest/redback-rest-api/src/main/java/org/apache/archiva/redback/rest/api/services/LdapGroupMappingService.java
redback-integrations/redback-rest/redback-rest-services/src/main/java/org/apache/archiva/redback/rest/services/DefaultLdapGroupMappingService.java [new file with mode: 0644]

index 1ad54af4597bd21360ad5a64425bd4f6724f53c6..262fa4b8ee6503e601907e82218a0b2064936237 100644 (file)
@@ -38,7 +38,7 @@ import java.util.Map;
  * @author Olivier Lamy
  * @since 2.1
  */
-@Service("ldapRoleMapperConfiguration#default")
+@Service( "ldapRoleMapperConfiguration#default" )
 public class DefaultLdapRoleMapperConfiguration
     implements LdapRoleMapperConfiguration
 {
@@ -46,10 +46,9 @@ public class DefaultLdapRoleMapperConfiguration
     private Logger log = LoggerFactory.getLogger( getClass() );
 
     @Inject
-    @Named(value = "userConfiguration#default")
+    @Named( value = "userConfiguration#default" )
     private UserConfiguration userConf;
 
-
     public void addLdapMapping( String ldapGroup, List<String> roles )
         throws MappingException
     {
@@ -61,6 +60,12 @@ public class DefaultLdapRoleMapperConfiguration
         log.warn( "removeLdapMapping not implemented" );
     }
 
+    public void updateLdapMapping( String ldapGroup, List<String> roles )
+        throws MappingException
+    {
+        log.warn( "removeLdapMapping not implemented" );
+    }
+
     public void setLdapGroupMappings( Map<String, Collection<String>> mappings )
         throws MappingException
     {
index d3fc17d5831841d549053d81a2f1b851fbb8310d..f2f3aa19ec42101fdc1b1bcba9e33d053e89c719 100644 (file)
@@ -39,6 +39,15 @@ public interface LdapRoleMapperConfiguration
     void addLdapMapping( String ldapGroup, List<String> roles )
         throws MappingException;
 
+    /**
+     * update an existing mapping
+     * @param ldapGroup
+     * @param roles
+     * @throws MappingException
+     */
+    void updateLdapMapping( String ldapGroup, List<String> roles )
+        throws MappingException;
+
     /**
      * remove a mapping
      *
index caaec016ff1004d877a883844c7d0e8feb072185..a9a66ebf29798d0fb7b011f51918cf9226ceb8e3 100644 (file)
@@ -19,25 +19,28 @@ package org.apache.archiva.redback.rest.api.model;
  */
 
 import javax.xml.bind.annotation.XmlRootElement;
+import java.io.Serializable;
+import java.util.Collection;
 import java.util.List;
 
 /**
  * @author Olivier Lamy
  * @since 2.1
  */
-@XmlRootElement( name = "ldapGroupMapping" )
+@XmlRootElement(name = "ldapGroupMapping")
 public class LdapGroupMapping
+    implements Serializable
 {
     private String group;
 
-    private List<String> roleNames;
+    private Collection<String> roleNames;
 
     public LdapGroupMapping()
     {
         // no op
     }
 
-    public LdapGroupMapping( String group, List<String> roleNames )
+    public LdapGroupMapping( String group, Collection<String> roleNames )
     {
         this.group = group;
         this.roleNames = roleNames;
@@ -53,12 +56,12 @@ public class LdapGroupMapping
         this.group = group;
     }
 
-    public List<String> getRoleNames()
+    public Collection<String> getRoleNames()
     {
         return roleNames;
     }
 
-    public void setRoleNames( List<String> roleNames )
+    public void setRoleNames( Collection<String> roleNames )
     {
         this.roleNames = roleNames;
     }
diff --git a/redback-integrations/redback-rest/redback-rest-api/src/main/java/org/apache/archiva/redback/rest/api/model/StringList.java b/redback-integrations/redback-rest/redback-rest-api/src/main/java/org/apache/archiva/redback/rest/api/model/StringList.java
new file mode 100644 (file)
index 0000000..97dba5b
--- /dev/null
@@ -0,0 +1,55 @@
+package org.apache.archiva.redback.rest.api.model;
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import javax.xml.bind.annotation.XmlRootElement;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * jaxrs fail to return List<String> so use this contains for rest services returning that
+ *
+ * @author Olivier Lamy
+ * @since 2.1
+ */
+@XmlRootElement( name = "stringList" )
+public class StringList
+{
+    private List<String> strings;
+
+    public StringList()
+    {
+        // no op
+    }
+
+    public StringList( List<String> strings )
+    {
+        this.strings = strings;
+    }
+
+    public List<String> getStrings()
+    {
+        return strings == null ? new ArrayList<String>( 0 ) : strings;
+    }
+
+    public void setStrings( List<String> strings )
+    {
+        this.strings = strings;
+    }
+}
index 170cfeb09539760512ac7872393c972d2f7c3122..46b6950dcca3c9d991eeccfbbea4adbb64ccf090 100644 (file)
@@ -21,6 +21,7 @@ package org.apache.archiva.redback.rest.api.services;
 import org.apache.archiva.redback.authorization.RedbackAuthorization;
 import org.apache.archiva.redback.integration.security.role.RedbackRoleConstants;
 import org.apache.archiva.redback.rest.api.model.LdapGroupMapping;
+import org.apache.archiva.redback.rest.api.model.StringList;
 
 import javax.ws.rs.Consumes;
 import javax.ws.rs.DELETE;
@@ -44,7 +45,7 @@ public interface LdapGroupMappingService
     @GET
     @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } )
     @RedbackAuthorization( permissions = RedbackRoleConstants.USER_ADMINISTRATOR_ROLE )
-    String getLdapGroups()
+    StringList getLdapGroups()
         throws RedbackServiceException;
 
 
diff --git a/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/apache/archiva/redback/rest/services/DefaultLdapGroupMappingService.java b/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/apache/archiva/redback/rest/services/DefaultLdapGroupMappingService.java
new file mode 100644 (file)
index 0000000..62c595f
--- /dev/null
@@ -0,0 +1,186 @@
+package org.apache.archiva.redback.rest.services;
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.archiva.redback.common.ldap.MappingException;
+import org.apache.archiva.redback.common.ldap.connection.LdapConnection;
+import org.apache.archiva.redback.common.ldap.connection.LdapConnectionFactory;
+import org.apache.archiva.redback.common.ldap.connection.LdapException;
+import org.apache.archiva.redback.common.ldap.role.LdapRoleMapper;
+import org.apache.archiva.redback.common.ldap.role.LdapRoleMapperConfiguration;
+import org.apache.archiva.redback.rest.api.model.LdapGroupMapping;
+import org.apache.archiva.redback.rest.api.model.StringList;
+import org.apache.archiva.redback.rest.api.services.LdapGroupMappingService;
+import org.apache.archiva.redback.rest.api.services.RedbackServiceException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.inject.Inject;
+import javax.inject.Named;
+import javax.naming.NamingException;
+import javax.naming.directory.DirContext;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author Olivier Lamy
+ * @since 2.1
+ */
+public class DefaultLdapGroupMappingService
+    implements LdapGroupMappingService
+{
+    private Logger log = LoggerFactory.getLogger( getClass() );
+
+    @Inject
+    private LdapRoleMapper ldapRoleMapper;
+
+    @Inject
+    @Named( value = "ldapRoleMapperConfiguration#default" )
+    private LdapRoleMapperConfiguration ldapRoleMapperConfiguration;
+
+    @Inject
+    private LdapConnectionFactory ldapConnectionFactory;
+
+    public StringList getLdapGroups()
+        throws RedbackServiceException
+    {
+        LdapConnection ldapConnection = null;
+
+        DirContext context = null;
+
+        try
+        {
+            ldapConnection = ldapConnectionFactory.getConnection();
+            return new StringList( ldapRoleMapper.getAllGroups( context ) );
+        }
+        catch ( LdapException e )
+        {
+            log.error( e.getMessage(), e );
+            throw new RedbackServiceException( e.getMessage() );
+        }
+        catch ( MappingException e )
+        {
+            log.error( e.getMessage(), e );
+            throw new RedbackServiceException( e.getMessage() );
+        }
+        finally
+        {
+            closeContext( context );
+            closeLdapConnection( ldapConnection );
+        }
+    }
+
+    public List<LdapGroupMapping> getLdapGroupMappings()
+        throws RedbackServiceException
+    {
+        try
+        {
+            Map<String, Collection<String>> map = ldapRoleMapperConfiguration.getLdapGroupMappings();
+            List<LdapGroupMapping> ldapGroupMappings = new ArrayList<LdapGroupMapping>( map.size() );
+            for ( Map.Entry<String, Collection<String>> entry : map.entrySet() )
+            {
+                LdapGroupMapping ldapGroupMapping = new LdapGroupMapping( entry.getKey(), entry.getValue() );
+                ldapGroupMappings.add( ldapGroupMapping );
+            }
+
+            return ldapGroupMappings;
+        }
+        catch ( MappingException e )
+        {
+            log.error( e.getMessage(), e );
+            throw new RedbackServiceException( e.getMessage() );
+        }
+    }
+
+    public Boolean addLdapGroupMapping( LdapGroupMapping ldapGroupMapping )
+        throws RedbackServiceException
+    {
+        try
+        {
+            ldapRoleMapperConfiguration.addLdapMapping( ldapGroupMapping.getGroup(),
+                                                        new ArrayList( ldapGroupMapping.getRoleNames() ) );
+        }
+        catch ( MappingException e )
+        {
+            log.error( e.getMessage(), e );
+            throw new RedbackServiceException( e.getMessage() );
+        }
+        return Boolean.TRUE;
+    }
+
+    public Boolean removeLdapGroupMapping( String group )
+        throws RedbackServiceException
+    {
+        try
+        {
+            ldapRoleMapperConfiguration.removeLdapMapping( group );
+        }
+        catch ( MappingException e )
+        {
+            log.error( e.getMessage(), e );
+            throw new RedbackServiceException( e.getMessage() );
+        }
+        return Boolean.TRUE;
+    }
+
+    public Boolean updateLdapGroupMapping( LdapGroupMapping ldapGroupMapping )
+        throws RedbackServiceException
+    {
+        try
+        {
+            ldapRoleMapperConfiguration.updateLdapMapping( ldapGroupMapping.getGroup(),
+                                                           new ArrayList( ldapGroupMapping.getRoleNames() ) );
+        }
+        catch ( MappingException e )
+        {
+            log.error( e.getMessage(), e );
+            throw new RedbackServiceException( e.getMessage() );
+        }
+        return Boolean.TRUE;
+    }
+
+    //------------------
+    // utils
+    //------------------
+
+    protected void closeLdapConnection( LdapConnection ldapConnection )
+    {
+        if ( ldapConnection != null )
+        {
+            ldapConnection.close();
+        }
+    }
+
+    protected void closeContext( DirContext context )
+    {
+        if ( context != null )
+        {
+            try
+            {
+                context.close();
+            }
+            catch ( NamingException e )
+            {
+                log.warn( "skip issue closing context: {}", e.getMessage() );
+            }
+        }
+    }
+}