Example usage for javax.naming.directory DirContext createSubcontext

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

Introduction

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

Prototype

public DirContext createSubcontext(String name, Attributes attrs) throws NamingException;

Source Link

Document

Creates and binds a new context, along with associated attributes.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Attributes attrs = new BasicAttributes(true);
    Attribute objclass = new BasicAttribute("objectclass");
    objclass.add("top");
    objclass.add("extensible");
    attrs.put(objclass);//from   ww  w.j  av  a 2  s. co m

    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);
    Context entry = ctx.createSubcontext("cn=Sample", attrs);
}

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 www  . j a  v  a  2 s  .c o m*/
        // 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:com.swdouglass.joid.store.DirectoryStoreImpl.java

public DirectoryStoreImpl() {
    try {// www.  j ava 2s  .c o  m
        // Set up environment for creating the initial context
        initialCtx = DirectoryUtil.getInitialDirContext();

        this.setOuOpenID(OU.concat(DirectoryUtil.getProperty(OU_OPENID_PROP, OU_OPENID_PROP_DEFAULT)));
        this.setOuAssociation(
                OU.concat(DirectoryUtil.getProperty(OU_ASSOCIATION_PROP, OU_ASSOCIATION_PROP_DEFAULT)));
        this.setOuNonce(OU.concat(DirectoryUtil.getProperty(OU_NONCE_PROP, OU_NONCE_PROP_DEFAULT)));

        //try to create parent contexes
        Attributes attrs = new BasicAttributes(true); // case-ignore
        Attribute objclass = new BasicAttribute("objectclass");
        objclass.add("top");
        objclass.add("organizationalUnit");
        attrs.put(objclass);
        DirContext baseCtx = initialCtx.createSubcontext(ouOpenID, attrs);
        baseCtx.createSubcontext(ouAssociation, attrs);
        baseCtx.createSubcontext(ouNonce, attrs);

    } catch (NameAlreadyBoundException ex) {
        //ignore
    } catch (Exception ex) {
        log.warn(ex);
        ex.printStackTrace();
    }
}

From source file:org.orbeon.oxf.processor.LDAPProcessor.java

private void add(DirContext ctx, Add add) {
    try {/*  w w  w  .  ja  v  a2s . c  om*/
        ctx.createSubcontext(add.getName(), add.getAttributes());
    } catch (NamingException e) {
        throw new OXFException("LDAP Add Failed", e);
    }
}

From source file:it.infn.ct.security.utilities.LDAPUtils.java

public static boolean addOrganisation(LDAPUser lus, Organization org) {
    boolean registration = false;
    DirContext ctx = null;
    try {//  ww  w.  j  a v  a 2 s . c  o  m
        ctx = getAuthContext(lus.getUsername(), lus.getPassword());

        Attributes attrsBag = new BasicAttributes();

        Attribute oc = new BasicAttribute("objectClass");
        oc.add("organization");
        oc.add("top");
        attrsBag.put(oc);

        Attribute o = new BasicAttribute("o", org.getKey());
        attrsBag.put(o);

        Attribute description = new BasicAttribute("description", org.getDescription());
        attrsBag.put(description);

        if (org.getReference() != null && !org.getReference().isEmpty()) {
            Attribute registeredAddr = new BasicAttribute("registeredAddress", org.getReference());
            attrsBag.put(registeredAddr);
        }

        ResourceBundle rb = ResourceBundle.getBundle("ldap");
        ctx.createSubcontext(
                "o=" + org.getKey() + ",c=" + org.getCountryCode() + "," + rb.getString("organisationsRoot"),
                attrsBag);

        registration = true;
    } catch (NameNotFoundException ex) {
        _log.error(ex);
    } catch (NamingException e) {
        _log.error(e);
    } finally {
        if (ctx != null) {
            try {
                ctx.close();
            } catch (Exception e) {
                // Never mind this.
            }
        }
    }

    return registration;

}

From source file:org.easy.ldap.LdapDao.java

/**
 * @param parentDn//from  w  w  w . j  av a2s . c  o  m
 * @param subContextName
 * @param attributes
 */
public void createSubContext(final LdapName parentDn, final Rdn subContextRdn, final Attributes attributes) {
    DirContext ctx = null;

    try {
        LdapName subContextName = NamingFactory.createName(subContextRdn);
        ctx = contextFactory.createContext(parentDn.toString());
        ctx.createSubcontext(subContextName.toString(), attributes);
    } catch (NamingException e) {
        throw new java.lang.RuntimeException(subContextRdn.toString() + "," + parentDn.toString(), e);
    }
}

From source file:org.apache.archiva.redback.rest.services.LdapGroupMappingServiceTest.java

private void createGroup(DirContext context, String groupName, String dn) throws Exception {

    Attributes attributes = new BasicAttributes(true);
    BasicAttribute objectClass = new BasicAttribute("objectClass");
    objectClass.add("top");
    objectClass.add("groupOfUniqueNames");
    attributes.put(objectClass);//from  ww  w  .  j  av a2s.  co m
    attributes.put("cn", groupName);
    BasicAttribute basicAttribute = new BasicAttribute("uniquemember");

    basicAttribute.add("uid=admin," + suffix);

    attributes.put(basicAttribute);
    context.createSubcontext(dn, attributes);
}

From source file:org.apache.directory.server.jndi.ServerContextFactory.java

private void ensureLdifFileBase(DirContext root) {
    Attributes entry = new LockableAttributesImpl("ou", "loadedLdifFiles", true);
    entry.put("objectClass", "top");
    entry.get("objectClass").add("organizationalUnit");
    try {//ww w.j a  v  a2  s .  c  om
        root.createSubcontext(LDIF_FILES_DN, entry);
        log.info("Creating " + LDIF_FILES_DN);
    } catch (NamingException e) {
        log.info(LDIF_FILES_DN + " exists");
    }
}

From source file:org.apache.archiva.redback.authentication.ldap.LdapBindAuthenticatorTest.java

private void bindUserObject(DirContext context, String cn, String dn) throws Exception {
    Attributes attributes = new BasicAttributes(true);
    BasicAttribute objectClass = new BasicAttribute("objectClass");
    objectClass.add("top");
    objectClass.add("inetOrgPerson");
    objectClass.add("person");
    objectClass.add("organizationalperson");
    attributes.put(objectClass);/*from  w  ww. j a  v a 2s.co m*/
    attributes.put("cn", cn);
    attributes.put("sn", "foo");
    attributes.put("mail", "foo");
    attributes.put("userPassword", passwordEncoder.encodePassword("foo"));
    attributes.put("givenName", "foo");
    context.createSubcontext(dn, attributes);
}

From source file:org.apache.directory.server.jndi.ServerContextFactory.java

private void addFileEntry(DirContext root, File ldif) throws NamingException {
    String rdnAttr = File.separatorChar == '\\' ? WINDOWSFILE_ATTR : UNIXFILE_ATTR;
    String oc = File.separatorChar == '\\' ? WINDOWSFILE_OC : UNIXFILE_OC;

    Attributes entry = new LockableAttributesImpl(rdnAttr, getCanonical(ldif), true);
    entry.put("objectClass", "top");
    entry.get("objectClass").add(oc);
    root.createSubcontext(buildProtectedFileEntry(ldif), entry);
}