directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From szoer...@apache.org
Subject svn commit: r547981 - in /directory/sandbox/szoerner/groovyldap/src: ./ main/ main/groovy/ main/java/ main/java/org/ main/java/org/apache/ main/java/org/apache/directory/ main/java/org/apache/directory/groovyldap/
Date Sat, 16 Jun 2007 21:02:13 GMT
Author: szoerner
Date: Sat Jun 16 14:02:12 2007
New Revision: 547981

URL: http://svn.apache.org/viewvc?view=rev&rev=547981
Log:
A simple prototype to start with

Added:
    directory/sandbox/szoerner/groovyldap/src/
    directory/sandbox/szoerner/groovyldap/src/main/
    directory/sandbox/szoerner/groovyldap/src/main/groovy/
    directory/sandbox/szoerner/groovyldap/src/main/groovy/demoAdd.groovy
    directory/sandbox/szoerner/groovyldap/src/main/groovy/demoRead.groovy
    directory/sandbox/szoerner/groovyldap/src/main/groovy/demoSearch.groovy
    directory/sandbox/szoerner/groovyldap/src/main/java/
    directory/sandbox/szoerner/groovyldap/src/main/java/org/
    directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/
    directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/
    directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/
    directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/DirContextToMapObjectFactory.java
    directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/LDAPConnection.java
    directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/SearchScope.java

Added: 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=auto&rev=547981
==============================================================================
--- directory/sandbox/szoerner/groovyldap/src/main/groovy/demoAdd.groovy (added)
+++ directory/sandbox/szoerner/groovyldap/src/main/groovy/demoAdd.groovy Sat Jun 16 14:02:12
2007
@@ -0,0 +1,14 @@
+import org.apache.directory.groovyldap.LDAPConnection
+
+con = new LDAPConnection(hostname: 'zanzibar', port: 10389)
+con.connect('uid=admin,ou=system', 'secret');
+
+heather = [:]
+heather.objectclass = ['top', 'person']
+heather.sn = 'Nova'
+heather.cn = 'Heather Nova'
+
+con.add('cn=Heather Nova,dc=example,dc=com', heather)
+
+   
+con.disconnect()
\ No newline at end of file

Added: directory/sandbox/szoerner/groovyldap/src/main/groovy/demoRead.groovy
URL: http://svn.apache.org/viewvc/directory/sandbox/szoerner/groovyldap/src/main/groovy/demoRead.groovy?view=auto&rev=547981
==============================================================================
--- directory/sandbox/szoerner/groovyldap/src/main/groovy/demoRead.groovy (added)
+++ directory/sandbox/szoerner/groovyldap/src/main/groovy/demoRead.groovy Sat Jun 16 14:02:12
2007
@@ -0,0 +1,12 @@
+import org.apache.directory.groovyldap.LDAPConnection
+
+con = new LDAPConnection(hostname: 'zanzibar', port: 10389)
+con.connect('uid=admin,ou=system', 'secret');
+
+// Simple entry lookup via dn
+tori = con.read('cn=Tori Amos,dc=example,dc=com')
+println "DN: ${tori.dn}"
+println "Common name: ${tori.cn}"
+println "Object classes: ${tori.objectclass}"
+
+con.disconnect()

Added: directory/sandbox/szoerner/groovyldap/src/main/groovy/demoSearch.groovy
URL: http://svn.apache.org/viewvc/directory/sandbox/szoerner/groovyldap/src/main/groovy/demoSearch.groovy?view=auto&rev=547981
==============================================================================
--- directory/sandbox/szoerner/groovyldap/src/main/groovy/demoSearch.groovy (added)
+++ directory/sandbox/szoerner/groovyldap/src/main/groovy/demoSearch.groovy Sat Jun 16 14:02:12
2007
@@ -0,0 +1,13 @@
+import org.apache.directory.groovyldap.*
+
+con = new LDAPConnection(hostname: 'zanzibar', port: 10389)
+con.connect('uid=admin,ou=system', 'secret');
+
+// search op
+results = con.search('dc=example,dc=com', SearchScope.ONE, '(objectClass=*)')
+for (result in results) {
+  entry = result.object
+  println entry.cn
+}
+
+con.disconnect()
\ No newline at end of file

Added: 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/DirContextToMapObjectFactory.java?view=auto&rev=547981
==============================================================================
--- directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/DirContextToMapObjectFactory.java
(added)
+++ directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/DirContextToMapObjectFactory.java
Sat Jun 16 14:02:12 2007
@@ -0,0 +1,94 @@
+/*
+ *  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.groovyldap;
+
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.Map;
+
+import javax.naming.Context;
+import javax.naming.Name;
+import javax.naming.NamingEnumeration;
+import javax.naming.directory.Attribute;
+import javax.naming.directory.Attributes;
+import javax.naming.directory.DirContext;
+import javax.naming.spi.DirObjectFactory;
+
+
+/**
+ * A JNDI ObjectFactory, which transforms a DirContext object into a map which contains its
attributes.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ *
+ */
+public class DirContextToMapObjectFactory implements DirObjectFactory
+{
+
+    public Object getObjectInstance( Object obj, Name name, Context ctx, Hashtable env, Attributes
attrs )
+        throws Exception
+    {
+
+        if ( obj instanceof DirContext )
+        {
+
+            DirContext dctx = ( DirContext ) obj;
+
+            Map<String, Object> map = new HashMap<String, Object>();
+            map.put( "dn", dctx.getNameInNamespace() );
+
+            Attributes as = dctx.getAttributes( "" );
+            NamingEnumeration<? extends Attribute> e = as.getAll();
+            while ( e.hasMore() )
+            {
+                Attribute attribute = e.next();
+                String attrName = attribute.getID().toLowerCase();
+
+                if ( attribute.size() == 1 )
+                {
+                    map.put( attrName, attribute.get() );
+                }
+                else
+                {
+                    List<Object> l = new ArrayList<Object>();
+                    for ( int i = 0; i < attribute.size(); ++i )
+                    {
+                        l.add( attribute.get( i ) );
+                    }
+                    map.put( attrName, l );
+                }
+            }
+            return map;
+        }
+        else
+        {
+            return null;
+        }
+    }
+
+
+    public Object getObjectInstance( Object obj, Name name, Context ctx, Hashtable env )
+    {
+        return null;
+    }
+}

Added: directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/LDAPConnection.java
URL: http://svn.apache.org/viewvc/directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/LDAPConnection.java?view=auto&rev=547981
==============================================================================
--- directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/LDAPConnection.java
(added)
+++ directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/LDAPConnection.java
Sat Jun 16 14:02:12 2007
@@ -0,0 +1,160 @@
+/*
+ *  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.groovyldap;
+
+
+import java.util.Collection;
+import java.util.Map;
+import java.util.Properties;
+
+import javax.naming.Context;
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+import javax.naming.directory.Attribute;
+import javax.naming.directory.BasicAttribute;
+import javax.naming.directory.BasicAttributes;
+import javax.naming.directory.SearchControls;
+import javax.naming.directory.SearchResult;
+import javax.naming.ldap.InitialLdapContext;
+import javax.naming.ldap.LdapContext;
+
+
+/**
+ * An LDAP connection to be used whithin Groovy scripts. 
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$*
+ */
+public class LDAPConnection
+{
+
+    private String hostname = "localhost";
+
+    private int port = 389;
+
+    private LdapContext ctx;
+
+
+    public LDAPConnection()
+    {
+    }
+
+
+    protected String calculateUrl()
+    {
+        String url = "ldap://" + hostname + ":" + port + "/";
+        return url;
+    }
+
+
+    protected Properties createDefaultEnv()
+    {
+        Properties env = new Properties();
+        env.setProperty( Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"
);
+        env.setProperty( Context.PROVIDER_URL, calculateUrl() );
+        env.setProperty( Context.OBJECT_FACTORIES, "org.apache.directory.groovyldap.DirContextToMapObjectFactory"
);
+        return env;
+    }
+
+
+    public void connect() throws NamingException
+    {
+        Properties env = createDefaultEnv();
+        ctx = new InitialLdapContext( env, null );
+    }
+
+
+    public void connect( String dn, String password ) throws NamingException
+    {
+        Properties env = createDefaultEnv();
+        env.setProperty( Context.SECURITY_PRINCIPAL, dn );
+        env.setProperty( Context.SECURITY_CREDENTIALS, password );
+        ctx = new InitialLdapContext( env, null );
+    }
+
+
+    public String getHostname()
+    {
+        return hostname;
+    }
+
+
+    public void setHostname( String hostname )
+    {
+        this.hostname = hostname;
+    }
+
+
+    public int getPort()
+    {
+        return port;
+    }
+
+
+    public void setPort( int port ) throws NamingException
+    {
+        this.port = port;
+    }
+
+
+    public Object read( String dn ) throws NamingException
+    {
+        return ctx.lookup( dn );
+    }
+
+
+    public void disconnect() throws NamingException
+    {
+        ctx.close();
+    }
+    
+    public void add (String dn, Map<String, Object> attributes) throws NamingException
{
+        
+        BasicAttributes attrs = new BasicAttributes();
+        
+        for (String key: attributes.keySet()) {
+            Attribute attr = new BasicAttribute(key);
+            Object value = attributes.get( key );
+            if (value instanceof Collection) {
+                Collection values = ( Collection ) value;
+                for (Object val: values) {
+                    attr.add( val );
+                }
+            } else {
+                attr.add( value );
+            }
+            attrs.put( attr );
+        }
+        
+        ctx.createSubcontext( dn, attrs );
+    }
+
+
+    public NamingEnumeration<SearchResult> search( String base, SearchScope scope,
String filter )
+        throws NamingException
+    {
+
+        SearchControls ctls = new SearchControls();
+        ctls.setSearchScope( scope.getJndiValue() );
+        ctls.setReturningObjFlag( true );
+
+        return ctx.search( base, filter, ctls );
+    }
+}

Added: directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/SearchScope.java
URL: http://svn.apache.org/viewvc/directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/SearchScope.java?view=auto&rev=547981
==============================================================================
--- directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/SearchScope.java
(added)
+++ directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/SearchScope.java
Sat Jun 16 14:02:12 2007
@@ -0,0 +1,50 @@
+/*
+ *  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.groovyldap;
+
+
+import javax.naming.directory.SearchControls;
+
+
+/**
+ * Enumeration for the search scope options. To be used in LDAP search operations.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public enum SearchScope
+{
+
+    BASE(SearchControls.OBJECT_SCOPE), ONE(SearchControls.ONELEVEL_SCOPE), SUB(SearchControls.SUBTREE_SCOPE);
+
+    private int jndiValue;
+
+
+    private SearchScope( int jndiValue )
+    {
+        this.jndiValue = jndiValue;
+    }
+
+
+    public int getJndiValue()
+    {
+        return jndiValue;
+    }
+}



Mime
View raw message