directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From szoer...@apache.org
Subject svn commit: r550100 - in /directory/sandbox/szoerner/groovyldap/src/main: groovy/ java/org/apache/directory/groovyldap/ java/org/apache/directory/groovyldap/jndi/ java/org/apache/directory/groovyldap/util/
Date Sat, 23 Jun 2007 20:33:04 GMT
Author: szoerner
Date: Sat Jun 23 13:33:03 2007
New Revision: 550100

URL: http://svn.apache.org/viewvc?view=rev&rev=550100
Log:
Support for delete and for adding LDIF entries added

Added:
    directory/sandbox/szoerner/groovyldap/src/main/groovy/demoAddLDIF.groovy
    directory/sandbox/szoerner/groovyldap/src/main/groovy/demoDelete.groovy
    directory/sandbox/szoerner/groovyldap/src/main/groovy/demoSearchAndDelete.groovy
    directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/jndi/
    directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/jndi/DirContextToMapObjectFactory.java
      - copied, changed from r550064, directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/DirContextToMapObjectFactory.java
    directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/util/
    directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/util/Util.java
      - copied, changed from r550064, directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/Util.java
Removed:
    directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/DirContextToMapObjectFactory.java
    directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/Util.java
Modified:
    directory/sandbox/szoerner/groovyldap/src/main/groovy/demoAdd.groovy
    directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/LDAP.java

Modified: directory/sandbox/szoerner/groovyldap/src/main/groovy/demoAdd.groovy
URL: http://svn.apache.org/viewvc/directory/sandbox/szoerner/groovyldap/src/main/groovy/demoAdd.groovy?view=diff&rev=550100&r1=550099&r2=550100
==============================================================================
--- directory/sandbox/szoerner/groovyldap/src/main/groovy/demoAdd.groovy (original)
+++ directory/sandbox/szoerner/groovyldap/src/main/groovy/demoAdd.groovy Sat Jun 23 13:33:03
2007
@@ -9,3 +9,4 @@
 ]
 ldap.add('cn=Heather Nova,dc=example,dc=com', heather)
 
+assert ldap.exists('cn=Heather Nova,dc=example,dc=com');
\ No newline at end of file

Added: directory/sandbox/szoerner/groovyldap/src/main/groovy/demoAddLDIF.groovy
URL: http://svn.apache.org/viewvc/directory/sandbox/szoerner/groovyldap/src/main/groovy/demoAddLDIF.groovy?view=auto&rev=550100
==============================================================================
--- directory/sandbox/szoerner/groovyldap/src/main/groovy/demoAddLDIF.groovy (added)
+++ directory/sandbox/szoerner/groovyldap/src/main/groovy/demoAddLDIF.groovy Sat Jun 23 13:33:03
2007
@@ -0,0 +1,16 @@
+import org.apache.directory.groovyldap.LDAP
+
+ldap = LDAP.newInstance('ldap://zanzibar:10389', 'uid=admin,ou=system' ,'secret')
+
+name = 'Heather Nova'
+
+ldap.add("""
+dn: cn=${name},dc=example,dc=com
+objectClass: person
+objectClass: top
+cn: ${name}
+sn: Nova
+description: The description
+""");
+    
+assert ldap.exists('cn=Heather Nova,dc=example,dc=com');
\ No newline at end of file

Added: directory/sandbox/szoerner/groovyldap/src/main/groovy/demoDelete.groovy
URL: http://svn.apache.org/viewvc/directory/sandbox/szoerner/groovyldap/src/main/groovy/demoDelete.groovy?view=auto&rev=550100
==============================================================================
--- directory/sandbox/szoerner/groovyldap/src/main/groovy/demoDelete.groovy (added)
+++ directory/sandbox/szoerner/groovyldap/src/main/groovy/demoDelete.groovy Sat Jun 23 13:33:03
2007
@@ -0,0 +1,8 @@
+import org.apache.directory.groovyldap.LDAP
+
+ldap = LDAP.newInstance('ldap://zanzibar:10389', 'uid=admin,ou=system' ,'secret')
+
+assert ldap.exists('cn=Heather Nova,dc=example,dc=com')
+
+ldap.delete('cn=Heather Nova,dc=example,dc=com')
+

Added: directory/sandbox/szoerner/groovyldap/src/main/groovy/demoSearchAndDelete.groovy
URL: http://svn.apache.org/viewvc/directory/sandbox/szoerner/groovyldap/src/main/groovy/demoSearchAndDelete.groovy?view=auto&rev=550100
==============================================================================
--- directory/sandbox/szoerner/groovyldap/src/main/groovy/demoSearchAndDelete.groovy (added)
+++ directory/sandbox/szoerner/groovyldap/src/main/groovy/demoSearchAndDelete.groovy Sat Jun
23 13:33:03 2007
@@ -0,0 +1,7 @@
+import org.apache.directory.groovyldap.*
+
+ldap = LDAP.newInstance('ldap://zanzibar:10389', 'uid=admin,ou=system' ,'secret')
+
+ldap.eachEntry(base:'dc=example,dc=com', filter:'(objectClass=person)', scope:'ONE') { entry
->
+    ldap.delete(entry.dn)
+}

Modified: directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/LDAP.java
URL: http://svn.apache.org/viewvc/directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/LDAP.java?view=diff&rev=550100&r1=550099&r2=550100
==============================================================================
--- directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/LDAP.java
(original)
+++ directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/LDAP.java
Sat Jun 23 13:33:03 2007
@@ -29,9 +29,11 @@
 import java.util.Properties;
 
 import javax.naming.Context;
+import javax.naming.NameNotFoundException;
 import javax.naming.NamingEnumeration;
 import javax.naming.NamingException;
 import javax.naming.directory.Attribute;
+import javax.naming.directory.Attributes;
 import javax.naming.directory.BasicAttributes;
 import javax.naming.directory.ModificationItem;
 import javax.naming.directory.SearchControls;
@@ -39,6 +41,8 @@
 import javax.naming.ldap.InitialLdapContext;
 import javax.naming.ldap.LdapContext;
 
+import org.apache.directory.groovyldap.util.Util;
+
 
 /**
  * A wrapper class which provides LDAP functionality to Groovy.
@@ -64,7 +68,7 @@
         Properties env = new Properties();
         env.setProperty( Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"
);
         env.setProperty( Context.PROVIDER_URL, url );
-        env.setProperty( Context.OBJECT_FACTORIES, "org.apache.directory.groovyldap.DirContextToMapObjectFactory"
);
+        env.setProperty( Context.OBJECT_FACTORIES, "org.apache.directory.groovyldap.jndi.DirContextToMapObjectFactory"
);
         if ( !anonymousBind )
         {
             env.setProperty( Context.SECURITY_PRINCIPAL, bindUser );
@@ -127,7 +131,7 @@
 
 
     /**
-     * LDAP add operation. Adds a new entry to the directory.
+     * LDAP add operation for a map. Adds a new entry to the directory.
      * 
      * @param dn DN of the entry
      * @param attributes attributes of the entry
@@ -142,7 +146,22 @@
             Attribute attr = Util.createAttribute( key, attributes.get( key ) );
             attrs.put( attr );
         }
+        addInternal( dn, attrs );
+    }
+
 
+    /**
+     * LDAP add operation for an LDIF entry. Adds a new entry to the directory, which is
given in LDIF. The dn is given via the first LDIF line.
+     */
+    public void add( String ldifEntry ) throws NamingException
+    {
+        String dn = Util.getDNFromLDIFEntry( ldifEntry );
+        Attributes attrs = Util.getAttributesFromLDIF( ldifEntry );
+        addInternal( dn, attrs );
+    }
+
+    protected void addInternal( String dn, Attributes attrs ) throws NamingException
+    {
         LdapContext ctx = null;
         try
         {
@@ -159,20 +178,108 @@
         }
     }
 
+    /**
+     * LDAP delete operation. Deletes an entry from the directory.
+     * 
+     * @param dn DN of the entry
+     * @param attributes attributes of the entry
+     * @throws NamingException
+     */
+    public void delete( String dn ) throws NamingException
+    {
+        if ( !exists( dn ) )
+        {
+            throw new NameNotFoundException( "Entry " + dn + " does not exist!" );
+        }
+
+        LdapContext ctx = null;
+        try
+        {
+            ctx = new InitialLdapContext( createEnvironment(), null );
+            ctx.destroySubcontext( dn );
+        }
+        catch ( NamingException ne )
+        {
+            throw ne;
+        }
+        finally
+        {
+            ctx.close();
+        }
+    }
+
+
+    /**
+     * Check whether an entry with the given DN exists. The method performs a search to check
this.
+     */
+    public boolean exists( String dn ) throws NamingException
+    {
+        LdapContext ctx = null;
+        boolean result = false;
+        try
+        {
+            ctx = new InitialLdapContext( createEnvironment(), null );
+
+            SearchControls ctls = new SearchControls();
+            ctls.setSearchScope( SearchControls.OBJECT_SCOPE );
+            ctls.setReturningAttributes( new String[0] );
+            ctls.setReturningObjFlag( false );
+
+            try
+            {
+                ctx.search( dn, "(objectClass=*)", ctls );
+                result = true;
+            }
+            catch ( NameNotFoundException nne )
+            {
+            }
+        }
+        catch ( NamingException ne )
+        {
+            throw ne;
+        }
+        finally
+        {
+            ctx.close();
+        }
+        return result;
+    }
+
 
     public void eachEntry( String filter, String base, SearchScope scope, Closure closure
) throws NamingException
     {
 
+        Search search = new Search();
+        search.setFilter( filter );
+        search.setBase( base );
+        search.setScope( scope );
+
+        eachEntry( search, closure );
+    }
+
+
+    public void eachEntry( Map<String, Object> searchParams, Closure closure ) throws
NamingException
+    {
+        Search search = new Search( searchParams );
+        eachEntry( search, closure );
+    }
+
+
+    public void eachEntry( Search search, Closure closure ) throws NamingException
+    {
+
         LdapContext ctx = null;
         try
         {
             ctx = new InitialLdapContext( createEnvironment(), null );
 
             SearchControls ctls = new SearchControls();
-            ctls.setSearchScope( scope.getJndiValue() );
+            ctls.setSearchScope( search.getScope().getJndiValue() );
+            ctls.setReturningAttributes( search.getAttrs() );
             ctls.setReturningObjFlag( true );
 
-            NamingEnumeration<SearchResult> enm = ctx.search( base, filter, ctls );
+            NamingEnumeration<SearchResult> enm = ctx.search( search.getBase(), search.getFilter(),
search
+                .getFilterArgs(), ctls );
             while ( enm.hasMore() )
             {
                 SearchResult sr = enm.next();

Copied: directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/jndi/DirContextToMapObjectFactory.java
(from r550064, directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/DirContextToMapObjectFactory.java)
URL: http://svn.apache.org/viewvc/directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/jndi/DirContextToMapObjectFactory.java?view=diff&rev=550100&p1=directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/DirContextToMapObjectFactory.java&r1=550064&p2=directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/jndi/DirContextToMapObjectFactory.java&r2=550100
==============================================================================
--- directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/DirContextToMapObjectFactory.java
(original)
+++ directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/jndi/DirContextToMapObjectFactory.java
Sat Jun 23 13:33:03 2007
@@ -17,7 +17,7 @@
  *  under the License. 
  *  
  */
-package org.apache.directory.groovyldap;
+package org.apache.directory.groovyldap.jndi;
 
 
 import java.util.ArrayList;

Copied: directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/util/Util.java
(from r550064, directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/Util.java)
URL: http://svn.apache.org/viewvc/directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/util/Util.java?view=diff&rev=550100&p1=directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/Util.java&r1=550064&p2=directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/util/Util.java&r2=550100
==============================================================================
--- directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/Util.java
(original)
+++ directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/util/Util.java
Sat Jun 23 13:33:03 2007
@@ -17,13 +17,18 @@
  *  under the License. 
  *  
  */
-package org.apache.directory.groovyldap;
+package org.apache.directory.groovyldap.util;
 
 
+import java.io.IOException;
+import java.io.LineNumberReader;
+import java.io.StringReader;
 import java.util.Collection;
 
 import javax.naming.directory.Attribute;
+import javax.naming.directory.Attributes;
 import javax.naming.directory.BasicAttribute;
+import javax.naming.directory.BasicAttributes;
 
 /**
  * Utility methods.
@@ -45,7 +50,7 @@
      * @param value 
      * @return a JNDI attribute object
      */
-    static Attribute createAttribute( String name, Object value )
+    public static Attribute createAttribute( String name, Object value )
     {
         Attribute attr = new BasicAttribute( name );
         if ( value instanceof Collection )
@@ -62,5 +67,85 @@
         }
         return attr;
     }
+    
+    /** 
+     */
+    public static Attributes getAttributesFromLDIF( String ldifEntry )
+    {
+        Attributes attrs = new BasicAttributes();
+        
+        StringReader sr = new StringReader(ldifEntry);
+        LineNumberReader lnr = new LineNumberReader(sr);
+        
+        try {
+            String line = null;
+            boolean dnFound = false;
+            while ((line = lnr.readLine()) != null) {    
+                System.out.println("Line: ["+line+"]");
+                
+                if (!dnFound && line.trim().equals( "" )) {
+                    continue;
+                }
+                int pos = line.indexOf( ':' ); 
+                if (pos == -1) {
+                    throw new IllegalArgumentException("error in LDIF, line"+lnr.getLineNumber()+",
no ':' found.");
+                }
+                String attrName = line.substring( 0, pos ).trim();
+                
+                if (attrName.equals( "dn" ) && dnFound) {
+                    throw new IllegalArgumentException("error in LDIF, line"+lnr.getLineNumber()+",
dn found twice");
+                } else if (attrName.equals( "dn" )) {
+                    dnFound = true;
+                } else {
+                    String value = line.substring( pos+1 ).trim();
+                    if (attrs.get( attrName ) == null) {
+                        Attribute attr = new BasicAttribute(attrName, value);
+                        attrs.put( attr );
+                    } else {
+                        Attribute attr = attrs.get( attrName );
+                        attr.add( value );
+                    }
+                }
+            }
+        } catch (IOException io) {
+            // ignored
+        }
+        
+        return attrs;
+    }
+    
+    /** 
+     */
+    public static String getDNFromLDIFEntry( String ldifEntry )
+    {
+        String dn = null;
+        
+        StringReader sr = new StringReader(ldifEntry);
+        LineNumberReader lnr = new LineNumberReader(sr);
+        
+        try {
+            String line = null;
+            boolean dnFound = false;
+            while ((line = lnr.readLine()) != null) {    
+                if (!dnFound && line.trim().equals( "" )) {
+                    continue;
+                }
+                int pos = line.indexOf( ':' ); 
+                if (pos == -1) {
+                    throw new IllegalArgumentException("error in LDIF, line"+lnr.getLineNumber()+",
no ':' found.");
+                }
+                
+                String attrName = line.substring( 0, pos ).trim();
+                if (attrName.equals( "dn" )) {
+                    dn = line.substring( pos+1 ).trim();
+                } 
+            }
+        } catch (IOException io) {
+            // ignored
+        }
+        
+        return dn;
+    }
+
 
 }



Mime
View raw message