You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ka...@apache.org on 2013/08/13 16:02:26 UTC

svn commit: r1513498 - in /directory/escimo/trunk/ldap/src/main: java/org/apache/directory/scim/ldap/ java/org/apache/directory/scim/ldap/handlers/ java/org/apache/directory/scim/ldap/schema/ resources/

Author: kayyagari
Date: Tue Aug 13 14:02:26 2013
New Revision: 1513498

URL: http://svn.apache.org/r1513498
Log:
o load Groups resource schema
o handler for memebers attribute of Groups resource

Added:
    directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/handlers/MembersAttributeHandler.java
Modified:
    directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/LdapSchemaMapper.java
    directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/schema/UserSchema.java
    directory/escimo/trunk/ldap/src/main/resources/escimo-ldap-mapping.xml

Modified: directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/LdapSchemaMapper.java
URL: http://svn.apache.org/viewvc/directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/LdapSchemaMapper.java?rev=1513498&r1=1513497&r2=1513498&view=diff
==============================================================================
--- directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/LdapSchemaMapper.java (original)
+++ directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/LdapSchemaMapper.java Tue Aug 13 14:02:26 2013
@@ -37,13 +37,13 @@ import org.apache.directory.ldap.client.
 import org.apache.directory.scim.AttributeHandler;
 import org.apache.directory.scim.SchemaMapper;
 import org.apache.directory.scim.ldap.schema.ComplexType;
+import org.apache.directory.scim.ldap.schema.GroupSchema;
 import org.apache.directory.scim.ldap.schema.MultiValType;
 import org.apache.directory.scim.ldap.schema.ResourceSchema;
 import org.apache.directory.scim.ldap.schema.SimpleType;
 import org.apache.directory.scim.ldap.schema.SimpleTypeGroup;
 import org.apache.directory.scim.ldap.schema.TypedType;
 import org.apache.directory.scim.ldap.schema.UserSchema;
-import org.apache.directory.scim.schema.BaseType;
 import org.dom4j.Document;
 import org.dom4j.DocumentHelper;
 import org.dom4j.Element;
@@ -62,7 +62,7 @@ public class LdapSchemaMapper implements
 
     private static final Logger LOG = LoggerFactory.getLogger( LdapSchemaMapper.class );
 
-    private Map<String, BaseType> groupSchema = new HashMap<String, BaseType>();
+    private GroupSchema groupSchema;
 
     private UserSchema userSchema;
 
@@ -97,6 +97,12 @@ public class LdapSchemaMapper implements
     }
 
 
+    public GroupSchema getGroupSchema()
+    {
+        return groupSchema;
+    }
+
+
     public void loadMappings()
     {
         InputStream in = this.getClass().getClassLoader().getResourceAsStream( "escimo-ldap-mapping.xml" );
@@ -144,20 +150,17 @@ public class LdapSchemaMapper implements
             userSchema.setAtHandlers( atHandlersMap );
 
             List<Element> lstSchema = root.elements( "schema" );
+            
             List<Element> lstRef = elmUser.elements( "schemaRef" );
             parseResourceSchema( lstRef, lstSchema, userSchema );
 
-            /*
             Element elmGroup = root.element( "groupType" );
-            
-            String baseDn = elmGroup.attributeValue( "baseDn" );
-            String filter = elmGroup.attributeValue( "filter" );
+            String groupBaseDn = elmGroup.attributeValue( "baseDn" );
+            String groupFilter = elmGroup.attributeValue( "filter" );
 
-            userSchema = new GroupSchema( baseDn, filter );
-            
-            List<Element> lstSchema = elmGroup.elements( "schema" );
-            List<Element> lstRef = elmGroup.elements( "schemaRef" );
-            */
+            groupSchema = new GroupSchema( baseDn, filter );
+            List<Element> lstGroupRef = elmGroup.elements( "schemaRef" );
+            parseResourceSchema( lstGroupRef, lstSchema, groupSchema );
         }
         catch ( Exception e )
         {

Added: directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/handlers/MembersAttributeHandler.java
URL: http://svn.apache.org/viewvc/directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/handlers/MembersAttributeHandler.java?rev=1513498&view=auto
==============================================================================
--- directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/handlers/MembersAttributeHandler.java (added)
+++ directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/handlers/MembersAttributeHandler.java Tue Aug 13 14:02:26 2013
@@ -0,0 +1,279 @@
+/*
+ *   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.
+ *
+ */
+package org.apache.directory.scim.ldap.handlers;
+
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.directory.api.ldap.model.constants.SchemaConstants;
+import org.apache.directory.api.ldap.model.cursor.EntryCursor;
+import org.apache.directory.api.ldap.model.entry.Attribute;
+import org.apache.directory.api.ldap.model.entry.BinaryValue;
+import org.apache.directory.api.ldap.model.entry.Entry;
+import org.apache.directory.api.ldap.model.entry.Value;
+import org.apache.directory.api.ldap.model.exception.LdapException;
+import org.apache.directory.api.ldap.model.filter.BranchNode;
+import org.apache.directory.api.ldap.model.filter.ExprNode;
+import org.apache.directory.api.ldap.model.filter.FilterParser;
+import org.apache.directory.api.ldap.model.filter.FilterVisitor;
+import org.apache.directory.api.ldap.model.filter.SimpleNode;
+import org.apache.directory.api.ldap.model.message.SearchScope;
+import org.apache.directory.api.util.Strings;
+import org.apache.directory.scim.AttributeHandler;
+import org.apache.directory.scim.MultiValAttribute;
+import org.apache.directory.scim.RequestContext;
+import org.apache.directory.scim.SimpleAttribute;
+import org.apache.directory.scim.SimpleAttributeGroup;
+import org.apache.directory.scim.ldap.LdapResourceProvider;
+import org.apache.directory.scim.schema.BaseType;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * TODO MembersAttributeHandler.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class MembersAttributeHandler implements AttributeHandler
+{
+
+    private static final Logger LOG = LoggerFactory.getLogger( MembersAttributeHandler.class );
+
+
+    @Override
+    public void handle( BaseType bt, Object srcResource, RequestContext ctx )
+    {
+        if ( !bt.getName().equals( "members" ) )
+        {
+            LOG.warn(
+                "MembersAttributeHandler can only be used on members multivalue attribute, invalid attribute name {}",
+                bt.getName() );
+            return;
+        }
+
+        Entry groupEntry = ( Entry ) srcResource;
+
+        Attribute memberAt = groupEntry.get( SchemaConstants.UNIQUE_MEMBER_AT );
+        if ( memberAt == null )
+        {
+            memberAt = groupEntry.get( SchemaConstants.MEMBER_AT );
+        }
+
+        if ( memberAt == null )
+        {
+            LOG.debug( "Neither member or uniqueMember attribute is present in the entry {}", groupEntry.getDn() );
+            return;
+        }
+
+        List<SimpleAttributeGroup> lstAtGroup = new ArrayList<SimpleAttributeGroup>();
+
+        Iterator<Value<?>> itr = memberAt.iterator();
+        while ( itr.hasNext() )
+        {
+            Value<?> val = itr.next();
+            SimpleAttributeGroup sg = getMemberDetails( val.getString(), ctx );
+            if ( sg != null )
+            {
+                lstAtGroup.add( sg );
+            }
+        }
+
+        if ( !lstAtGroup.isEmpty() )
+        {
+            MultiValAttribute mv = new MultiValAttribute( bt.getName(), lstAtGroup );
+            ctx.getUser().addAttribute( bt.getUri(), mv );
+        }
+    }
+
+
+    private SimpleAttributeGroup getMemberDetails( String dn, RequestContext ctx )
+    {
+        LdapResourceProvider provider = ( LdapResourceProvider ) ctx.getProviderService();
+
+        Entry memberEntry = provider.fetchEntryByDn( dn );
+
+        if ( memberEntry == null )
+        {
+            return null;
+        }
+
+        SimpleAttributeGroup sg = null;
+        
+        try
+        {
+            List<SimpleAttribute> lst = new ArrayList<SimpleAttribute>();
+
+            SimpleAttribute id = new SimpleAttribute( "id", memberEntry.get( SchemaConstants.ENTRY_UUID_AT )
+                .getString() );
+            lst.add( id );
+
+            String locationVal = ctx.getUriInfo().getBaseUri().toString();
+            locationVal = locationVal + "Users/" + id.getValue();
+
+            SimpleAttribute ref = new SimpleAttribute( "$ref", locationVal );
+            lst.add( ref );
+
+            SimpleAttribute display = new SimpleAttribute( "display", memberEntry.getDn().getRdn().getValue()
+                .getString() );
+            lst.add( display );
+
+            sg = new SimpleAttributeGroup( lst );
+        }
+        catch ( LdapException ex )
+        {
+            LOG.warn( "Failed to get attributes from entry {}", memberEntry.getDn() );
+        }
+
+        return sg;
+    }
+
+
+    private List<Entry> getMemberEntriesUsingFilter( String filter, String baseDn, Entry userEntry,
+        LdapResourceProvider provider )
+    {
+        if ( Strings.isEmpty( baseDn ) )
+        {
+            baseDn = ""; // RootDSE
+        }
+
+        if ( Strings.isEmpty( filter ) )
+        {
+            return Collections.EMPTY_LIST;
+        }
+
+        List<Entry> lst = new ArrayList<Entry>();
+
+        try
+        {
+            ExprNode rootNode = FilterParser.parse( filter );
+
+            FilterTokenVisitor tv = new FilterTokenVisitor( userEntry );
+            tv.visit( rootNode );
+
+            EntryCursor cursor = provider.getConnection().search( baseDn, rootNode.toString(), SearchScope.SUBTREE,
+                SchemaConstants.ALL_ATTRIBUTES_ARRAY );
+            while ( cursor.next() )
+            {
+                Entry mvEntry = cursor.get();
+                lst.add( mvEntry );
+            }
+
+            cursor.close();
+        }
+        catch ( Exception e )
+        {
+            LOG.warn( "Failed to get the groups using the filter {} and base DN {}", filter, baseDn );
+            LOG.warn( "", e );
+        }
+
+        return lst;
+    }
+
+    class FilterTokenVisitor implements FilterVisitor
+    {
+        private Entry entry;
+
+
+        FilterTokenVisitor( Entry entry )
+        {
+            this.entry = entry;
+        }
+
+
+        public boolean canVisit( ExprNode node )
+        {
+            return node instanceof ExprNode;
+        }
+
+
+        public List<ExprNode> getOrder( BranchNode node, List<ExprNode> children )
+        {
+            return children;
+        }
+
+
+        public boolean isPrefix()
+        {
+            return false;
+        }
+
+
+        public Object visit( ExprNode node )
+        {
+            if ( node instanceof BranchNode )
+            {
+                BranchNode bnode = ( BranchNode ) node;
+
+                // --------------------------------------------------------------------
+                // we want to check each child leaf node to see if it must be expanded
+                // children that are branch nodes are recursively visited
+                // --------------------------------------------------------------------
+
+                final List<ExprNode> children = bnode.getChildren();
+
+                for ( ExprNode child : children )
+                {
+                    visit( child );
+                }
+            }
+            else
+            {
+                if ( node instanceof SimpleNode )
+                {
+                    SimpleNode sn = ( SimpleNode ) node;
+                    String val = sn.getValue().getString();
+                    if ( val.startsWith( "$" ) )
+                    {
+                        Attribute at = entry.get( val.substring( 1 ) );
+                        if ( at != null )
+                        {
+                            try
+                            {
+                                Value<?> newVal = null;
+                                if ( !at.isHumanReadable() )
+                                {
+                                    newVal = new BinaryValue( at.getAttributeType(), at.getBytes() );
+                                }
+                                else
+                                {
+                                    newVal = new org.apache.directory.api.ldap.model.entry.StringValue(
+                                        at.getAttributeType(), at.getString() );
+                                }
+
+                                sn.setValue( newVal );
+                            }
+                            catch ( LdapException e )
+                            {
+                                LOG.warn( "Failed to set the value for the attribute {} in the filter", at );
+                            }
+                        }
+                    }
+                }
+            }
+
+            return null;
+        }
+
+    }
+}

Modified: directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/schema/UserSchema.java
URL: http://svn.apache.org/viewvc/directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/schema/UserSchema.java?rev=1513498&r1=1513497&r2=1513498&view=diff
==============================================================================
--- directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/schema/UserSchema.java (original)
+++ directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/ldap/schema/UserSchema.java Tue Aug 13 14:02:26 2013
@@ -19,9 +19,6 @@
  */
 package org.apache.directory.scim.ldap.schema;
 
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
 
 /**
  * TODO UserSchema.

Modified: directory/escimo/trunk/ldap/src/main/resources/escimo-ldap-mapping.xml
URL: http://svn.apache.org/viewvc/directory/escimo/trunk/ldap/src/main/resources/escimo-ldap-mapping.xml?rev=1513498&r1=1513497&r2=1513498&view=diff
==============================================================================
--- directory/escimo/trunk/ldap/src/main/resources/escimo-ldap-mapping.xml (original)
+++ directory/escimo/trunk/ldap/src/main/resources/escimo-ldap-mapping.xml Tue Aug 13 14:02:26 2013
@@ -108,17 +108,7 @@
     <schema id="group" uri="urn:scim:schemas:core:1.0">
         <attribute name="displayName" mappedTo="cn" />
         <attribute name="id" mappedTo="entryUUID" />
-        <complex-attribute name="members">
-            <at-group>
-                <attribute name="streetAddress" mappedTo="street" />
-                <attribute name="locality" mappedTo="l" />
-                <attribute name="region" mappedTo="region" />
-                <attribute name="postalCode" mappedTo="postalCode" />
-                <attribute name="country" mappedTo="" />
-                <formatted format="$streetAddress $locality" />
-            </at-group>
-        </complex-attribute>
-
+        <multival-attribute name="members" handlerRef="membersHandler"/>
     </schema>
 
     <userType basedDn="ou=system" filter="(objectClass = inetOrgPerson)">
@@ -139,5 +129,7 @@
             class="org.apache.directory.scim.ldap.handlers.GroupsAttributeHandler" />
         <handler name="photosHandler"
             class="org.apache.directory.scim.ldap.handlers.PhotosAttributeHandler" />
+        <handler name="membersHandler"
+            class="org.apache.directory.scim.ldap.handlers.MembersAttributeHandler" />
     </atHandlers>
 </entities>
\ No newline at end of file