Example usage for javax.naming.directory InitialDirContext destroySubcontext

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

Introduction

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

Prototype

public void destroySubcontext(String name) throws NamingException 

Source Link

Usage

From source file:org.alfresco.repo.security.authentication.ldap.LDAPInitialDirContextFactoryImpl.java

public static void main(String[] args) {
    // ....build a pyramid selling scheme .....

    // A group has three user members and 2 group members .... and off we go ....
    // We make the people and groups to represent this and stick them into LDAP ...used to populate a test data base for user and groups

    int userMembers = Integer.parseInt(args[3]);

    ApplicationContext applicationContext = ApplicationContextHelper.getApplicationContext();
    LDAPInitialDirContextFactory factory = (LDAPInitialDirContextFactory) applicationContext
            .getBean("ldapInitialDirContextFactory");

    InitialDirContext ctx = null;
    try {/*from   w  ww .j a v a2s.c  o  m*/
        ctx = factory.getInitialDirContext("cn=" + args[0] + "," + args[2], args[1]);

        /* Values we'll use in creating the entry */
        Attribute objClasses = new BasicAttribute("objectclass");
        objClasses.add("top");
        objClasses.add("person");
        objClasses.add("organizationalPerson");
        objClasses.add("inetOrgPerson");

        for (int i = 0; i < userMembers; i++) {

            Attribute cn = new BasicAttribute("cn", "User" + i + " TestUser");
            Attribute sn = new BasicAttribute("sn", "TestUser");
            Attribute givenNames = new BasicAttribute("givenName", "User" + i);
            Attribute telephoneNumber = new BasicAttribute("telephoneNumber", "123");
            Attribute uid = new BasicAttribute("uid", "User" + i);
            Attribute mail = new BasicAttribute("mail", "woof@woof");
            Attribute o = new BasicAttribute("o", "Alfresco");
            Attribute userPassword = new BasicAttribute("userPassword", "bobbins");
            /* Specify the DN we're adding */
            String dn = "cn=User" + i + " TestUser," + args[2];

            Attributes orig = new BasicAttributes();
            orig.put(objClasses);
            orig.put(cn);
            orig.put(sn);
            orig.put(givenNames);
            orig.put(telephoneNumber);
            orig.put(uid);
            orig.put(mail);
            orig.put(o);
            orig.put(userPassword);

            try {
                ctx.destroySubcontext(dn);
            } catch (NamingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            ctx.createSubcontext(dn, orig);
        }

    } catch (NamingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        if (ctx != null) {
            try {
                ctx.close();
            } catch (NamingException e) {

                e.printStackTrace();
            }
        }
    }

}

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

/**
 * Reproduces the problem with/*  w  w w  . j  a  v a  2 s  .c  o  m*/
 * <a href="http://issues.apache.org/jira/browse/DIREVE-239">DIREVE-239</a>.
 *
 * @throws Exception if anything goes wrong
 */
@Test
public void testAdminAccessBug() throws Exception {
    getLdapServer().getDirectoryService().setAllowAnonymousAccess(true);

    // Use the SUN JNDI provider to hit server port and bind as anonymous

    final Hashtable<String, Object> env = new Hashtable<String, Object>();

    env.put(Context.PROVIDER_URL, Network.ldapLoopbackUrl(getLdapServer().getPort()));
    env.put("java.naming.ldap.version", "3");
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");

    Attributes attributes = new BasicAttributes(true);
    Attribute objectClass = new BasicAttribute("objectClass");
    objectClass.add("top");
    objectClass.add("organizationalUnit");
    attributes.put(objectClass);
    attributes.put("ou", "blah");
    InitialDirContext ctx = new InitialDirContext(env);
    ctx.createSubcontext("ou=blah,ou=system", attributes);
    SearchControls controls = new SearchControls();
    controls.setSearchScope(SearchControls.OBJECT_SCOPE);
    controls.setReturningAttributes(new String[] { "+" });
    NamingEnumeration<SearchResult> list = ctx.search("ou=blah,ou=system", "(objectClass=*)", controls);
    SearchResult result = list.next();
    list.close();
    Attribute creatorsName = result.getAttributes().get("creatorsName");
    assertEquals("", creatorsName.get());
    ctx.destroySubcontext("ou=blah,ou=system");
}

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

/**
 * Test case for <a href="http://issues.apache.org/jira/browse/DIREVE-284" where users in
 * mixed case partitions were not able to authenticate properly.  This test case creates
 * a new partition under dc=aPache,dc=org, it then creates the example user in the JIRA
 * issue and attempts to authenticate as that user.
 *
 * @throws Exception if the user cannot authenticate or test fails
 */// w ww.  ja  va  2 s . com
@Test
public void testUserAuthOnMixedCaseSuffix() throws Exception {
    getLdapServer().getDirectoryService().setAllowAnonymousAccess(true);

    Hashtable<String, Object> env = new Hashtable<String, Object>();

    env.put(Context.PROVIDER_URL, Network.ldapLoopbackUrl(getLdapServer().getPort()) + "/dc=aPache,dc=org");
    env.put("java.naming.ldap.version", "3");
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    InitialDirContext ctx = new InitialDirContext(env);
    Attributes attrs = ctx.getAttributes("");
    assertTrue(attrs.get("dc").get().equals("aPache"));

    Attributes user = new BasicAttributes("cn", "Kate Bush", true);
    Attribute oc = new BasicAttribute("objectClass");
    oc.add("top");
    oc.add("person");
    oc.add("organizationalPerson");
    oc.add("inetOrgPerson");
    user.put(oc);
    user.put("sn", "Bush");
    user.put("userPassword", "Aerial");
    ctx.createSubcontext("cn=Kate Bush", user);

    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_CREDENTIALS, "Aerial");
    env.put(Context.SECURITY_PRINCIPAL, "cn=Kate Bush,dc=aPache,dc=org");

    InitialDirContext userCtx = new InitialDirContext(env);
    assertNotNull(userCtx);

    ctx.destroySubcontext("cn=Kate Bush");
}