Search in sources :

Example 1 with LdapName

use of javax.naming.ldap.LdapName in project OpenAttestation by OpenAttestation.

the class DN method parseLdapName.

private void parseLdapName(String distinguishedName) {
    try {
        LdapName dn = new LdapName(distinguishedName);
        for (int i = 0; i < dn.size(); i++) {
            Rdn rdn = dn.getRdn(i);
            map.put(rdn.getType(), rdn.getValue().toString());
        }
    } catch (InvalidNameException e) {
        log.error("Cannot extract Common Name from Distinguished Name", e);
    }
}
Also used : InvalidNameException(javax.naming.InvalidNameException) Rdn(javax.naming.ldap.Rdn) LdapName(javax.naming.ldap.LdapName)

Example 2 with LdapName

use of javax.naming.ldap.LdapName in project hadoop by apache.

the class LdapGroupsMapping method getRelativeDistinguishedName.

/**
   * A helper method to get the Relative Distinguished Name (RDN) from
   * Distinguished name (DN). According to Active Directory documentation,
   * a group object's RDN is a CN.
   *
   * @param distinguishedName A string representing a distinguished name.
   * @throws NamingException if the DN is malformed.
   * @return a string which represents the RDN
   */
private String getRelativeDistinguishedName(String distinguishedName) throws NamingException {
    LdapName ldn = new LdapName(distinguishedName);
    List<Rdn> rdns = ldn.getRdns();
    if (rdns.isEmpty()) {
        throw new NamingException("DN is empty");
    }
    Rdn rdn = rdns.get(rdns.size() - 1);
    if (rdn.getType().equalsIgnoreCase(groupNameAttr)) {
        String groupName = (String) rdn.getValue();
        return groupName;
    }
    throw new NamingException("Unable to find RDN: The DN " + distinguishedName + " is malformed.");
}
Also used : NamingException(javax.naming.NamingException) Rdn(javax.naming.ldap.Rdn) LdapName(javax.naming.ldap.LdapName)

Example 3 with LdapName

use of javax.naming.ldap.LdapName in project midpoint by Evolveum.

the class TestExpressionFunctions method testComposeDnWithSuffix.

@Test
public void testComposeDnWithSuffix() throws Exception {
    final String TEST_NAME = "testComposeDnWithSuffix";
    TestUtil.displayTestTile(TEST_NAME);
    BasicExpressionFunctions basic = createBasicFunctions();
    assertEquals("cn=foo,ou=baz,o=bar", basic.composeDnWithSuffix(new Rdn("cn", "foo"), "ou=baz,o=bar"));
    assertEquals("cn=foo,ou=baz,o=bar", basic.composeDnWithSuffix(new Rdn("cn", "foo"), new LdapName("ou=baz,o=bar")));
    assertEquals("cn=foo,ou=baz,o=bar", basic.composeDnWithSuffix("cn", "foo", "ou=baz,o=bar"));
    assertEquals("cn=foo,ou=baz,o=bar", basic.composeDnWithSuffix("cn", PrismTestUtil.createPolyString("foo"), "ou=baz,o=bar"));
    assertEquals("cn=foo,ou=baz,o=bar", basic.composeDnWithSuffix("cn", PrismTestUtil.createPolyStringType("foo"), "ou=baz,o=bar"));
    assertEquals("cn=foo,ou=baz,o=bar", basic.composeDnWithSuffix("cn", "foo", new LdapName("ou=baz,o=bar")));
    assertEquals("cn=foo,ou=baz\\,baz,o=bar", basic.composeDnWithSuffix("cn", "foo", "ou=baz\\,baz,o=bar"));
    assertEquals("cn=foo,ou=baz\\,baz,o=bar", basic.composeDnWithSuffix("cn", "foo", new LdapName("ou=baz\\,baz,o=bar")));
    assertEquals("cn=foo\\,foo,ou=baz,o=bar", basic.composeDnWithSuffix("cn", "foo,foo", "ou=baz,o=bar"));
    assertEquals("cn=foo\\,foo,ou=baz,o=bar", basic.composeDnWithSuffix("cn", "foo,foo", new LdapName("ou=baz,o=bar")));
    assertEquals("cn=foo\\=foo,ou=baz,o=bar", basic.composeDnWithSuffix("cn", "foo=foo", "ou=baz,o=bar"));
    assertEquals("cn=foo\\=foo,ou=baz,o=bar", basic.composeDnWithSuffix("cn", "foo=foo", new LdapName("ou=baz,o=bar")));
    assertEquals("ou=baz,o=bar", basic.composeDnWithSuffix("ou=baz,o=bar"));
    assertEquals("ou=baz, o=bar", basic.composeDnWithSuffix("ou=baz, o=bar"));
    assertEquals("OU=baz, o=bar", basic.composeDnWithSuffix("OU=baz, o=bar"));
    assertEquals("ou=baz,o=bar", basic.composeDnWithSuffix(new LdapName("ou=baz,o=bar")));
    assertEquals(null, basic.composeDnWithSuffix(null));
    assertEquals(null, basic.composeDnWithSuffix());
    assertEquals(null, basic.composeDnWithSuffix(""));
    assertEquals(null, basic.composeDnWithSuffix("   "));
}
Also used : BasicExpressionFunctions(com.evolveum.midpoint.model.common.expression.functions.BasicExpressionFunctions) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) Rdn(javax.naming.ldap.Rdn) LdapName(javax.naming.ldap.LdapName) Test(org.testng.annotations.Test)

Example 4 with LdapName

use of javax.naming.ldap.LdapName in project midpoint by Evolveum.

the class DistinguishedNameMatchingRule method match.

/* (non-Javadoc)
	 * @see com.evolveum.midpoint.model.match.MatchingRule#match(java.lang.Object, java.lang.Object)
	 */
@Override
public boolean match(String a, String b) throws SchemaException {
    if (StringUtils.isBlank(a) && StringUtils.isBlank(b)) {
        return true;
    }
    if (StringUtils.isBlank(a) || StringUtils.isBlank(b)) {
        return false;
    }
    LdapName dnA;
    try {
        dnA = new LdapName(a);
    } catch (InvalidNameException e) {
        throw new SchemaException("String '" + a + "' is not a DN: " + e.getMessage(), e);
    }
    LdapName dnB;
    try {
        dnB = new LdapName(b);
    } catch (InvalidNameException e) {
        throw new SchemaException("String '" + b + "' is not a DN: " + e.getMessage(), e);
    }
    return dnA.equals(dnB);
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) InvalidNameException(javax.naming.InvalidNameException) LdapName(javax.naming.ldap.LdapName)

Example 5 with LdapName

use of javax.naming.ldap.LdapName in project nifi by apache.

the class CertificateUtils method compareDNs.

/**
 * Returns true if the two provided DNs are equivalent, regardless of the order of the elements. Returns false if one or both are invalid DNs.
 *
 * Example:
 *
 * CN=test1, O=testOrg, C=US compared to CN=test1, O=testOrg, C=US -> true
 * CN=test1, O=testOrg, C=US compared to O=testOrg, CN=test1, C=US -> true
 * CN=test1, O=testOrg, C=US compared to CN=test2, O=testOrg, C=US -> false
 * CN=test1, O=testOrg, C=US compared to O=testOrg, CN=test2, C=US -> false
 * CN=test1, O=testOrg, C=US compared to                           -> false
 *                           compared to                           -> true
 *
 * @param dn1 the first DN to compare
 * @param dn2 the second DN to compare
 * @return true if the DNs are equivalent, false otherwise
 */
public static boolean compareDNs(String dn1, String dn2) {
    if (dn1 == null) {
        dn1 = "";
    }
    if (dn2 == null) {
        dn2 = "";
    }
    if (StringUtils.isEmpty(dn1) || StringUtils.isEmpty(dn2)) {
        return dn1.equals(dn2);
    }
    try {
        List<Rdn> rdn1 = new LdapName(dn1).getRdns();
        List<Rdn> rdn2 = new LdapName(dn2).getRdns();
        return rdn1.size() == rdn2.size() && rdn1.containsAll(rdn2);
    } catch (InvalidNameException e) {
        logger.warn("Cannot compare DNs: {} and {} because one or both is not a valid DN", dn1, dn2);
        return false;
    }
}
Also used : InvalidNameException(javax.naming.InvalidNameException) Rdn(javax.naming.ldap.Rdn) LdapName(javax.naming.ldap.LdapName)

Aggregations

LdapName (javax.naming.ldap.LdapName)103 Rdn (javax.naming.ldap.Rdn)48 InvalidNameException (javax.naming.InvalidNameException)32 NamingException (javax.naming.NamingException)26 Attribute (javax.naming.directory.Attribute)22 Attributes (javax.naming.directory.Attributes)14 SearchResult (javax.naming.directory.SearchResult)14 ArrayList (java.util.ArrayList)12 Test (org.junit.Test)10 LdapContext (javax.naming.ldap.LdapContext)9 IOException (java.io.IOException)7 List (java.util.List)7 X509Certificate (java.security.cert.X509Certificate)6 HashMap (java.util.HashMap)6 TreeSet (java.util.TreeSet)6 SearchControls (javax.naming.directory.SearchControls)6 HashSet (java.util.HashSet)5 NoSuchElementException (java.util.NoSuchElementException)5 InitialLdapContext (javax.naming.ldap.InitialLdapContext)5 X500Principal (javax.security.auth.x500.X500Principal)5