Search in sources :

Example 1 with LdapCtx

use of com.sun.jndi.ldap.LdapCtx in project jdk8u_jdk by JetBrains.

the class ldapURLContextFactory method getUsingURLIgnoreRootDN.

static ResolveResult getUsingURLIgnoreRootDN(String url, Hashtable<?, ?> env) throws NamingException {
    LdapURL ldapUrl = new LdapURL(url);
    DirContext ctx = new LdapCtx("", ldapUrl.getHost(), ldapUrl.getPort(), env, ldapUrl.useSsl());
    String dn = (ldapUrl.getDN() != null ? ldapUrl.getDN() : "");
    // Represent DN as empty or single-component composite name.
    CompositeName remaining = new CompositeName();
    if (!"".equals(dn)) {
        // if nonempty, add component
        remaining.add(dn);
    }
    return new ResolveResult(ctx, remaining);
}
Also used : LdapURL(com.sun.jndi.ldap.LdapURL) LdapCtx(com.sun.jndi.ldap.LdapCtx) DirContext(javax.naming.directory.DirContext)

Example 2 with LdapCtx

use of com.sun.jndi.ldap.LdapCtx in project wildfly by wildfly.

the class ExternalContextBindingTestCase method testWithActualLDAPContext.

private void testWithActualLDAPContext(boolean withCache) throws Exception {
    InitialContext ctx = null;
    InitialDirContext ldapContext1 = null;
    InitialDirContext ldapContext2 = null;
    try {
        ctx = new InitialContext();
        String initialDirContext = withCache ? "java:global/ldap-cache" : "java:global/ldap";
        LOGGER.debug("looking up " + initialDirContext + " ....");
        ldapContext1 = (InitialDirContext) ctx.lookup(initialDirContext);
        ldapContext2 = (InitialDirContext) ctx.lookup(initialDirContext);
        Assert.assertNotNull(ldapContext1);
        Assert.assertNotNull(ldapContext2);
        if (withCache) {
            Assert.assertSame(ldapContext1, ldapContext2);
        } else {
            Assert.assertNotSame(ldapContext1, ldapContext2);
        }
        LOGGER.debug("acquired external LDAP context: " + ldapContext1.toString());
        LdapCtx c = (LdapCtx) ldapContext1.lookup("dc=jboss,dc=org");
        c = (LdapCtx) c.lookup("ou=People");
        Attributes attributes = c.getAttributes("uid=jduke");
        Assert.assertTrue(attributes.get("description").contains("awesome"));
        // resource injection
        LookupEjb ejb = (LookupEjb) ctx.lookup("java:module/LookupEjb");
        Assert.assertNotNull(ejb);
        c = ejb.getLdapCtx();
        Assert.assertNotNull(c);
        c = (LdapCtx) c.lookup("ou=People");
        attributes = c.getAttributes("uid=jduke");
        Assert.assertTrue(attributes.get("description").contains("awesome"));
    } finally {
        if (ctx != null) {
            ctx.close();
        }
        if (ldapContext1 != null) {
            ldapContext1.close();
        }
        if (ldapContext2 != null) {
            ldapContext2.close();
        }
    }
}
Also used : Attributes(javax.naming.directory.Attributes) LdapCtx(com.sun.jndi.ldap.LdapCtx) InitialDirContext(javax.naming.directory.InitialDirContext) InitialContext(javax.naming.InitialContext)

Aggregations

LdapCtx (com.sun.jndi.ldap.LdapCtx)2 LdapURL (com.sun.jndi.ldap.LdapURL)1 InitialContext (javax.naming.InitialContext)1 Attributes (javax.naming.directory.Attributes)1 DirContext (javax.naming.directory.DirContext)1 InitialDirContext (javax.naming.directory.InitialDirContext)1