@Test public void testDnConformsToSearchBaseDefinitions() { Object[][] datas = new Object[][] { { "cn=exampleuser1,ou=people,ou=example,dc=communote,dc=com", "ou=people,ou=example,dc=communote,dc=com", true, true }, { "cn=exampleuser1,ou=people,ou=example,dc=communote,dc=com", "ou=people,ou=example,dc=communote,dc=com", false, true }, { "cn=exampleuser1,ou=people,ou=example,dc=communote,dc=com", "ou=people,ou=example,dc=communote,dc=com", true, true }, { "cn=exampleuser1,oU=people,ou=example,dc=communote,Dc=com", "ou=people,ou=example,dc=communote,dc=com", false, true }, { "cn=exampleuser1,ou=people,ou=example,dc=communote,dc=com", "ou=example,dc=communote,dc=com", true, true }, { "cn=exampleuser1,ou=people,ou=example,dc=communote,dc=com", "OU=example,DC=communote,dc=com", true, true }, { "cn=exampleuser1,ou=people,ou=example,dc=communote,dc=com", "OU=example,DC=communote,dc=com", false, false }, { "cn=exampleuser1,ou=people,ou=example,dc=communote,dc=com", "OU=example,DC=example,dc=com", false, false }, { "cn=exampleuser1,ou=people,ou=example,dc=communote,dc=com", "OU=example,DC=example,dc=com", true, false } }; Collection<LdapSearchBaseDefinition> searchBaseDefs = new HashSet<LdapSearchBaseDefinition>(); LdapSearchBaseDefinition ldapSearchBaseDefinition = LdapSearchBaseDefinition.Factory .newInstance("", true); searchBaseDefs.add(ldapSearchBaseDefinition); for (Object[] data : datas) { ldapSearchBaseDefinition.setSearchBase(data[1].toString()); ldapSearchBaseDefinition.setSearchSubtree((Boolean) data[2]); boolean conforms = LdapUtils.dnConformsToSearchBaseDefinitions(data[0].toString(), searchBaseDefs); Assert.assertEquals(conforms, data[3], "Wrong result for " + data[0] + " and " + data[1]); } }
public static boolean dnConformsToSearchBaseDefinitions(String dn, Collection<LdapSearchBaseDefinition> searchBaseDefs) { boolean conforms = false; try { Name name = new LdapName(dn); for (LdapSearchBaseDefinition searchBaseDefinition : searchBaseDefs) { Name searchBaseName = new LdapName(searchBaseDefinition.getSearchBase()); if (name.equals(searchBaseName)) { conforms = true; break; } if (searchBaseDefinition.isSearchSubtree()) { if (name.startsWith(searchBaseName)) { conforms = true; break; } } else { name.remove(name.size() - 1); if (name.equals(searchBaseName)) { conforms = true; break; } } } } catch (InvalidNameException e) { LOGGER.debug(e.getMessage(), e); return false; } return conforms; }
LdapUtils { public static boolean dnConformsToSearchBaseDefinitions(String dn, Collection<LdapSearchBaseDefinition> searchBaseDefs) { boolean conforms = false; try { Name name = new LdapName(dn); for (LdapSearchBaseDefinition searchBaseDefinition : searchBaseDefs) { Name searchBaseName = new LdapName(searchBaseDefinition.getSearchBase()); if (name.equals(searchBaseName)) { conforms = true; break; } if (searchBaseDefinition.isSearchSubtree()) { if (name.startsWith(searchBaseName)) { conforms = true; break; } } else { name.remove(name.size() - 1); if (name.equals(searchBaseName)) { conforms = true; break; } } } } catch (InvalidNameException e) { LOGGER.debug(e.getMessage(), e); return false; } return conforms; } }
LdapUtils { public static boolean dnConformsToSearchBaseDefinitions(String dn, Collection<LdapSearchBaseDefinition> searchBaseDefs) { boolean conforms = false; try { Name name = new LdapName(dn); for (LdapSearchBaseDefinition searchBaseDefinition : searchBaseDefs) { Name searchBaseName = new LdapName(searchBaseDefinition.getSearchBase()); if (name.equals(searchBaseName)) { conforms = true; break; } if (searchBaseDefinition.isSearchSubtree()) { if (name.startsWith(searchBaseName)) { conforms = true; break; } } else { name.remove(name.size() - 1); if (name.equals(searchBaseName)) { conforms = true; break; } } } } catch (InvalidNameException e) { LOGGER.debug(e.getMessage(), e); return false; } return conforms; } private  LdapUtils(); }
LdapUtils { public static boolean dnConformsToSearchBaseDefinitions(String dn, Collection<LdapSearchBaseDefinition> searchBaseDefs) { boolean conforms = false; try { Name name = new LdapName(dn); for (LdapSearchBaseDefinition searchBaseDefinition : searchBaseDefs) { Name searchBaseName = new LdapName(searchBaseDefinition.getSearchBase()); if (name.equals(searchBaseName)) { conforms = true; break; } if (searchBaseDefinition.isSearchSubtree()) { if (name.startsWith(searchBaseName)) { conforms = true; break; } } else { name.remove(name.size() - 1); if (name.equals(searchBaseName)) { conforms = true; break; } } } } catch (InvalidNameException e) { LOGGER.debug(e.getMessage(), e); return false; } return conforms; } private  LdapUtils(); static void closeNamingEnumeration(NamingEnumeration<?> enumeration); static String convertToByteFormatString(byte[] bytes); static boolean dnConformsToSearchBaseDefinitions(String dn,
            Collection<LdapSearchBaseDefinition> searchBaseDefs); static String escapeLdapSearchFilterValue(String value); static List<String> getAttributeValues(Attributes attributes, String ldapAttributeName); static ExternalUserVO queryUserByName(String emailOrAlias); static void setSynchronizationFields(ExternalUserVO userVO); static Pair<String, String> splitAfterFirstRdn(String dn); }
LdapUtils { public static boolean dnConformsToSearchBaseDefinitions(String dn, Collection<LdapSearchBaseDefinition> searchBaseDefs) { boolean conforms = false; try { Name name = new LdapName(dn); for (LdapSearchBaseDefinition searchBaseDefinition : searchBaseDefs) { Name searchBaseName = new LdapName(searchBaseDefinition.getSearchBase()); if (name.equals(searchBaseName)) { conforms = true; break; } if (searchBaseDefinition.isSearchSubtree()) { if (name.startsWith(searchBaseName)) { conforms = true; break; } } else { name.remove(name.size() - 1); if (name.equals(searchBaseName)) { conforms = true; break; } } } } catch (InvalidNameException e) { LOGGER.debug(e.getMessage(), e); return false; } return conforms; } private  LdapUtils(); static void closeNamingEnumeration(NamingEnumeration<?> enumeration); static String convertToByteFormatString(byte[] bytes); static boolean dnConformsToSearchBaseDefinitions(String dn,
            Collection<LdapSearchBaseDefinition> searchBaseDefs); static String escapeLdapSearchFilterValue(String value); static List<String> getAttributeValues(Attributes attributes, String ldapAttributeName); static ExternalUserVO queryUserByName(String emailOrAlias); static void setSynchronizationFields(ExternalUserVO userVO); static Pair<String, String> splitAfterFirstRdn(String dn);  }