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:Ssl.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");

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

    // 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  w  w  .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:UseFactory.java

  public static void main(String[] args) {
  // Set up environment for creating initial context
  Hashtable<String, Object> env = new Hashtable<String, Object>(11);
  env//  w  ww .  j  a  v a 2  s.  co  m
      .put(Context.INITIAL_CONTEXT_FACTORY,
          "com.sun.jndi.ldap.LdapCtxFactory");
  env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

  // Specify the socket factory
  env.put("java.naming.ldap.factory.socket", "CustomSocketFactory");

  // 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 {
    // 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:GetAllAttrs.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  a v  a2s. co  m*/
        // Create the initial context
        DirContext ctx = new InitialDirContext(env);

        // Get all the attributes of named object
        Attributes answer = ctx.getAttributes("cn=Ted Geisel, ou=People");

        // Print the answer
        printAttrs(answer);

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

From source file:SearchTimeLimit.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.j  av  a2 s.c om
        // Create initial context
        DirContext ctx = new InitialDirContext(env);

        // Set search controls to limit count to 'timeout'
        SearchControls ctls = new SearchControls();
        ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        ctls.setTimeLimit(timeout); //

        // Search for objects with those matching attributes
        NamingEnumeration answer = ctx.search("", "(objectclass=*)", ctls);

        // Print the answer
        printSearchEnumeration(answer);

        // Close the context when we're done
        ctx.close();
    } catch (TimeLimitExceededException e) {
        System.out.println("time limit exceeded");
    } catch (Exception e) {
        System.err.println(e);
    }
}

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

        // Set search controls to limit count to 'expected'
        SearchControls ctls = new SearchControls();
        ctls.setCountLimit(expected);

        // Search for objects with those matching attributes
        NamingEnumeration answer = ctx.search("ou=People", "(sn=M*)", ctls);

        // Print the answer
        printSearchEnumeration(answer);

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

From source file:se.vgregion.service.barium.BariumRestClientIT.java

License:asdf

public static void main(String[] args) {

    try {//ww  w .  j  a  v  a 2 s. c o  m
        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, "LDAP://my.ldap.server:389"); //replace with your server URL/IP
        //only DIGEST-MD5 works with our Windows Active Directory
        env.put(Context.SECURITY_AUTHENTICATION, "DIGEST-MD5"); //No other SALS worked with me
        env.put(Context.SECURITY_PRINCIPAL, "user1"); // specify the username ONLY to let Microsoft Happy
        env.put(Context.SECURITY_CREDENTIALS, "secret1"); //the password

        DirContext ctx = new InitialDirContext(env);

        ctx.close();

    } catch (NamingException ne) {
        System.out.println("Error authenticating user:");
        System.out.println(ne.getMessage());
        return;
    }

    //if no exception, the user is already authenticated.
    System.out.println("OK, successfully authenticating user");
}

From source file:fr.aliasource.webmail.ldap.Configuration.java

DirContext getConnection() throws NamingException {
    try {/*from w  w w  .  java 2 s. c om*/
        return new InitialDirContext(env);
    } catch (NamingException e) {
        logger.error(e.getMessage(), e);
        throw e;
    }
}

From source file:org.pentaho.di.trans.steps.mailvalidator.MailValidation.java

/**
 * verify if there is a mail server registered to the domain name. and return the email servers count
 *//*from  w ww .j  a  v a2 s  .c om*/
public static int mailServersCount(String hostName) throws NamingException {
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
    DirContext ictx = new InitialDirContext(env);
    Attributes attrs = ictx.getAttributes(hostName, new String[] { "MX" });
    Attribute attr = attrs.get("MX");
    if (attr == null) {
        return (0);
    }
    return (attr.size());
}

From source file:cyrille.jndi.LdapTest.java

@Test
public void test() 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, "ldap://localhost:389");
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
    env.put(Context.SECURITY_CREDENTIALS, "secret");
    DirContext dirContext = new InitialDirContext(env);

    Attributes attributes = dirContext.getAttributes("uid=aeinstein,ou=Users,dc=example,dc=com");
    for (NamingEnumeration<Attribute> attributesEnumeration = (NamingEnumeration<Attribute>) attributes
            .getAll(); attributesEnumeration.hasMore();) {
        Attribute attribute = attributesEnumeration.next();
        System.out.print(attribute.getID() + "=");

        for (NamingEnumeration<?> attributeValues = attribute.getAll(); attributeValues.hasMore();) {
            Object value = attributeValues.next();
            if (value instanceof byte[] && "userpassword".equals(attribute.getID())) {
                byte[] bytes = (byte[]) value;
                System.out.print(new String(bytes) + ", ");
            } else {
                System.out.print(value + ", ");
            }//  ww  w .j  ava  2 s.com
        }
        System.out.println();
    }
}

From source file:eionet.gdem.dcm.conf.LdapTest.java

protected DirContext getDirContext() throws NamingException {
    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, url);
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    DirContext ctx = new InitialDirContext(env);
    return ctx;/*from   www .  j a  v  a 2  s  .  co m*/
}