Ok, thanks Emmanuel.

Here's a patch for SSL support.

-pgm

On Tue, Sep 15, 2009 at 3:21 PM, Emmanuel Lecharny <[email protected]>wrote:

> Pete McKinstry wrote:
>
>> Has there been any interest in the groovy ldap library? I've been playing
>> with it & noticed that it doesn't support SSL connections. I'm wondering
>> if
>> there'd be interest in me sending in a patch or is the project basically
>> abandoned?
>>
>>
> The project is not abandonned, just dormant.
>
> Fill free to send some patches, we will review them and eventually inject
> them into the code base.
>
>
> --
> --
> cordialement, regards,
> Emmanuel Lécharny
> www.iktek.com
> directory.apache.org
>
>
>


-- 
Pete McKinstry
c: (206) 948.8098
Index: src/test/java/org/apache/directory/groovyldap/LDAPTest.java
===================================================================
--- src/test/java/org/apache/directory/groovyldap/LDAPTest.java	(revision 0)
+++ src/test/java/org/apache/directory/groovyldap/LDAPTest.java	(revision 0)
@@ -0,0 +1,79 @@
+/*
+ *  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.Context;
+import java.util.Properties;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ *
+ */
+public class LDAPTest {
+
+    /**
+     * Check default values for a search
+     */
+    @Test
+    public void testUrlParsing()
+    {
+        {
+            TestableLDAP ldap = new TestableLDAP("ldap://localhost:389";);
+            Properties env = ldap.getEnvironment();
+            Assert.assertNull(env.getProperty(Context.SECURITY_AUTHENTICATION));
+            Assert.assertNull(env.getProperty(Context.SECURITY_PROTOCOL));
+        }
+        {
+            TestableLDAP ldap = new TestableLDAP("ldaps://localhost");
+            Properties env = ldap.getEnvironment();
+            Assert.assertEquals("simple", env.getProperty(Context.SECURITY_AUTHENTICATION));
+            Assert.assertEquals("ssl", env.getProperty(Context.SECURITY_PROTOCOL));
+        }
+        {
+            TestableLDAP ldap = new TestableLDAP("ldap://localhost:636";);
+            Properties env = ldap.getEnvironment();
+            Assert.assertEquals("simple", env.getProperty(Context.SECURITY_AUTHENTICATION));
+            Assert.assertEquals("ssl", env.getProperty(Context.SECURITY_PROTOCOL));
+        }
+        {
+            TestableLDAP ldap = new TestableLDAP("ldaps://localhost:636");
+            Properties env = ldap.getEnvironment();
+            Assert.assertEquals("simple", env.getProperty(Context.SECURITY_AUTHENTICATION));
+            Assert.assertEquals("ssl", env.getProperty(Context.SECURITY_PROTOCOL));
+        }
+    }
+
+    /**
+     * So that test has access to protected info for verification.
+     */
+    public class TestableLDAP extends LDAP {
+
+        private TestableLDAP(String url) {
+            super(url);
+        }
+        
+        public Properties getEnvironment() {
+            return super.createEnvironment();
+        }
+    }
+
+}
Index: src/main/java/org/apache/directory/groovyldap/LDAP.java
===================================================================
--- src/main/java/org/apache/directory/groovyldap/LDAP.java	(revision 815842)
+++ src/main/java/org/apache/directory/groovyldap/LDAP.java	(working copy)
@@ -42,8 +42,9 @@
 import javax.naming.ldap.LdapName;
 
 import org.apache.directory.groovyldap.util.Util;
+import org.apache.commons.logging.LogFactory;
+import org.apache.commons.logging.Log;
 
-
 /**
  * A wrapper class which provides LDAP functionality to Groovy.
  * 
@@ -52,12 +53,15 @@
  */
 public class LDAP
 {
+    private static final Log log = LogFactory.getLog(LDAP.class);
+    
     private static final String DEFAULT_URL = "ldap://localhost:389/";;
 
     private String url;
 
     private boolean anonymousBind;
-
+    private boolean ssl;
+    
     private String bindUser;
 
     private String bindPassword;
@@ -74,6 +78,11 @@
             env.setProperty( Context.SECURITY_PRINCIPAL, bindUser );
             env.setProperty( Context.SECURITY_CREDENTIALS, bindPassword );
         }
+        if (ssl) {
+            log.debug("ldap connection is ssl enabled");
+            env.put(Context.SECURITY_AUTHENTICATION, "simple");
+            env.put(Context.SECURITY_PROTOCOL, "ssl");
+        }
         return env;
     }
 
@@ -88,17 +97,37 @@
     {
         this.url = url;
         this.anonymousBind = true;
+        /*
+         * By default it will assume urls starting w/ ldaps or using the default
+         * SSL port of 636 should be going over an SSL connection. This can
+         * be overridden by providing a ssl param to the factory or constructor
+         * method. 
+         */
+        if (this.url.startsWith("ldaps") || this.url.endsWith("636")) {
+            this.ssl = true;
+        } else {
+            this.ssl = false;
+        }
     }
 
 
     protected LDAP( String url, String bindUser, String bindPassword )
     {
-        this.url = url;
+        this(url);
         this.anonymousBind = false;
         this.bindUser = bindUser;
         this.bindPassword = bindPassword;
     }
 
+    protected LDAP( String url, String bindUser, String bindPassword, boolean ssl )
+    {
+        this(url);
+        this.anonymousBind = false;
+        this.bindUser = bindUser;
+        this.bindPassword = bindPassword;
+        this.ssl = true;
+    }
+
     /**
      * Creates a new LDAP object with default parameters. It will anonymously connect to localhost on port 389. 
      */
@@ -117,6 +146,10 @@
         return new LDAP( url, bindUser, bindPassword );
     }
 
+    public static LDAP newInstance( String url, String bindUser, String bindPassword, boolean ssl )
+    {
+        return new LDAP( url, bindUser, bindPassword, ssl );
+    }
 
     /**
      * Search scope ONE (one level)
Index: pom.xml
===================================================================
--- pom.xml	(revision 815842)
+++ pom.xml	(working copy)
@@ -73,6 +73,11 @@
       <groupId>groovy</groupId>
       <artifactId>groovy</artifactId>
     </dependency>
+    <dependency>
+      <groupId>commons-logging</groupId>
+      <artifactId>commons-logging</artifactId>
+      <version>1.1</version>
+    </dependency>
   </dependencies>
   
   <scm>

Reply via email to