Example usage for javax.naming.directory DirContext getAttributes

List of usage examples for javax.naming.directory DirContext getAttributes

Introduction

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

Prototype

public Attributes getAttributes(String name, String[] attrIds) throws NamingException;

Source Link

Document

Retrieves selected attributes associated with a named object.

Usage

From source file:Main.java

public static void main(String args[]) throws Exception {
    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", "dns://123.123.123.123/");

    String dns_attributes[] = { "MX", "A", "HINFO" };

    DirContext ctx = new InitialDirContext(env);
    Attributes attrsl = ctx.getAttributes("http://www.yourserver.com", dns_attributes);
    for (int z = 0; z < dns_attributes.length; z++) {
        Attribute attr = attrsl.get(dns_attributes[z]);

        if (attr != null) {
            System.out.print(dns_attributes[z] + ": ");
            for (Enumeration vals = attr.getAll(); vals.hasMoreElements();) {
                System.out.println(vals.nextElement());
            }//from ww w .  j  ava 2s  .c  om
        }
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "yourURL");

    DirContext ctx = new InitialDirContext(env);

    // Get an attribute of that type
    Attributes attrs = ctx.getAttributes("cn=YourName, ou=People", new String[] { "cn" });
    Attribute cnAttr = attrs.get("cn");

    // Get its attribute definition
    DirContext cnSchema = cnAttr.getAttributeDefinition();

    // Get cnSchema's attributes
    Attributes cnAttrs = cnSchema.getAttributes("");
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String url = "ldap://localhost/o=JNDITutorial";
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, url);

    DirContext ctx = new InitialDirContext(env);
    String[] attrIDs = { "sn", "number", "value", "mail" };

    Attributes answer = ctx.getAttributes("cn=yourName, ou=People", attrIDs);

    NamingEnumeration e = answer.getAll();
    while (e.hasMore()) {
        Attribute attr = (Attribute) e.next();
        System.out.println(attr.getID());
    }//from  w w w .  j av  a 2  s  .  c  om
}

From source file:ServerSasl.java

public static void main(String[] args) {

    try {//from  w  ww. j a  va 2  s.  com
        // 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:ModAttrs.java

public static void main(String[] args) {

    // Set up the environment for creating the initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

    try {// w w w.ja  va  2s  .  c  o  m
        // Create the initial context
        DirContext ctx = new InitialDirContext(env);
        String name = "cn=Ted Geisel, ou=People";

        // Save original attributes
        Attributes orig = ctx.getAttributes(name, new String[] { "mail", "telephonenumber", "jpegphoto" });

        // Specify the changes to make
        ModificationItem[] mods = new ModificationItem[3];

        // Replace the "mail" attribute with a new value
        mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                new BasicAttribute("mail", "geisel@wizards.com"));

        // Add additional value to "telephonenumber"
        mods[1] = new ModificationItem(DirContext.ADD_ATTRIBUTE,
                new BasicAttribute("telephonenumber", "+1 555 555 5555"));

        // Remove the "jpegphoto" attribute
        mods[2] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, new BasicAttribute("jpegphoto"));

        // Perform the requested modifications on the named object
        ctx.modifyAttributes(name, mods);

        // Check attributes
        System.out.println("**** new attributes *****");
        printAttrs(ctx.getAttributes(name));

        // Revert changes
        ctx.modifyAttributes(name, DirContext.REPLACE_ATTRIBUTE, orig);

        // Check that the attributes got restored
        System.out.println("**** reverted to original attributes *****");
        printAttrs(ctx.getAttributes(name));

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

From source file:GetAttrs.java

public static void main(String[] args) {

    // Set up the environment for creating the initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

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

        // Specify the ids of the attributes to return
        String[] attrIDs = { "sn", "telephonenumber", "golfhandicap", "mail" };

        // Get the attributes requested
        Attributes answer = ctx.getAttributes("cn=Ted Geisel, ou=People", attrIDs);

        // Print the answer
        printAttrs(answer);

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

From source file:Main.java

public static void main(String args[]) throws Exception {
    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, INITCTX);
    env.put(Context.PROVIDER_URL, MY_HOST);
    DirContext ctx = new InitialDirContext(env);
    SearchControls constraints = new SearchControls();
    constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
    NamingEnumeration results = ctx.search(MY_SEARCHBASE, MY_FILTER, constraints);
    while (results != null && results.hasMore()) {
        SearchResult sr = (SearchResult) results.next();
        String dn = sr.getName() + ", " + MY_SEARCHBASE;

        System.out.println("Distinguished Name is " + dn);
        Attributes ar = ctx.getAttributes(dn, MY_ATTRS);
        if (ar == null) {
            System.out.println("Entry " + dn + " has none of the specified attributes\n");
            return;
        }/*  w  ww  .  jav a  2  s .  c  o m*/
        for (int i = 0; i < MY_ATTRS.length; i++) {
            Attribute attr = ar.get(MY_ATTRS[i]);
            if (attr == null) {
                continue;
            }
            System.out.println(MY_ATTRS[i] + ":");
            for (Enumeration vals = attr.getAll(); vals.hasMoreElements();) {
                System.out.println("\t" + vals.nextElement());

            }
        }
    }
}

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

/**
 * Returns the hostname associated with the specified IP address by the provided nameserver.
 * // ww  w  . java  2 s. c  o m
 * @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>//from w w w  .  j a va2 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:it.infn.ct.security.utilities.LDAPUtils.java

public static List<LDAPUser> getIdPUserList() {
    List<LDAPUser> users = new ArrayList<LDAPUser>();
    NamingEnumeration results = null;
    DirContext ctx;
    ResourceBundle rb = ResourceBundle.getBundle("ldap");
    try {//from ww  w  .j a v a 2 s.  co m
        ctx = getMainAuthContext();
        String attrs[] = { "uniqueMember" };
        Attribute userGAttr = ctx.getAttributes(rb.getString("usersGroup"), attrs).get("uniqueMember");
        for (int i = 0; i < userGAttr.size(); i++) {
            String userDN = (String) userGAttr.get(i);
            users.add(getUser(userDN.substring(userDN.indexOf("=") + 1, userDN.indexOf(","))));

        }

    } catch (NamingException ex) {
        _log.error(ex);
    }

    return users;
}