Search in sources :

Example 46 with InitialDirContext

use of javax.naming.directory.InitialDirContext in project CzechIdMng by bcvsolutions.

the class AdUserConnectorType method deleteTestUser.

/**
 * Delete the wizard test user.
 */
protected void deleteTestUser(String entryDN, String port, String host, String adUser, String adPassword, boolean ssl) {
    DirContext ldapContext = null;
    try {
        // Init LDAP context.
        Hashtable<String, String> ldapEnv = getAdEnvironment(host, port, adUser, adPassword, ssl);
        ldapContext = new InitialDirContext(ldapEnv);
        // Delete the entry.
        ldapContext.destroySubcontext(entryDN);
    } catch (CommunicationException ex) {
        throw new ResultCodeException(AccResultCode.WIZARD_AD_COMMUNICATION_EXCEPTION, ImmutableMap.of("host", host), ex);
    } catch (NamingException ex) {
        throw new ResultCodeException(AccResultCode.WIZARD_AD_OPERATION_FAILED, ImmutableMap.of("dn", entryDN), ex);
    } finally {
        if (ldapContext != null) {
            try {
                ldapContext.close();
            } catch (NamingException e) {
                // Only log it.
                LOG.error(e.getLocalizedMessage(), e);
            }
        }
    }
}
Also used : CommunicationException(javax.naming.CommunicationException) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) NamingException(javax.naming.NamingException) InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) InitialDirContext(javax.naming.directory.InitialDirContext)

Example 47 with InitialDirContext

use of javax.naming.directory.InitialDirContext in project CzechIdMng by bcvsolutions.

the class AdUserConnectorType method findDnsHostName.

/**
 * Find dnsHostName (domain) on the AD.
 */
protected String findDnsHostName(String port, String host, String adUser, String adPassword, boolean ssl) {
    DirContext ldapContext = null;
    try {
        // Init LDAP context.
        Hashtable<String, String> ldapEnv = getAdEnvironment(host, port, adUser, adPassword, ssl);
        ldapContext = new InitialDirContext(ldapEnv);
        // Get the configuration naming context.
        Attributes ldapContextAttributes = ldapContext.getAttributes("");
        return ldapContextAttributes.get("dnsHostName").get().toString();
    } catch (CommunicationException ex) {
        throw new ResultCodeException(AccResultCode.WIZARD_AD_COMMUNICATION_EXCEPTION, ImmutableMap.of("host", host), ex);
    } catch (NamingException ex) {
        throw new ResultCodeException(AccResultCode.WIZARD_AD_OPERATION_FAILED, ImmutableMap.of("dn", "dnsHostName"), ex);
    } finally {
        if (ldapContext != null) {
            try {
                ldapContext.close();
            } catch (NamingException e) {
                // Only log it.
                LOG.error(e.getLocalizedMessage(), e);
            }
        }
    }
}
Also used : CommunicationException(javax.naming.CommunicationException) Attributes(javax.naming.directory.Attributes) BasicAttributes(javax.naming.directory.BasicAttributes) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) NamingException(javax.naming.NamingException) InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) InitialDirContext(javax.naming.directory.InitialDirContext)

Example 48 with InitialDirContext

use of javax.naming.directory.InitialDirContext in project CzechIdMng by bcvsolutions.

the class AdUserConnectorType method createTestUser.

/**
 * Create test certificates.
 */
protected String createTestUser(String username, String entryDN, String port, String host, String adUser, String adPassword, boolean ssl) {
    DirContext ldapContext = null;
    try {
        // Init LDAP context.
        Hashtable<String, String> ldapEnv = getAdEnvironment(host, port, adUser, adPassword, ssl);
        ldapContext = new InitialDirContext(ldapEnv);
        // Entry's attributes.
        Attribute cn = new BasicAttribute("cn", username);
        Attribute oc = new BasicAttribute("objectClass");
        oc.add("top");
        oc.add("person");
        oc.add("organizationalPerson");
        oc.add("inetOrgPerson");
        // Build the entry
        BasicAttributes entry = new BasicAttributes();
        entry.put(cn);
        entry.put(oc);
        // Add the entry.
        DirContext context = ldapContext.createSubcontext(MessageFormat.format("CN={0},{1}", username, entryDN), entry);
        Attributes attributes = context.getAttributes("");
        return (String) attributes.get("distinguishedname").get();
    } catch (NameAlreadyBoundException ex) {
        throw new ResultCodeException(AccResultCode.WIZARD_AD_CONNECTOR_DN_ALREADY_EXISTS, ImmutableMap.of("dn", MessageFormat.format("CN={0},{1}", username, entryDN)), ex);
    } catch (CommunicationException ex) {
        throw new ResultCodeException(AccResultCode.WIZARD_AD_COMMUNICATION_EXCEPTION, ImmutableMap.of("host", host), ex);
    } catch (NamingException ex) {
        throw new ResultCodeException(AccResultCode.WIZARD_AD_OPERATION_FAILED, ImmutableMap.of("dn", MessageFormat.format("CN={0},{1}", username, entryDN)), ex);
    } finally {
        if (ldapContext != null) {
            try {
                ldapContext.close();
            } catch (NamingException e) {
                // Only log it.
                LOG.error(e.getLocalizedMessage(), e);
            }
        }
    }
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) BasicAttributes(javax.naming.directory.BasicAttributes) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) CommunicationException(javax.naming.CommunicationException) BasicAttribute(javax.naming.directory.BasicAttribute) IcAttribute(eu.bcvsolutions.idm.ic.api.IcAttribute) Attribute(javax.naming.directory.Attribute) Attributes(javax.naming.directory.Attributes) BasicAttributes(javax.naming.directory.BasicAttributes) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) NamingException(javax.naming.NamingException) InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) InitialDirContext(javax.naming.directory.InitialDirContext)

Example 49 with InitialDirContext

use of javax.naming.directory.InitialDirContext in project CzechIdMng by bcvsolutions.

the class AdUserConnectorType method assignTestUserToGroup.

/**
 * Assign the wizard test user to test group.
 */
protected void assignTestUserToGroup(String userDN, String groupDN, String port, String host, String adUser, String adPassword, boolean ssl) {
    DirContext ldapContext = null;
    try {
        // Init LDAP context.
        Hashtable<String, String> ldapEnv = getAdEnvironment(host, port, adUser, adPassword, ssl);
        ldapContext = new InitialDirContext(ldapEnv);
        ModificationItem[] roleMods = new ModificationItem[] { new ModificationItem(DirContext.ADD_ATTRIBUTE, new BasicAttribute("member", userDN)) };
        ldapContext.modifyAttributes(groupDN, roleMods);
    } catch (CommunicationException ex) {
        throw new ResultCodeException(AccResultCode.WIZARD_AD_COMMUNICATION_EXCEPTION, ImmutableMap.of("host", host), ex);
    } catch (NamingException ex) {
        throw new ResultCodeException(AccResultCode.WIZARD_AD_OPERATION_FAILED, ImmutableMap.of("dn", userDN), ex);
    } finally {
        if (ldapContext != null) {
            try {
                ldapContext.close();
            } catch (NamingException e) {
                // Only log it.
                LOG.error(e.getLocalizedMessage(), e);
            }
        }
    }
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) ModificationItem(javax.naming.directory.ModificationItem) CommunicationException(javax.naming.CommunicationException) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) NamingException(javax.naming.NamingException) InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) InitialDirContext(javax.naming.directory.InitialDirContext)

Example 50 with InitialDirContext

use of javax.naming.directory.InitialDirContext in project rabbitmq-java-client by rabbitmq.

the class DnsSrvRecordAddressResolver method lookupSrvRecords.

protected List<SrvRecord> lookupSrvRecords(String service, String dnsUrls) throws IOException {
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
    env.put("java.naming.provider.url", dnsUrls);
    List<SrvRecord> records = new ArrayList<SrvRecord>();
    try {
        DirContext ctx = new InitialDirContext(env);
        Attributes attributes = ctx.getAttributes(service, new String[] { "SRV" });
        NamingEnumeration<?> servers = attributes.get("srv").getAll();
        while (servers.hasMore()) {
            records.add(mapSrvRecord((String) servers.next()));
        }
    } catch (NamingException e) {
        throw new IOException("Error during DNS SRV query", e);
    }
    return records;
}
Also used : Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) Attributes(javax.naming.directory.Attributes) NamingException(javax.naming.NamingException) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) InitialDirContext(javax.naming.directory.InitialDirContext) IOException(java.io.IOException)

Aggregations

InitialDirContext (javax.naming.directory.InitialDirContext)131 DirContext (javax.naming.directory.DirContext)71 NamingException (javax.naming.NamingException)67 Hashtable (java.util.Hashtable)51 Attributes (javax.naming.directory.Attributes)34 Attribute (javax.naming.directory.Attribute)24 SearchResult (javax.naming.directory.SearchResult)22 SearchControls (javax.naming.directory.SearchControls)19 IOException (java.io.IOException)11 CommunicationException (javax.naming.CommunicationException)11 Test (org.junit.Test)11 Properties (java.util.Properties)10 BasicAttributes (javax.naming.directory.BasicAttributes)9 ArrayList (java.util.ArrayList)8 LoginException (javax.security.auth.login.LoginException)8 NameClassPair (javax.naming.NameClassPair)7 HashSet (java.util.HashSet)6 NamingEnumeration (javax.naming.NamingEnumeration)6 BasicAttribute (javax.naming.directory.BasicAttribute)6 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)5