[jboss-cvs] Picketlink SVN: r1374 - in idm/trunk: picketlink-idm-core/src/test/java/org/picketlink/idm/impl and 2 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon Feb 6 17:13:06 EST 2012


Author: mposolda
Date: 2012-02-06 17:13:05 -0500 (Mon, 06 Feb 2012)
New Revision: 1374

Added:
   idm/trunk/picketlink-idm-core/src/test/java/org/picketlink/idm/impl/helper/
   idm/trunk/picketlink-idm-core/src/test/java/org/picketlink/idm/impl/helper/ToolsTestCase.java
Modified:
   idm/trunk/picketlink-idm-core/src/main/java/org/picketlink/idm/impl/helper/Tools.java
   idm/trunk/picketlink-idm-ldap/src/main/java/org/picketlink/idm/impl/store/ldap/LDAPIdentityStoreImpl.java
Log:
JBEPP-980 Possibility to compare DN with whitespaces

Modified: idm/trunk/picketlink-idm-core/src/main/java/org/picketlink/idm/impl/helper/Tools.java
===================================================================
--- idm/trunk/picketlink-idm-core/src/main/java/org/picketlink/idm/impl/helper/Tools.java	2012-02-03 21:35:33 UTC (rev 1373)
+++ idm/trunk/picketlink-idm-core/src/main/java/org/picketlink/idm/impl/helper/Tools.java	2012-02-06 22:13:05 UTC (rev 1374)
@@ -33,6 +33,8 @@
 import java.util.Map;
 import java.util.logging.Level;
 import java.util.logging.Logger;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 /**
  * @author <a href="mailto:boleslaw.dawidowicz at redhat.com">Boleslaw Dawidowicz</a>
@@ -40,7 +42,10 @@
  */
 public class Tools
 {
+   private static final String DN_REGEX = "([^=,\\\\]*(\\\\.)?)+";
 
+   private static Logger log = Logger.getLogger(Tools.class.getName());
+
    private static MBeanServer instance = null;
 
    public static <E> List<E> toList(Enumeration<E> e)
@@ -258,5 +263,74 @@
       throw new IllegalStateException("No 'jboss' MBeanServer found!");
    }
 
+   /**
+    * @param dn1
+    * @param dn2
+    * @return true if first DN ends with second Ldap DN. It will ignore whitespaces in the path. See {@link #dnFormatWhitespaces}
+    */
+   public static boolean dnEndsWith(String dn1, String dn2)
+   {
+      String dn1Formatted = dnFormatWhitespaces(dn1);
+      String dn2Formatted = dnFormatWhitespaces(dn2);
 
+      return dn1Formatted.endsWith(dn2Formatted);
+   }
+
+   /**
+    * @param dn1
+    * @param dn2
+    * @return true if first DN equals second Ldap DN. It will ignore whitespaces in the path. See {@link #dnFormatWhitespaces}
+    */
+   public static boolean dnEquals(String dn1, String dn2)
+   {
+      String dn1Formatted = dnFormatWhitespaces(dn1);
+      String dn2Formatted = dnFormatWhitespaces(dn2);
+
+      return dn1Formatted.equals(dn2Formatted);
+   }
+
+   /**
+    * Format whitespaces in DN records path. It won't affect whitespaces inside some record, but it will affect
+    * whitespaces at the beginning or at the end of single path argument.
+    *
+    * Examples:
+    * input="uid=root, ou=Organization, o=gatein,dc=example,dc=com " , output="uid=root,ou=Organization,o=gatein,dc=example,dc=com"
+    * input="uid=root, ou=My Big Organization Unit,o=gatein org,dc= example ,dc=com " , output="uid=root,ou=My Big Organization Unit,o=gatein org,dc=example,dc=com"
+    *
+    * @param inputDn
+    * @return formatted inputDn
+    */
+   public static String dnFormatWhitespaces(String inputDn)
+   {
+      String inputlc = inputDn.toLowerCase();
+
+      StringBuilder result = new StringBuilder();
+      int last = 0;
+
+      Pattern pattern = Pattern.compile(DN_REGEX);
+      Matcher m = pattern.matcher(inputlc);
+      while (m.find())
+      {
+         if (m.group().length() == 0)
+         {
+            continue;
+         }
+
+         last++;
+         if (last > 1)
+         {
+            result.append(last%2 == 0 ? '=' : ',');
+         }
+         result.append(m.group().trim());
+      }
+
+      if (log.isLoggable(Level.FINER))
+      {
+         log.log(Level.FINER, "Input to format=\"" + inputDn + "\", Output from format=\"" + result.toString() + "\"");
+      }
+
+      return result.toString();
+   }
+
+
 }

Added: idm/trunk/picketlink-idm-core/src/test/java/org/picketlink/idm/impl/helper/ToolsTestCase.java
===================================================================
--- idm/trunk/picketlink-idm-core/src/test/java/org/picketlink/idm/impl/helper/ToolsTestCase.java	                        (rev 0)
+++ idm/trunk/picketlink-idm-core/src/test/java/org/picketlink/idm/impl/helper/ToolsTestCase.java	2012-02-06 22:13:05 UTC (rev 1374)
@@ -0,0 +1,55 @@
+/*
+* JBoss, a division of Red Hat
+* Copyright 2012, Red Hat Middleware, LLC, and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+
+package org.picketlink.idm.impl.helper;
+
+import junit.framework.TestCase;
+
+/**
+ * @author <a href="mailto:mposolda at redhat.com">Marek Posolda</a>
+ */
+public class ToolsTestCase extends TestCase
+{
+
+   public void testDnComparison()
+   {
+      String root = "uid=root, ou=Organization, o=gatein,dc=example,dc=com ";
+      String john = "uid=john, ou=My Big Organization Unit,o=gatein org,dc= example ,dc=com ";
+      String mary = "uid=mary,ou=OrganizationUnit,o=gatein,dc=example,dc=com";
+      String organization1 = " ou=Organization,o=gatein,  dc=example ,dc=com";
+      String organization2 = "ou=My Big Organization Unit,o=gatein org,dc= example ,dc=com";
+      String organization3 = "uid=mary,ou=OrganizationUnit,o=gatein,dc=example,dc=com";
+
+      String escapeCharsDn = "cn= some\\,\\,thin\\=g , ou= pl\\ at\\.form ,o=gr\\=oup\\=,o=gatein ";
+
+      assertEquals("uid=root,ou=organization,o=gatein,dc=example,dc=com", Tools.dnFormatWhitespaces(root));
+      assertEquals("uid=john,ou=my big organization unit,o=gatein org,dc=example,dc=com", Tools.dnFormatWhitespaces(john));
+      assertEquals("uid=mary,ou=organizationunit,o=gatein,dc=example,dc=com", Tools.dnFormatWhitespaces(mary));
+      assertTrue(Tools.dnEndsWith(root, organization1));
+      assertTrue(Tools.dnEndsWith(john, organization2));
+      assertTrue(Tools.dnEndsWith(mary, organization3));
+      assertFalse(Tools.dnEndsWith(root, organization3));
+
+      assertEquals("cn=some\\,\\,thin\\=g,ou=pl\\ at\\.form,o=gr\\=oup\\=,o=gatein", Tools.dnFormatWhitespaces(escapeCharsDn));
+   }
+
+}

Modified: idm/trunk/picketlink-idm-ldap/src/main/java/org/picketlink/idm/impl/store/ldap/LDAPIdentityStoreImpl.java
===================================================================
--- idm/trunk/picketlink-idm-ldap/src/main/java/org/picketlink/idm/impl/store/ldap/LDAPIdentityStoreImpl.java	2012-02-03 21:35:33 UTC (rev 1373)
+++ idm/trunk/picketlink-idm-ldap/src/main/java/org/picketlink/idm/impl/store/ldap/LDAPIdentityStoreImpl.java	2012-02-06 22:13:05 UTC (rev 1374)
@@ -22,7 +22,6 @@
 
 package org.picketlink.idm.impl.store.ldap;
 
-import org.picketlink.idm.api.cfg.IdentityConfigurationRegistry;
 import org.picketlink.idm.common.exception.IdentityException;
 import org.picketlink.idm.impl.NotYetImplementedException;
 import org.picketlink.idm.impl.api.SimpleAttribute;
@@ -54,7 +53,6 @@
 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
 import java.lang.reflect.Constructor;
-import java.lang.reflect.InvocationTargetException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -811,7 +809,7 @@
 
             for (String typeCtx : typeCtxs)
             {
-               if (dn.toLowerCase().endsWith(typeCtx.toLowerCase()))
+               if (Tools.dnEndsWith(dn, typeCtx))
                {
                   matches.add(possibleType);
                   break;
@@ -833,7 +831,7 @@
             for (IdentityObjectType match : matches)
             {
                LDAPIdentityObjectImpl entry = (LDAPIdentityObjectImpl)this.findIdentityObject(ctx, name, match);
-               if (entry != null && entry.getDn().equalsIgnoreCase(dn))
+               if (entry != null && Tools.dnEquals(entry.getDn(), dn))
                {
                   type = match;
                   break;
@@ -2297,8 +2295,8 @@
                {
                   String memberRef = memberValues.nextElement().toString();
 
-                  if ((fromTypeConfig.isParentMembershipAttributeDN() && memberRef.equals(ldapToIO.getDn())) ||
-                     (!fromTypeConfig.isParentMembershipAttributeDN() && memberRef.equals(ldapToIO.getName())))
+                  if ((fromTypeConfig.isParentMembershipAttributeDN() && Tools.dnEquals(memberRef, ldapToIO.getDn())) ||
+                     (!fromTypeConfig.isParentMembershipAttributeDN() && Tools.dnEquals(memberRef, ldapToIO.getName())))
                   {
                      //TODO: impl lacks support for rel type
                      relationships.add(new LDAPIdentityObjectRelationshipImpl(MEMBERSHIP_TYPE, ldapFromIO, ldapToIO));
@@ -2317,8 +2315,8 @@
                {
                   String memberRef = memberValues.nextElement().toString();
 
-                  if ((toTypeConfig.isChildMembershipAttributeDN() && memberRef.equals(ldapFromIO.getDn())) ||
-                     (!toTypeConfig.isChildMembershipAttributeDN() && memberRef.equals(ldapFromIO.getName())))
+                  if ((toTypeConfig.isChildMembershipAttributeDN() && Tools.dnEquals(memberRef, ldapToIO.getDn())) ||
+                     (!toTypeConfig.isChildMembershipAttributeDN() && Tools.dnEquals(memberRef, ldapToIO.getName())))
                   {
                      //TODO: impl lacks support for rel type
                      relationships.add(new LDAPIdentityObjectRelationshipImpl(MEMBERSHIP_TYPE, ldapFromIO, ldapToIO));



More information about the jboss-cvs-commits mailing list