Example usage for javax.naming.directory InitialDirContext InitialDirContext

List of usage examples for javax.naming.directory InitialDirContext InitialDirContext

Introduction

In this page you can find the example usage for javax.naming.directory InitialDirContext InitialDirContext.

Prototype

public InitialDirContext() throws NamingException 

Source Link

Document

Constructs an initial DirContext.

Usage

From source file:ServerSasl.java

public static void main(String[] args) {

    try {//w  ww. jav  a 2s.c  o  m
        // Create initial context
        DirContext ctx = new InitialDirContext();

        // Read supportedSASLMechanisms from root DSE
        Attributes attrs = ctx.getAttributes("ldap://localhost:389",
                new String[] { "supportedSASLMechanisms" });

        System.out.println(attrs);

        // Close the context when we're done
        ctx.close();
    } catch (NamingException e) {
        e.printStackTrace();
    }
}

From source file:com.ery.ertc.estorm.util.DNS.java

/**
 * Returns the hostname associated with the specified IP address by the provided nameserver.
 * //from  www  .  j  a  v  a 2  s .com
 * @param hostIp
 *            The address to reverse lookup
 * @param ns
 *            The host name of a reachable DNS server
 * @return The host name associated with the provided IP
 * @throws NamingException
 *             If a NamingException is encountered
 */
public static String reverseDns(InetAddress hostIp, String ns) throws NamingException {
    //
    // Builds the reverse IP lookup form
    // This is formed by reversing the IP numbers and appending in-addr.arpa
    //
    String[] parts = hostIp.getHostAddress().split("\\.");
    String reverseIP = parts[3] + "." + parts[2] + "." + parts[1] + "." + parts[0] + ".in-addr.arpa";

    DirContext ictx = new InitialDirContext();
    Attributes attribute = ictx.getAttributes("dns://" // Use "dns:///" if the default
            + ((ns == null) ? "" : ns) +
            // nameserver is to be used
            "/" + reverseIP, new String[] { "PTR" });
    ictx.close();

    String hostname = attribute.get("PTR").get().toString();
    int hostnameLength = hostname.length();
    if (hostname.charAt(hostnameLength - 1) == '.') {
        hostname = hostname.substring(0, hostnameLength - 1);
    }
    return hostname;
}

From source file:com.buaa.cfs.net.DNS.java

/**
 * Returns the hostname associated with the specified IP address by the provided nameserver.
 * <p>//  w  ww  .ja v a2  s  .  com
 * Loopback addresses
 *
 * @param hostIp The address to reverse lookup
 * @param ns     The host name of a reachable DNS server
 *
 * @return The host name associated with the provided IP
 *
 * @throws NamingException If a NamingException is encountered
 */
public static String reverseDns(InetAddress hostIp, String ns) throws NamingException {
    //
    // Builds the reverse IP lookup form
    // This is formed by reversing the IP numbers and appending in-addr.arpa
    //
    String[] parts = hostIp.getHostAddress().split("\\.");
    String reverseIP = parts[3] + "." + parts[2] + "." + parts[1] + "." + parts[0] + ".in-addr.arpa";

    DirContext ictx = new InitialDirContext();
    Attributes attribute;
    try {
        attribute = ictx.getAttributes("dns://" // Use "dns:///" if the default
                + ((ns == null) ? "" : ns) +
                // nameserver is to be used
                "/" + reverseIP, new String[] { "PTR" });
    } finally {
        ictx.close();
    }

    String hostname = attribute.get("PTR").get().toString();
    int hostnameLength = hostname.length();
    if (hostname.charAt(hostnameLength - 1) == '.') {
        hostname = hostname.substring(0, hostnameLength - 1);
    }
    return hostname;
}

From source file:org.apache.hadoop.net.DNS.java

/**
 * Returns the hostname associated with the specified IP address by the
 * provided nameserver./*  www  .  j av a 2  s. c  om*/
 *
 * Loopback addresses 
 * @param hostIp The address to reverse lookup
 * @param ns The host name of a reachable DNS server
 * @return The host name associated with the provided IP
 * @throws NamingException If a NamingException is encountered
 */
public static String reverseDns(InetAddress hostIp, @Nullable String ns) throws NamingException {
    //
    // Builds the reverse IP lookup form
    // This is formed by reversing the IP numbers and appending in-addr.arpa
    //
    String[] parts = hostIp.getHostAddress().split("\\.");
    String reverseIP = parts[3] + "." + parts[2] + "." + parts[1] + "." + parts[0] + ".in-addr.arpa";

    DirContext ictx = new InitialDirContext();
    Attributes attribute;
    try {
        attribute = ictx.getAttributes("dns://" // Use "dns:///" if the default
                + ((ns == null) ? "" : ns) +
                // nameserver is to be used
                "/" + reverseIP, new String[] { "PTR" });
    } finally {
        ictx.close();
    }

    String hostname = attribute.get("PTR").get().toString();
    int hostnameLength = hostname.length();
    if (hostname.charAt(hostnameLength - 1) == '.') {
        hostname = hostname.substring(0, hostnameLength - 1);
    }
    return hostname;
}

From source file:org.sonar.plugins.ldap.LdapAutodiscovery.java

/**
 * Get LDAP server(s) from DNS./*from  ww  w.  ja v a  2 s  .co  m*/
 *
 * @param domain DNS domain
 * @return LDAP server(s) or empty if unable to determine
 */
public List<LdapSrvRecord> getLdapServers(String domain) {
    try {
        return getLdapServers(new InitialDirContext(), domain);
    } catch (NamingException e) {
        LOG.error("Unable to determine LDAP server(s) from DNS", e);
        return Collections.emptyList();
    }
}

From source file:org.apache.directory.server.operations.bind.SaslBindIT.java

/**
 * Tests to make sure the server properly returns the supportedSASLMechanisms.
 *///from   www .  j  ava2 s .  c o  m
@Test
public void testSupportedSASLMechanisms() throws Exception {
    // We have to tell the server that it should accept anonymous
    // auth, because we are reading the rootDSE
    getLdapServer().getDirectoryService().setAllowAnonymousAccess(true);

    // Point on rootDSE
    DirContext context = new InitialDirContext();

    Attributes attrs = context.getAttributes(Network.ldapLoopbackUrl(getLdapServer().getPort()),
            new String[] { "supportedSASLMechanisms" });

    //             Thread.sleep( 10 * 60 * 1000 );
    NamingEnumeration<? extends Attribute> answer = attrs.getAll();
    Attribute result = answer.next();
    assertEquals(6, result.size());
    assertTrue(result.contains(SupportedSaslMechanisms.GSSAPI));
    assertTrue(result.contains(SupportedSaslMechanisms.DIGEST_MD5));
    assertTrue(result.contains(SupportedSaslMechanisms.CRAM_MD5));
    assertTrue(result.contains(SupportedSaslMechanisms.NTLM));
    assertTrue(result.contains(SupportedSaslMechanisms.PLAIN));
    assertTrue(result.contains(SupportedSaslMechanisms.GSS_SPNEGO));
}