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(Hashtable<?, ?> environment) throws NamingException 

Source Link

Document

Constructs an initial DirContext using the supplied environment.

Usage

From source file:BadPasswd.java

public static void main(String[] args) {

    // Set up environment for creating 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");

    // Authenticate as S. User and give incorrect password
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, "cn=S. User, ou=NewHires, o=JNDITutorial");
    env.put(Context.SECURITY_CREDENTIALS, "notmysecret");

    try {// w  w w. j a  va2s  .c  o  m
        // Create initial context
        DirContext ctx = new InitialDirContext(env);

        System.out.println(ctx.lookup("ou=NewHires"));

        // do something useful with ctx

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

From source file:UseDiff.java

public static void main(String[] args) {

    // Set up environment for creating 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");

    // Authenticate as S. User and password "mysecret"
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, "cn=S. User, ou=NewHires, o=JNDITutorial");
    env.put(Context.SECURITY_CREDENTIALS, "mysecret");

    try {//w  w w .j a va  2  s.  c om
        // Create initial context
        DirContext ctx = new InitialDirContext(env);

        // do something useful with ctx
        System.out.println(ctx.lookup("ou=NewHires"));

        // Change to using no authentication
        ctx.addToEnvironment(Context.SECURITY_AUTHENTICATION, "none");

        System.out.println(ctx.lookup("ou=NewHires"));

        // do something useful with ctx

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

From source file:External.java

public static void main(String[] args) {
    // Set up environment for creating 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:636/o=JNDITutorial");

    // Principal & credentials will be obtained from the connection
    env.put(Context.SECURITY_AUTHENTICATION, "EXTERNAL");

    // Specify SSL
    env.put(Context.SECURITY_PROTOCOL, "ssl");

    try {//from   w  w w .  j  av  a2 s .c  o m
        // Create initial context
        DirContext ctx = new InitialDirContext(env);

        System.out.println(ctx.lookup("ou=NewHires"));

        // do something useful with ctx

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

From source file:Create.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 {/*from  w w  w  . j ava2s  . c  om*/
        // Create the initial context
        DirContext ctx = new InitialDirContext(env);

        // Create attributes to be associated with the new context
        Attributes attrs = new BasicAttributes(true); // case-ignore
        Attribute objclass = new BasicAttribute("objectclass");
        objclass.add("top");
        objclass.add("organizationalUnit");
        attrs.put(objclass);

        // Create the context
        Context result = ctx.createSubcontext("ou=NewOu", attrs);

        // Check that it was created by listing its parent
        NamingEnumeration list = ctx.list("");

        // Go through each item in list
        while (list.hasMore()) {
            NameClassPair nc = (NameClassPair) list.next();
            System.out.println(nc);
        }

        // Close the contexts when we're done
        result.close();
        ctx.close();
    } catch (NamingException e) {
        System.out.println("Create failed: " + e);
    }
}

From source file:Digest.java

public static void main(String[] args) {

    // Set up environment for creating 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");

    // Authenticate as C. User and password "mysecret"
    env.put(Context.SECURITY_AUTHENTICATION, "DIGEST-MD5");

    env.put(Context.SECURITY_PRINCIPAL, "dn:cn=C. User, ou=NewHires, o=JNDITutorial");
    env.put(Context.SECURITY_CREDENTIALS, "mysecret");

    env.put("com.sun.jndi.ldap.trace.ber", System.out);

    try {/*from w  w  w . j  ava  2 s .  c om*/
        // Create initial context
        DirContext ctx = new InitialDirContext(env);

        System.out.println(ctx.lookup("ou=NewHires"));

        // do something useful with ctx

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

From source file:FullName.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 {//  ww  w.  ja v a 2 s.  c o m
        // Create initial context
        DirContext ctx = new InitialDirContext(env);

        NamingEnumeration answer = ctx.search("ou=People", null);

        // Print the answer
        printSearchEnumeration(answer);

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

From source file:SerObjWithCodebase.java

public static void main(String[] args) {

    if (args.length != 1) {
        System.err.println("usage: java SerObjWithCodebase <codebase URL>");
        System.exit(-1);//from w w w . j  ava  2s .  c  o  m
    }

    String codebase = args[0];

    // Set up environment for creating 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 {
        // Create the initial context
        DirContext ctx = new InitialDirContext(env);

        // Create object to be bound
        Flower f = new Flower("rose", "pink");

        // Perform bind and specify codebase
        ctx.bind("cn=Flower", f, new BasicAttributes("javaCodebase", codebase));

        // Check that it is bound
        Flower f2 = (Flower) ctx.lookup("cn=Flower");
        System.out.println(f2);

        // Close the context when we're done
        ctx.close();
    } catch (NamingException e) {
        System.out.println("Operation failed: " + e);
    }
}

From source file:DigestRealm.java

public static void main(String[] args) {

    // Set up environment for creating 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");

    // Authenticate as C. User and password "mysecret" in realm "JNDITutorial"
    env.put(Context.SECURITY_AUTHENTICATION, "DIGEST-MD5");

    env.put(Context.SECURITY_PRINCIPAL, "dn:cn=C. User, ou=NewHires, o=JNDITutorial");
    env.put(Context.SECURITY_CREDENTIALS, "mysecret");
    env.put("java.naming.security.sasl.realm", "JNDITutorial");

    try {//  w w  w . java 2 s. c om
        // Create initial context
        DirContext ctx = new InitialDirContext(env);

        System.out.println(ctx.lookup("ou=NewHires"));

        // do something useful with ctx

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

From source file:Ldaps.java

public static void main(String[] args) {
    // Set up environment for creating initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");

    // Specify LDAPS URL
    env.put(Context.PROVIDER_URL, "ldaps://localhost:636/o=JNDITutorial");

    // Authenticate as S. User and password "mysecret"
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, "cn=S. User, ou=NewHires, o=JNDITutorial");
    env.put(Context.SECURITY_CREDENTIALS, "mysecret");

    try {//from   w  ww  . j ava 2 s .  com
        // Create initial context
        DirContext ctx = new InitialDirContext(env);

        System.out.println(ctx.lookup("ou=NewHires"));

        // ... do something useful with ctx

        // 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 {//  ww w . ja va2 s . 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();
    }
}